Code Final
Code Final
***** EXAMPLE:
Write a code to count pulses coming from external T1CK.
When the count reaches 100, the microcontroller set the pin
RA4 to 1. Assume the initial value of the timer is 34.
Ans:
#include <p32xxxx.h>
void main (void)
{ TRISACLR=0x0010;
T1CON = 0;
TMR1=34; // load TMR1
PR1 = 100; // Load period
T1CONSET = 0x8000; // start timer
while(IF0bits.T1IF == 0);
T1CONCLR = 0x8000;
PORTAINV = 0x0010;
while(1);
}
***** Steps to write the code for proper interrupt
exception initialization & usage:
Step 1. #include Standard Headers
Example
In this example, we define a Timer 2 ISR function T2Interrupt() that is configured as a IPL-level-7
process, and to which is assigned the shadow-register set (as defined by the DEVCFG3FSRSSEL<2:0> (Device
Configuration Word 3) bit settings shown):
#include <xc.h>
#include <sys/attribs.h>
/* Config bits */
//Step 5
#pragma config FSRSSEL = PRIORITY_7 /* Assign the SRS to level 7 priority handlers */
// Toggle LED A1
// LATAbits.LATA1 = ~ LATAbits.LATA1;
int main(void)
T2CON = 0x0;
/* Set Timer 2 interrupt priority to 7 it must match with priority we have set in ISR */
INTCONSET = 0x1000; // setting MVEC bit of INTCON to 1=> Multi Vector Mode
} // end of main
11. Wait until conversion is done by polling DONE bit is set (AD1CON1>)
Sample Code: