该板子为共阳数码管。Y7C控制每个数码管的内容,Y6C控制显示数码管的位置。
只需要先通过Y6C打开需要显示的数码管,即让对应的数码管com口为1,再通过Y7C向该数码管写入内容即可。
#include "reg52.h"
unsigned char SMG[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f}; //0~F - .
void Delay(unsigned int t)
{
while(t--);
while(t--);
}
void SelectHc138(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)
{
SelectHc138(6);
P0 = 0x01 << pos; //数码管的位置
SelectHc138(7);
P0 = dat; //数码管的内容
}
void Smg_Static()
{
unsigned char i,j;
//8个数码管单独显示0~9
for(i = 0;i < 8 ;i++) //i为数码管的位置,j为数码管的内容
{
for(j = 0;j <10;j++)
{
ShowSmg_Bit(SMG[j],i); // 显示第i个数码管内容为j
Delay(60000);
}
}
//8个数码管同时显示0~F
for(j = 0;j < 16;j++)
{
SelectHc138(6);
P0 = 0xff;
SelectHc138(7);
P0 = SMG[j];
Delay(60000);
Delay(60000);
}
}
void main()
{
while(1)
{
Smg_Static();
}
}