STM32 驱动TM1650 程序源码
时间: 2023-10-24 13:04:32 浏览: 276
TM1650驱动.rar
5星 · 资源好评率100%
以下是STM32驱动TM1650的程序源码,仅供参考:
```
#include "stm32f10x.h"
#include "stdio.h"
#define SDA_OUT() GPIOB->CRL &= 0XFF0FFFFF; GPIOB->CRL |= 0X00300000
#define SDA_IN() GPIOB->CRL &= 0XFF0FFFFF; GPIOB->CRL |= 0X00800000
#define TM1650_SCL_H() GPIOB->BSRR = GPIO_Pin_12
#define TM1650_SCL_L() GPIOB->BRR = GPIO_Pin_12
#define TM1650_SDA_H() GPIOB->BSRR = GPIO_Pin_13
#define TM1650_SDA_L() GPIOB->BRR = GPIO_Pin_13
u8 TM1650_Table[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
void TM1650_Start(void)
{
SDA_OUT();
TM1650_SDA_H();
TM1650_SCL_H();
delay_us(2);
TM1650_SDA_L();
delay_us(2);
TM1650_SCL_L();
delay_us(2);
}
void TM1650_Stop(void)
{
SDA_OUT();
TM1650_SCL_L();
TM1650_SDA_L();
delay_us(2);
TM1650_SCL_H();
TM1650_SDA_H();
delay_us(2);
}
void TM1650_WriteByte(u8 dat)
{
u8 i;
SDA_OUT();
for(i=0; i<8; i++)
{
if((dat<<i)&0x80) TM1650_SDA_H();
else TM1650_SDA_L();
delay_us(2);
TM1650_SCL_H();
delay_us(2);
TM1650_SCL_L();
delay_us(2);
}
}
void TM1650_Display(u8 *buf)
{
u8 i;
TM1650_Start();
TM1650_WriteByte(0x44);
TM1650_Stop();
TM1650_Start();
TM1650_WriteByte(0x40);
for(i=0; i<4; i++)
{
TM1650_WriteByte(TM1650_Table[buf[i]]);
}
TM1650_Stop();
TM1650_Start();
TM1650_WriteByte(0x8f);
TM1650_Stop();
}
int main(void)
{
u8 buf[4] = {0, 1, 2, 3};
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while(1)
{
buf[0]++;
if(buf[0] > 9)
{
buf[0] = 0;
buf[1]++;
}
if(buf[1] > 9)
{
buf[1] = 0;
buf[2]++;
}
if(buf[2] > 9)
{
buf[2] = 0;
buf[3]++;
}
if(buf[3] > 9)
{
buf[3] = 0;
}
TM1650_Display(buf);
delay_ms(500);
}
}
```
注:上述代码仅供参考,具体实现方式可能因不同的STM32型号和开发板而有所不同。
阅读全文