1、/这是一个四位数码管动态显示 c 语言程序,每隔一秒加一,直至加到 9999 为止/使用时需采用锁存器#include #include“stdio.h“unsigned char code LED=0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90;unsigned char LEDbuff=0xff,0xff,0xff,0xff;/定义数码管的位选段sbit SEG_bit_1 = P01;sbit SEG_bit_2 = P02;sbit SEG_bit_3 = P03;sbit SEG_bit_4 = P04;unsig
2、ned int cnt=0;unsigned int sec =0;unsigned int mini=0;unsigned int hour=0;unsigned char i=0;/*void delay(unsigned int z )unsigned int x,y;for(x=z;x0;x-)for(y=110;y0;y-);*/void interrupttimer0() interrupt 1 /*设置中断函数*/TH0=0xfc;TL0=0x18;cnt+;/P1=0x80;/delay(1);switch(i)case 0: SEG_bit_1 = 0;SEG_bit_4 =
3、 1;P1=LEDbuff0;i+;break;case 1: SEG_bit_4 = 0;SEG_bit_3 = 1;P1=LEDbuff1;i+;break;case 2: SEG_bit_3 = 0;SEG_bit_2 = 1;P1=LEDbuff2&;i+;break;case 3: SEG_bit_2 = 0;SEG_bit_1 = 1;P1=LEDbuff3;i=0;break;default:break;/*void serial_init()/TMOD = 0x20; /定时器 T/C1 工作方式 2SCON = 0x50; /串口工作方式 1,允许串口接收(SCON = 0x
4、40 时禁止串口接收)TH1 = 0xF3; /定时器初值高 8 位设置TL1 = 0xF3; /定时器初值低 8 位设置PCON = 0x80; /波特率倍频(屏蔽本句波特率为 2400)TR1 = 1; /定时器启动 */void main()/serial_init(); /串口初始化/TMOD=0x21; /*设置定时器*/TMOD=0x01;TH0=0xfc;TL0=0x18;TR0=1;EA=1; /*设置中断*/ET0=1;while(1)if(0=TF0)if(cnt=1000)cnt=0;sec+;if (sec=60) sec=0;mini+;if (mini=60) mini=0;hour+;if (hour=24) hour=0;LEDbuff0=LEDsec%10; /*设置数码管显示位*/LEDbuff1=LEDsec/10%10;LEDbuff2=LEDmini%10;LEDbuff3=LEDmini/10%10;/ TI=1; /使用 printf 函数前须先将发送标志位 TI 置 1/ printf(“%4d %x ,%x %xn “,sec,LEDbuff0,LEDbuff1,LEDbuff2);/ TI=0;