#include <reg52.h>
#define uchar unsigned char
sbit output = P2^0; //
uchar period = 250;
uchar high = 50;
uchar tcount = 0;
void main()
{
TMOD = 0x01; // set Timer1, working at mode 1(16 bit timer)
TH0 = (65536 - 10000) / 256; // 10ms定時時間
TL0 = (65536 - 10000) % 256;
EA = 1; // open global interrupt
ET0 = 1; // open Timer0 interrupt
TR0 = 1; // start Timer0
while(1);
}
void timer0() interrupt 1 using 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
tcount++;
if(tcount == high) //關鍵兩步:高電平時間high,之后輸出0
{
output = 0;
}else if(tcount == period){
tcount = 0;
output = 1; //一周期結束后重新高電平
}
}
|