一、训练任务
在CT107D单片机综合训练平台上实现LED的基本控制,首先让8路LED指示灯闪烁3遍然后熄灭,接着依次点亮LED指示灯,继电器吸合一会儿后断开,然后依次熄灭LED指示灯,蜂鸣器鸣叫一会儿后关闭,程序循环实现上述功能。
对程序进行进一步优化
二、原理简绍
2.1 原理图
2.2 ULN2003
注意是:非门
三、代码展示
#include "reg52.h" //引入51头文件 typedef unsigned int uint; typedef unsigned char uchar; //重命名 sbit HC138_A = P2^5; sbit HC138_B = P2^6; sbit HC138_C = P2^7; //sbit定义特殊功能寄存器的位变量 //定义好P25\P26\P27三个地址 //定义延时函数 void Delays(uint t) { while(t--); while(t--); } void InitHC138(uint n) { switch(n) { case 4: HC138_C = 1; HC138_B = 0; HC138_A = 0; break; case 5: HC138_C = 1; HC138_B = 0; HC138_A = 1; break; case 6: HC138_C = 1; HC138_B = 1; HC138_A = 0; break; case 7: HC138_C = 1; HC138_B = 1; HC138_A = 1; break; } } void OutPutP0(uint n, uchar dat) { InitHC138(n); P0 = dat; } void LEDRuning() { uint i; for(i = 0; i < 3; i++) { OutPutP0(4,0x00); Delays(60000); Delays(60000); OutPutP0(4,0xff); Delays(60000); Delays(60000); } for(i = 1; i <= 8; i++) { P0 = 0XFF << i; Delays(60000); Delays(60000); } OutPutP0(5,0x10); Delays(60000); Delays(60000); OutPutP0(5,0x00); for(i = 1; i <= 8; i++) { OutPutP0(4,~(0XFF << i)); Delays(60000); Delays(60000); } OutPutP0(5,0x40); Delays(60000); Delays(60000); OutPutP0(5,0x00); } void Initsys() { HC138_A = 1; HC138_B = 0; HC138_C = 1; P0 = 0X00; } void main() { while(1) { Initsys(); LEDRuning(); } }