...ST的HAL使用总结(1).串口中断

部分程序:缓存

uint8_t aRxBuffer[1];
uint8_t UART1RxBuff[10],UART1RxBuffCount=0;
unsigned char g_flag=0;
函数

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART3) 


UART1RxBuff[UART1RxBuffCount++] = aRxBuffer[0]; 
if(UART1RxBuffCount <10)
{
HAL_UART_Receive_IT(&huart3, aRxBuffer,1); 
}

else if(UART1RxBuffCount ==10)
{
UART1RxBuffCount=0;
g_flag=1;
}



oop

}测试



int main(void)
{
  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */


  /* MCU Configuration----------------------------------------------------------*/


  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();


  /* USER CODE BEGIN Init */


  /* USER CODE END Init */


  /* Configure the system clock */
  SystemClock_Config();


  /* USER CODE BEGIN SysInit */


  /* USER CODE END SysInit */


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();


  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
printf("ST_CUBE SERI PORT \n\r");
HAL_UART_Receive_IT(&huart3, aRxBuffer,1);  
  /* USER CODE END 2 */


  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {


  /* USER CODE END WHILE */


  /* USER CODE BEGIN 3 */
if(g_flag == 1)
{
g_flag =0;

HAL_UART_Transmit_IT(&huart3,(uint8_t*)UART1RxBuff,sizeof(UART1RxBuff));
HAL_UART_Receive_IT(&huart3, aRxBuffer,1);
}
  }
  /* USER CODE END 3 */


ui

}spa


注意 (1)HAL_UART_Receive_IT()中若是缓存设置大于1,出现不进入接收中断现象,没弄清楚,因此改成1,由本身写相关缓存程序;ip

(2) 接收回调函数尽可能内容少回调函数

(3) 测试稳定it