【51单片机Keil+Proteus8.9】AT89C51+7段数码管两位计数器

两位计数器显示

  • 设计思路

电路设计

  1. 使用AT89C51单片机,外部引出两个管脚P3^3和P3^4当作按钮分别作为start和stop,对计时状态进行控制,由于是两位计数器,引出两个管脚P3^0和P3^1作为两个数码管的片选信号,工作时快速交替利用人眼视觉暂留效应实现不间断显示。
  2. 显示屏选用共阳极7段两位数码管7seg-mpx2-ca-blue,将其A至G引脚接至单片机AT89C51单片机的P2组引脚,工作时单片机向显示屏发出数据,进行计数值的显示。

代码设计

  1. 根据共阳极数码管特性,提前在函数内部定义一个arr[10]数组,以16进制形式包含了0-9数值的数据,使用时直接调用数组中的一个就可以。
  2. 定义了一个时间延时函数delay(int d),用于作为计数器两位数值高位和低位的交替时间。
  3. 利用整型变量i从0-99自增作为时间,将i/10就可以得到高位的值,i%10就得到低位的值,从而完成计数。
  • 原理图

  • 测试过程及结果说明

  • 1.在keil端进行代码编写,将实验代码进行编译,结果正确无任何错误。

  • 2.打开Proteus软件,绘制原理图,放置元件,双击AT89C51单片机芯片,将keil端编译之后生成的HEX文件加载到芯片内。

  • 启动仿真

    按下第一个按钮,即P3^3对应的start按钮,电路开始计数,显示屏开始显示数字并自增,如下图

  • 然后按下第二个按钮,即P3^4对应的stop按钮,电路暂停计数,显示屏数字保持不变,如下图

  • 最后按下第一个按钮start,电路从0开始计数

  • #include<reg51.h>

    sbit S1 = P3^0; // Select line 1 for Display 1

    sbit S2 = P3^1; // Select Line 2 for Display 2

    sbit START = P3^3; //START

    sbit STOP = P3^4; //STOP

    void delay(int d)

    {

    int i,j;

    for(i=0;i<d;i++)

    for(j=0;j<255;j++);

    }

    void main()

    {

    //DPδÁ¬½Ó£¬×î¸ßÎ>>ÈÎÒâ

    int arr[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,0x80, 0x90}; // ******Your codes is required here!!!

    int i,on,off;

    static int num1,num2,temp1,temp2;

    while(1){

    if ((START==0)&&(STOP==1))

    {

    on = 1;

    off = 0;

    }

    else

    {

    on = 0;

    off = 1;

    }

    while(on && !off)

    {

    for(i=0;i<100;i++)

    {

    num1 = i/10; //First Digit Separated

    temp1 = num1;

    S1 = 1; //Select Line 1

    S2 = 0;

    P2 = arr[num1]; //Display 1 will indicate number

    delay(45);

    num2 = i%10; //Second Digit Separated

    // *****Your codes is required here!!!

    temp2 = num2;

    S1 = 0;

    S2 = 1;

    P2 = arr[num2];

    delay(45);

    if(STOP==0)

    {

    off = 1;

    on = 0;

    break;

    }

    }

    while(off && !on)

    {

    S1=1;

    S2=0;

    P2 = arr[temp1];

    delay(10);

    S1=0;

    S2=1;

    P2 = arr[temp2];

    delay(30);

    if (START==0)

    {

    off = 0;

    on = 1;

    break;

    }

    }

    }

    }

    }

    复制代码
    #include<reg51.h>
    sbit S1 = P3^0; // Select line 1 for Display 1
    sbit S2 = P3^1; // Select Line 2 for Display 2
    sbit START = P3^3; //START 
    sbit STOP  = P3^4; //STOP
    void delay(int d)
    {
    	int i,j;
    	for(i=0;i<d;i++)
    	for(j=0;j<255;j++);
    }
    void main()
    {
    	//DPδÁ¬½Ó£¬×î¸ßÎ>>ÈÎÒâ
    	int arr[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,0x80, 0x90}; // ******Your codes is required here!!!
    	int i,on,off;
    	static int num1,num2,temp1,temp2;
    	while(1){
    	if ((START==0)&&(STOP==1))
    	{
    		 on = 1;
    		off = 0;
    	}
    	else
    	{
    		 on = 0;
    		off = 1;
    	}
    	while(on && !off)
    	{
    		for(i=0;i<100;i++)
    		{
    			num1 = i/10;    //First Digit Separated
    			temp1 = num1;
    			S1 = 1;					//Select Line 1
    			S2 = 0;
    			P2 = arr[num1];  //Display 1 will indicate number
    			delay(45);
    			
    			num2 = i%10;    //Second Digit Separated
                    // *****Your codes is required here!!!
    			temp2 = num2;
    			S1 = 0;
    			S2 = 1;
    			P2 = arr[num2];
    			delay(45);
    			
    			if(STOP==0)
    			{
    				off = 1;
    				 on = 0;
    				break;
    			}
    		}
    		while(off && !on)
    		{
    			S1=1;
    			S2=0;
    			P2 = arr[temp1];
    			delay(10);
    			S1=0;
    			S2=1;
    			P2 = arr[temp2];
    			delay(30);
    			if (START==0)
    			{
    				off = 0;
    				 on = 1;
    				break;
    			}
    		}
    	}
    	
    }
    }

    可私信获取项目文件

相关推荐
whaosoft-14329 分钟前
51c嵌入式※~合集7~Linux
嵌入式硬件
LeonDL1682 小时前
YOLOv8 在单片机上的几种部署方案
人工智能·python·单片机·嵌入式硬件·深度学习·yolo·yolov8 在单片机上的部署
LeonDL1682 小时前
YOLOv8 在单片机上部署的缺点和应对方案
python·单片机·嵌入式硬件·深度学习·yolo·yolov8在单片机上的缺点·yolov8 在单片机上的优化
尚久龙2 小时前
STM32实现RS485通讯
stm32·单片机·嵌入式硬件
嵌入式Linux,2 小时前
ESP32和STM32 就不应该放在一起比,
stm32·单片机·嵌入式硬件
硬件智障3 小时前
51单片机实现流水灯
单片机·嵌入式硬件·51单片机
暮雪倾风3 小时前
【STM32】ST-Link V2.1制作
stm32·单片机·嵌入式硬件
yuanpan3 小时前
支持python的单片机有哪些
开发语言·python·单片机
MaoXian_n4 小时前
[IMX] 05.串口 - UART
汇编·arm开发·驱动开发·单片机·嵌入式硬件
Camellia03115 小时前
嵌入式学习--江协51单片机day8
嵌入式硬件·学习·51单片机