【STM32WBA52CG】+4 外设应用二 串口使用
<div class='showpostmsg'> 本帖最后由 damiaa 于 2023-8-7 09:37 编辑<p><strong>【</strong><strong>STM32WBA52CG】+</strong><strong>4</strong><strong> STM32WBA </strong><strong>外设应用</strong><strong>一</strong> <strong>按键和</strong><strong>点灯</strong></p>
<p><a href="https://bbs.eeworld.com.cn/thread-1250745-1-1.html"><strong>【NUCLEO-WBA52CG】+</strong><strong>1开箱贴</strong></a></p>
<p><a href="https://bbs.eeworld.com.cn/thread-1250558-1-1.html"><strong>【</strong><strong>STM32WBA52CG】+</strong><strong>2</strong><strong> STM32WBA Web Bluetooth试玩</strong></a></p>
<p><a href="https://bbs.eeworld.com.cn/forum.php?mod=viewthread&tid=1252139&page=1&extra=#pid3251401"><strong>【</strong><strong>STM32WBA52CG】+3 STM32WBA </strong><strong>外设应用</strong><strong>一</strong><strong> 按键和点灯</strong></a> <strong>之后继续</strong></p>
<p><strong>板子有个串口连接到了stlinkv</strong><strong>3</strong></p>
<p><strong></strong></p>
<p><strong>板子资料里面显示是:PA</strong><strong>8 </strong><strong>(VCP</strong><strong>1_</strong><strong>RX)PB</strong><strong>12</strong><strong>(V</strong><strong>CP1_</strong><strong>TX)</strong></p>
<p><strong></strong></p>
<p><strong>修改串口配置:</strong></p>
<p><strong></strong></p>
<p><strong>主程序中前面加入</strong></p>
<pre>
<code>/* USER CODE BEGIN PV */
uint8_t rx1_buff;
#define R1XB_MAX 100
uint8_t rx1_b;
uint8_t rx1_b_index=0;
/* USER CODE END PV */</code></pre>
<p>MX_USART1_UART_Init()<strong>初始化中</strong><strong>最后</strong><strong>加入</strong></p>
<p><strong>HAL_UART_Receive_</strong><strong>IT</strong>(&huart1, (uint8_t *)rx1_buff, 1);</p>
<pre>
<code>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 */
}</code></pre>
<p><strong>给板子加入串口回调函数</strong><strong>和led显示函数</strong><strong>:</strong></p>
<pre>
<code>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_buff;
rx1_b_index =rx1_b_index%R1XB_MAX;
if(rx1_buff ==0x0d)
{
//set 1 0 0 //10
if((rx1_b == 's')&&(rx1_b == 'e')&&(rx1_b == 't')&&(rx1_b == ' ')&& (rx1_b_index >=10 )){
setled(rx1_b-0x30,rx1_b-0x30,rx1_b-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 */</code></pre>
<p><strong>主程序中就加个循环延时</strong></p>
<pre>
<code>/* USER CODE BEGIN WHILE */
while (1){
HAL_Delay(100);
}
/* USER CODE END WHILE */
...</code></pre>
<p><strong>串口自动发送命令:发</strong><strong>送命令 回复命令</strong><strong>显示 </strong> <strong>led闪烁</strong></p>
<p><strong></strong></p>
<p><strong></strong></p>
<p><strong></strong></p>
<p>2860eba48151d479092c3582429fd141<br />
</p>
<p><strong>谢谢</strong></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>继续跟着楼主学习4 外设应用二 串口使用,收藏</p>
Jacktang 发表于 2023-8-8 07:45
继续跟着楼主学习4 外设应用二 串口使用,收藏
<p>感谢支持!</p>
页:
[1]