此程序现已经过测验,电路衔接是:一个2位的共阴数码管,位选是P3.6,P3.7口,段选是P0口,还有如下3个按键
sbit buzz = P2^2;
sbit clear = P2^0;
sbit start = P2^1;
分别是喇叭,铲除和开端 与gnd相连即可.
#include < reg51.h >
#include < intrins.h >
const unsigned char LED7Code[]={
0xC0, // 0
0xF9, // 1
0xA4, // 2
0xB0, // 3
0x99, // 4
0x92, // 5
0x82, // 6
0xF8, // 7
0x80, // 8
0x90, // 9
0x88, // A
0x83, // B
0xC6, // C
0xA1, // D
0x86, // E
0x8E // F
};
sbit buzz = P2^2;
sbit clear = P2^0;
sbit start = P2^1;
unsigned char count = 0;
unsigned char timer1s_flag;
unsigned char hw,lw;
unsigned char flag;
unsigned char clear_flag;
unsigned char start_flag;
//函数声明
void key_scna(void);
/*********************************************************/
void main()
{
TMOD=0x01 ;
TH0=0xed ;
TL0=0xff ;
EA=1;
ET0=1;
TR0=1 ;
while(1)
{
if(timer1s_flag>=200){
timer1s_flag = 0;
if((count>0)&&(count<=40)){
//count–;
hw=count/10;
lw=count%10;
P0=0XFF;
buzz=0;
}
else if(count==0){
hw=count/10;
lw=count%10;
count=42;
P0=0X00;
buzz=1;
}
count–;
}//500ms
//key_scan();
}
}
/*********************************************************/
// 定时器0中止服务程序.
/*********************************************************/
void timer0() interrupt 1 using 2
{
TH0=0xee ;
TL0=0x04 ;
timer1s_flag++;
if(flag==0){
P3=0X40;
P1=LED7Code[hw];
flag = 1;
}
else{
P3=0X80;
P1=LED7Code[lw];
flag = 0;
}
}
/*********************************************************/
//按键扫描程序.
/*********************************************************/
void key_scna(void)
{
;
}