tm1638 stm32
时间: 2023-10-10 07:05:25 浏览: 30
The TM1638 is a chip that provides a combination of seven-segment display, LED indicators, and push-buttons. It communicates with the microcontroller through a 3-wire interface, making it a popular choice for building simple user interfaces with microcontrollers like the STM32.
To use the TM1638 with an STM32, you need to connect the chip's pins to the STM32's GPIO pins. The connections required are:
- CLK: Connect to any GPIO pin of the STM32 that supports the SPI clock signal.
- DIO: Connect to any GPIO pin of the STM32 that supports the SPI data signal.
- STB: Connect to any GPIO pin of the STM32 that can be used as a digital output.
Once you have connected the TM1638 to the STM32, you need to write code to communicate with the chip. This involves sending commands to the chip to turn on/off the LEDs, display digits, and read the state of the push-buttons.
Here is an example code snippet that initializes the TM1638 and turns on the first LED:
```
#include "stm32f4xx.h"
#define CLK_PIN GPIO_Pin_5
#define DIO_PIN GPIO_Pin_6
#define STB_PIN GPIO_Pin_7
void TM1638_Init(void)
{
// Initialize GPIO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = CLK_PIN | DIO_PIN | STB_PIN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// Send initialization commands to TM1638
TM1638_SendCommand(0x8F); // Display control, set brightness to maximum
TM1638_SendCommand(0x44); // Write to display, auto increment address
}
void TM1638_SetLED(uint8_t led, uint8_t state)
{
uint8_t data[4] = {0};
data[0] = 0x44; // Write to display, auto increment address
data[1] = 0xC0 + led; // Address of LED
data[2] = state ? 0x01 : 0x00; // LED state
TM1638_SendData(data, 3);
}
void TM1638_SendCommand(uint8_t cmd)
{
GPIO_ResetBits(GPIOA, STB_PIN);
for(int i = 0; i < 8; i++)
{
GPIO_ResetBits(GPIOA, CLK_PIN);
if(cmd & 0x01)
GPIO_SetBits(GPIOA, DIO_PIN);
else
GPIO_ResetBits(GPIOA, DIO_PIN);
cmd >>= 1;
GPIO_SetBits(GPIOA, CLK_PIN);
}
GPIO_SetBits(GPIOA, STB_PIN);
}
void TM1638_SendData(uint8_t *data, uint8_t len)
{
GPIO_ResetBits(GPIOA, STB_PIN);
for(int i = 0; i < len; i++)
{
uint8_t cmd = data[i];
for(int j = 0; j < 8; j++)
{
GPIO_ResetBits(GPIOA, CLK_PIN);
if(cmd & 0x01)
GPIO_SetBits(GPIOA, DIO_PIN);
else
GPIO_ResetBits(GPIOA, DIO_PIN);
cmd >>= 1;
GPIO_SetBits(GPIOA, CLK_PIN);
}
}
GPIO_SetBits(GPIOA, STB_PIN);
}
int main(void)
{
TM1638_Init();
TM1638_SetLED(0, 1);
while(1)
{
// Main program loop
}
}
```
In this code, the `TM1638_Init` function initializes the GPIO pins and sends the initialization commands to the TM1638. The `TM1638_SetLED` function sets the state of the specified LED. The `TM1638_SendCommand` and `TM1638_SendData` functions are used to send commands and data to the TM1638, respectively.
In the main function, we call `TM1638_Init` to initialize the chip and then call `TM1638_SetLED` to turn on the first LED. The program then enters a loop where it waits for other events to occur.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)