Differences

This shows you the differences between two versions of the page.

tutorials:pic24:pic24_irq [2010/11/19 08:06]
chrism
tutorials:pic24:pic24_irq [2010/11/19 08:08] (current)
chrism
Line 4: Line 4:
==== Getting Started ==== ==== Getting Started ====
To get started open the MPLAB IDE and start a new project for a PIC24 device.  If you do not know how to start a new project, then please go read the Programming Basics tutorial for the PIC18 device.  Once you have a new project open, select a PIC24 device you have in the lab, and start a new file.  Name the file something simple like, main.c, for simplicity. To get started open the MPLAB IDE and start a new project for a PIC24 device.  If you do not know how to start a new project, then please go read the Programming Basics tutorial for the PIC18 device.  Once you have a new project open, select a PIC24 device you have in the lab, and start a new file.  Name the file something simple like, main.c, for simplicity.
 +==== Programming Interrupts ====
 +
 +Since the project is set up we shall begin going over the code bit by bit to teach you what you need to know about interrupts for the PIC24.
 +
 +<code>
 +#include <p24F16KA102.h>
 +void Initialize(void);
 +void InitializeIRQ(void);
 +int main(void)
 +{
 +> Initialize();
 +> InitializeIRQ();
 +> while(1);
 +}
 +
 +</code>
 +The above code is just a basic outline in order to set things up.  We declared which PIC we are using, which is the PIC24F16KA102, and we have outlined two functions, Initialize() and InitializeIRQ().  The while loop keeps the processor in an infinite loop and does not allow a reset condition to occur.  The next set of code is displayed below.
 +<code>
 +void Initialize(void)
 +{
 +> //Set ports to digital mode
 +> AD1PCFG = 0xFFFF; //Set all I/O ports as digital
 +> //Setup TRIS registers
 +> TRISA = 0x0000; //Set port A as outputs
 +> TRISB = 0xFFFF; //Set port B as inputs
 +> PMD1 = 0x0000;    //Turn module power on
 +> PMD2 = 0x0000;    //Turn module power on
 +> PMD3 = 0x0000;    //Turn module power on
 +> PMD4 = 0x0000;    //Turn module power on
 +}
 +</code>
 +
 +Let's look at what is going on in the Initialize function.  First, we are setting all of our ports to digital outputs like we did in the previous tutorial.  If you need a refresher, please go back and read over that tutorial again.  Next, we are setting PORTA as outputs, and PORTB as inputs.  This tutorial could be done with either port A or port B.
 +
 +Finally, the PMDx registers are being set to allow power to go to all of the peripheral features of the PIC24F16KA102.  Several of the PIC24's have this register set.  Basically, to allow you to consume less power, the device can turn the clock signal going off to a peripheral.  This allows the PIC24 to retain its settings while effectively turning the module off.  Later on if you wanted to turn the module back on after initial setup, you could do it without having to go through the setup all over again.
 +=== Initialize Interrupts ===
 +
 +<code>
 +void InitializeIRQ(void)
 +{
 + //CPU Control Register
 + SRbits.IPL = 0; //CPU has an interrupt priority level 0 for interrupt control
 + CORCONbits.IPL3 = 0;        //CPU has an interrupt priority level less than 7
 + //Interrupt Control Registers
 + INTCON1bits.NSTDIS = 1; //Nesting Interrupts is disabled
 + INTCON2 = 0x00; //Reset INTCON2 register
 + INTCON2bits.INT2EP = 1; //Negative or Falling Edge trigger
 + INTCON2bits.INT1EP = 1; //Negative or Falling Edge trigger
 + INTCON2bits.INT0EP = 0; //Positive or Rising Edge trigger
 + //Interrupt Flag Registers
 + IFS0 = 0x00; //Reset Interrupt Flag 0 register
 + IFS1 = 0x00; //Reset Interrupt Flag 1 register
 + IFS3 = 0x00; //Reset Interrupt Flag 3 register
 + IFS4 = 0x00; //Reset Interrupt Flag 4 register
 +        IEC0bits.INT0IE = 1; //Enable the INT0 Interrupt
 +        IEC1bits.INT1IE = 1;            //Enable the INT1 Interrupt
 +        IPC0bits.INT0IP = 7; //INT0 Priority level is 7
 +        IPC5bits.INT1IP = 5;            //INT1 Priority level is 5
 + return;
 +}
 +</code>
 +
 +Basically, in the above section of code we enabled the CPU interrupt priority Level to something less than everything else, and we set the appropriate registers to begin setting the other interrupts.  The code disabled the ability for the processor to nest interrupts.  Interrupt nesting is pre-emption of lower priority interrupts by another high priority interrupt.  If a higher priority interrupt pre-empts the lower priority interrupt, then the higher priority interrupt context would be saved on he previous lower priority one.  Thus the depth of the interrupts is two, and the nesting of the interrupts is two.
 +The next section of code enables the INT0 and INT1 interrupt. We will use one interrupt for one set of latched ports, and the other for the other set of latched ports to toggle a few LEDs.
 +
 +=== Define Interrupts ===
 +<code>
 +void __attribute__ (( interrupt, shadow, no_auto_psv )) _INT0Interrupt(void)
 +{
 + //Clear the ADC Interrupt Flag
 + IFS0bits.INT0IF = 0; //Clear INT0 interrupt flag
 +        LATA = !LATA;
 +}
 +void __attribute__ (( interrupt, shadow, no_auto_psv )) _INT1Interrupt(void)
 +{
 + //Clear the ADC Interrupt Flag
 + IFS1bits.INT1IF = 0; //Clear ANT1 IRQ flag
 +        LATB = !LATB;
 + return;
 +}
 +</code>
 +
 +The above section of code has the interrupts defined for the PIC24.  Let's take a closer look at the code for a bit.  First off all interrupts are voids as they do not return anything.  Next, we define the attributes of the interrupt.  We declare the interrupt attribute, the shadow attribute, and the no_auto_psv attributes.  The interrupt attribute declares the function as an interrupt.  The shadow attribute tells the processor to copy all values that need to be stored into shadow registers.  The no_auto_psv attribute tells the compiler to not allocate the variable in program space designated for automatic program space visibility window access.  Any variable in auto_psv space can be read but not written to.  Next, we will move on to the hardware setup in order to get this small project working.  When ready to compile, compile the code and have fun.
-==== Programming ==== 
==== Hardware Setup ==== ==== Hardware Setup ====
duty free alcohol airport duty free cigs uk buy duty free cuban cigars where to buy cosmetics duty free fragrances buy tobacco duty free