Hello. I am trying to fire timer2 and timer3 interrupts by using statement. But only timer2 interrupt fires. Please help. Here is my 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 Frequency
void main()
{ // Start of main
// Setting Up Inputs and Outputs
TRISAbits.TRISA0 = 0; // Set RA0 as digital output
TRISAbits.TRISA1 = 0; // Set RA1 as digital output
TRISDbits.TRISD5 = 1; // Set RD5 as digital input
TRISDbits.TRISD13 = 1; // Set RD13 as digital input
// Initializing outputs
LATAbits.LATA0 = 0; // RA0 is set to 0
LATAbits.LATA1 = 0; // RA1 is set to 0
// Reading the inputs
PORTDbits.RD5;
PORTDbits.RD13;
if(PORTDbits.RD5==0)
{
__builtin_disable_interrupts(); // disabling interrupts
T2CON = 0;
PR2 = 200; // Load the period register 2 with maximum count value
TMR2=0;// initialize timer 2 counter register
T2CON = 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 LOCATION
IFS0CLR = 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 interrupts
INTCONbits.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 value
TMR3=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 LOCATION
IFS0bits.T3IF = 0; // clear Timer3 interrupt flag
IEC0bits.T3IE=0; // clear Timer3 interrupt
IPC3bits.T3IP = 4; // Set Timer3 priority to 7
IPC3bits.OC3IS = 0; // set Timer3 int subpriority = 0
IEC0bits.T2IE= 1; // Enable Timer2 interrupt
IEC0bits.T3IE=1; // Enable Timer3 interrupt
T3CONSET = 0x8000; // enable or start timer 3
__builtin_enable_interrupts(); // enabling interrupts
INTCONbits.MVEC = 1; // enable Multi Vector Interrupts
}// end if
while(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
}