已知51单片机89C51的晶振为24Mhz,请利用定时器0定时的方法在P1.0口输出频率为100Hz,占空比为3⼀5的矩形波

2024-12-25 18:22:02
推荐回答(2个)
回答1:

START:MOV R0,#5
MOV TMOD,#01H
MOV TH0,#0B1H
MOV TL0,#0E0H
SETB TR0
NEXT: JB TF0,NEXT1
SJMP NEXT
NEXT1:CJNE R0,#5,NEXT2
CLR TF0
SETB P1.0
SJMP LAST
NEXT2:CJNE R0,#4,NEXT3
CLR TF0
SETB P1.0
SJMP LAST
NEXT3:CJNE R0,#3,NEXT4
CLR TF0
SETB P1.0
SJMP LAST
NEXT4:CJNE R0,#2,NEXT5
CLR TF0
SETB P1.0
SJMP LAST
NEXT5:CJNE R0,#1,LAST
CLR TF0
CLR P1.0
SJMP LAST
LAST:DEC R0
CJNE R0,#00H,FZH
SJMP NEXT
FZH: SJMP START
END

回答2:

#include

#define uchar unsigned char
uchar cnt;
sbit pluse=P1^0;
void t0isr() interrupt 1
{
cnt++;
if(cnt<120)pluse=1;
else pluse=0;
if(cnt>200)cnt=0;
}
main()
{
TMOD=0x01;
TH0=156;
TL0=156;
TR0=1;
ET0=1;
EA=1;
while(1);
}