参考图:
程序分析:每个小灯闪两次,依次累加常亮,最后所有灯再闪烁两次。
这个程序主要是延时程序、单片机引脚口的控制,流程化控制,相对比较简单,首先还是绘制原理图,同上节一样,首先搜索对应的元件添加到元件列表当中,具体如下:
AT89C51 ----> 51单片机 BUTTON ----> 按钮 CAP ----> 普通电容 CAP-ELEC ----> 电解电容要分正负极 CRYSTAL ----> 晶振 LED-BULE ----> LED发光二极管(蓝色)*颜色可以自己选择 RESISTOR ----> 电阻
绘制原理图,如下图所示:
源代码:
//添加头文件,C51必要文件,主要定义的是51/52系列单片机 #include "reg52.h" //引用延时函数 void Delay1MS(unsigned int t); //定义引脚,这里定义P0引脚 sbit P00 = P0^0; sbit P01 = P0^1; sbit P02 = P0^2; sbit P03 = P0^3; sbit P04 = P0^4; sbit P05 = P0^5; sbit P06 = P0^6; sbit P07 = P0^7; int main(void){ //进入循环 while(1){ //P00闪烁两次 P00 = 1; Delay1MS(200); P00 = 0; Delay1MS(200); P00 = 1; Delay1MS(200); P00 = 0; Delay1MS(200); //P00常亮1s P00 = 0; Delay1MS(1000); P00 = 1; Delay1MS(50); //P01闪烁两次 P01 = 1; Delay1MS(200); P01 = 0; Delay1MS(200); P01 = 1; Delay1MS(200); P01 = 0; Delay1MS(200); //P00、P01常亮1s P00 = 0; P01 = 0; Delay1MS(1000); P00 = 1; P01 = 1; Delay1MS(50); //P02闪烁两次 P02 = 1; Delay1MS(200); P02 = 0; Delay1MS(200); P02 = 1; Delay1MS(200); P02 = 0; Delay1MS(200); //P00、P01、P02常亮1s P00 = 0; P01 = 0; P02 = 0; Delay1MS(1000); P00 = 1; P01 = 1; P02 = 1; Delay1MS(50); //P03闪烁两次 P03 = 1; Delay1MS(200); P03 = 0; Delay1MS(200); P03 = 1; Delay1MS(200); P03 = 0; Delay1MS(200); //P00、P01、P02、P03常亮1s P00 = 0; P01 = 0; P02 = 0; P03 = 0; Delay1MS(1000); P00 = 1; P01 = 1; P02 = 1; P03 = 1; Delay1MS(50); //P04闪烁两次 P04 = 1; Delay1MS(200); P04 = 0; Delay1MS(200); P04 = 1; Delay1MS(200); P04 = 0; Delay1MS(200); //P00、P01、P02、P03、P04常亮1s P00 = 0; P01 = 0; P02 = 0; P03 = 0; P04 = 0; Delay1MS(1000); P00 = 1; P01 = 1; P02 = 1; P03 = 1; P04 = 1; Delay1MS(50); //P05闪烁两次 P05 = 1; Delay1MS(200); P05 = 0; Delay1MS(200); P05 = 1; Delay1MS(200); P05 = 0; Delay1MS(200); //P00、P01、P02、P03、P04、P05常亮1s P00 = 0; P01 = 0; P02 = 0; P03 = 0; P04 = 0; P05 = 0; Delay1MS(1000); P00 = 1; P01 = 1; P02 = 1; P03 = 1; P04 = 1; P05 = 1; Delay1MS(50); //P06闪烁两次 P06 = 1; Delay1MS(200); P06 = 0; Delay1MS(200); P06 = 1; Delay1MS(200); P06 = 0; Delay1MS(200); //P00、P01、P02、P03、P04、P05、P06常亮1s P00 = 0; P01 = 0; P02 = 0; P03 = 0; P04 = 0; P05 = 0; P06 = 0; Delay1MS(1000); P00 = 1; P01 = 1; P02 = 1; P03 = 1; P04 = 1; P05 = 1; P06 = 1; Delay1MS(50); //P07闪烁两次 P07 = 1; Delay1MS(200); P07 = 0; Delay1MS(200); P07 = 1; Delay1MS(200); P07 = 0; Delay1MS(200); //P00、P01、P02、P03、P04、P05、P06、P07常亮1s P00 = 0; P01 = 0; P02 = 0; P03 = 0; P04 = 0; P05 = 0; P06 = 0; P07 = 0; Delay1MS(1000); P00 = 1; P01 = 1; P02 = 1; P03 = 1; P04 = 1; P05 = 1; P06 = 1; P07 = 1; Delay1MS(50); //八个LED发光二极管第一次闪烁 P00 = 0; P01 = 0; P02 = 0; P03 = 0; P04 = 0; P05 = 0; P06 = 0; P07 = 0; Delay1MS(200); P00 = 1; P01 = 1; P02 = 1; P03 = 1; P04 = 1; P05 = 1; P06 = 1; P07 = 1; Delay1MS(200); //八个LED发光二极管第二次闪烁 P00 = 0; P01 = 0; P02 = 0; P03 = 0; P04 = 0; P05 = 0; P06 = 0; P07 = 0; Delay1MS(200); P00 = 1; P01 = 1; P02 = 1; P03 = 1; P04 = 1; P05 = 1; P06 = 1; P07 = 1; Delay1MS(200); } } //1m延时程序 void Delay1MS(unsigned int t){ unsigned char i; while(--t != 0){ for(i = 0; i < 125; i++); } }
编译:
导入Protues运行:
效果:
结语:本章节是上一节的延续,主要是掌握并熟练Protues 绘图方法、单片机引脚连接方式、Keil引脚流程控制程序的编写,这里可以根据本节程序内容自行进行修改,编写出好看的LED程序。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)