博客
关于我
51单片机——LED流水灯
阅读量:681 次
发布时间:2019-03-17

本文共 1212 字,大约阅读时间需要 4 分钟。

51单片机——LED流水灯

一、查表法实现流水灯

查看开发板的原理图,知道了控制LED2~LED7的端口为P0口,所以可以通过对P0口赋值控制发光二极管的亮灭。比如,用十六进制表示P0的值,P0=0xfe,即LED2发光,其他LED熄灭。要实现流水灯的话,让发光二极管从LED2到LED7顺序发光就可以了,也就是说,P0口的值依次为0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,简简单单。

 

而查表法就是把P0口要表示的值做成数组,再利用'code'将其存入ROM中,使用时,让P0口等于数组中的某个值。

程序如下:

#include 
sbit ADDR0=P1^0; sbit ADDR1=P1^1;sbit ADDR2=P1^2;sbit ADDR3=P1^3;sbit ENLED=P1^4;void delay(unsigned char x);unsigned char code aa[]=//P0口查表{0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};void main(){ unsigned i; ADDR0=0; //按原理图端口初始化 ADDR1=1; ADDR2=1; ADDR3=1; ENLED=0; while(1) { for(i=0;i<8;i++) { P0=aa[i]; delay(250); } }}void delay(unsigned char x){ unsigned n; while(--x) for(n=0;n<250;n++);}

二、循环法

用查表法实现流水灯时,发现对P0口赋值是由规律的,即0的位置在左移,如果让0左移到最高位后,重新从最低为开始左移,也可以实现流水灯。介就是循环法,也是蛮简单的,可以加入一个叫'intrins.h'的头文件来做。

程序如下:

#include 
#include
sbit ADDR0=P1^0; sbit ADDR1=P1^1;sbit ADDR2=P1^2;sbit ADDR3=P1^3;sbit ENLED=P1^4;void delay(unsigned char x);void main(){ unsigned char i,a=0xfe; ADDR0=0; //按原理图端口初始化 ADDR1=1; ADDR2=1; ADDR3=1; ENLED=0; while(1) { for(i=0;i<8;i++) { P0=a; a=_crol_(a,1); delay(250); } }}void delay(unsigned char x){ unsigned n; while(--x) for(n=0;n<250;n++);}

 

转载地址:http://jmgqz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>