新手学到51单片机定时器:写一个定时器1计时1秒,让P2口灯以不同频率闪烁,各位前辈看到多指教下:
#include<reg51.h>
typedef unsigned char u8;
typedef unsigned int u16;
u8 a,b;
sbit led0 = P2^0;
sbit led1 = P2^1;
sbit led2 = P2^2;
sbit led3 = P2^3;
void time0_init(void)
{
TMOD = 0x01;
TH0 = 0x4b;
TL0 = 0xfc;
TR0 = 1;
}
void time(void)
{
if(TF0 == 1)
{
TH0 = 0x4b;
TL0 = 0xfc;
TF0 = 0;
a++;
}
if(a == 20)
{
a = 0;
b++;
}
}
void d0(void)
{
led0 = b % 2;
led1 = ~led0;
led3 = b % 4;
//led3 = b % 60;
}
void main()
{
time0_init();
while(1)
{
time();
d0();
}
}
|