2017-09-04 369 views
0

我試圖以經由UART/USARTstm32L476發現板接收數據終端PC。我能夠將數據傳輸到PC,但我無法接收任何數據。STM32 UART/USART接收問題(沒有中斷)

我的代碼如下:

/* Includes ------------------------------------------------------------------*/ 
#include "main.h" 
#include "stm32l4xx_hal.h" 

/* USER CODE BEGIN Includes */ 

/* USER CODE END Includes */ 

/* Private variables ---------------------------------------------------------*/ 
UART_HandleTypeDef huart2; 

/* USER CODE BEGIN PV */ 
/* Private variables ---------------------------------------------------------*/ 

/* USER CODE END PV */ 

/* Private function prototypes -----------------------------------------------*/ 
void SystemClock_Config(void); 
static void MX_GPIO_Init(void); 
static void MX_USART2_UART_Init(void); 

/* USER CODE BEGIN PFP */ 
/* Private function prototypes -----------------------------------------------*/ 

/* USER CODE END PFP */ 
uint8_t cmd[] = {4,7,8,9}; 
uint8_t data[10]; 
uint8_t data1[10]; 
uint32_t err_code = 0; 
int count = 0; 
/* USER CODE BEGIN 0 */ 
int stat_re; 
int mydata = 1; 
/* USER CODE END 0 */ 

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_USART2_UART_Init(); 

    /* USER CODE BEGIN 2 */ 

    /* USER CODE END 2 */ 

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

     // HAL_UART_Transmit(&huart2,(uint8_t*) cmd, 4, 5); 
     // HAL_Delay(1000); 

     /* 
     while (!(USART_ISR_RXNE)); 
     mydata = (int)(USART_RDR_RDR & 0xFF);*/ 


    HAL_UART_Receive(&huart2, data, 10, 5000) 
     data1[10] = data[10]; 

    } 


} 

/** System Clock Configuration 
*/ 
void SystemClock_Config(void) 
{ 

    RCC_OscInitTypeDef RCC_OscInitStruct; 
    RCC_ClkInitTypeDef RCC_ClkInitStruct; 
    RCC_PeriphCLKInitTypeDef PeriphClkInit; 

    /**Initializes the CPU, AHB and APB busses clocks 
    */ 
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; 
    RCC_OscInitStruct.MSIState = RCC_MSI_ON; 
    RCC_OscInitStruct.MSICalibrationValue = 0; 
    RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; 
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; 
    RCC_OscInitStruct.PLL.PLLM = 1; 
    RCC_OscInitStruct.PLL.PLLN = 40; 
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7; 
    RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; 
    RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; 
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 
    { 
    _Error_Handler(__FILE__, __LINE__); 
    } 

    /**Initializes the CPU, AHB and APB busses clocks 
    */ 
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 
           |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) 
    { 
    _Error_Handler(__FILE__, __LINE__); 
    } 

    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2; 
    PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_SYSCLK; 
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) 
    { 
    _Error_Handler(__FILE__, __LINE__); 
    } 

    HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); 

    /**Configure the main internal regulator output voltage 
    */ 
    if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) 
    { 
    _Error_Handler(__FILE__, __LINE__); 
    } 

    /**Configure the Systick interrupt time 
    */ 
    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 

    /**Configure the Systick 
    */ 
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 

    /* SysTick_IRQn interrupt configuration */ 
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 
} 

/* USART2 init function */ 
static void MX_USART2_UART_Init(void) 
{ 

    huart2.Instance = USART2; 
    huart2.Init.BaudRate = 115200; 
    huart2.Init.WordLength = UART_WORDLENGTH_8B; 
    huart2.Init.StopBits = UART_STOPBITS_1; 
    huart2.Init.Parity = UART_PARITY_NONE; 
    huart2.Init.Mode = UART_MODE_TX_RX; 
    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 
    huart2.Init.OverSampling = UART_OVERSAMPLING_16; 
    huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; 
    huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; 
    if (HAL_UART_Init(&huart2) != HAL_OK) 
    { 
    _Error_Handler(__FILE__, __LINE__); 
    } 

} 

/** Configure pins as 
     * Analog 
     * Input 
     * Output 
     * EVENT_OUT 
     * EXTI 
    PA8 ------> RCC_MCO 
*/ 
static void MX_GPIO_Init(void) 
{ 

    GPIO_InitTypeDef GPIO_InitStruct; 

    /* GPIO Ports Clock Enable */ 
    __HAL_RCC_GPIOA_CLK_ENABLE(); 
    __HAL_RCC_GPIOB_CLK_ENABLE(); 
    __HAL_RCC_GPIOE_CLK_ENABLE(); 

    /*Configure GPIO pin : PB2 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_2; 
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 

    /*Configure GPIO pin : PE8 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_8; 
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); 

    /*Configure GPIO pin : PA8 */ 
    GPIO_InitStruct.Pin = GPIO_PIN_8; 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 
    GPIO_InitStruct.Alternate = GPIO_AF0_MCO; 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 

} 

/* USER CODE BEGIN 4 */ 

/* USER CODE END 4 */ 

/** 
    * @brief This function is executed in case of error occurrence. 
    * @param None 
    * @retval None 
    */ 
void _Error_Handler(char * file, int line) 
{ 
    /* USER CODE BEGIN Error_Handler_Debug */ 
    /* User can add his own implementation to report the HAL error return state */ 
    while(1) 
    { 
    } 
    /* USER CODE END Error_Handler_Debug */ 
} 

#ifdef USE_FULL_ASSERT 

/** 
    * @brief Reports the name of the source file and the source line number 
    * where the assert_param error has occurred. 
    * @param file: pointer to the source file name 
    * @param line: assert_param error line source number 
    * @retval None 
    */ 
void assert_failed(uint8_t* file, uint32_t line) 
{ 
    /* USER CODE BEGIN 6 */ 
    /* User can add his own implementation to report the file name and line number, 
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 
    /* USER CODE END 6 */ 

} 

#endif 

/** 
    * @} 
    */ 

/** 
    * @} 
*/ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 

請讓我知道如果你知道這個問題。 我曾嘗試去除連接到操縱桿的電阻器,該電阻器也連接到接收引腳USART2

+1

你從最初的例子改變了什麼? –

+1

如果你有一個像這樣的數組'uint8_t data [10];'然後訪問它的最後一個元素,如:data1 [10] = data [10];'是錯誤的。 10長陣列的最後一個元素位於索引9,因爲第一個索引是0。 –

回答

0

您似乎缺少GPIO配置。

對於這個特定的MCU,可以將USART2 RX複用功能分配給PA3或PD6。 TX是PA2或PD5。選擇你需要的變體並初始化引腳 - 在這兩種情況下它都是AF7。還要確保USART和GPIO的時鐘已啓用。

0

事實上,您的代碼會一直阻塞,直到接收到10個字節或5秒超時。然後,您正在嘗試讀取數組中的1個字節,但您的讀取超出了範圍。 要檢查UART正常工作,你可以嘗試一個非常簡單的回聲是這樣的:

while(1) 
{ 
    uint8_t echo; 
    //Blocks indefinitely until 1 byte is received 
    HAL_UART_Receive(&huart2, &echo, 1, HAL_MAX_DELAY); 
    //Sends back the byte 
    HAL_UART_Transmit(&huart2, &echo, 1, HAL_MAX_DELAY); 
}