TM1637是一种带键盘扫描接口的LED(发光二极管显示器)驱动控制专用电路,内部集成有MCU数字接口,数据锁存器,LED高压驱动,键盘扫描等电路。采用功率CMOS工艺,显示模式(8段×6位),支持共阳数码管输出,键扫描(8×2bit),增强型抗干扰按键识别电路,辉度调节电路(占空比8级可调),两线串行接口(CLK,DIO),振荡方式:内置RC振荡(450Khz+-5%),内置上电复位电路,内置自动消隐电路。引脚功能:当传输数据时,当SCL高电平时,DIO由高变低则代表开始信号,当SCL高电平时,DIO由低变高则代表停止信号,只有当SCL低电平时DIO上的数据才可以发生改变,在SCL高电平时DIO上的数据要保持不变,此时数据发送出去,当正确传输8位数据后TM1637会第九个时钟在DIO管脚上给出一个ACK信号,把DIO拉低。传输时序类似于I2C通信。传输数据是低位在前高位在后。传输过程:下面仅是数码管驱动程序:(注有待改进)驱动的下面这种数码管,普通也行,只是看不到1s的闪烁效果。/***********************************Describe:TM1673控制芯片,可以设置时间的倒计时(定时不准,可以自己校准),四个数码管0xc0,0xc1,0xc2,0xc3****Time:2015.05.14****Author:zys********************************/#includereg52.h#includeintrins.hsbitCLK=P0^0;//时钟信号sbitDIO=P0^1;//数据/地址数据//共阴极数码管unsignedcharcodeSEGData[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//1-9unsignedcharcodeSEGDataDp[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//有小数点只用于地址0xc1unsignedchardpFlag=0;//控制第二个数码管的dp的显示voidTM1637_start(void);voidTM1637_stop(void);voidTM1637_write1Bit(unsignedcharmBit);voidTM1637_write1Byte(unsignedcharmByte);voidTM1637_writeCammand(unsignedcharmData);voidTM1637_writeData(unsignedcharaddr,unsignedcharmData);voidtime_set(charhour,charmin);voidtime_judge(void);voidtime_display(void);voidtimer0_init(void);voiddelay_140us(void);voiddelay_1ms(unsignedinti);unsignedintcountTime=0;unsignedchartime,sec;struct{charshi;charge;}Hour;struct{charshi;charge;}Min;/*********************************************************************主函数****************************************************************/voidmain(void){timer0_init();time_set(5,0);//设置5小时倒计时while(1){time_display();//显示时间if(countTime=10){dpFlag=0;}elseif(countTime=20){dpFlag=1;}else{countTime=0;time_judge();}}}/*********************************************************************名称:voidtime_set(charhour,charmin)*功能:设置时间*输入:charhour,charmin*输出:无**************************************************************/voidtime_set(charhour,charmin){if((hour0)||(min0)){hour=0;min=0;}Hour.shi=hour/10;Hour.ge=hour%10;Min.shi=min/10;Min.ge=min%10;}/**************************************************************名称:time_judge(void)*功能:判断时间变化*输入:void*输出:无**************************************************************/voidtime_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=-1)){Min.shi=5;if(Hour.shi&&(--Hour.ge=-1)){Hour.ge=9;if(--Hour.shi=-1){Hour.shi=0;}}elseif((!Hour.shi)&&(--Hour.ge=-1)){Hour.ge=0;}}elseif((!Hour.shi)&&(!Hour.ge)&&(--Min.shi=-1)){Min.shi=0;}}elseif(((!Hour.shi)&&(!Hour.ge)&&(!Min.shi))&&(Min.ge=-1)){Min.ge=0;}}}/*********************************************************************名称:voidtime_display(void)*功能:显示时间*输入:void*输出:无**************************************************************/voidtime_display(void){TM1637_writeCammand(0x44);TM1637_writeData(0xc0,SEGData[Hour.shi]);if(dpFlag)TM1637_writeData(0xc1,SEGDataDp[Hour.ge]);//小数点标志为1则用小数点那个数组elseTM1637_writeData(0xc1,SEGData[Hour.ge]);TM1637_writeData(0xc2,SEGData[Min.shi]);TM1637_writeData(0xc3,SEGData[Min.ge]);TM1637_writeCammand(0x8a);}/*********************************************************************名称:voidTM1637_start(void)*功能:start信号*输入:void*输出:无**************************************************************/voidTM1637_start(void){CLK=1;DIO=1;delay_140us();DIO=0;delay_140us();CLK=0;delay_140us();}/*********************************************************************名称:voidTM1637_stop(void)*功能:stop信号*输入:void*输出:无**************************************************************/voidTM1637_stop(void){CLK=0;delay_140us();DIO=0;delay_140us();CLK=1;delay_140us();DIO=1;delay_140us();}/*********************************************************************名称:voidTM1637_write1Bit(unsignedcharmBit)*功能:写1bit*输入:unsignedcharmBit*输出:无**************************************************************/voidTM1637_write1Bit(unsignedcharmBit){CLK=0;delay_140us();if(mBit)DIO=1;elseDIO=0;delay_140us();CLK=1;delay_140us();}/*********************************************************************名称:voidTM1637_write1Byte(unsignedcharmByte)*功能:写1byte*输入:unsignedcharmByte*输出:无**************************************************************/voidTM1637_write1Byte(unsignedcharmByte){charloop=0;for(loop=0;loop8;loop++){TM1637_write1Bit((mByteloop)&0x01);//取得最低位}CLK=0;delay_140us();DIO=1;delay_140us();CLK=1;delay_140us();while(DIO==1);//获得应答位}/*********************************************************************名称:voidTM1637_writeCammand(unsignedcharmData)*功能:写指令1byte*输入:unsignedcharmData*输出:无**************************************************************/voidTM1637_writeCammand(unsignedcharmData){TM1637_start();TM1637_write1Byte(mData);//数据TM1637_stop();}/*********************************************************************名称:voidTM1637_writeData(unsignedcharaddr,unsignedcharmData)*功能:固定地址写数据1byte*输入:unsignedcharaddr,unsignedcharmData*输出:无**************************************************************/voidTM1637_writeData(unsig