设计任意流水灯电路,实现两种以上方式的流水灯效果,通过按键切换或者自动切换。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- #define led P2
- typedef unsigned int u16; //对数据类型进行声明定义
- typedef unsigned char u8;
- //延迟函数
- void delay(){
- u16 a, b;
- for(a = 0; a < 400; a++)
- for(b = 0; b < 400; b++);
- }
- // LED点亮方式:依次点亮(一次一个,循环三次)
- void yici(){
- char i, j;
- for(j = 0; j < 1; j++){
- led = 0xfe;
- for(i = 0; i < 7; i++){
- led = _cror_(led, 1);
- delay();
- }
- led = 0x7f;
- for(i = 0; i < 7; i++){
- led = _crol_(led, 1);
- delay();
- }
- }
- }
- // 从两边到中间
- char toMiddle[] = {0x7e, 0xbd, 0xdb, 0xe7, 0xdb, 0xbd, 0x7e};//LED灯点亮方式
- void bianToMiddle(){
- char i, j;
- for(j = 0; j < 1; j++){//循环三遍
- for(i = 0; i < 7; i++){//LED点亮方式:从两边到中间
- led = toMiddle[i];
- delay();
- }
- }
- }
- //由上向下依次点亮LED灯
- char toBottom[] = {0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
- void topToBottom(){
- char i, j;
- for(j = 0; j < 1; j++){
- led = 0xff;
- for(i = 0; i < 8; i++){
- led = led << 1;
- delay();
- }
- for(i = 6; i >= 0; i--){
- led = toBottom[i];
- delay();
- }
- }
- }
- void main(){
- while(1){
- yici(); //LED灯依次亮
- bianToMiddle();//LED灯从两边向中间逐次点亮
- led = 0xff; //关闭LED灯
- topToBottom();//由上向下依次点亮LED灯
- }
- }
复制代码 Keil代码与Proteus仿真下载:
实验10.7z
(71.94 KB, 下载次数: 55)
|