一、硬件介绍
TM1640 是一种LED(发光二极管显示器)驱动控制专用电路,内部集成有MCU 数字接口、数据锁存器、 LED 驱动等电路。
二、接口说明
微处理器的数据通过两线总线接口和 TM1640 通信,在输入数据时当 CLK 是高电平时,DIN 上的信号必须保持不变;CLK 上的时钟信号为低电平时,DIN 上的信号才能改变。
数据的输入总是低位在前,高位在后传输.数据输入的开始条件是 CLK 为高电平时,DIN 由高变低;结束条件是 CLK 为高时,DIN 由低电平变为高电平。指令数据传输过程如下图:



注意数据的输入条件和结束条件,输入条件是CLK 为高电平时,DIN由低变高
三、驱动代码
我采用以自动地址写数据格式来和TM1640通信
数据指令

//#define sCLK_H gpio_bit_reset(GPIOA,GPIO_PIN_8)
//#define sCLK_L gpio_bit_set(GPIOA,GPIO_PIN_8)
//#define sDATA_H gpio_bit_reset(GPIOA,GPIO_PIN_10)
//#define sDATA_L gpio_bit_set(GPIOA,GPIO_PIN_10)
uint8_t SmgBuff[21]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xbf,
0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0x00};
void TM1640_start ()
{
sCLK_L;
sDATA_H;
sCLK_H;
delay_us(1);
sDATA_L;
delay_us(1);
sCLK_L;
delay_us(1);
}
void TM1640_stop ()
{
sCLK_L;
sDATA_L;
sCLK_H;
delay_us(1);
sDATA_H;
delay_us(1);
}
void send_byte (uint num)
{
uint i
for (i= 8
{
sCLK_L
if (num&0x01)
{
sDATA_H
delay_us(1)
}
else
{
sDATA_L
delay_us(1)
}
sCLK_H
num = num>>1
}
delay_us(1)
}
TM1640_start ();
send_byte (0xc0); //输入首地址
send_byte (SmgBuff[0]); //输入个数码管的显示数据;
send_byte (SmgBuff[1]);
send_byte (SmgBuff[2]);
send_byte (SmgBuff[3]);
TM1640_stop ();

TM1640_start ();
send_byte (0x0f);//最亮
TM1640_stop ();
四、实物展示

原作者:兆易创新GD32 MCU swag