int digitalPin= 2;
int weiPin= 10;//公共引脚的接口
int count = -1;
/*
因为这段代码是递增计数器
需要从0开始递增
而后面的循环函数会直接使count变量直接开始自加一后直接显示
为了第一次显示的是0
所以在此处赋值-1
*/
void setup() {
for(int x=0; x<4; x++) {
pinMode(weiPin+x, OUTPUT); //将各个公共引脚设置为输出模式
digitalWrite(weiPin+x, HIGH);//将各个公共引脚设置为高电平
}
for(int x=0; x<8; x++) {
pinMode(digitalPin+x, OUTPUT);//将数码管引脚设置为输出模式
}
}
void loop() {
count++;
if(count<10){//个位数显示
display(4,count);//调用函数显示第4位数字为count的值
delay(1000);//等待
}else if(count<100){//十位数显示
for(int i=0;i<50;i++){
display(3,count/10);
delay(10);
display(4,count%10);
delay(10);
}}else if(count<1000){//百位数显示
for(int i=0;i<50;i++){
display(2,count/100);
delay(20/3);
display(3,count/10%10);
delay(20/3);
display(4,count%10);
delay(20/3);
}
}
}
void displayDigital(unsigned int digit) {
unsigned char abcdefgh[][8] = {//定义一个数组,存储不同数字的abcdefgh各段的取值
{0,0,0,0,0,0,1,1},//显示0时数码管abcdefgh的状态
{1,0,0,1,1,1,1,1},//1
{0,0,1,0,0,1,0,1},//2
{0,0,0,0,1,1,0,1},//3
{1,0,0,1,1,0,0,1},//4
{0,1,0,0,1,0,0,1},//5
{0,1,0,0,0,0,0,1},//6
{0,0,0,1,1,1,1,1},//7
{0,0,0,0,0,0,0,1},//8
{0,0,0,1,1,0,0,1},//9
{0,0,0,1,0,0,0,1},//A
{1,1,0,0,0,0,0,1},//b
{0,1,1,0,0,0,1,1},//C
{1,0,0,0,0,1,0,1},//d
{0,1,1,0,0,0,0,1},//E
{0,1,1,1,0,0,0,1},//F
{1,1,1,1,1,1,1,0},//DOT = 16
{1,1,1,1,1,1,0,1},//MINUS= 17
{1,1,1,1,1,1,1,1} //BLANK =18
};
if(digit > 18)return;
for (unsigned int x=0; x<8; x++)
digitalWrite( digitalPin+ x, abcdefgh[digit][x] );
}
//指定位显示给定数字函数
void display(unsigned int wei, unsigned int digit) {
const int BLANK=18;
for(int x=0; x<=3; x++) {
digitalWrite(weiPin + x,LOW);// 数字位引脚置低电平
}
displayDigital(BLANK); //调用函数关闭所有显示字段
digitalWrite(weiPin +wei-1 , HIGH);//选择位显引脚
displayDigital(di
代码注解尝试
最新推荐文章于 2024-09-07 10:59:43 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)