TM1681是一种存储器交换LED显示控制的驱动芯片,可以选择多重的ROW/COM模式(32ROW/8COM和24ROW/16COM),可以用来驱动点阵LED。该芯片提供了软件设置的16 个级别的脉宽调制控制输出,可以调整LED 循环显示的亮度。
单片机源程序如下:
- #include "TM1681.h"
- #include "delay.h"
- #include "usart.h"
- #include "Buzzer.h"
- #include "SysTick.h"
- /** Write multiple bits in an 8-bit device register.
- * @param slaveAddr I2C slave device address
- * @param regAddr Register regAddr to write to
- * @param bitStart First bit position to write (0-7)
- * @param length Number of bits to write (not more than 8)
- * @param data Right-aligned value to write
- */
- static unsigned char temp;
- static unsigned char x=1;
- //unsigned char channel_1=1;
- unsigned char Enable_1,Enable_2,Enable_3,Enable_4,Enable_5,Enable_6;
- extern unsigned char channel;
- extern unsigned char NumDis[24],TIME[10],G_1,K1;
- extern int16_t cache[6],data[6];
- //__IO u32 Time_1;
- extern __IO u32 Total_time;
- void TM1637_Delay_us(unsigned int Nus)
- {
- for(;Nus>0;Nus--)
- {
- __nop();
- }
- }
- void TM1637_Start(void)
- {
- TM1637_DIO_OUT();
- TM1637_CLK (1);
- TM1637_OUT_DIO1 = 1;
- TM1637_Delay_us(2);
- TM1637_OUT_DIO1 = 0;
- TM1637_Delay_us(2);
- TM1637_CLK ( 0);
- }
- void TM1637_Ack(void)
- {
- TM1637_DIO_IN();
- TM1637_CLK ( 0);
- TM1637_Delay_us(5);
- while(TM1637_READ_DIO1);
- TM1637_CLK (1);
- TM1637_Delay_us(2);
- }
- void TM1637_Stop(void)
- {
- TM1637_DIO_OUT();
- TM1637_CLK ( 0);
- TM1637_Delay_us(2);
- TM1637_OUT_DIO1 = 0;
- TM1637_Delay_us(2);
- TM1637_CLK (1);
- TM1637_Delay_us(2);
- TM1637_OUT_DIO1 = 1;
- TM1637_Delay_us(2);
- TM1637_CLK( 0);
- TM1637_OUT_DIO1 = 0;
- }
- void TM1637_WriteByte(unsigned char oneByte)
- {
- unsigned char i;
- TM1637_DIO_OUT();
- for(i=0;i<8;i++)
- {
- TM1637_CLK(0);
- if(oneByte&0x01)
- {
- TM1637_OUT_DIO1 = 1;
- }
- else
- {
- TM1637_OUT_DIO1 = 0;
- }
- TM1637_Delay_us(3);
- TM1637_CLK(1);
- oneByte=oneByte>>1;
- }
- }
- void TM1637_DisplayChar_1(unsigned char ch,unsigned char p)
- {
- if(ch>23)ch=0;//防止数组越界
- TM1637_Start();
- TM1637_WriteByte(0x44);//0x44固定模式有利于控制显示位,0x40则采用显示地址自加模式这里不使用
- TM1637_Ack();
- TM1637_Stop();
- TM1637_Start();
-
- TM1637_WriteByte(0xC0+p);//0X00地址开始显示
- TM1637_Ack();
-
- TM1637_WriteByte(NumDis[ch]);//显示
- TM1637_Ack();
-
- TM1637_Stop();
- TM1637_Start();
- TM1637_WriteByte(0x8C);
- TM1637_Ack();
- TM1637_Stop();
-
- }
- void TM1637_Display_1(void)
- {
- unsigned char a=0,b=0,c=0,d=0;
- if(cache[0]<0)
- {
- d=((-cache[0])%60)%10;
- c=((-cache[0])%60)/10;
- b=((-cache[0])/60)%10;
- a=0x40;
- }
- else
- {
- d=(cache[0]%60)%10;
- c=(cache[0]%60)/10;
- b=(cache[0]/60)%10;
- a=(cache[0]/60)/10;
- }
- TM1637_Start();
- TM1637_WriteByte(0x40);//0x40则采用显示地址自加1模式
- TM1637_Ack();
- TM1637_Stop();
- TM1637_Start();
- TM1637_WriteByte(0xC0);//0X00地址开始显示
- TM1637_Ack();
-
- TM1637_WriteByte(TIME[a]);//显示1
- TM1637_Ack();
- if(x==1)
- {
- temp=TIME[b];
- x++;
- }
- temp^=0x80; //10000000^11001111=01001111;01001111^10000000=11001111
- TM1637_WriteByte(temp);//显示2和跳动的冒号
- TM1637_Ack();
-
- TM1637_WriteByte(TIME[c]);//显示0
- TM1637_Ack();
-
- TM1637_WriteByte(TIME[d]);//显示0
- TM1637_Ack();
-
- TM1637_WriteByte(0xFF);
- TM1637_Ack();
-
- TM1637_WriteByte(0xFF);
- TM1637_Ack();
-
- TM1637_Stop();
- TM1637_Start();
- TM1637_WriteByte(0x8c);
- TM1637_Ack();
- TM1637_Stop();
- }
- void time_1(void)
- {
-
- if(x>=2)
- {
- if((Enable_1==1)&&(channel==0))
- {
- G_1=1;
- cache[0]--;
- if(cache[0]<0)
- {
- if((-cache[0])>599)
- {
- Enable_1=0;
- }
- K1=1;
- }
- }
- x=1;
- }
- }
- void Digital_off_1(void)
- {
- TM1637_Start();
- TM1637_WriteByte(0x88);
- TM1637_Ack();
- TM1637_Stop();
-
- }
复制代码- /*******************************************************************************
- * @file TM1637.h
- * @author
- * @date
- *******************************************************************************/
- #ifndef TM1637_H
- #define TM1637_H
- #include "sys.h"
-
- #define TM1637_OUT_DIO1 PCout(0)
- #define TM1637_READ_DIO1 PCin(0)
- //IO方向设置
- #define TM1637_DIO_IN() {GPIOC->CRL&=0XF000000F;GPIOC->CRL|=0X08888880;}
- #define TM1637_DIO_OUT() {GPIOC->CRL&=0XF000000F;GPIOC->CRL|=0X03333330;}
- /**
- * @brief Configuration Dots's Mode enumeration
- */
- typedef enum
- {
- DulDot = 0x00,
- LowDot= 0x01,
- HighDot= 0x2,
- }DisDotSetting;
- typedef enum
- {
- sty = 0x00,
- Jep = 0x01,
- }DisDotMode;
- void TM1637_Delay_us(unsigned int Nus);
- void TM1637_Display_1(void);
- void TM1637_DisplayChar_1(unsigned char ch,unsigned char p);
- void time_1(void);
- void TM1637_Display_2(void);
- void TM1637_DisplayChar_2(unsigned char ch,unsigned char p);
- void time_2(void);
- void TM1637_Display_3(void);
- void TM1637_DisplayChar_3(unsigned char ch,unsigned char p);
- void time_3(void);
- void TM1637_Display_4(void);
- void TM1637_DisplayChar_4(unsigned char ch,unsigned char p);
- void time_4(void);
- void TM1637_Display_5(void);
- void TM1637_DisplayChar_5(unsigned char ch,unsigned char p);
- void time_5(void);
- void TM1637_Display_6(void);
- void TM1637_DisplayChar_6(unsigned char ch,unsigned char p);
- void time_6(void);
- void Digital_off_1(void);
- void Digital_off_2(void);
- void Digital_off_3(void);
- void Digital_off_4(void);
- void Digital_off_5(void);
- void Digital_off_6(void);
- #endif
复制代码
上面2个文件 帖到网页上面了
原理图参考:http://www.51hei.com/bbs/dpj-213103-1.html
|