How to Use Timer-0 of AT89C51 in Mode-0 (13-bit mode)
When the timer-0 is in 13-bit mode, the timer can only contains 8192 values. It will overflow back to zero after 8192 counts. In this mode, 0-4 bits of TL0 register and 0-7 bits of TH0 register are used.
1.1 Initialization
The following function initialize the timer-0 in mode-0 with internal ON/OFF control configuration.
void InitTimer0Mode0(void)
{
// reset timer-0 higher byte register
TH0 = 0x00;
// reset timer-0 lower byte register
TL0 = 0x00;
// GATE1– C/T1– T1M1– T1M0– GATE0– C/T0– T0M1– T0M0
// 0 0 0 0 0 0 0 0
TMOD = 0x00;
}
{
// reset timer-0 higher byte register
TH0 = 0x00;
// reset timer-0 lower byte register
TL0 = 0x00;
// GATE1– C/T1– T1M1– T1M0– GATE0– C/T0– T0M1– T0M0
// 0 0 0 0 0 0 0 0
TMOD = 0x00;
}
1.2 Start Timer
The following function start timer-0 from software.
void StartTimer0(void)
{
// set timer-0 run bit (in TCON register)
TR0 = 1;
}
{
// set timer-0 run bit (in TCON register)
TR0 = 1;
}
1.3 Stop Timer
The following function stop timer-0 from software.
void StopTimer0(void)
{
// reset timer-0 run bit (in TCON register)
TR0 = 0;
}
{
// reset timer-0 run bit (in TCON register)
TR0 = 0;
}
1.4 Monitor Timer-0 Overflow
The following function monitor overflow of timer-0 in mode-0. It return set bit when timer-0 is overflow.
bit MonOverFlowTimer0(void)
{
// this bit is set by controller when timer value
// reach its maximum value 8192
return TF0;
}
{
// this bit is set by controller when timer value
// reach its maximum value 8192
return TF0;
}
1.5 Reset Timer-0 Overflow Bit
As timer-0 overflow bit is set automatically by the controller but it has to be reset manually. The following function reset the overflow bit of timer-0.
void ResetOverFlowBitTimer0(void)
{
// this bit is clear manually
TF0 = 0;
}
{
// this bit is clear manually
TF0 = 0;
}
1.6 Pre-Set Timer-0 Registers
You can load maximum 8191 value in the timer-0 data registers. If you load 2000 value in the timer-0 registers, it means timer will overflow after 2000 timer pulses. In the following function you can pass up to 8192 value.
void SetValTimer0Mode0(unsigned int tick_val)
{
unsigned int val = 8192 – tick_val;
TH0 = val / 32;
TL0 = val % 32;
}
{
unsigned int val = 8192 – tick_val;
TH0 = val / 32;
TL0 = val % 32;
}
1.7 Download APIs
You can download complete set of APIs here.
1.8 Example of Timer-0 Mode-0 (Software ON/OFF)
This example toggles the port-1 of controller with a timer cycle delay. You are free to reuse anywhere and can be download complete project here.
1.9 Example of Timer-0 Mode-0 (External ON/OFF)
This example toggles the port-1 of controller with a timer cycle delay, but the timer ON/OFF control is from external. When P3.2 (INT0) bit is high then timer-0 is start and when this bit is low then timer-0 is off. Only the GATE0 bit is set in TMOD register. The following function initialize the timer-0 in mode-0 with external ON/OFF control.
void InitTimer0Mode0(void)
{
// reset timer-0 higher byte register
TH0 = 0x00;
// reset timer-0 lower byte register
TL0 = 0x00;
// GATE1– C/T1– T1M1– T1M0– GATE0– C/T0– T0M1– T0M0
// 0 0 0 0 1 0 0 0
TMOD = 0x08;
}
{
// reset timer-0 higher byte register
TH0 = 0x00;
// reset timer-0 lower byte register
TL0 = 0x00;
// GATE1– C/T1– T1M1– T1M0– GATE0– C/T0– T0M1– T0M0
// 0 0 0 0 1 0 0 0
TMOD = 0x08;
}
You are free to reuse anywhere and can be download complete project here.
Comments
Post a Comment