main.c函数具体代码
/* USER CODE BEGIN 0 */
uint8_t send_data[] = "hello world\r\n";
uint8_t receive_data;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1){
if(receive_data=='1'){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 0);
}
else if(receive_data=='0'){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 1);
}
else{
//HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
}
}
HAL_UART_Receive_IT(&huart1, &receive_data, 1);
}
/* USER CODE END 0 */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] The application entry point.
* @retval int
*/
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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, &receive_data, 1); //在这里面一定不要忘记开启串口中断
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_UART_Transmit(&huart1, send_data , sizeof(send_data), 1000);
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
|