Skip to content

This code example showcases vectored-interrupt operation with three interrupt sources: Timer 0, Timer 1 and Interrupt on change. The result will be displayed in the MPLAB® Data Visualizer through the UART interface.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic18f57q43-cnano-interrupt-mplab-melody

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCHP

Vectored Interrupt Demo for Microchip University Interrupt Class

This code example showcases vectored interrupt functionality using three separate interrupt sources: Timer 0, Timer 1 and Interrupt-on-Change. Timer 1 is configured as a high-priority interrupt source. Timer 0 and Interrupt-on-Change are configured as low-priority interrupt sources. The results of this code example will be displayed in the MPLAB® Data Visualizer through the UART interface.

Related Documentation

Software Used

Hardware Used

Setup

The PIC18F57Q43 Curiosity Nano Development Board is the selected hardware platform for this code example. The diagram below shows the hardware connections for this example.

Connection diagram if using Curiosity Nano Base for Click boards™:

Connection diagram without using Curiosity Nano Base for Click boards™:

Project Configuration

The following section gives a step by step tutorial of the complete project configuration for this code example.

Project Creation

  1. To create a new project go to "File >> New Project". Once the new project window opens, select Standalone Project and then click Next.
  2. Enter "PIC18F57Q43" as the device and select PIC18F57Q43 Curiosity Nano as the Tool for this project. Once this is complete click Next.
  3. For the compiler, select "XC8 version 2.35 (or newer) and then click Next.
  4. Enter "pic18f57q43-interrupt" as the project name and click Finish to finish the project creation.

Opening MCC

After creating the project, the next step is to open the MPLAB Code Configurator (MCC) to set the system and peripheral configurations. This can be done by clicking the MCC icon on the Toolbar, or by going to "Tools >> Embedded "and clicking MPLAB® Code Configurator: Open/Close.

Clock Control Configuration

The Clock Control configuration window can be found under the System Settings drop down in MCC:

To configure the device system clock, select the High Frequency Internal Oscillator (HFINTOSC) as the Clock Source, as well as the 1_MHz option for the HF Internal Clock drop down menu and 16 in the Clock Divider drop down menu.

Peripheral Drivers Configuration

Timer 0

Add Timer 0 (TMR0) to the MPLABX project by selecting TMR0 from the Driver drop down menu in MCC.

  1. In the Timer 0 Hardware Settings, make sure the timer is disabled by unchecking the Enable Timer box. Select 1:8 as the Clock Prescaler, select 1:1 as the Postscaler, and select 16-bit as the Timer Mode. Additionally, select LFINTOSC as the Clock Source and enter "5" seconds as the Requested Period.
  2. In the Timer 0 Interrupt Settings, check the TMR Interrupt Enable check box to enable interrupts for this peripheral.

Timer 1

Add Timer 1 (TMR1) to the MPLABX project by selecting TMR1 from the Driver drop down menu in MCC.

  1. In the MCC Easy View configuration window select LFINTOSC as the Timer 1 Clock Source and select 1:8 as the Prescaler.
  2. In the Timer Period field enter "5" and the "Actual Period" field should show that the Timer 1 period is 5 seconds. In the Timer 1 Interrupt Settings, check the TMR Interrupt Enable check box to enable interrupts for this peripheral.

UART 1

To configure UART 1, add the UART module to the project by selecting UART from the Driver drop down menu in MCC.

  1. After doing this, in the "UART PLIB Selector" drop down menu that shows up, select UART1 from the available options.
  2. In the Builder Window, click on UART1 PLIB and UART1 to configure the different UART1 settings.

In the "UART1PLIB" configuration window:

  1. In Hardware Settings, enable Receive, Transmit and UART options by checking the respective boxes.
  2. In Advanced Settings, select high speed in Baud Rate Generator Speed Select.

In the "UART1" configuration window:

  1. Set the Requested Baudrate equal to the Calculated Baudrate, "7813". This will give the baud rate error about 0.006%, which is close to 0.
  2. Enable Redirect Printf to UART option.

Pins Configuration

To configure the pins for this project, open the Pin Grid View window:

  1. Configure the UART1 RX1 pin on RF1 and the UART1 TX1 pin on RF0 by clicking the corresponding options in the Pin Grid View. The pins should change from open locks to closed locks.
  2. Configure RB4 as a GPIO input pin by clicking the corresponding lock box in the Pin Grid View. The pin should change from an open lock to a closed lock.

To configure the pins for this project, using the the Pins window which can be found under the system settings drop down menu in MCC:

  1. Deselect the "Analog", "Slew Rate" and "Input Level Control" options for pins RF1 and RF0.
  2. Deselect the "Slew Rate" and "Input Level Control" options for pin RB4. Select "Weak Pullup" on pin RB4.
  3. Enter "Switch" as a Custom Name for pin RB4. Select "any" in the "Interrupt On Change" drop down menu. This makes it so that an interrupt is triggered by a positive or negative edge on pin RB4.

Interrupt Manager Configuration

In the Interrupt Manager configuration window which can be found under the System Settings drop down in MCC:

  1. Enable Vectored Interrupts by checking the corresponding box. There will be two Interrupt Vector Tables that represent high and low priority interrupt vectors.
  2. Check the "Enable" and "High Priority" options for Timer 1 Interrupt vector.
  3. Check the "Enable" option and uncheck the "High Priority" option for "Interrupt On Change" and "Timer 0" Interrupt vectors.

Melody Code Generation

Click the Generate button in MCC to generate the corresponding Melody code.
\

Interrupt Callback and Main Routine

Once the code has been generated by Melody, add the following interrupt callback functions and main routine into the main.c file.

Timer1 Interrupt Callback

void Timer1_Callback(void){
    for (int i=10; i>0; i--){
        INTERRUPT_GlobalInterruptHighDisable(); 
        INTERRUPT_GlobalInterruptLowDisable(); 
        printf("[High Priority] Timer1 ISR is executing ... Counting down: %d \r\n\n", i);
        INTERRUPT_GlobalInterruptHighEnable();
        INTERRUPT_GlobalInterruptLowEnable(); 
    }
    PIE3bits.TMR1IE = 0; // Disable Timer1
}

Timer0 Interrupt Callback

void Timer0_Callback(void) {
    for (int i=10; i>0; i--){
        INTERRUPT_GlobalInterruptHighDisable(); 
        INTERRUPT_GlobalInterruptLowDisable(); 
        printf("[Low Priority] Timer0 ISR is executing ... Counting down: %d \r\n\n", i);
        INTERRUPT_GlobalInterruptHighEnable();
        INTERRUPT_GlobalInterruptLowEnable(); 
    }
    PIE3bits.TMR0IE = 0; // Disable Timer0
}

RB4 IOC Callback

void RB4_IOC_Callback(void){
    for (int i=10; i>0; i--){
        INTERRUPT_GlobalInterruptHighDisable(); 
        INTERRUPT_GlobalInterruptLowDisable(); 
        printf("[Low Priority] IOC ISR is executing ... Counting down: %d \r\n\n", i);
        INTERRUPT_GlobalInterruptHighEnable();
        INTERRUPT_GlobalInterruptLowEnable(); 
    }
    PIE0bits.IOCIE = 0; // Disable RB4 IOC
}

Main Routine

int main(void)
{
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts 
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts 
    // Use the following macros to: 

    // Enable the Global High Interrupts 
    INTERRUPT_GlobalInterruptHighEnable(); 

    // Disable the Global High Interrupts 
    //INTERRUPT_GlobalInterruptHighDisable(); 

    // Enable the Global Low Interrupts 
    INTERRUPT_GlobalInterruptLowEnable(); 

    // Disable the Global Low Interrupts 
    //INTERRUPT_GlobalInterruptLowDisable(); 

    bool done = false;  
   
    Timer0_OverflowCallbackRegister (Timer0_Callback);
    TMR1_OverflowCallbackRegister (Timer1_Callback);
    RB4_SetInterruptHandler (RB4_IOC_Callback);

    printf("---------------------------------------------------------------------------\r\n\n");
    printf("   Demo 1: High/Low-Priority Interrupt While Executing Main Routine        \r\n\n");
    printf("   Demo 2: High-Priority Interrupt with a Low-Priority Interrupt Pending   \r\n\n");
    printf("   Demo 3: High-Priority Interrupt Preempting Low-Priority Interrupts      \r\n\n");
    printf("   Demo 4: Simultaneous High- and Low-Priority Interrupts                  \r\n\n");
    printf("                   Please enter a number: (1/2/3/4)                        \r\n\n");
    while(!done) {
        done = true;
        UART1_Enable();
        

        while(!(UART1_IsRxReady()));
        switch(UART1_Read()){   
            case 49: //Decimal 1 = ASCII 49
                        // Demo 1: High/Low-Priority Interrupt While Executing Main Routine
                        printf("-------------------------------------------------------------------------\r\n\n");
                        printf("Entering Demo 1: High/Low-Priority Interrupt While Executing Main Routine\r\n\n");
                        printf("-------------------------------------------------------------------------\r\n\n");
                        break;
                    case 50: //Decimal 2 = ASCII 50
                        // Demo 2: High-Priority Interrupt with a Low-Priority Interrupt Pending
                        printf("------------------------------------------------------------------------------\r\n\n");
                        printf("Entering Demo 2: High-Priority Interrupt with a Low-Priority Interrupt Pending\r\n\n");
                        printf("------------------------------------------------------------------------------\r\n\n");
                        TMR1_Start();
                        break;
                    case 51: //Decimal 3 = ASCII 51
                        // Demo 3: High-Priority Interrupt Preempting Low-Priority Interrupts
                        printf("---------------------------------------------------------------------------\r\n\n");
                        printf("Entering Demo 3: High-Priority Interrupt Preempting Low-Priority Interrupts\r\n\n");
                        printf("---------------------------------------------------------------------------\r\n\n");
                        TMR1_Start();
                        break;
                    case 52: //Decimal 4 = ASCII 52
                        // Demo 4: Simultaneous High- and Low-Priority Interrupts
                        printf("---------------------------------------------------------------\r\n\n");
                        printf("Entering Demo 4: Simultaneous High- and Low-Priority Interrupts\r\n\n");
                        printf("---------------------------------------------------------------\r\n\n");
                        TMR1_Start();
                        Timer0_Start();
                        break;
                    default:
                        printf("Please enter a valid number!!! (1/2/3/4) \r\n\n");
                        UART1_Disable(); // FIFO Reset
                        done = false;
                        break;
        }
    }
    
    for (int i=10; i>0; i--){
        INTERRUPT_GlobalInterruptHighDisable(); 
        INTERRUPT_GlobalInterruptLowDisable(); 
        printf("        Main routine is executing ... Counting down: %d \r\n\n", i);
        INTERRUPT_GlobalInterruptHighEnable();
        INTERRUPT_GlobalInterruptLowEnable(); 
    }
    
    printf("-----------\r\n\n");
    printf("End of Demo\r\n\n");
    printf("-----------\r\n\n");

    while(1)
    {
    }    
}

System.c Routine

Timer1_Initialize(); should change to TMR1_Initialize();

Operation

  1. Insert the PIC18F57Q43 Curiosity Nano into the Curiosity Nano Base board (if Curiosity Nano Base board is used).

  2. Connect the PIC18F57Q43 Curiosity Nano development board to a PC using a micro USB cable to provide power and to allow for programming and debugging.


  1. Open the pic18f57q43-interrupt.X project in MPLAB® X IDE.

  2. Set pic18f57q43-interrupt.X project as the main project within the MPLAB® X IDE. This can be done by right clicking on the project name in the Projects tab and then clicking Set as Main Project.

  3. Select the PIC18F57Q43 Curiosity Nano in the project settings within the MPLAB® X IDE. This can be done using the following steps.

  • Right click on the project and select Properties.

  • Select the PIC18F57Q43 Curiosity Nano in the "Connected Hardware Tool" and click OK.

Project Building

Click the Clean and Build Main Project icon in the toolbar. The "BUILD SUCCESSFUL" message in the Output window means no error comes up during compilation.

Device Programming

Click the Make and Program Device Main Project in the toolbar.
The "Programming complete" message in the Output window means the firmware has been successfully downloaded to the Curiosity Nano board.


Once the device has been successfully programmed, open the MPLAB® Data Visualizer within the MPLAB® X IDE.

  • Once the MPLAB® Data Visualizer has launched, open the drop down list and select the COMn port for serial connection and Click the Start streaming COMn port button.

  • In the MPLAB® Data Visualizer terminal window, click COMn port as the source.

  1. Enter the number "1", "2", "3", or "4" into the MPLAB® Data Visualizer terminal to launch the respective demo on the PIC18F57Q43 Curiosity Nano Development Board. Details pertaining to each of the available demos are shown in the figures below:

Demo 1: High/Low-Priority Interrupt While Executing Main Routine

Demo 2: High-Priority Interrupt with a Low-Priority Interrupt Pending

Demo 3: High-Priority Interrupt Preempting Low-Priority Interrupts

Demo 4: Simultaneous High- and Low-Priority Interrupts

Result

The figures below show the resulting terminal outputs for each of the available demos:

Demo 1


Demo 2


Demo 3


Demo 4


Summary

This code example showcases the vectored interrupt feature of the PIC18F57Q43. This demo is for the Microchip University 8-bit PIC® MCU Peripheral Deep Dive Interrupt class.

About

This code example showcases vectored-interrupt operation with three interrupt sources: Timer 0, Timer 1 and Interrupt on change. The result will be displayed in the MPLAB® Data Visualizer through the UART interface.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •