快捷搜索: 王者荣耀 脱发

【蓝桥杯单片机】共阳数码管

一、电路图

段选 位选 通过Y6选择八个数码管的一个或多个,再通过Y7进行数码管的显示。 段码:共阳数码管 数字1的显示: dp=1 g=1 f=1 e=1 d=1 c=0 b=0 a=1(亮为0,灭为1) 1111 1001(二进制) 0xf9(十六进制) 其他类似,段码表为: duanma[18] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf}

二、实现代码

1.静态显示

例题:实现每一个数码管0-9的显示,再进行所有数码管0-15的显示。

#include "reg52.h"

unsigned char code duanma[18] = {
          
   0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//code不会占用内存

void Delay(t)
{
          
   
   while(t--);
   while(t--);
}

void selectHC573(unsigned char n)
{
          
   
  switch(n)
 {
          
   
   case 4:
    P2=(P2 & 0x1f) | 0x80;
   break; 
   case 5:
    P2=(P2 & 0x1f) | 0xa0;
   break;
   case 6:
    P2=(P2 & 0x1f) | 0xc0;
   break; 
   case 7:
    P2=(P2 & 0x1f) | 0xe0;
   break; 
 }
}

void showSMG_Bit(unsigned char dat,unsigned char pos)
{
          
   
   selectHC573(6); //数码管位置
   P0=0x01 << pos;
   selectHC573(7); //数码管内容
   P0=dat;
}

void smg_Static()
{
          
   
   unsigned char i,j;
   
   for(i=0;i<8;i++)
   {
          
   
	  for(j=0;j<10;i++)
	  {
          
   
	    showSMG_Bit(duanma[j],i);
	    Delay(5000);
        Delay(5000);
	  }
   }
	  
   for(i=0;i<16;i++)
   {
          
   
      selectHC573(6); //数码管位置
      P0=0xff;
      selectHC573(7); //数码管内容
      P0=duanma[i];
	  Delay(5000);
	  Delay(5000);
   }
}

void  main()
{
          
   
  while(1)
  {
          
   
     smg_Static();
  }
}

2.动态显示

例题:使八个数码管同时分别显示1-8。

#include "reg52.h"

unsigned char code duanma[18] = {
          
   0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};//code不会占用内存

void selectHC573(unsigned char channel)
{
          
   
  switch(channel)
 {
          
   
   case 4:
    P2=(P2 & 0x1f) | 0x80;
   break; 
   case 5:
    P2=(P2 & 0x1f) | 0xa0;
   break;
   case 6:
    P2=(P2 & 0x1f) | 0xc0;
   break; 
   case 7:
    P2=(P2 & 0x1f) | 0xe0;
   break; 
 }
}

void diaplaysmg_bit(unsigned char value,unsigned char pos)
{
          
   
  selectHC573(6);
  P0 = 0x01 << pos;
  selectHC573(7);
  P0 = value;
}

void Delay(unsigned int t)
{
          
   
  while(t--);
}

void display()
{
          
   
  diaplaysmg_bit(duanma[1],0);
  Delay(300);
  diaplaysmg_bit(duanma[2],1);
  Delay(300);
  diaplaysmg_bit(duanma[3],2);
  Delay(300);
  diaplaysmg_bit(duanma[4],3);
  Delay(300);
  diaplaysmg_bit(duanma[5],4);
  Delay(300);
  diaplaysmg_bit(duanma[6],5);
  Delay(300);
  diaplaysmg_bit(duanma[7],6);
  Delay(300);
  diaplaysmg_bit(duanma[8],7);
  Delay(300); 
  //利用视觉残留的原理,只要切换的够快,在人眼中就是静止的。
}

void main()
{
          
   
  while(1)
  {
          
   
  	 display();
  }
}
经验分享 程序员 微信小程序 职场和发展