#include
unsigned char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6,0x89};
unsigned char yi,er,san,si,wu,liu,qi,ba;
void display_12(yi,er);
void display_34(san,si);
void display_56(wu,liu);
void display_78(qi,ba);
void main(){
yi=10;er=10;san=10;si=10;wu=10;liu=10;qi=10;ba=10;
while(1){
display_12(yi,er);
display_34(san,si);
display_56(wu,liu);
display_78(qi,ba);
}
//数码管
void display_12(yi,er){
P2=0XC0;P0=0X01;P2=0XE0;P0=seg[yi];delayms(1);
P2=0XC0;P0=0X02;P2=0XE0;P0=seg[er];delayms(1);
}
void display_34(san,si){
P2=0XC0;P0=0X04;P2=0XE0;P0=seg[san];delayms(1);
P2=0XC0;P0=0X08;P2=0XE0;P0=seg[si];delayms(1);
}
void display_56(wu,liu){
P2=0XC0;P0=0X10;P2=0XE0;P0=seg[wu];delayms(1);
P2=0XC0;P0=0X20;P2=0XE0;P0=seg[liu];delayms(1);
}
void display_78(qi,ba){
P2=0XC0;P0=0X40;P2=0XE0;P0=seg[qi];delayms(1);
P2=0XC0;P0=0X80;P2=0XE0;P0=seg[ba];delayms(1);
P2=0XC0;P0=0XFF;P2=0XE0;P0=0XFF;delayms(1);
}
数码管放在中断的写法:
#include
unsigned char time0_i;
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned char disp[7];
void Timer0Init(void);
void display();
void main(){
Timer0Init();
EA=1;ET0=1;
yi=1;er=1;san=1;si=2;wu=1;liu=1;qi=1;ba=1;
display();
while(1);
}
//每隔1ms左移一位,控制数码管
void time0() interrupt 1{
P2=0XC0;P0=0X01<<time0_i;
P2=0XE0;P0=disp[time0_i];
time0_i++;
if(time0_i==8)time0_i=0;
}
//赋值,实现较为简单的控制
void display(){
disp[0]=seg[yi];disp[1]=seg[er];disp[2]=seg[san];disp[3]=seg[si];
disp[4]=seg[wu];disp[5]=seg[liu];disp[6]=seg[qi];disp[7]=seg[ba];
}
//定时器0初始化1ms
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
}