任务描述:用89C51单片机实现如下功能:
硬件电路图
元件清单
C语言程序
#include unsigned char tube1[]={0x5b,0x3f,0x5b,0x3f};//数码管初始状态2020 字符码 unsigned char tube2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴极字符码 sbit key1=P2^0; sbit key2=P2^1; sbit key3=P2^2; sbit key4=P2^3; sbit led1=P3^0; sbit led2=P3^1; sbit led3=P3^2; sbit led4=P3^3; void delay(unsigned int time); void main() { unsigned char i,W; while(1) { if(key1==1&key2==1&key3==1) //初始状态 { W=0x01; P3=0X00; for(i=0;i<4;i++) { P1 =W; W<<=1; P0=tube1[i]; delay(10); P1 =0x00; } } if(key1==0) // 按键1按下 { delay(10); if(key1==0) { led1=1; led2=0; led3=0; led4=0; W=0x08; for(i=0;i<10;i++) { if(key2==0|key3==0|key4==0) break; P1 =W; P0=tube2[i]; delay(1000); P1 =0x00; } } while(!key1); delay(10); while(!key1); } if(key2==0) //按键2按下 { delay(10); if(key2==0) { led1=0; led3=0; led2=1; led4=1; W=0x04; for(i=0;i<10;i=i+2) { if(key1==0|key3==0|key4==0) break; P1 =W; P0=tube2[i]; delay(1000); P1 =0x00; } } while(!key2); delay(10); while(!key2); } if(key3==0) //按键3按下 { delay(10); if(key3==0) { led1=1; led3=1; led2=0; led4=0; W=0x02; for(i=1;i<10;i=i+2) { if(key2==0|key1==0|key4==0) break; P1 =W; P0=tube2[i]; delay(1000); P1 =0x00; } } while(!key3); delay(10); while(!key3); } } } void delay(unsigned int time) { unsigned int j; for(;time> 0;time--) for(j=0;j<125;j++); }
相关文章