数码管使用技巧
1如何运用数码管配合按键切换界面显示切换界面
方法:定义一个变量来显示显示界面;通过按键来改变变量的数值;从而切换显示界面。
#include <STC15F2K60S2.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)//锁存器控制函数
{
P0=p0date;
P2=(P2&0X1F)|p2date;
P2&=0X1F;
}
unsigned char smg_count;
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//共阳极段码
unsigned char display_mode;//界面显示
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(display_mode==0)//显示第一个界面
{
smg_display[0]=smg_du[0];
smg_display[1]=smg_du[1];
smg_display[2]=smg_du[2];
smg_display[3]=smg_du[3];
smg_display[4]=smg_du[4];
smg_display[5]=smg_du[5];
smg_display[6]=smg_du[6];
smg_display[7]=smg_du[7];
}
if(display_mode==1)//显示第二个界面
{
smg_display[0]=smg_du[1];
smg_display[1]=smg_du[2];
smg_display[2]=smg_du[3];
smg_display[3]=smg_du[4];
smg_display[4]=smg_du[5];
smg_display[5]=smg_du[6];
smg_display[6]=smg_du[7];
smg_display[7]=smg_du[8];
}
}
}
unsigned char trig_btn;
unsigned char con_btn;
unsigned int key_count;
void key_btn()//按键扫描函数
{
unsigned char readdate=P3^0xff;
trig_btn=readdate&(con_btn^0xff);
con_btn=readdate;
}
void key_process()//按键处理函数
{
if(key_count>10)//每十毫秒扫描一次按键
{
key_count=0;
key_btn();
if(trig_btn==0x08)//按键s4
{
display_mode++;
if(display_mode==2)//切换界面0 1
{
display_mode=0;
}
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0x04;
EA=1;
}
void main()
{
Timer2Init();
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
key_process();
}
}
void service_timer2() interrupt 12
{
smg_count++;
key_count++;
smg_show();
}
2如何让数码管数据实现一秒亮灭
在定时器中设置一个一秒的定时;然后再用一个标志位显示,如果到了一秒就让数码管亮一秒没到的话就熄灭。
#include <STC15F2K60S2.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=(P2&0X1F)|p2date;
P2&=0X1F;
}
unsigned char smg_count;
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char display_mode;
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
bit smg_blink;
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(smg_blink)
{
smg_display[0]=smg_du[0];
smg_display[1]=smg_du[1];
smg_display[2]=smg_du[2];
smg_display[3]=smg_du[3];
smg_display[4]=smg_du[4];
smg_display[5]=smg_du[5];
smg_display[6]=smg_du[6];
smg_display[7]=smg_du[7];
}
else
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=0x00;
smg_display[7]=0x00;
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0x04;
EA=1;
}
void main()
{
Timer2Init();//定时器初始化
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
}
}
void service_timer2() interrupt 12//定时器使用函数
{
unsigned int time_count;//定时器计时变量
time_count++;
if(time_count>1000)//定时一秒
{
time_count=0;
smg_blink=!smg_blink;
}
smg_count++;
smg_show();
}
3数码管显示有用数据,没有显示的熄灭。
方法:按照十位百位千位判断
#include <STC15F2K60S2.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=(P2&0X1F)|p2date;
P2&=0X1F;
}
unsigned char smg_count;
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char display_mode;
unsigned int number=12345;
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(number>9999)//如果大于9999
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=smg_du[number/10000];
smg_display[4]=smg_du[number/1000%10];
smg_display[5]=smg_du[number/100%10];
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else if(number>999) //如果大于999
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=smg_du[number/1000%10];
smg_display[5]=smg_du[number/100%10];
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else if(number>99)//如果大于99
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=smg_du[number/100%10];
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else if(number>9)//如果大于9
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=0x00;
smg_display[7]=smg_du[number%10];
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0x04;
EA=1;
}
void main()
{
Timer2Init();//定时器初始化
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
}
}
void service_timer2() interrupt 12//定时器使用函数
{
smg_count++;
smg_show();
}