1、#includesbit CLK = P00; /时钟信号sbit DIO = P01; /数据/地址数据 /共阴极数码管unsigned char code SEGData=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;/1-9unsigned char code SEGDataDp=0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef; /有小数点只用于地址0xc1unsigned char dpFlag = 0; /控制第二个数码管的dp的显示void TM1637_start( void
2、 );void TM1637_stop( void );void TM1637_write1Bit(unsigned char mBit);void TM1637_write1Byte(unsigned char mByte);void TM1637_writeCammand(unsigned char mData);void TM1637_writeData(unsigned char addr, unsigned char mData);void time_set(char hour, char min);void time_judge( void );void time_display(
3、 void );void timer0_init( void );void delay_140us( void );void delay_1ms(unsigned int i);unsigned int countTime = 0;unsigned char time, sec;struct char shi; char ge;Hour;Min;/* 主函数*/void main( void ) timer0_init(); time_set(5, 0); /设置5小时倒计时 while(1) time_display(); /显示时间 if(countTime = 10) dpFlag =
4、0; else= 20) dpFlag = 1; countTime = 0; time_judge(); /* 名称 : void time_set(char hour, char min)* 功能 : 设置时间* 输入 : char hour, char min* 输出 : 无*/void time_set(char hour, char min) if(hour 0) | (min0) hour = 0; min = 0; Hour.shi = hour/10; Hour.ge = hour%10; Min.shi = min/10; Min.ge = min%10; /* time_j
5、udge( void ) 判断时间变化 voidvoid time_judge( void ) if(+sec = 60) /60秒 sec = 0; if(Hour.shi | Hour.ge | Min.shi | Min.ge) & (-Min.ge = -1) Min.ge = 9; if(Hour.shi) |(Hour.ge) & (-Min.shi Min.shi = 5; if(Hour.shi & (-Hour.ge Hour.ge = 9; if(-Hour.shi = -1) Hour.shi = 0; else if(!Hour.shi) & Hour.ge = 0;
6、else if(! (!Hour.ge) & Min.shi = 0; if(!Min.shi) ) & (Min.ge Min.ge = 0; void time_display( void ) 显示时间void time_display( void ) TM1637_writeCammand(0x44); TM1637_writeData(0xc0, SEGDataHour.shi); if(dpFlag) TM1637_writeData(0xc1, SEGDataDpHour.ge); /小数点标志为1则用小数点那个数组 else TM1637_writeData(0xc1, SEGD
7、ataHour.ge); TM1637_writeData(0xc2, SEGDataMin.shi); TM1637_writeData(0xc3, SEGDataMin.ge); TM1637_writeCammand(0x8a); void TM1637_start( void ) start信号void TM1637_start( void ) CLK = 1; DIO = 1; delay_140us(); DIO = 0; CLK = 0; void TM1637_stop( void ) stop信号void TM1637_stop( void ) void TM1637_wri
8、te1Bit(unsigned char mBit ) 写1bit unsigned char mBitvoid TM1637_write1Bit(unsigned char mBit ) if(mBit) DIO = 1; DIO = 0; void TM1637_write1Byte(unsigned char mByte) 写1byte unsigned char mBytevoid TM1637_write1Byte(unsigned char mByte) char loop = 0; for(loop = 0; loop loop)&0x01); /取得最低位 while(DIO
9、= 1); /获得应答位 void TM1637_writeCammand(unsigned char mData) 写指令1byte unsigned char mDatavoid TM1637_writeCammand(unsigned char mData) TM1637_start(); TM1637_write1Byte(mData); /数据 TM1637_stop(); void TM1637_writeData(unsigned char addr, unsigned char mData) 固定地址写数据1byte unsigned char addr, unsigned c
10、har mDatavoid TM1637_writeData(unsigned char addr, unsigned char mData) TM1637_write1Byte(addr); /地址 void timer0_init() 定时50ms,实际运行中由于指令运行造成的延时,实际时间肯定大于50msvoid timer0_init( void ) TMOD=0X01; TH0=(65535-50000)/256; TL0=(65535-50000)%256; ET0=1; EA=1; TR0=1; void timer0_isr() interrupt 1 /50ms / countTime+; Delay_1ms(unsigned int i) 延时子程序,延时时间为 140usvoid delay_140us( void ) int i; for(i=0; i20; i+) _nop_(); 延时子程序,延时时间为 1ms * x x (延时一毫秒的个数)*/*void Delay_1ms(unsigned int i)/1ms延时 unsigned char x,j; for(j=0;ji;j+) for(x=0;x=148;x+);*/
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1