RYMCU

Nebula Pi 开发板评测报告(六)——数码管模块功能测试

ii11nnocent 3 月前
# 数码管 # Nebula Pi # 评测报告

数码管显示分为静态和动态显示两部分。
静态显示数字 8 的程序和实效图如下:

#include "reg52.h"

sbit Duan = P0^6;  //位定义数码管段选
sbit Wei  = P0^7;  //位定义数码管位选

void main(void)
{
 P2 = 0xF7; //P2=1111 0111,即控制WEI4输出0,选中最右侧数码管
 Wei = 1;
 Wei = 0;  //位锁存,此时WEI4=0会一直保持
 
 P2 = 0x7F; //输出数字8的段码
 Duan = 1;
 Duan = 0;  //段锁存
}

4.png

动态显示数字 2021 的程序如下:

#include "reg52.h"

sbit Duan = P0^6;  //位定义数码管段选
sbit Wei  = P0^7;  //位定义数码管位选

unsigned char code smgduan[] = {0x3F,0x06,0x5B,0x4F,
                 0x66,0x6D,0x7D,0x07,
                 0x7F,0x6F,0x77,0x7C,
                 0x39,0x5E,0x79,0x71};  //数码管0~F段码
unsigned int i;

void delay(unsigned int i)
{
 while(i--);
}


void main(void)
{
 while(1)
 {
  for(i=1;i<5;i++)
  {
   switch(i)
   {
    case(1):
     P2 = 0xFE; //选中第一个数码管
     Wei = 1;
     Wei = 0;  //位锁存
     P2 = smgduan[2]; //显示数字2
     Duan = 1;
     Duan = 0;
     break;
    case(2):
     P2 = 0xFD; //选中第二个数码管
     Wei = 1;
     Wei = 0;  //位锁存
     P2 = smgduan[0]; //显示数字0
     Duan = 1;
     Duan = 0;
     break;
    case(3):
     P2 = 0xFB; //选中第二个数码管
     Wei = 1;
     Wei = 0;  //位锁存
     P2 = smgduan[2]; //显示数字2
     Duan = 1;
     Duan = 0;
     break;
    case(4):
     P2 = 0xF7; //选中第二个数码管
     Wei = 1;
     Wei = 0;  //位锁存
     P2 = smgduan[1]; //显示数字2
     Duan = 1;
     Duan = 0;
     break;  
   }
   delay(100);
   P2 = 0x00;
   Duan = 1;
   Duan = 0;   //消影   
  }

 }
}

5.png

使用数码管 + 定时器实现 888 秒的倒计时程序如下(修改程序中 time 变量的值可修改倒计时的最大值):

#include "reg52.h"  
  
sbit Duan = P0^6;       //位定义数码管段选  
sbit Wei  = P0^7;       //位定义数码管位选  
  
unsigned char code smgduan[] = {0x3F,0x06,0x5B,0x4F,  
                                0x66,0x6D,0x7D,0x07,  
                                0x7F,0x6F,0x77,0x7C,  
                                0x39,0x5E,0x79,0x71};     //数码管0~F段码  
unsigned int i;  
unsigned int time = 888;            //初始时间显示888秒  
unsigned int ge,shi,bai;            //定义个位、十位、百位储存变量  
  
  
void delay(unsigned int i)  
{  
    while(i--);  
}  
  
void Timer0_Init(void);  
  
void Timer0_Init(void)  
{  
     TMOD = 0x01;  //工作方式1,16位定时器  
     TH0 = 0x4C;   //定时50ms初值高八位  
     TL0 = 0x00;   //定时50ms初值低八位  
     ET0 = 1;    //开定时器0中断  
     EA = 1;     //开总中断  
     TR0 = 1;    //开启定时器0  
}  
  
void main(void)  
{  
    Timer0_Init();          //定时器0初始化  
    while(1)  
    {  
        ge  = time%10;  
        shi = (time/10)%10;  
        bai = (time/100)%10;    //分离time中的个位、十位和百位  
                  
        for(i=1;i<5;i++)  
        {  
            switch(i)  
            {  
                case(1):  
                    P2 = 0xFE;  //选中第一个数码管  
                    Wei = 1;  
                    Wei = 0;        //位锁存  
                    P2 = smgduan[0];    //显示数字0  
                    Duan = 1;  
                    Duan = 0;  
                    break;  
                case(2):  
                    P2 = 0xFD;  //选中第二个数码管  
                    Wei = 1;  
                    Wei = 0;        //位锁存  
                    P2 = smgduan[bai];  //显示百位  
                    Duan = 1;  
                    Duan = 0;  
                    break;  
                case(3):  
                    P2 = 0xFB;  //选中第二个数码管  
                    Wei = 1;  
                    Wei = 0;        //位锁存  
                    P2 = smgduan[shi];  //显示十位  
                    Duan = 1;  
                    Duan = 0;  
                    break;  
                case(4):  
                    P2 = 0xF7;  //选中第二个数码管  
                    Wei = 1;  
                    Wei = 0;        //位锁存  
                    P2 = smgduan[ge];   //显示个位  
                    Duan = 1;  
                    Duan = 0;  
                    break;        
            }  
            delay(100);  
            P2 = 0x00;  
            Duan = 1;  
            Duan = 0;           //消影              
        }  
  
    }  
}  
  
void Timer0() interrupt 1 using 1  
{  
     static unsigned int count; //计数变量  
     count++;  
     TH0 = 0x4C;   //重装初值,定时50ms初值高八位  
     TL0 = 0x00;   //定时50ms初值低八位  
     if( count == 20 )  //count=20时定时了1s  
     {  
      time--; //时间减一    
      count = 0;    //i清零,重新计数  
     }  
    }

888.mp4

后发布评论