用51单片机写一个简单的脉冲计数程序

2024-11-18 18:25:59
推荐回答(1个)
回答1:

#include
#define uint unsigned int
uint num=0,pnum=0;
sbit key=P1^0;
bit flag=0;
void ext0() interrupt 0
{
pnum++;
}
void t0isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
if(num>=20)
{
TR0=0;
EX0=0;
flag=1;
}
}
main()
{
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
SCON=0x50;
TR1=1;
TH1=0xfd;
TL1=0xfd;
ET0=1;
EA=1;
IT0=1;
while(1)
{
while(key);
while(key==0);
TR0=1;
EX0=1;
while(flag==0);
printf("%d",pnum);
}
}