View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
Timer1 on Pic24fj128ga010 |
Posted: Fri Feb 17, 2017 10:01 am |
|
|
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: 1345
|
|
Posted: Fri Feb 17, 2017 11:04 am |
|
|
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: 19494
|
|
Posted: Fri Feb 17, 2017 12:12 pm |
|
|
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: 1345
|
|
Posted: Fri Feb 17, 2017 1:03 pm |
|
|
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
|
|
Posted: Fri Feb 17, 2017 2:28 pm |
|
|
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: 19494
|
|
Posted: Sat Feb 18, 2017 12:36 am |
|
|
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
|
|
Posted: Mon Feb 20, 2017 2:09 am |
|
|
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: 19494
|
|
Posted: Mon Feb 20, 2017 3:05 am |
|
|
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
|
|
Posted: Wed Feb 22, 2017 10:25 am |
|
|
Hi,
Just to say ok, it's work....
The correct response is the first, thanks to jeremiah ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19494
|
|
Posted: Wed Feb 22, 2017 10:32 am |
|
|
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. |
|
|
|