[原创]
STM32 L4 串口发送指令控制灯闪烁
- #include "stm32l4xx_hal.h"
- #include "periph.h"
- UART_HandleTypeDef UartHandle;
- uint8_t aRxBuffer;
- int main()
- {
- char ch[]="hello word";
- HAL_Init();
- SystemClock_Config();
- Gpio_Inits();
- __HAL_RCC_USART2_CLK_ENABLE();
-
-
- UartHandle.Instance = USART2;
- UartHandle.Init.BaudRate = 9600;
- UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
- UartHandle.Init.StopBits = UART_STOPBITS_1;
- UartHandle.Init.Parity = UART_PARITY_NONE;
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- UartHandle.Init.Mode = UART_MODE_TX_RX;
-
- HAL_UART_Init(&UartHandle);
-
-
- HAL_NVIC_SetPriority(USART2_IRQn, 1, 0);
- HAL_NVIC_EnableIRQ(USART2_IRQn); //¿aÆô′®¿úÖD¶Ï2
-
-
- HAL_UART_Receive_IT(&UartHandle,&aRxBuffer,1);
-
- HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 10, 0xFFFF);
-
- while(1)
- {
- switch(aRxBuffer)
- {
- case '1':
- {
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_RESET);
-
- break;
- }
- case '2':
- {
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
-
- break;
- }
- default:
- {
- HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
- HAL_Delay(500);
- HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
- HAL_Delay(500);
- break;
- }
-
- }
-
- }
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *husartx)
- {
- HAL_UART_Transmit(&UartHandle,&aRxBuffer,1,0); //·¢ËíArxBuffer
- HAL_UART_Receive_IT(&UartHandle,&aRxBuffer,1); //ÖØD¿aÆô½óêÕÖD¶Ï
- }
- void USART2_IRQHandler(void)
- {
- HAL_UART_IRQHandler(&UartHandle);
- }
此内容由EEWORLD论坛网友xutong原创,如需转载或用于商业用途需征得作者同意并注明出处
暂无评论,赶紧抢沙发吧