用stm32zet6写一个使用串口三通过hc08蓝牙模块发送数据和串口中断接收数据的程序
时间: 2023-06-12 22:04:13 浏览: 158
以下是一个使用STM32F103ZET6控制器,通过USART3串口和HC08蓝牙模块进行数据通信的例程。在本程序中,我们使用了STM32CubeMX来进行初始化配置,所以您需要使用STM32CubeMX软件生成代码,并将其导入到您的开发环境中。本例程还使用了HAL库提供的函数。
```c
#include "main.h"
#include "stdio.h"
#include "string.h"
UART_HandleTypeDef huart3;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
char rx_data[1];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
while (1)
{
if(HAL_UART_Receive_IT(&huart3, (uint8_t *)rx_data, 1) == HAL_OK) //启用串口中断
{
if(rx_data[0] == '1') //判断接收到的数据
{
char buffer[20];
sprintf(buffer, "SEND DATA 1\r\n"); //将数据格式化
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, strlen(buffer), 0xFFFF); //发送数据
}
else if(rx_data[0] == '2')
{
char buffer[20];
sprintf(buffer, "SEND DATA 2\r\n");
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, strlen(buffer), 0xFFFF);
}
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //UART接收中断回调函数
{
if(huart->Instance == USART3)
{
HAL_UART_Receive_IT(&huart3, (uint8_t *)rx_data, 1); //重新启用接收中断
}
}
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);
/** 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.HSIState = RCC_HSI_OFF;
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_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 9600;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
```
在本例程中,我们使用串口3和HC08蓝牙模块进行数据通信。当接收到数据时,我们会检查数据的内容,并根据数据内容发送不同的数据。在此之前,我们先启用了串口3的中断功能,在接收到数据时,会触发中断回调函数。在中断回调函数中,我们重新启用了接收中断,以便能够继续接收数据。
阅读全文