|
|
View previous topic :: View next topic |
Author |
Message |
tcruise7771
Joined: 12 Apr 2013 Posts: 24
|
Timer 2 Question. |
Posted: Tue Jun 25, 2013 5:49 am |
|
|
Hello .
I have a question about Timer 2. I have a program for PWM.
I am running it on PIC18F4550 at 5MHz using external crystal of 20MHz, and fuses HS, PLL5 and CPUDIV4 ( so it will run at 5MHz).
Anyway the program runs fine, i am using setup_timer_2(T2_DIV_BY_16, 255, 1); to set the PWM frequency = 5 000 0000 / 4instruction/ 16/255 = 306.372 Hz. I am also using the timer2 for interrupt with #int_timer2 and void timer2_isr(void), which is called during my program and also works fine. Tested the frequency with oscilloscopes and it is exactly 306.3 Hz
My question is, how i can calculate the time it takes the timer 2 to increment with the above setup. Is it 1000ms/306.372Hz= 3.264 ms for increment and 3.264*255=832 ms to overflow and interrupt? Or is it 3.264 ms for overflow ?
According to CCS C manual :
Code: |
setup_timer_2 ( T2_DIV_BY_4, 0xc0, 2);
// At 20mhz, the timer will increment every 800ns,
// will overflow every 154.4us,
// and will interrupt every 308.8us.
|
But i couldn't understand whats the formula ? 20 000 000 / 4 instructions / 4 (T2DIV) / 192(c0) / 2^2(prescaler2) ? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Jun 25, 2013 6:32 am |
|
|
Quote: | Anyway the program runs fine , i am using setup_timer_2(T2_DIV_BY_16, 255, 1); to set the PWM frequency = 5 000 0000 / 4instruction/ 16/255 = 306.372 Hz. I am also using the timer2 for interrupt with #int_timer2 and void timer2_isr(void) , which is called during my program and also works fine . Tested the frequency with oscilloscopes and it is exactly 306.3 Hz |
With your setup_timer_2, the repeat frequency is 5,000,000/4/16/256.
Equates to 305.18Hz.
The 255 in your setup is 1 less than the period.
Using small values for the period, you will see the differnce more easily on your 'scope.
Timer2 is incrementing every 16 machine cycles because of the DIV_BY_16.
The last parameter (x) tells the timer_2 to trigger an interrupt after x repeats.
It's all in the data sheet and the CCS manual.
You have to read both, very carefully.
Mike |
|
|
tcruise7771
Joined: 12 Apr 2013 Posts: 24
|
|
Posted: Tue Jun 25, 2013 6:50 am |
|
|
Mike you are right , i just checked the video i made for the comparison with real oscilloscope and it was showing 305.1 but somehow i remembered it showed exact value.
You are right that i should read the datasheets, but right now i am out of time cos i am finishing up my diploma ( like a huge senior project ) that has 4 modules( PCBs with PICs ) that i had to prepare for litterally no time and i have to present it before 1st july, so when i was making everything i was just trying to make it run as desired without fully and deeply understanding everything. Thats why i have been asking couple of redicilous questions for the last week
Thanks for explaining it so clear to me. Btw the last value, the prescaler will have no meaning to the PWM frequency signal right , the frequency will be the same it will just wait more for the interrupt ? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Jun 25, 2013 7:35 am |
|
|
Quote: | Btw the last value, the prescaler will have no meaning to the PWM frequency signal right , the frequency will be the same it will just wait more for the interrupt ? | Yes. Like I said, it's all in the data sheet.
Mike
BTW
Big risk, putting your head on the block, without a good understanding of what you are claiming as your own work.
Welcome to the real world. What happens when the adjudicators ask awkward questions? |
|
|
|
|
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
|