开发板上,实现数码管的动态显示,在6个数码管中,前面4位显示年份“2022”,后两位是月份,从1月份开始,每隔一段时间加1个月,到12月之后又从1月开始递增,如此往复。这个程序应该怎么搞哇,我的老是提示有风险
每隔一段时间加1个月?你这个月份跟正常月份不一样的吗?
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0xff};//共阳段码表
uchar month;
void delay_ms(uchar xms) //@11.0592MHz
{
uchar i,j;
while(xms)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
xms--;
}
}
void HC138_select(uchar 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 SMG_mode(uchar channel,uchar num) //数码管选择显示通道和数字
{
HC138_select(6); //打开位选
P0 = 0x01 << channel;
HC138_select(7); //打开段选
P0 = num;
}
void SMG_show()
{
//显示年份
SMG_mode(0,table[2]);
delay_ms(2); //此处延时不能过长
SMG_mode(1,table[0]);
delay_ms(2);
SMG_mode(2,table[2]);
delay_ms(2);
SMG_mode(3,table[2]);
delay_ms(2);
//显示--
SMG_mode(4,table[16]);
delay_ms(2);
SMG_mode(5,table[16]);
delay_ms(2);
//显示月份
SMG_mode(6,table[month/10]);
delay_ms(2);
SMG_mode(7,table[month%10]);
delay_ms(2);
}
void delay_SMG(uchar t) //让变化的数码管那两位显示不闪烁
{
while(t--)
{
SMG_show();
}
}
void system_init()
{
HC138_select(4); //关闭LED
P0 = 0xff;
HC138_select(5); //关闭蜂鸣器和继电器
P0 = 0x00;
}
void main()
{
system_init();
while(1)
{
SMG_show();
month++;
if(month > 12)
{
month = 1;
}
delay_SMG(50);
}
}