零知开源快速入门15-数码管显示(1位)

一、工具原料

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

二、硬件连接

1、硬件连接原理图

2、实际连线

其中a引脚接线控制数码管a号位显示,b引脚对应控制数码管b号位显示,以此类推控制整个数码管显示.

三、方法步骤

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

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


							
							
	/**
	*    文件: 数码管-1位.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
	*    -- 零知开源,让电子制作变得更简单! --
	*    时间: 2018/06/26 16:13
	*    说明: 测试1位数码管
	**/
	 
	//下面为定义a~h, dp的引脚
	int a = 2;//a段
	int b = 3;
	int c = 5;
	int d = 6;
	int e = 9;
	int f = 1;
	int g = 0;
	int dp = 4;
	 
	//分别显示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);
	void setup() {
	        int i = 0;
	        for(i=0; i<=6; i++)
	        {
	                pinMode(i, OUTPUT);
	        }
	        pinMode(9, OUTPUT);
	}
	 
	void loop() {
	        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
	{
	        digitalWrite(a,HIGH);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	 
	void digital_show_1(void) //显示数字1
	{
	        digitalWrite(a,LOW);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	 
	void digital_show_2(void) //显示数字2
	{        
	        digitalWrite(a,HIGH);
	        digitalWrite(b,HIGH);
			digitalWrite(c,LOW);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,LOW);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	 
	void digital_show_3(void) //显示数字3
	{        
	        digitalWrite(a,HIGH);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	 
	void digital_show_4(void) //显示数字4
	{
	        digitalWrite(c,HIGH);
	        digitalWrite(b,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	        digitalWrite(a,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(d,LOW);
	}
	void digital_show_5(void) //显示数字5
	{
	        digitalWrite(a,HIGH);
	        digitalWrite(b, LOW);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,HIGH);
	        digitalWrite(e, LOW);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_6(void) //显示数字6
	{        
	        digitalWrite(a,HIGH);
	        digitalWrite(b,LOW);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_7(void) //显示数字7
	{        
	        digitalWrite(a,HIGH);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,LOW);
	        digitalWrite(e,LOW);
	        digitalWrite(f,LOW);
	        digitalWrite(g,LOW);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_8(void) //显示数字8
	{        
	        digitalWrite(a,HIGH);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,HIGH);
	        digitalWrite(e,HIGH);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}
	void digital_show_9(void) //显示数字9
	{
	        digitalWrite(a,HIGH);
	        digitalWrite(b,HIGH);
	        digitalWrite(c,HIGH);
	        digitalWrite(d,HIGH);
	        digitalWrite(e, LOW);
	        digitalWrite(f,HIGH);
	        digitalWrite(g,HIGH);
	        digitalWrite(dp,HIGH);
	}										
										
							
							
						

四、成果展示

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