TM1637四段数码管模块驱动(.c和.h).docx
1 .留意事项写地址可以自动加一(0x40),写指定(0x44)OXCO0xc3,分别是4段数码管的地址OXSa开显示,0x80关显示通信方式IIC,但写数据式低位在前别反了.TM1637.CttindudeTM1637.h,Mindude"delay,h"1.CDWRlTE模式连续写入数据unsignedcharconstNum()=0x3C0x06,0x5b,0×4t0xz0x6d,0x7d,0x07.0x7f,0×6fr0x77z0x40)/01234S6789A-IIC-delay*t*t»4*4»«»*<*>*«*t*tx*tt*>t*voidIiCJnitOPDJ)DRl=OX0C;PD,CRl=OxFF;PD-CR2=0x;PO,ODR=OxOC;SC1._H;SDA.H;)voidIIC_delay(void)unsignedchari;for(i三0ji<20ii*÷)asm("nop");启动lievoidStartJIC(VOid)(SC1.-H;给芯片发送起先信号SOAH;IIC_delay();SDA_1.;IIC_delay();SC1.-1.;起先信号传送完UJdelay();停止IIC*/voidStopJIC(VOid)SC1.1.;IIjdeIay();SOA,1.;IlJdelavO;SC1._H;Ujdelay();SDA_H;voidackJIC(>SC1.1.;SOA.1.;asm("nopH);asm('nop");asm("nop");/while(SDA,R);SC1.H;asm("nop";asm(*nop");asm("nop");SC1.1.;),写一个字节数据t*KSt*t*JtMtX>4*BtKt*4*t*4*lC>4tStvoidWriteByte(unsigedcharWriteData)(unsignedchari;SC1._1.;for(i=0;i<8;i+|if(WnteData&0x01)SDA_H;elseSDA.l;WriteData>>=l;C.delay();sa_H;IIC-delay();SC1.j;C,delay();ackJC();推断芯片发过来的ACK应答信号voidWriteJEDSROM(UnSignedcharaddr,unsignedcharWData)StartJICO;调用起先信号WriteBYte(add。;写起始地址吩附(OCOH),地址OOH单元.WmeByte(WData);给显示寄存器写显示数据,值可依据实参变更Stop-IICO;调用结束信号,一个字节吩时发送完毕,可以发送下一个吩咐voidClear_1.ed(void)SDA_H;SC1._H;StartJICO;圜用起先伯号WrrteByte(OX44);写吩咐40H(数据设汽),stop.HC();词用结束信号,一个字节吩时发送完毕,可以发送下一个吩咐Write_1.EDSRoM(OXCOQX40);最高位wrlte_1.EDSROM(OxCl,Ox40);中间位Write_1.EDSRoM(OXC2,0x40);最低位Wrlte_1.EDSROM(OXe3,0x40);最低位IIJdeIayO;StartJICO;调用起先信号WriteByte(Ch<8a);送开屏吩咐.(8BHM亮度可以依据低三位Wi整.;显示亮度0x80,关显示StopJICO;调用结束信号,一个字节吩咐发送完毕,可以发送下一个吩咐voidose-1.ed(vod)SDA_H;SC1.-H;start。);调用起先信号WmeBYte(OX80);送开屏吩咐,<8BH>,亮度可以依据低三位调整。;显示亮度0x80,关显示stop_IIC(>圜用结束信号,一个字节吩咐发送完毕,可以发送下一个吩咐voidShow-1.ed(intT)(u8NumO,Numl,NUm2,NUm3;unsignedlongH;/T=S80;H=T;H=98HH2;/Num0=(u8KTl0);/Numl=(u8)(T%l0100);/Num2=(u8)(T%10010);/NUm3=(u8)(T%10);Num0=(u8)(H10(M);Numl=(u8)(H%100000000000);Num2=(u8)(H%10001001;Num3=(u8)(H%ll0);SDA_H;SC1._H;StartJICO;两用起先信号Wme8te(0x44);写吩咐40H(数据设汽).stope);调用结束信号,一个字节吩时发送完毕,可以发送下一个吩咐Write_1.EDSRoM(OXCONum(NumO);最高位WrlteJEDSROM(OXCl,NumNuml);中间位Write_1.EDSRoM(OXC2,NUmINiJm2);最低位WrlteJEDSROM(OXC3,NUmNum3);以低位IQdeIaW);StartJiq);调用起先信号WrrteByte(0x8a);送开屏吩咐,(8BH),亮度可以依据低三位圜整.;显示亮度SSPJIC0;调用结束信号,个字节吩咐发送完毕,可以发送下一个吩咐voidShoW_Ied_One(U8Num0,u8NUml,u8Num2,u8NUm3)SDA_H;SC1.H;StartJICO;调用起先信号WriteBYte(OX44);写吩咐40H(数据设咒.stop©);两用结束信号,一个字节吩咐发送完毕,可以发送下一个吩咐Wrlte_1.EDSROM(OXC0,NUmNum。);/Ai福位Write_1.EDSRoM(OXC1.Num(Numl);中间位write-1.EDSROM(OxC2,Num(Num2);公低位write.1.EDSROM(OxC3,Num(Num3);最低位IQdeIayO;StartJICO;调用起先信号WriteByte(Ox8a);送开屏吩咐,8BH),亮度可以依据低三位调整。;显示亮度StopJICO;调用结束信号,一个字节吩咐发送完毕,可以发送下一个吩咐TM1637.HMifndef_TM1637_h_Mdefine_TM1637_h_lnclude"Iostm8sl03f3.h"defineu8unsignedcharNdefineul6unsignedInt/SC1.-PD3MdefineSC1._H(PD,ODR=(1<<3)«define$Cl_1.(PD_ODR&=(1«3)/SDA-PD2什defineSDA_H"defineSDAJ.MefineSDAR(PD_ODR=1<<2)(PD.ODR&=-(l«2)PDIDRIDR2函数声明voidIICJnit(void)IIC初始化voidC1ear-1.ed(void);voidShow_1.ed(intT);voidShoW_led_0ne(u8Num0,u8NUmI,u8Num2ru8NUm3);voidose.1.ed(vod);Wendif