CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC24E and RTC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
championx



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

PIC24E and RTC
PostPosted: Fri Aug 15, 2014 10:01 am     Reply with quote

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: 19490

View user's profile Send private message

PostPosted: Fri Aug 15, 2014 2:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 06, 2015 11:56 am     Reply with quote

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: 19490

View user's profile Send private message

PostPosted: Wed May 06, 2015 1:07 pm     Reply with quote

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: 1344

View user's profile Send private message

PostPosted: Wed May 06, 2015 5:44 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 06, 2015 7:26 pm     Reply with quote

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: 19490

View user's profile Send private message

PostPosted: Thu May 07, 2015 12:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 07, 2015 8:37 am     Reply with quote

Thanks Ttelmah, that did the trick! it was the stack.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group