题目:
在8位数码管中,前面4位显示年份“2018”,接着2位是分割符“—”,最后两位是月份,从1月份开始,每隔一段时间加1个月,到12月之后又从1月份开始递增,如此往复。
#include "reg52.h"
unsigned char month=1; //定义月份
//共阳极数码管
unsigned char code duan_code[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
//选择锁存器
void Select_HC573(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 SEG_Bit(unsigned char pos,unsigned char value)
{
Select_HC573(6); //位选
P0=0x01<<pos;
Select_HC573(7); //段选
P0=value;
}
//数码管延时函数
void Delay_SEG(unsigned int t)
{
while(t--);
}
//数码管静态显示
void SEG_Dynamic()
{
SEG_Bit(0,duan_code[2]); //2
Delay_SEG(500);
SEG_Bit(1,duan_code[0]); //0
Delay_SEG(500);
SEG_Bit(2,duan_code[1]); //1
Delay_SEG(500);
SEG_Bit(3,duan_code[8]); //8
Delay_SEG(500);
SEG_Bit(4,duan_code[16]); //-
Delay_SEG(500);
SEG_Bit(5,duan_code[16]); //-
Delay_SEG(500);
SEG_Bit(6,duan_code[month/10]); //月份的十位
Delay_SEG(500);
SEG_Bit(7,duan_code[month%10]); //月份的个位
Delay_SEG(500);
}
void delay(unsigned char t)
{
while(t--)
{
SEG_Dynamic();
}
}
void main()
{
while(1)
{
SEG_Dynamic();
month++;
if(month>12)
{
month=1;
}
delay(100);
}
}