tjc8048k070_011串口屏与stm32f407zgt6通信代码
时间: 2023-08-04 14:08:26 浏览: 235
好的,以下是基于HAL库实现的tjc8048k070_011串口屏与STM32F407ZGT6的通信代码示例:
```c
#include "stm32f4xx_hal.h"
#include <string.h>
#define UART_TX_BUF_SIZE 128
#define UART_RX_BUF_SIZE 128
UART_HandleTypeDef huart;
uint8_t uart_tx_buf[UART_TX_BUF_SIZE];
uint8_t uart_rx_buf[UART_RX_BUF_SIZE];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
// 初始化串口屏
uint8_t init_cmd[] = {0x55, 0x03, 0x00, 0x01, 0x01, 0xf9};
HAL_UART_Transmit(&huart, init_cmd, sizeof(init_cmd), 1000);
// 发送字符串到串口屏
uint8_t str_cmd[] = {0x55, 0x05, 0x00, 0x02, 0x00, 0x08, 'H', 'e', 'l', 'l', 'o', '!', 0x92};
HAL_UART_Transmit(&huart, str_cmd, sizeof(str_cmd), 1000);
while (1)
{
// 接收串口屏发回的数据
uint8_t rx_len = 0;
while (1)
{
if (HAL_UART_Receive(&huart, &uart_rx_buf[rx_len], 1, 1000) == HAL_TIMEOUT)
{
break;
}
rx_len++;
if (rx_len >= UART_RX_BUF_SIZE)
{
break;
}
if (uart_rx_buf[rx_len-1] == 0x92) // 收到校验位表示数据接收完整
{
break;
}
}
if (rx_len > 0)
{
// 处理接收到的数据
// ...
}
// 发送指令到串口屏
uint8_t cmd[] = {0x55, 0x03, 0x00, 0x03, 0x01, 0xf8};
HAL_UART_Transmit(&huart, cmd, sizeof(cmd), 1000);
HAL_Delay(1000); // 延时等待串口屏执行指令
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART3_UART_Init(void)
{
huart.Instance = USART3;
huart.Init.BaudRate = 9600;
huart.Init.WordLength = UART_WORDLENGTH_8B;
huart.Init.StopBits = UART_STOPBITS_1;
huart.Init.Parity = UART_PARITY_NONE;
huart.Init.Mode = UART_MODE_TX_RX;
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
while(1) {}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */
```
在上述代码中,我们使用USART3初始化了STM32F407ZGT6的串口功能,并通过HAL库提供的HAL_UART_Transmit和HAL_UART_Receive函数实现了与tjc8048k070_011串口屏之间的通信。在主函数中,我们首先发送初始化命令到串口屏,然后发送一个显示字符串的命令,并通过HAL_UART_Receive函数接收串口屏发回的数据,最后循环发送一个指令到串口屏并延时等待其执行。
需要注意的是,上述代码仅为示例代码,实际使用时需要根据具体的需求进行修改和调试。另外,需要根据tjc8048k070_011串口屏的通信协议文档来设置正确的波特率、校验位等参数,以确保STM32F407ZGT6与串口屏之间的通信正常。
阅读全文