stm32串口发送数据,比如发送“AT”把接收的数据作判断,如果是“OK”,发下一条指令“AR”,不是择返回“error”,采用HAL库编写
时间: 2023-10-02 10:05:18 浏览: 117
好的,针对你的问题,我可以给出一个使用HAL库编写的示例代码,它可以通过USART串口发送“AT”指令,并判断返回的数据是否为“OK”,如果是则发送“AR”指令,否则返回“error”信息,具体代码如下:
```c
#include "stm32f1xx_hal.h"
#include <string.h>
#define USART_HANDLE huart1
#define USART_TIMEOUT 1000
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
char buf[32];
uint32_t start_time;
uint8_t receive_byte;
int i;
/* Initialize HAL and system clock */
HAL_Init();
SystemClock_Config();
/* Initialize GPIO and USART */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* Send "AT" command */
HAL_UART_Transmit(&USART_HANDLE, (uint8_t*)"AT\r\n", strlen("AT\r\n"), USART_TIMEOUT);
/* Wait for response */
start_time = HAL_GetTick();
i = 0;
while (HAL_GetTick() - start_time < USART_TIMEOUT) {
if (HAL_UART_Receive(&USART_HANDLE, &receive_byte, 1, USART_TIMEOUT) == HAL_OK) {
buf[i++] = receive_byte;
if (i >= 2 && buf[i-2] == 'O' && buf[i-1] == 'K') {
/* Send "AR" command */
HAL_UART_Transmit(&USART_HANDLE, (uint8_t*)"AR\r\n", strlen("AR\r\n"), USART_TIMEOUT);
break;
}
}
}
/* If no response received, return error */
if (i == 0 || (i < 2 || !(buf[i-2] == 'O' && buf[i-1] == 'K'))) {
HAL_UART_Transmit(&USART_HANDLE, (uint8_t*)"error\r\n", strlen("error\r\n"), USART_TIMEOUT);
}
while (1);
return 0;
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
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_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure USART Tx and Rx pins */
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
需要注意的是,使用HAL库编写的代码通常比使用标准库编写的代码更加简洁易读,同时也可以提高代码的可维护性和可移植性。但是,HAL库也会增加代码的体积和复杂度,因此在编写代码时需要根据实际需求进行选择。
阅读全文