PIC 32 Timer Interrupts - Page 1
Forum section: electronics / general

author: abrarbaig
author_rank: Contributor
timestamp: November 08, 2020, 05:52:58 am
content: Hello. I am trying to fire timer2 and timer3 interrupts by using statement. But only timer2 interrupt fires. Please help. Here is my code:Code:/** File: CSE371_201_Lab6.c* Author: Administrator** Created on November 5, 2020, 3:22 PM** CSE371 201 Lab6 Timer Interrupts* This code will blink an LED connected to pin RA0 using Timer2 and its corresponding interrupt.** Note that the frequency of blinking is configured by the 'setPR2()' macro*/// PIC32MX795F512L Configuration Bit Settings// 'C' source line config statements// DEVCFG3#pragma config USERID = 0xFFFF // Enter Hexadecimal value (Enter Hexadecimal value)#pragma config FSRSSEL = PRIORITY_7 // SRS Select (SRS Priority 7)#pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)#pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)#pragma config FCANIO = ON // CAN I/O Pin Select (Default CAN I/O)#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module)#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module)// DEVCFG2#pragma config FPLLIDIV = DIV_12 // PLL Input Divider (12x Divider)#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)// DEVCFG1#pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)#pragma config IESO = ON // Internal/External Switch Over (Enabled)#pragma config POSCMOD = HS // Primary Oscillator Configuration (HS osc mode)#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)#pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))// DEVCFG0#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)#pragma config PWP = OFF // Program Flash Write Protect (Disable)#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)#pragma config CP = OFF // Code Protect (Protection Disabled)// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.// Include Header Files#include <xc.h> // Include the PIC32 Peripheral Library#include<sys/attribs.h>#include<plib.h>// Config Bits// PIC32MX795F512L Configuration Bit Settings// 'C' source line config statements// Use project enums instead of #define for ON and OFF.// Defines#define SYSCLK 80000000L // System Clock Frequencyvoid main(){ // Start of main// Setting Up Inputs and OutputsTRISAbits.TRISA0 = 0; // Set RA0 as digital outputTRISAbits.TRISA1 = 0; // Set RA1 as digital outputTRISDbits.TRISD5 = 1; // Set RD5 as digital inputTRISDbits.TRISD13 = 1; // Set RD13 as digital input// Initializing outputsLATAbits.LATA0 = 0; // RA0 is set to 0LATAbits.LATA1 = 0; // RA1 is set to 0// Reading the inputsPORTDbits.RD5;PORTDbits.RD13;if(PORTDbits.RD5==0){__builtin_disable_interrupts(); // disabling interruptsT2CON = 0;PR2 = 200; // Load the period register 2 with maximum count valueTMR2=0;// initialize timer 2 counter registerT2CON = 0x0; // Configure T2 Control register with prescalar 1:1 and internal clock source// Interrupt Setup for Timer2 In datasheet: TABLE 7-1: INTERRUPT IRQ, VECTOR AND BIT LOCATIONIFS0CLR = 0x0100; // clear Timer2 interrupt flag, IFS0<8>IEC0CLR= 0x0100; // clear Timer2 interrupt, IEC0<8>IPC2CLR = 0x001F; // clear Timer2 priority/subpriority fields IPC2<4:0>IPC2SET = 0x0010; // set Timer2 int priority = 4, IPC2<4:2>IPC2SET = 0x0000; // set Timer2 int subpriority = 0, IPC2<1:0>IEC0SET= 0x0100; // Enable Timer2 interrupt, IEC0<8>T2CONSET = 0x8000; // enable or start timer 2__builtin_enable_interrupts(); // enabling interruptsINTCONbits.MVEC = 1; // enable Multi Vector Interrupts}while(1);if(PORTDbits.RD13==0){// Turn on 16-bit Timer3, set prescaler to 1:256 (frequency is Pbclk / 256)T3CON = 0;PR3 = 200; // Load the period register 3 with maximum count valueTMR3=0;// initialize timer 3 counter register//__builtin_disable_interrupts(); // disabling interrupts// Interrupt Setup for Timer3 In datasheet: TABLE 7-1: INTERRUPT IRQ, VECTOR AND BIT LOCATIONIFS0bits.T3IF = 0; // clear Timer3 interrupt flagIEC0bits.T3IE=0; // clear Timer3 interruptIPC3bits.T3IP = 4; // Set Timer3 priority to 7IPC3bits.OC3IS = 0; // set Timer3 int subpriority = 0IEC0bits.T2IE= 1; // Enable Timer2 interruptIEC0bits.T3IE=1; // Enable Timer3 interruptT3CONSET = 0x8000; // enable or start timer 3__builtin_enable_interrupts(); // enabling interruptsINTCONbits.MVEC = 1; // enable Multi Vector Interrupts}// end ifwhile(1);} // end main// Timer2 Interrupt Service Routine (ISR))void __ISR(_TIMER_2_VECTOR,ipl4srs) Timer2Handler(void){LATAbits.LATA0 = ~LATAbits.LATA0;/* ISR code inserted here */IFS0CLR = 0x0100; // clear Timer2 int flag, IFS0<8>}// Timer3 Interrupt Service Routine (ISR))void __ISR(_TIMER_3_VECTOR,ipl4srs) Timer3Handler(void){LATAbits.LATA1 = ~LATAbits.LATA1;/* ISR code inserted here */IFS0bits.T3IF = 0; // clear Timer3 int flag}

author: cv007
author_rank: Frequent Contributor
timestamp: November 08, 2020, 08:12:13 am
content: while(1);You will never make it past that line to get to the timer3 setup code.You may also want to start using the SET/CLR register versions when manipulating pins like LAT or clearing flags, or anything else where atomic access is needed.

author: abrarbaig
author_rank: Contributor
timestamp: November 08, 2020, 08:34:49 am
content: Thanks for your reply. I commented out the first while(1); line Now both int. fired. Looks like conditional statements are ignored. How can I make it conditional.

author: TomS_
author_rank: Frequent Contributor
timestamp: November 13, 2020, 08:03:53 am
content: What are you trying to make conditional? At the moment you enable both timers if some pins are logic low, and evidently both of those pins must be low if the two interrupts are firing. But after that there are no conditional statements anywhere else.

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 13, 2020, 03:43:13 pm
content: Yeah. The first "while(1);" is unconditionally executed, so anything after that would never execute. What did you expect?Did you want to actually execute the second part (after this first "while(1);" only after the first interrupt has fired? If so, you'd need to add a way of getting out of this 'while()'. By setting a flag in the first ISR for instance. And if that was not what you wanted to achieve, then you need to explain.

author: abrarbaig
author_rank: Contributor
timestamp: November 22, 2020, 06:10:25 am
content: I need to fire the interrupt based on condition.If one pin( RD6) goes low, then first int fires,if the other pin(RD13) goes low, then the other int fires.

author: abrarbaig
author_rank: Contributor
timestamp: November 22, 2020, 06:12:31 am
content: I meant RD5