stm32L0xx是支持单线串口的 但是我无法调试出效果 以下是我的程序 无法接受 无法发送 我是在能进行正常通信的程序下修改的 main中的初始化函数 static void MX_USART2_UART_Init(void) { 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.Mode = UART_MODE_TX_RX; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_HalfDuplex_Init(&UartHandle) != HAL_OK) { Error_Handler(); } if(HAL_HalfDuplex_Init(&UartHandle) != HAL_OK) { Error_Handler(); } if(HAL_HalfDuplex_Init(&UartHandle) != HAL_OK) { Error_Handler(); } if(HAL_UART_Receive_IT(&UartHandle,(uint8_t *)&temp,1) != HAL_OK) { //这一句写在main函数的while(1)上面。用于启动程序启动一次中断接收 HAL_UART_Transmit_IT(&UartHandle, (uint8_t *)&"ERROR\r\n",7); while(1); } } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) //done { buff[buff_count]=temp[0]; buff_count=buff_count+1; if(buff_count>=8) { buff_count = 0; arg0 = buff[0]; arg1 = buff[1]; arg2 = buff[2]; arg3 = buff[3]; arg4 = buff[4]; arg5 = buff[5]; arg6 = buff[6]; arg7 = buff[7]; } memset(temp,0,8); HAL_UART_Receive_IT(&UartHandle,(uint8_t *)&temp,1); //重新打开串口中断 } stm32l0xx_hal_msp.c中的函数 void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART2) { /* USER CODE BEGIN USART2_MspInit 0 */ /* USER CODE END USART2_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_USART2_CLK_ENABLE(); /**USART2 GPIO Configuration PA2 ------> USART2_TX */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USART2 interrupt Init */ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } } void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) { if(huart->Instance==USART2) { /* USER CODE BEGIN USART2_MspDeInit 0 */ /* USER CODE END USART2_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART2_CLK_DISABLE(); /**USART2 GPIO Configuration PA2 ------> USART2_TX */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2); /* USART2 interrupt DeInit */ HAL_NVIC_DisableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspDeInit 1 */ /* USER CODE END USART2_MspDeInit 1 */ } } 中断函数 void USARTx_IRQHandler(void) { HAL_UART_IRQHandler(&UartHandle); } 在控制时使用这两句进行控制 HAL_HalfDuplex_EnableTransmitter(&UartHandle); HAL_HalfDuplex_EnableReceiver(&UartHandle); 麻烦懂得说一下 |
二、使用HAL直接配置。。。。
三、代码部分,一般没人有心情看
已经上了示波器 这部分代码已经是uart的全部代码了 库部分的不用贴上啦 设置是按照hal格式来的
的确没现象 故障就是无法通信 包括收发都是