快速掌握数码管——附三个实验

数码管简介:

在这里插入图片描述

在这里插入图片描述

数码管段位表:

在这里插入图片描述

实验一:一位数码管动态显示 0 ~ 9

Proteus 仿真电路图:

本次实验采用的是选用的是共阳极六位数码管
P0口段选,进行字码显示
P2口位选,控制哪个数码管显示

在这里插入图片描述

C51代码 :

#include<reg51.h>

typedef unsigned char uchar;
typedef unsigned int uint;

uchar code num[10]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};  // 0~9字码

void delay(uint x){  //延时函数
	while(x--);
}

uchar i=0;

void main(){

	while(1){
		for(i=0; i<10; i++){  //按键检测
			P2 = 0x01;  //位选最左数码管显示 

			P0 = num[i];  //显示数字,直接给 P0 口赋值
			delay(50000);

			P0 = 0xff;  //消影
		}
	}
}


实验二:六位数码管动态显示

Proteus 仿真电路图:

本次实验采用的是选用的是共阳极六位数码管
P0口段选,进行字码显示
P2口位选,控制哪个数码管显示

在这里插入图片描述

C51代码:

#include<reg51.h>

typedef unsigned char uchar;
typedef unsigned int uint;

uchar code num[10]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};  // 0~9字码

uchar code led[6]={0x01, 0x02, 0x04, 0x08, 0x10, 0x20};	 //led位选

void delay(uint x){  //延时函数
	while(x--);
}

uchar i=0;

void main(){

	while(1){
		for(i=0; i<6; i++){
			P2 = led[i];  //数码管位选

			P0 = num[i];
			delay(50000);

			P0 = 0xff;  //消影
			P2 = 0;
		}
	}
}

实验三:按键实现一位数码管加

Proteus 仿真电路图:

本次实验采用的是选用的是共阳极六位数码管
P0口段选,进行字码显示
P2口位选,控制哪个数码管显示

在这里插入图片描述

C51代码:

#include<reg51.h>

typedef unsigned char uchar;
typedef unsigned int uint;

sbit button = P3^7;  //按钮; 按下一次加一

uchar code num[10]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};  // 0~9字码

void delay(uint x){  //延时函数 
	while(x--);
}

uchar i=0;  //全局变量

void main(){

	P2 = 0x01;  //位选最左边数码管显示

	while(1){
		
		if(button == 0){  //按键检测
		
			delay(1000);  //消抖
			if(button == 0){
				i++;
			}
			while(!button);
		}

		if(i>9){	    //判断是否超过数字9,超过 9 复位到 0 从新开始计算 
			i = 0;
		}

		P0 = num[i];
		delay(1000);
	}
}


可以再添加一个按键,再编写一个按键检测程序,实现按一次 i 值减 1 ,便可实现按键数码管减的实验

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guan_qiqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值