|
|
View previous topic :: View next topic |
Author |
Message |
iVoVa
Joined: 12 Mar 2011 Posts: 7
|
Explain about code timer0, help me ! |
Posted: Sat Mar 12, 2011 11:39 pm |
|
|
Code: |
#include <18F4520.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=4000000)
#byte PORTB = 0xF81
int16 count = 0;
int8 a;
//Chuong trinh ngat TMR0
#int_timer0
void interrupt_timer0()
{
set_timer0(0x23);
++count;
if(count == 8)// 8*131ms ~ 1s
{
count=0;
rotate_left(&a,1);
}
}//
void main(void)
{
set_tris_b(0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
set_timer0(0x23);// T_out = 2*(65535 - 35)*1us = 131ms
enable_interrupts(global);
enable_interrupts(int_timer0);
a = 0x01;
while(true)
{
PORTB = a;
}
}
|
I use Timer to set PORTB = 0x01,0x02,0x04........delay within time is 1 sec.
I have imitated with Proteus and it run correct. But I do not understand how timer0 can be recount with this code. I think that PORTB = 0x01, when timer is overflowed, PORTB = 0x02, Program will stop here. Help me explain why Timer0 recount without enable_interrupt(init_timer0) .
I am Vietnamese, so my english skill is so bad. Please try to understand what I said. Thanks for reading.
Last edited by iVoVa on Sun Mar 13, 2011 6:54 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sun Mar 13, 2011 4:00 am |
|
|
OK.
Think of the timer, like a sort of clock face. It is numbered from 0 (at the top), round to 65535.
Behind it is a gearbox, so it takes 65536 turns of the input shaft to go round once.
Then behind this is another gearbox, with a number of output shafts fed from the motor. The motor goes through a 4:1 reduction, then feeds the first shaft. There are four output shafts. One at the motor speed/4, one at half this, one at 1/4 this, and one at one eighth this.
Then there is one final thing. At the top of the face, is a bell. This rings when the hand goes from 65535, to 0.
When you start, the motor is spinning, but none of the four shafts connect to the main 'face' gearbox. Then:
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
Connects the shaft at motor/8 to the face gearbox.
Then:
set_timer0(0x23);
Moves the hand forwards to '35'.
The hand now moves, and counts round to the top, where the bell rings.
enable_interrupts(global);
enable_interrupts(int_timer0);
says 'when the bell rings, execute the int_timer0 code'.
At no point is the gearbox disconnected (setup_timer_0(RTCC_OFF)), or the bell stopped (disable_interrupts(INT_TIMER0)), so the bell keeps ringing at regular intervals (close to one second).
The timing will not be perfect, since the hand moves forwards a little, between the bell going off (timer interrupt triggering), and the code actually reaching the instruction to move the hand forward a little in the interrupt code. These counts will be 'extra', So the code will take this little amount extra each second, and will run about 0.1% slow.
The point is that once started, the hand keeps going round, and so the interrupt code will keep being called.
Best Wishes |
|
|
iVoVa
Joined: 12 Mar 2011 Posts: 7
|
|
Posted: Sun Mar 13, 2011 6:58 am |
|
|
@Ttelmah : Thanks for helping ! |
|
|
|
|
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
|