Author Topic: PIC 32 Timer Interrupts  (Read 1656 times)

0 Members and 2 Guests are viewing this topic.

Offline abrarbaigTopic starter

  • Contributor
  • Posts: 30
  • Country: sa
PIC 32 Timer Interrupts
« on: November 08, 2020, 05:52:58 am »
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: [Select]
/*
 * 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
}
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 892
Re: PIC 32 Timer Interrupts
« Reply #1 on: November 08, 2020, 08:12:13 am »
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.
 

Offline abrarbaigTopic starter

  • Contributor
  • Posts: 30
  • Country: sa
Re: PIC 32 Timer Interrupts
« Reply #2 on: November 08, 2020, 08:34:49 am »
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.
 

Offline TomS_

  • Frequent Contributor
  • **
  • Posts: 857
  • Country: gb
Re: PIC 32 Timer Interrupts
« Reply #3 on: November 13, 2020, 08:03:53 am »
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.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 16382
  • Country: fr
Re: PIC 32 Timer Interrupts
« Reply #4 on: November 13, 2020, 03:43:13 pm »
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.
 

Offline abrarbaigTopic starter

  • Contributor
  • Posts: 30
  • Country: sa
Re: PIC 32 Timer Interrupts
« Reply #5 on: November 22, 2020, 06:10:25 am »
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.
 

Offline abrarbaigTopic starter

  • Contributor
  • Posts: 30
  • Country: sa
Re: PIC 32 Timer Interrupts
« Reply #6 on: November 22, 2020, 06:12:31 am »
I meant RD5
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf