利用定时器控制LED (2)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验七利用定时器控制LED
一实验目的掌握定时中断的简单编程:中断的使用。

二实验内容MCS-51的P0口接8个LED,共阳极,用定时器编程实现LED的循环点亮。

三实验程序
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar T_Count = 0;
void main()
{
P0 = 0xfe;
TMOD = 0x01;0000 0001
TH0 = (65536-4000)/256;
TL0 = (65536-4000)%256;
EA=1;
ET0=1;
TR0 = 1;
while(1);
}
void tmer_0() interrupt 1
{ TH0 = (65536-4000)/256;
TL0 = (65536-4000)%256;
if(++T_Count == 100)
{
P0 = _crol_(P0,1);
T_Count = 0;
}
}
四思考(要求上交程序和仿真截图)
1 修改程序,要求1S灯循环一个
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar T_Count = 0;
void main()
{
P0 = 0xfe;
TMOD = 0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
EA=1;
ET0=1;
TR0 = 1;
while(1);
}
void tmer_0() interrupt 1
{ TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
if(++T_Count == 20)
{
P0 = _crol_(P0,1);
T_Count = 0;
}
}
2、用T1实现定时,500ms亮一个灯。

#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar T_Count = 0;
void main()
{
P0 = 0xfe;
TMOD = 0x10;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
EA=1;
ET1=1;
TR1 = 1;
while(1);
}
void tmer_0() interrup t 3
{ TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
if(++T_Count == 10)
{
P0 = _crol_(P0,1);
T_Count = 0;
}
}
六实验总结
MOV A,23H ADD A,#99H DA A MOV 23H,A CJNE A,#99H,EXIT MOV 23H,#23H NE XT3:
CJNE R1,#02H,NEXT4 选择键功能标志为2,调分,否则跳出 MOV A,22H ADD A,#99H DA A MOV 22H,A CJNE A,#99H,EXIT MOV 22H,#59H NEXT4:
CJNE R1,#03H,EXIT 选择键功能标志为3,调秒,否则跳出 MOV 21H,#00H 如较少键按下直接清零秒 RET
/*****强制响铃键处理子程序*****/ DALING_KEY:
CJNE R5,#08H,EXIT 选择键键值 CPL BEEP RET
/*****万用返回程序*****/ EXIT: RET
/*****数码管字形编码表*****/ TABLE:
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0FFH ;字形显示编码
TABLE1:
DB 0FFH,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0FFH ;小时位的十位数编码,该位如果为0则不显示
/*****打铃时间对比程序(起床、熄灯)*****/ BIJIAO1: MOV A,23H CJNE A,#06H,B0 MOV A,22H
CJNE A,#00H,B0 6:00到 SETB 45H 开启响铃方式1 RET
B0: MOV A,23H CJNE A,#22H,EXIT_1 MOV A,22H
CJNE A,#30H,EXIT_1 22:30到 SETB 45H 开启响铃方式1 RET EXIT_1: CLR 45H RET
/*****打铃时间对比程序(上、下课)*****/ BIJIAO2: MOV A,23H CJNE A,#07H,B1 MOV A,22H
CJNE A,#30H,B1 7:30 SETB 44H 开启响铃方式2 RET
B1: MOV A,23H CJNE A,#08H,B2 MOV A,22H
CJNE A,#20H,B2 8:20 SETB 44H
RET
B2: MOV A,23H CJNE A,#08H,B3 MOV A,22H
CJNE A,#30H,B3 8:30 SETB 44H RET
B3: MOV A,23H CJNE A,#09H,B4 MOV A,22H
CJNE A,#20H,B4 9:20 SETB 44H RET
B4: MOV A,23H CJNE A,#09H,B5 MOV A,22H
CJNE A,#40H,B5 9:40 SETB 44H RET
B5: MOV A,23H CJNE A,#10H,B6 MOV A,22H
CJNE A,#30H,B6 10:30 SETB 44H RET
B6: MOV A,23H CJNE A,#10H,B7 MOV A,22H
CJNE A,#40H,B7 10:40 SETB 44H RET
B7: MOV A,23H CJNE A,#11H,EXIT_2 MOV A,22H
CJNE A,#30H,EXIT_2 11:30
SETB 44H RET EXIT_2: CLR 44H RET
/*****响铃方式1程序(响5秒停2秒再响5秒)*****/ DALING1:
JNB 45H,EXIT_DALING
MOV A,21H 响铃起始时间由秒实时控制 CJNE A,#00H,L1 CLR BEEP L1: MOV A,21H CJNE A,#05H,L2 SETB BEEP L2:
CJNE A,#07H,L3 CLR BEEP L3:
CJNE A,#12H,EXIT_DALING SETB BEEP RET
/*****响铃方式2程序(响5秒)*****/ DALING2:
JNB 44H,EXIT_DALING
MOV A,21H 响铃起始时间由秒实时控制 CJNE A,#00H,L4 CLR BEEP L4: MOV A,21H
CJNE A,#05H,EXIT_DALING SETB BEEP RET
EXIT_DALING: RET
END 程序结束。

相关文档
最新文档