How to Use Timer-0 of AT89C51 in Mode-1 (16-bit mode)
When the timer is in 16-bit mode, the timer can contains 65536 values. It will overflow back to zero after 65536 counts. In this mode, both TL0 and TH0 registers are used.
1.1 Initialization
The following function initialize the timer-0 in mode-1 with internal ON/OFF control configuration.
void InitTimer0Mode1(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 1
TMOD = 0x01;
}
{
// 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 1
TMOD = 0x01;
}
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-1. 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 65536
return TF0;
}
{
// this bit is set by controller when timer value
// reach its maximum value 65536
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 65535 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 65535 value.
void SetValTimer0Mode1(unsigned int tick_val)
{
unsigned int val = 65536 – tick_val;
TH0 = val / 256;
TL0 = val % 256;
}
{
unsigned int val = 65536 – tick_val;
TH0 = val / 256;
TL0 = val % 256;
}
1.7 Download APIs
You can download complete set of APIs here.
1.8 Example of Timer-0 Mode-1 (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-1 (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-1 with external ON/OFF control.
void InitTimer0Mode1(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 1
TMOD = 0x09;
}
{
// 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 1
TMOD = 0x09;
}
You are free to reuse anywhere and can be download complete project here.
Comments
Post a Comment