8051 Timer Example : To generate a 2sec ON and 1sec OFF PWM pulse
by using Timer 0 Mode 1 16-bit
operation.
#include
<reg51.h>
#include
<stdio.h>
sbit pwm = P1^0;
void
Timer0_delay(unsigned char);
void main()
{
while(1)
{
pwm = 0; //port 1 pin 1 is
low
Timer0_delay(100); // 1sec time delay by
using timer
pwm = 1; //port 1 pi1 1 is high
Timer0_delay(200); //2sec time delay by using timer
} ;
}
void
Timer0_delay(unsigned char x)
{
for (;x>0;x--) // loop rotates
up to above given value.
{
TMOD = 1; //Timer 0 mode
1 16-bit operation
TH0 = 0xD8; //load register
value by calculating 65536-10000=55536
TL0 = 0xF0; //55536 hex
value is 0xD8F0 load this value to register.
TR0 = 1; // command is used to start timer and
increment the
while(TF0!=1); //TH TL
register values up to over flow
will occur.
TF0=0; // timer 0
over flag we need to reset again
use.
};
}