《TM1637驱动数码管》由会员分享,可在线阅读,更多相关《TM1637驱动数码管(10页珍藏版)》请在金锄头文库上搜索。
1、TM1637是一种带键盘扫描接口的LED(发光二极管显示器)驱动控制专用电路,内部集成有MCU数字接口,数据锁存器,LED高压驱动,键盘扫描等电路。采用功率CMOS工艺,显示模式(8段6位),支持共阳数码管输出,键扫描(82bit),增强型抗干扰按键识别电路,辉度调节电路(占空比8级可调),两线串行接口(CLK,DIO),振荡方式:内置RC振荡(450Khz+-5%),内置上电复位电路,内置自动消隐电路。引脚功能:当传输数据时,当SCL高电平时,DIO由高变低则代表开始信号,当SCL高电平时,DIO由低变高则代表停止信号,只有当SCL低电平时DIO上的数据才可以发生改变,在SCL高电平时DIO
2、上的数据要保持不变,此时数据发送出去,当正确传输8位数据后TM1637会第九个时钟在DIO管脚上给出一个ACK信号,把DIO拉低。传输时序类似于I2C通信。传输数据是低位在前高位在后。传输过程:下面仅是数码管驱动程序:(注有待改进)驱动的下面这种数码管,普通也行,只是看不到1s的闪烁效果。/* * Describe: TM1673控制芯片,可以设置时间的倒计时(定时不准,可以自己校准),四个数码管0xc0,0xc1,0xc2,0xc3 * Time: 2015.05.14 * Author: zys */#include #includesbit CLK = P00; /时钟信号sbit DI
3、O = 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 );void TM1637_stop( void );void TM1
4、637_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( void );void timer0_init( void );voi
5、d delay_140us( void );void delay_1ms(unsigned int i);unsigned int countTime = 0;unsigned char time, sec;struct char shi;char ge;Hour;struct char shi;char ge;Min;/* 主函数*/void main( void )timer0_init();time_set(5, 0); /设置5小时倒计时while(1)time_display(); /显示时间if(countTime = 10)dpFlag = 0;elseif(countTime
6、= 20)dpFlag = 1;elsecountTime = 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_judge( voi
7、d )* 功能 : 判断时间变化* 输入 : void* 输出 : 无*/void 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 = -1)Min.shi = 5;if(Hour.shi & (-Hour.ge = -1)Hour.ge = 9;if(-Hour.shi = -1)Hour.shi = 0;elseif(!Hour.shi) &
8、(-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;/* 名称 : void time_display( void )* 功能 : 显示时间* 输入 : void* 输出 : 无*/void time_display( void )TM1637_writeCammand(0x44);TM1637_writeData(0xc0, SE
9、GDataHour.shi);if(dpFlag)TM1637_writeData(0xc1, SEGDataDpHour.ge); /小数点标志为1则用小数点那个数组 elseTM1637_writeData(0xc1, SEGDataHour.ge);TM1637_writeData(0xc2, SEGDataMin.shi);TM1637_writeData(0xc3, SEGDataMin.ge);TM1637_writeCammand(0x8a);/* 名称 : void TM1637_start( void )* 功能 : start信号* 输入 : void* 输出 : 无*/v
10、oid TM1637_start( void )CLK = 1;DIO = 1;delay_140us();DIO = 0;delay_140us();CLK = 0;delay_140us();/* 名称 : void TM1637_stop( void )* 功能 : stop信号* 输入 : void* 输出 : 无*/void TM1637_stop( void )CLK = 0;delay_140us();DIO = 0;delay_140us();CLK = 1;delay_140us();DIO = 1;delay_140us();/* 名称 : void TM1637_write1Bit(unsigned char mBit )* 功能 : 写1bit* 输入 : unsigned char mBit* 输出 : 无*/void TM1637_write1Bit(unsigned char mBit )CLK = 0;delay_140us();if(mBit)DIO = 1;elseDIO = 0;delay_140us();CLK= 1;delay_140