快速入门13-1位数码管显示

因为这个案例接线调试对于入门的小白来说比较复杂,所以我在这里做一次详细的接线解释以及原理解释,对代码调试做一些详细解释。

一、工具原料

电脑,windows系统
ESP8266开发板
micro-usb线
数码管1个(共阳)
220Ω 电阻8个
面包板一个+若干跳线

二、连接

共阳即正极接在电源,负极接在引脚,控制负极电频高低来控制显示)

我在图中标注了详细的接法(接法不唯一,根据你的代码来接)

a,b,c....dp对应设置了什么,就把a,b,c....dp接到对应的引脚。

三、代码

1、打开零知实验室软件开发工具,然后新建项目,输入以下代码:


							
	/**
	*    文件: 数码管-1位.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
	*    -- 零知开源,让电子制作变得更简单! --
	*    时间: 2018/05/31 6:13
	*    说明: 测试1位数码管
	**/
	   
	//下面为定义a~h, dp的引脚
	int a = D5;//a段
	int b = D6;
	int c = D7;
	int d = D4;
	int e = D2;
	int f = D1;
	int g = D0;
	int dp = D8;
	   
	int pinArray[8] = {D5,D6,D7,D4,D2,D1,D0,D8};//数组,通过下标获取
	   
	//分别显示0-9
	void digital_show_0(void);
	void digital_show_1(void);
	void digital_show_2(void);
	void digital_show_3(void);
	void digital_show_4(void);
	void digital_show_5(void);
	void digital_show_6(void);
	void digital_show_7(void);
	void digital_show_8(void);
	void digital_show_9(void);
	   
	// the setup routine runs once when you press reset:
	void setup() {
	       
	   
	    // put your setup code here, to run once:
	        int i = 0;
	        for(i=0; i<=7; i++)
	        {
	                pinMode( pinArray[i], OUTPUT);
	        }
	          
	           
	           
	           
	}
	   
	// the loop routine runs over and over again forever:
	void loop() {
	    // put your main code here, to run repeatedly:
	       
	        digital_show_0();
	       
	        delay(1000);
	       
	        digital_show_1();
	        delay(1000);
	       
	        digital_show_2();
	        delay(1000);
	        digital_show_3();
	        delay(1000);
	        digital_show_4();
	        delay(1000);
	        digital_show_5();
	        delay(1000);
	        digital_show_6();
	        delay(1000);
	        digital_show_7();
	        delay(1000);
	        digital_show_8();
	        delay(1000);
	        digital_show_9();
	        delay(1000);
	}
	   
	void digital_show_0(void) //显示数字0
	{
	        unsigned char j;
	        digitalWrite(a,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(c,LOW);
	        digitalWrite(d,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	   
	void digital_show_1(void) //显示数字1
	{
	        digitalWrite(a,HIGH);
	        digitalWrite(b,LOW);
	        digitalWrite(c,LOW);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	   
	void digital_show_2(void) //显示数字2
	{        
	        digitalWrite(a,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	   
	void digital_show_3(void) //显示数字3
	{        
	        digitalWrite(a,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(c,LOW);
	        digitalWrite(d,LOW);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	   
	void digital_show_4(void) //显示数字4
	{
	        digitalWrite(c,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	        digitalWrite(a,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(d,HIGH);
	}
	void digital_show_5(void) //显示数字5
	{
	        digitalWrite(a,LOW);
	        digitalWrite(b, HIGH);
	        digitalWrite(c,LOW);
	        digitalWrite(d,LOW);
	        digitalWrite(e, HIGH);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_6(void) //显示数字6
	{        
	        digitalWrite(a,LOW);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,LOW);
	        digitalWrite(d,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_7(void) //显示数字7
	{        
	        digitalWrite(a,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(c,LOW);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_8(void) //显示数字8
	{        
	        digitalWrite(a,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(c,LOW);
	        digitalWrite(d,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_9(void) //显示数字9
	{
	        digitalWrite(a,LOW);
	        digitalWrite(b,LOW);
	        digitalWrite(c,LOW);
	        digitalWrite(d,LOW);
	        digitalWrite(e, HIGH);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
							
						

2、按照之前的方式先【验证】,然后【上传】到开发板中。

四、成果展示

上面步骤成功完成后,我们就可以看到预期的结果了。