stm32芯片TM1637数码管显示标准库代码
时间: 2023-08-18 14:04:11 浏览: 221
STM32实现595驱动三个数码管源码(标准库和HAL库实现)
5星 · 资源好评率100%
以下是基于HAL库的TM1637数码管显示标准库代码:
首先需要定义TM1637的相关参数:
```c
#define TM1637_CLK_Pin GPIO_PIN_8
#define TM1637_CLK_GPIO_Port GPIOA
#define TM1637_DIO_Pin GPIO_PIN_9
#define TM1637_DIO_GPIO_Port GPIOA
#define TM1637_DIO_1 GPIOA->BSRR = TM1637_DIO_Pin
#define TM1637_DIO_0 GPIOA->BRR = TM1637_DIO_Pin
#define TM1637_CLK_1 GPIOA->BSRR = TM1637_CLK_Pin
#define TM1637_CLK_0 GPIOA->BRR = TM1637_CLK_Pin
```
然后定义TM1637的相关函数:
```c
void TM1637_Start(void)
{
TM1637_DIO_1;
TM1637_CLK_1;
HAL_Delay(2);
TM1637_DIO_0;
}
void TM1637_Stop(void)
{
TM1637_DIO_0;
TM1637_CLK_1;
HAL_Delay(2);
TM1637_DIO_1;
}
void TM1637_WriteByte(uint8_t data)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
TM1637_CLK_0;
if (data & 0x01)
TM1637_DIO_1;
else
TM1637_DIO_0;
data >>= 1;
TM1637_CLK_1;
}
TM1637_CLK_0;
TM1637_DIO_1;
TM1637_CLK_1;
TM1637_DIO_0;
}
void TM1637_Display(uint8_t *pBuffer)
{
uint8_t i;
TM1637_Start();
TM1637_WriteByte(0x40);
TM1637_Stop();
TM1637_Start();
TM1637_WriteByte(0xC0);
for (i = 0; i < 4; i++)
{
TM1637_WriteByte(pBuffer[i]);
}
TM1637_Stop();
TM1637_Start();
TM1637_WriteByte(0x88);
TM1637_Stop();
}
```
最后,在主函数中调用TM1637_Display函数即可实现数码管的显示:
```c
uint8_t pBuffer[4] = {0x00, 0x00, 0x00, 0x00};
while (1)
{
pBuffer[0] = 0x01;
pBuffer[1] = 0x23;
pBuffer[2] = 0x45;
pBuffer[3] = 0x67;
TM1637_Display(pBuffer);
HAL_Delay(500);
}
```
以上代码仅供参考,具体实现需要根据实际情况进行调整。
阅读全文