View previous topic :: View next topic |
Author |
Message |
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
Timer setting on pic24JG16GA004 |
Posted: Sat Jan 15, 2022 11:47 am |
|
|
Dear friends,
I have a problem when i try to set my timer.
I'm not able to understand correctly how it works.
My code
Code: |
#FUSES FRC_PLL, NOIOL1WAY, NOPROTECT, NOWDT, NOWRT, NOOSCIO, NODEBUG,SOSC,NOPR,NOCKSFSM
#USE delay(internal=32MHz)
setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64,7500);
enable_interrupts(int_timer1);
|
And in my interruption (overflow) function :
Code: |
#int_timer1
void timer1()
{
disable_interrupts(int_timer1);
output_high(led_rouge);
delay_ms(1);
output_low(led_rouge);
setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64,7500);
enable_interrupts(int_timer1);
}
|
it's work but i can't validate by calculation...
For me, the formula is :
Freq = (Fosc) / { Tick * TMR_div * (16bits - value))
with Fosc = 32Mhz
Tick = 2 (4 for a 16F/18F)
TMR_DIV = 64
Value = 7500.
My scope say me 33Hz / by calculation 4 hertz...
What is wrong ?
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sat Jan 15, 2022 1:02 pm |
|
|
Peripherals in the 16bit PIC's, by default are fed with Fosc/2.
Your timer will give:
32000000/(2*7500*64) = 33.3Hz.
Your calculation is simply wrong......
Don't see where you are getting '4' from anywhere....
You have the right values, but the maths is just wrong.
Freq = (Fosc) / { Tick * TMR_div * (16bits - value))
= 32000000/(2*64 *7500)
32000000/960000 = 33.33
No '4' anywhere!...... |
|
|
joseph20480
Joined: 21 Dec 2011 Posts: 42
|
|
Posted: Sat Jan 15, 2022 1:49 pm |
|
|
2 for 16bits mcu
4 for 8 bits mcu
... the number clock needed per instructions.
Thanks a lot
[Solved !]
Ttelmah wrote: | Peripherals in the 16bit PIC's, by default are fed with Fosc/2.
Your timer will give:
32000000/(2*7500*64) = 33.3Hz.
Your calculation is simply wrong......
Don't see where you are getting '4' from anywhere....
You have the right values, but the maths is just wrong.
Freq = (Fosc) / { Tick * TMR_div * (16bits - value))
= 32000000/(2*64 *7500)
32000000/960000 = 33.33
No '4' anywhere!...... |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sun Jan 16, 2022 4:29 am |
|
|
You were saying "by calculation 4 hertz...". That is simply wrong. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sun Jan 16, 2022 7:28 am |
|
|
Some more comments though on the code posted:
Code: |
#int_timer1
void timer1()
{
//disable_interrupts(int_timer1); //not wanted or needed. The
/interrupt is automatically disabled inside the handler
output_high(led_rouge);
delay_ms(1);
output_low(led_rouge);
//setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64,7500); //Not wanted
//or needed. The timer keeps it's settings.
//enable_interrupts(int_timer1); //again not wanted or needed.
}
|
Hopefully using the delay inside here was just for test. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1346
|
|
Posted: Sun Jan 16, 2022 9:50 am |
|
|
For some reason I also thought you wanted to put a period one less than your desired value? For example with a 32KHz crystal we typically load 32767 instead of 32768 for a 1 second ISR. I feel like the IF flag triggers after period value instead of immediately? Or am I remembering wrong. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sun Jan 16, 2022 11:05 am |
|
|
No, you are remembering exactly right. However the time result is still
33.3, the difference is only in the next decimal. |
|
|
|