1.动态显示法,实现数码管分别显示数字1-8; #include #define uchar unsigned char #define uint unsigned int uchar code Ledcode[]={ 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //定义一个数组里面放入1-8 void delay(uint x) { while(x--); } void main() { uchar i; while(1) { for(i=0;i<8;i++) { P3=1< unsigned char code LedCode[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40 }; unsigned char DispBuf[8]={3,3,3,5,5,2,2,3}; //指定要显示的数的位置 void delay(unsigned int x){ while(x--); } void main() { unsigned int i; for(i=0;i<8;i++){ P3=1< unsigned char code LedCode[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; unsigned char Hour=13,Min=23,Sec=25; unsigned char DispBuf[8]; void delay(unsigned int x) { while(x--); } void PutTime() { DispBuf[7]=Sec; //取对应秒数的个位放在最右边显示 DispBuf[6]=Sec/10; //取对应秒数的十位放在右起第二位显示 DispBuf[5]=10; //此处表示秒跟分之间用横杆隔开 DispBuf[4]=Min; DispBuf[3]=Min/10; DispBuf[2]=10; DispBuf[1]=Hour; DispBuf[0]=Hour/10; } void main() { long n=0; while(1) { unsigned char i; PutTime(); for(i=0;i<8;i++) { P3=1< unsigned char code LedCode[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; unsigned char Hour=13,Min=23,Sec=25; unsigned char DispBuf[8]; void delay(unsigned int x) { while(x--); } void PutTime() { DispBuf[7]=Sec; DispBuf[6]=Sec/10; DispBuf[5]=10; DispBuf[4]=Min; DispBuf[3]=Min/10; DispBuf[2]=10; DispBuf[1]=Hour; DispBuf[0]=Hour/10; } void main() { long n=0; while(1) { unsigned char i,ms; //定义一个ms用于控制秒数加一的延时 PutTime(); } } for(i=0;i<8;i++) { } ms++; //控制加一的时延 if(ms>=20) { } if(Sec>=60) //表示进位,当秒满六十则向分钟进一,同时秒数清零 { } if(Min>=60) { } if(Hour>=24) //当小时数满24小时时,则小时数清零,完成计时 { } Hour=0; Hour++;Min=0; Min++;Sec=0; Sec++;ms=0; P3=1<