View previous topic :: View next topic |
Author |
Message |
championx
Joined: 28 Feb 2006 Posts: 151
|
PIC24E and RTC |
Posted: Fri Aug 15, 2014 10:01 am |
|
|
Hi! I'm starting with pic24, and i have a simple question, is there a way to use the RTC module (with a 32khz crystal) and generate a 1 second interrupt on timer1 or any other timer?
Sorry, but I'm trying with this config:
Code: |
setup_rtc(RTC_ENABLE ,0); //enables internal RTCC
setup_timer1(T1_EXTERNAL_RTC);
enable_interrupts(int_timer1);
enable_interrupts(global);
|
And get an interruption every 2 seconds.
I'm using PICC 5.008 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Fri Aug 15, 2014 2:17 pm |
|
|
The 'standard' way, is just to set bit15 in the timer counter, inside the interrupt. This jumps it forward 32768 counts, so you get a 1 second tick. |
|
|
championx
Joined: 28 Feb 2006 Posts: 151
|
|
Posted: Wed May 06, 2015 11:56 am |
|
|
Hi Ttelmah, thanks for your answer, i have worked on the program and discovered that this settings are "restarting" the pic every once in a while...
If i comment this line:
Code: | setup_timer1(T1_EXTERNAL_RTC); |
or just disable the interrupt... then it works ok... no resets... Am i missing something? I just want a 1 second inerrupt.
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed May 06, 2015 1:07 pm |
|
|
The obvious reason is something you are doing in the interrupt.
I'd suggest you need to implement an error trap routine. A search here will find how to do this. My guess would be that your interrupt is causing either an address error trap, or a stack overflow. Possibly an address into an array that goes too far at some point in time, or a stack that is close to the limit, and results in an overflow, when the interrupt just happens to occur at a point in the main code with more data already on the stack. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Wed May 06, 2015 5:44 pm |
|
|
Just wanted to toss out that an optional way of setting the 1 second is in setup_timer1(). There is an optional second parameter that lets you specify the period in most PIC24 chips
If you do the following:
Code: |
setup_timer1(T1_EXTERNAL_RTC,32767);
|
It should fire every second assuming a 32768 crystal.
As far as the resets, you might just do a sanity check and look at the restart_cause() result and see if it sets as a trap or something else. There could be a bug in one of the CCS functions that enables the SW watchdog for example (I didn't see where you specified the particular part number of the PIC). It's most likely a trap (as mentioned by Ttelmah), but never hurts just to double check. |
|
|
championx
Joined: 28 Feb 2006 Posts: 151
|
|
Posted: Wed May 06, 2015 7:26 pm |
|
|
Hi Ttelmah, the interrupt just toggles a pin... nothing else... just output_toggle(LED_PIN)
When i read the restart cause it is: RESTART_TRAP_CONFLICT
Do you know why is that? The code is really simple. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu May 07, 2015 12:41 am |
|
|
Restart_cause only tells you that the restart was caused by an error trap. Not what the trap 'was'.
The point is that if you instead create a trap interrupt routine, then this will be called instead of restarting. You can then 'snapshot' the memory address that actually triggered the restart. How to do this is described here in the forum. Also, the trap interrupts are split into a series of separate ones, so there is 'ADDERR', 'STACKERR', and 'MATHERR', allowing you straight away to 'narrow down' the actual cause.
However my guess would be a stack overflow. Calling the interrupt takes a lot of stack space. What compiler version are you on?. There was a problem a while ago, with the default stack size being too small, for even quite basic programs.
Add:
#build(stack=512)
Up at the top of your program (up with the fuses).
If the problem disappears, then you have your answer.
If the compiler is old, try with stack=256 (the current default).
Some maths operations use a lot of stack. If you have a few subroutines (more on the stack), and the interrupt happens to be called from inside one using some maths, then running out of stack is easy. If your compiler is a little old, the default was 128, and this could run out, for simple things like an interrupt inside a print routine.... |
|
|
championx
Joined: 28 Feb 2006 Posts: 151
|
|
Posted: Thu May 07, 2015 8:37 am |
|
|
Thanks Ttelmah, that did the trick! it was the stack. |
|
|
|