本帖最后由 damiaa 于 2023-8-7 09:37 编辑
【STM32WBA52CG】+4 STM32WBA 外设应用一 按键和点灯
【NUCLEO-WBA52CG】+1开箱贴
【STM32WBA52CG】+2 STM32WBA Web Bluetooth试玩
【STM32WBA52CG】+3 STM32WBA 外设应用一 按键和点灯 之后继续
板子有个串口连接到了stlinkv3
板子资料里面显示是:PA8 (VCP1_RX)PB12(VCP1_TX)
修改串口配置:
主程序中前面加入
/* USER CODE BEGIN PV */
uint8_t rx1_buff[1];
#define R1XB_MAX 100
uint8_t rx1_b[R1XB_MAX];
uint8_t rx1_b_index=0;
/* USER CODE END PV */
MX_USART1_UART_Init()初始化中最后加入
HAL_UART_Receive_IT(&huart1, (uint8_t *)rx1_buff, 1);
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
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;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK){
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK){
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK){
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK){
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
HAL_UART_Receive_IT(&huart1, (uint8_t *)rx1_buff, 1);//记得加入这句
/* USER CODE END USART1_Init 2 */
}
给板子加入串口回调函数和led显示函数:
void setled(char ld1,char ld2,char ld3){
if(ld1==0)
HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_SET);
if(ld2==0)
HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);
if(ld3 ==0)
HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
UNUSED(huart);
if(huart->Instance == USART1){
rx1_b[rx1_b_index++] =rx1_buff[0];
rx1_b_index =rx1_b_index%R1XB_MAX;
if(rx1_buff[0] ==0x0d)
{
//set 1 0 0 //10
if((rx1_b[0] == 's')&&(rx1_b[1] == 'e')&&(rx1_b[2] == 't')&&(rx1_b[3] == ' ')&& (rx1_b_index >=10 )){
setled(rx1_b[4]-0x30,rx1_b[6]-0x30,rx1_b[8]-0x30);
rx1_b_index =0;
HAL_UART_Transmit_IT(&huart1, rx1_b, 10);
}
}
HAL_UART_Receive_IT(huart, rx1_buff, 1); //You need to toggle a breakpoint on this line!
}
}
/* USER CODE END 0 */
主程序中就加个循环延时
/* USER CODE BEGIN WHILE */
while (1){
HAL_Delay(100);
}
/* USER CODE END WHILE */
...
串口自动发送命令:发送命令 回复命令显示 led闪烁
57c3fbb5a3f350f99971e5eae334c616
谢谢