3394| 2
|
[入门] 循环显示8位数码管 |
循环显示8位数码管 先看演示视频吧。 1. 本次实验的是一位共阳数码管。8位数码管由7根棒状led(拼接数字)和一个led灯(拼接小数点)组成。通过分别控制8颗led灯的明灭,来显示不同的数字。 2. 先借Jane的图说明一下连线(https://mc.dfrobot.com.cn/thread-2539-1-1.html) a,b,c,d,e,f,g,DP,分别连接2-9数字针脚,+接开发板的10针脚,我只接了一个+ 3.使用数组,初始化0-9每个数字需要点亮的LED管,比如,数字1,只需将b和c管设置为0,其他管设置为1即可。因此,数字1的数组是{1,0,0,1,1,1,1,1}。其他数字同理。[mw_shl_code=c,true] int num0[8] = {0,0,0,0,0,0,1,1}; int num1[8] = {1,0,0,1,1,1,1,1}; int num2[8] = {0,0,1,0,0,1,0,1}; int num3[8] = {0,0,0,0,1,1,0,1}; int num4[8] = {1,0,0,1,1,0,0,1}; int num5[8] = {0,1,0,0,1,0,0,1}; int num6[8] = {0,1,0,0,0,0,0,1}; int num7[8] = {0,0,0,1,1,1,1,1}; int num8[8] = {0,0,0,0,0,0,0,1}; int num9[8] = {0,0,0,0,1,0,0,1}; [/mw_shl_code] 4. 初始化针脚模式 [mw_shl_code=c,true] void setup() { for(int i=2;i<=10;i++) pinMode(i,OUTPUT); } [/mw_shl_code] 5. 接下来就是循环显示出各个数字了。 [mw_shl_code=applescript,true] void loop() { digitalWrite(10,1); for(int i=2;i<=9;i++) digitalWrite(i,num0[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num1[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num2[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num3[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num4[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num5[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num6[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num7[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num8[i-2]); delay(1000); for(int i=2;i<=9;i++) digitalWrite(i,num9[i-2]); delay(1000); } [/mw_shl_code] 6. 进阶,封装函数 [mw_shl_code=c,true] //共阳八段数码管(含小数点),各数字定义,0亮,1灭 int num[10][8]={ {0,0,0,0,0,0,1,1}, {1,0,0,1,1,1,1,1}, {0,0,1,0,0,1,0,1}, {0,0,0,0,1,1,0,1}, {1,0,0,1,1,0,0,1}, {0,1,0,0,1,0,0,1}, {0,1,0,0,0,0,0,1}, {0,0,0,1,1,1,1,1}, {0,0,0,0,0,0,0,1}, {0,0,0,0,1,0,0,1} }; void ShowNum(int x,int y);//x显示的数字,y显示的秒数 void setup() { // put your setup code here, to run once: for(int i=2;i<=10;i++) pinMode(i,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(10,1); ShowNum(8,3000); } void ShowNum(int x,int y) { for(int i=2;i<=9;i++) digitalWrite(i,num[x][i-2]); delay(y); } [/mw_shl_code] 7.附件,完整代码 |
534 Bytes, 下载次数: 815
© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed