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

Timer1 on Pic24fj128ga010

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

Timer1 on Pic24fj128ga010
PostPosted: Fri Feb 17, 2017 10:01 am     Reply with quote

hi,
I'm working on the explorer16 demo board with a pic24fj128ga010 (PIM module).
My question is about the timer1.

Code:

//----------------------------------------
#device pic24fj128GA010
#include <24fj128GA010.h>
#fuses NOPROTECT,NOWDT
#use delay(internal=8000000)
//----------------------------------------


Code:

//-------------------------------------------------------
#int_timer1
void interruption_timer1()
{
   flash_led();
   set_timer1(value_timer1);
}
//-------------------------------------------------------


Code:

//-------------------------------------------------------
   // Timer1 - interruption 2-4 fois secondes
   setup_timer1(TMR_INTERNAL|TMR_DIV_BY_256);
   set_timer1(value_timer1);

   enable_interrupts(int_timer1);
   enable_interrupts(global);
//--------------------------------------------------------


My calculation:
Quote:

Step_timer=1/(Fosc / prescaler*2) with Fosc=8Mhz, prescaler=256

if i want T=0.05sec (F=20hz)
Quote:

Timer value = 65536-(T/step_timer) = 64754.


.... With this value i obtain 16hz...
please if you see a big mistake, help me !
jeremiah



Joined: 20 Jul 2010
Posts: 1358

View user's profile Send private message

PostPosted: Fri Feb 17, 2017 11:04 am     Reply with quote

Why not just setup the timer to have a period of 0.05s?

Code:

setup_timer1(TMR_INTERNAL|TMR_DIV_BY_256, 781);  //timer interrupts every 0.05


Trying to reset the timer manually leads to latency (both from the ISR and for your code to actually set it).

Also, are you sure it is 8MHz for the internal? A lot of PIC24's have a 7.xxx internal clock instead. I can't bring up the datasheet to check.

EDIT: also, you lose some accuracy by dividing by 256. If you are able to divide by 64 and raise the period, you'll get even closer.

Code:

setup_timer1(TMR_INTERNAL|TMR_DIV_BY_64, 3124);  //timer interrupts every 0.05


Again, I can't check the data sheet to verify if div by 64 is available or not, but hopefully you get the idea.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Feb 17, 2017 12:12 pm     Reply with quote

This chip does give 8mhZ (+/- 2%) from the internal oscillator.

Of course it's really silly to do the LED flashing first. The time used for this adds to the interrupt overhead to make the timings worse.

The next divider is /8. Use this and 6249.
jeremiah



Joined: 20 Jul 2010
Posts: 1358

View user's profile Send private message

PostPosted: Fri Feb 17, 2017 1:03 pm     Reply with quote

I think it might be divide by 8 and 24999:

8MHz/2 = 4MHz => Div_by_8 => 500kHz = 2us ticks

0.05s/2us = 25000 => 24999 period value.

Look correct?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 17, 2017 2:28 pm     Reply with quote

If his flash_led() routine looks something like this, it would explain
what he's seeing:
Code:
void flash_led(void)
{
output_high(LED_PIN);
delay_ms(10);
output_low(LED_PIN);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Feb 18, 2017 12:36 am     Reply with quote

Exactly.

Not posting it, I too guessed that if the flash is meant to be visible, it must involve some time somewhere....
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Mon Feb 20, 2017 2:09 am     Reply with quote

Sir... with this time i'm not able to see if my led is blinking.
(My scope is connected to my demo board :-))

Thanks for all reply. it was weekend, i will see your solution this morning.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Mon Feb 20, 2017 3:05 am     Reply with quote

The way to have an LED show the code is working, without a delay, is to use 'output_toggle'. This way it'll come on on the first interrupt, off on the second, etc. etc..
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Wed Feb 22, 2017 10:25 am     Reply with quote

Hi,

Just to say ok, it's work....
The correct response is the first, thanks to jeremiah !
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Feb 22, 2017 10:32 am     Reply with quote

The point about Jeremiah's later reply was that you can get a more accurate division using a smaller prescaler.

TMR_DIV_BY_8, 24999

This gives exactly 0.5seconds, while the /64 version will have a (tiny) error.
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