Timer2 for PIC32MX

Timer2 for PIC32MX

Postby Felix889 » Thu Jun 10, 2021 9:16 am

Hello,

Could someone please explain what is the proper way to use the Timer2 interrupt?
I found a lot of examples online on the official MICROCHIP website, but every example has an extra line of code.
If there are any design guidelines or documents where this information can be found, apart from the standard PIC32 datasheets?

I am trying to use Timer2 interrupt to enable LED when it is triggered, however, my code does not work for some reason.

My code:
#include<p32xxxx.h>
#include<plib.h>

// timer2 initialization
void timer2_init(void)
{
T2CONbits.TON = 0; /* turn off Timer 2 */
T2CONbits.TCKPS = 7; /* pre-scale = 1:256
PR2 = 11000;
TMR2 = 0;
}

void __ISR(_TIMER_2_VECTOR, ipl7) T2InterruptHandler(void)
{

// toggle RA1 pin
LATAbits.LATA1 ^= 1;
// reset the overflow interrupt flag
IFS0bits.T2IF = 0;
}

main()
{
// init I/Os
DDPCONbits.JTAGEN = 0;
TRISA = 0xff00; // PORTA LSB as outputs which are connected to LEDs
PORTA = 0;

// initialize timer
timer2_init();

IPC2bits.T2IP = 7;
IFS0bits.T2IF = 0;
IEC0bits.T2IE = 1;
T2CONbits.TON = 1;

while(1)
{
// just wait here
}
}// main

I would appreciate it if someone could help me with that.
Thank you.
Felix889
 
Posts: 5
Joined: Thu Jun 10, 2021 8:56 am

Re: Timer2 for PIC32MX

Postby AussieSusan » Tue Jun 15, 2021 3:41 am

Can you please expand on "...my code does not work..."?
What do you expect to see but don't? What have you tried to do and what were the results?
Have you tried running with the debugger to see what code is executed and when? (Does it even run at all?)
Can you show us the CONFIG options.
What compiler are you using? I'm guessing it is NOT the XC32.
You don't need the 'plib' include at this stage.
AussieSusan
Verified identity
 
Posts: 157
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Timer2 for PIC32MX

Postby Felix889 » Tue Jun 15, 2021 9:37 am

My Timer2 does not get executed because my LED in the interrupt routine never turns on.
I am using XC32 v1.00. I used a debugger and established that my interrupt is enabled and it is counting.
My interrupt routine is skipped by the debugger and the PORTA value remains to be 0 when I reach the main while(1) loop.
Using debugging I also established that IFS0bits.T2IF always remains to be 0, which means that my interrupt never gets the flag.
COSC = 0; PBDIV = 3; TCS = 0;
I hope that would help.
Could you please explain your way of setting any king of Timer interrupt if that is okay with you?
What steps do you follow and in what order, as probably that is critical and if any of those will be misplaced the interrupt will never be executed.
Thank you.
Felix889
 
Posts: 5
Joined: Thu Jun 10, 2021 8:56 am

Re: Timer2 for PIC32MX

Postby AussieSusan » Thu Jun 17, 2021 4:24 am

The XC32 compiler is now up to V3.
If yo are using the XC compilers then normally you just include "<xc.h>" and set the specific MCU that you are using in the IDE.
You don't show us the CONFIG options so I assume you have the oscillator set up to use the FRC with no post scalars.
Given that, it would seem that you are trying to flash the LED at about 3Hz.
I've not used the PIC32 devices so I assume that you have set up the ISR correctly. Also from what I can see in a random PIC32MX data sheet I chose (as you don't say which specific MCU you are using) and my knowledge of the other Microchip families, it would seem that you are o the right track with your code.
What happens if you set a breakpoint in the ISR? I must admit I don't understand what you mean when you say the debugger skips the ISR but setting a breakpoint in there should tell you for certain if it is being called. However I would be looking at the LATA register with the debugger as the PORT will always read 0 in analog mode.
Susan
I note that you don't clear the ANSELA bits so the RA0 an dRA1 are operating in analog mode. That should not impact your setting an output value though.
AussieSusan
Verified identity
 
Posts: 157
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Timer2 for PIC32MX

Postby Felix889 » Thu Jun 17, 2021 3:02 pm

Thank you for you reply.
Sorry, I am using PIC32MX360F512L. I did not show the CONFIG options because I do not know wo to use it.
This is my first time I am trying to set Timer2 interrupt.
When I put a breakpoint in the ISR routine, it gets broken (Broken Breakpoint) by the compiler when I debug the project. That is why I assumed my ISR is just being shipped all the time.
I have added LATA register to my watches screen and it have exactly the same value as my PORTA.
I am not clearing the ANSELA bits.
Felix889
 
Posts: 5
Joined: Thu Jun 10, 2021 8:56 am

Re: Timer2 for PIC32MX

Postby Felix889 » Thu Jun 17, 2021 3:22 pm

I have updated my MPLAB X to v5.30 and my XC to v2.30 and slightly modified the code and it is working now.

The code:
#include<xc.h>
#include <sys/attribs.h>


int divider = 0;

//timer2 initialization
void timer2_init (void)
{
T2CONbits.TON = 0; /* turn off Timer 2 */
T2CONbits.TCKPS = 7; /* pre-scale = 1:256 (T2CLKIN = 39062.5 Hz) */
PR2 = 11000;
TMR2 = 0;
}

__ISR(_TIMER_2_VECTOR, IPL1SOFT) Timer2Handler(void)
{
// toggle RD1 pin
LATAbits.LATA1 ^= 1;
IFS0bits.T2IF = 0;
}

main ()
{
DDPCONbits.JTAGEN = 0;
TRISA = 0xff00;
PORTA = 0;

//initialise timer
timer2_init();

//initialise interrupt
IPC2bits.T2IP = 1;
IFS0bits.T2IF = 0;
INTCONSET = _INTCON_MVEC_MASK;
INTCONbits.MVEC = 1;
__builtin_enable_interrupts();
IEC0bits.T2IE = 1;
T2CONbits.TON = 1;

while(1)
{
divider++;
if(divider==50000)
{
LATAbits.LATA0 ^= 1;
divider = 0;
}
}

}

Could you please explain how can I learn to set the CONFIG options and what are the proper steps of setting an interrupt?
Do we need to use:
INTCONSET = _INTCON_MVEC_MASK;
INTCONbits.MVEC = 1;
__builtin_enable_interrupts();

Thanks a lot for your help.
Felix889
 
Posts: 5
Joined: Thu Jun 10, 2021 8:56 am

Re: Timer2 for PIC32MX

Postby AussieSusan » Fri Jun 18, 2021 3:40 am

I really can't help you with setting up interrupts on the PIC32 devices as I've never used them. However the compiler user guide should have what you need. Also try using MCC (if your chip is supported) and see what it generates.
As for the CONFIG options, look in the 'Special Features' chapter of the data sheet. It will show all of the available options and their default settings. If you don't specify a CONFIG value, the default (in most cases I am aware of but this may not be true for the PIC32 devices) for each bit is '1'. As these are set in the "#pragma config" lines they typically use the name of the field in the data sheet and the values are often also in the data sheet. The User Guide mentions a palce where you can look for the config setting values.
Susan
AussieSusan
Verified identity
 
Posts: 157
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Timer2 for PIC32MX

Postby TJvV » Fri Jun 18, 2021 8:28 am

Hi,

Regarding the CONFIG options, you can find the details in the datasheet as Susan said.
What also really helps, is to open MPLAB X and go to Window > Target Memory Views > Configuration Bits.
This will open an editor where you can select the options you want and click Generate Source Code to Output.
That will print the actual code that you can copy and paste into your program.

I just noticed that RA0 pin is also used for the JTAG TMS signal in the PIC32MX3/4/5/6/7xx families.
Maybe disabling JTAG in the config bits will help.
TJvV
 
Posts: 1
Joined: Fri Jun 18, 2021 8:09 am
PIC experience: Professional 5+ years with MCHP products

Re: Timer2 for PIC32MX

Postby Felix889 » Fri Jun 18, 2021 9:27 am

Thanks a lot for your help guys.
Felix889
 
Posts: 5
Joined: Thu Jun 10, 2021 8:56 am


Return to PIC32 topics

Who is online

Users browsing this forum: No registered users and 3 guests