0% found this document useful (0 votes)
13 views

Code Final

Uploaded by

asookaf21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Code Final

Uploaded by

asookaf21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE 371

Code from the book and the slides

***** 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

Step 2. Provide Interrupt Service Routine (ISR)

Step 3. Configure Peripheral

Step 4. Configure Interrupt Controller

Step 5. Assign The Shadow Register Set to an IPL

Step 6. Set Interrupt Controller Mode

Step 7. Enable Interrupt Exceptions

Step 8. Enable Peripheral

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):

Here is the complete code

// Step 1. #include Standard Headers

#include <xc.h>

#include <sys/attribs.h>

/* Config bits (not all shown) */

/* Config bits */

//Step 5

#pragma config FSRSSEL = PRIORITY_7 /* Assign the SRS to level 7 priority handlers */

// Step 2. Provide Interrupt Service Routine (ISR)

void __ISR( 8 , IPL7SRS) T2Interrupt(void)

// Toggle LED A1

// LATAbits.LATA1 = ~ LATAbits.LATA1;

LATAINV = 0x2; // toggle RA1 bit


// Reset interrupt flag

IFS0CLR = 0x100; //0b1 0000 0000 = 0x100 , Clearing bit no 8 of IFS0

int main(void)

// Step 3. Configure Peripheral

/* turn off Timer 2 */

T2CON = 0x0;

/* pre-scale = 1:256 (T2CLKIN = 39062.5 Hz) */

T2CONSET = 0x70; 0b 0111 0000

PR2 = 3906; /* T2 period ~ 100mS */

TMR2 = 0; /* clear Timer 2 counter */

//Step 4. Configure Interrupt Controller

// Disable the interrupt globally

__builtin_disble_interrupts(); /* Set the CP0 Status IE bit to OFF on interrupts globally */

/* Set Timer 2 interrupt priority to 7 it must match with priority we have set in ISR */

IPC2SET = 0x1C; /* Set Timer 2 interrupt priority to 7 * 0b1 1100/

/* Reset Timer 2 interrupt flag */

IFS0CLR = 0x100; // 0b1 0000 0000 Clearing flag bit of T2

/* Enable interrupts from Timer 2 */

IEC0SET = 0x100;// Setting int enable bit of T2 to 1

//Step 6. Set Interrupt Controller Mode

INTCONSET = 0x1000; // setting MVEC bit of INTCON to 1=> Multi Vector Mode

// Step 7. Enable Interrupt Exceptions

__builtin_enable_interrupts(); /* Set the CP0 Status IE bit to turn on interrupts globally */

// Step 8. Enable Peripheral


/* Enable Timer 2 peripheral */

T2CONSET = 0x8000; // Turn ON the timer 2 0b 1000 0000 0000 0000

while(1); /* main application loop */

} // end of main

***** To configure the ADC module, perform the following


steps:
1.Configure the analog port pins in AD1PCFG<15:0>

2. Configure to Auto Sample Mode set ASAM bit AD1CON1<2>

3.Select the analog inputs to the ADC multiplexers in AD1CHS<32:0>

4.Select the voltage reference source using VCFG<2:0> (AD1CON2<15:13>)

5. Select the ADC clock source using ADRC (AD1CON3<15>)

6. Select the ADC clock pre-scaler using ADCS<7:0> (AD1CON3<7:0>).

7. Turn the ADC module on using AD1CON1<15>

8. Start the acquisition by enabling manual sample bit (AD1CON1<2>)

9.Wait until sampling is complete

10. Start conversion by disabling manual sample bit (AD1CON1<2>)

11. Wait until conversion is done by polling DONE bit is set (AD1CON1>)

12. Save the result from Buffer register to a variable

Sample Code:

AD1PCFG = 0xFF7F; // all PORTB = Digital but RB7 = analog

AD1CON1 = 0x0004; // ASAM bit = 1 implies acquisition

// starts immediately after last conversion is done

AD1CHS = 0x00070000; /* Connect RB7/AN7 as CH0 input in this example


RB7/AN7 is the input */

AD1CSSL = 0; // No channel selection

AD1CON3 = 0x0002; // Sample time manual, TAD = internal 6 TPB

AD1CON2 = 0; // Voltage Reference AVDD/AVSS

AD1CON1SET = 0x8000; // turn ON the ADC

You might also like