接着我的上一帖。我打算usart显示ADC转化后的数值 aResultDMA ,但是依旧有问题(显示0.000,如图所示)。求大神指导啊,V:chongzi15896main.c 文件内容: 1. #include "stm32l476g_discovery.h" 2. #include "stm32l476xx.h" 3. #include "stm32l4xx_hal_conf.h" 4. #include "adc.h" 5. #include "SysClock.h" 6. #include "LED.h" 7. #include "UART.h" 8. #include "main.h" 9. #include "string.h" 10. #include "stdio.h" 11. 12. char RxComByte = 0; 13. uint8_t buffer[BufferSize]; 14. /* ADC handle declaration */ 15. ADC_HandleTypeDef AdcHandle; 16. 17. /* ADC channel configurationstructure declaration */ 18. ADC_ChannelConfTypeDef sConfig; 19. 20. /* Converted value declaration */ 21. uint32_t aResultDMA; 22. 23. int main(void){ 24. 25. int a,n,i; 26. float b; 27. a=1100; 28. 29. 30. HAL_Init(); 31. System_Clock_Init();// SwitchSystem Clock = 80 MHz 32. LED_Init(); 33. UART2_Init(); 34. ADCx_CLK_ENABLE(); 35. ADCx_CHANNEL_GPIO_CLK_ENABLE(); 36. DMAx_CHANNELx_CLK_ENABLE(); 37. ADCx_FORCE_RESET(); 38. ADCx_RELEASE_RESET(); 39. 40. b=(float)aResultDMA*3300/4069; 41. while (1){ 42. n= sprintf((char *)buffer, "a = %d\t", a); 43. n+= sprintf((char *)buffer +n, "b = %f \r\n", b); 44. USART_Write(USART2,buffer, n); 45. a= a + 1; 46. for (i = 0; i < 8000000; i++); // Delay 47. Red_LED_On(); 48. 49. } 50. /* ### - 1 - Initialize ADCperipheral #################################### */ 51. AdcHandle.Instance = ADC1; 52. if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) 53. { 54. /* ADC de-initialization Error */ 55. Red_LED_On(); 56. } 57. 58. AdcHandle.Init.ClockPrescaler =ADC_CLOCK_ASYNC_DIV1; /* Asynchronous clock mode, inputADC clock not divided */ 59. AdcHandle.Init.Resolution =ADC_RESOLUTION_12B; /* 12-bit resolution for converteddata */ 60. AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment for converted data */ 61. AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion ononly 1 channel: channel set on rank 1) */ 62. AdcHandle.Init.EOCSelection =ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicateconversion end */ 63. AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled*/ 64. AdcHandle.Init.ContinuousConvMode = ENABLE; /* Continuous mode enabled(automatic conversion restart after each conversion) */ 65. AdcHandle.Init.NbrOfConversion = 1; /* Parameter discarded becausesequencer is disabled */ 66. AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer isdisabled */ 67. AdcHandle.Init.NbrOfDiscConversion =1; /* Parameter discarded becausesequencer is disabled */ 68. AdcHandle.Init.ExternalTrigConv =ADC_SOFTWARE_START; /* Software start to trigthe 1st conversion manually, without external event */ 69. AdcHandle.Init.ExternalTrigConvEdge =ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */ 70. AdcHandle.Init.DMAContinuousRequests = ENABLE; /* DMA circular mode selected */ 71. AdcHandle.Init.Overrun =ADC_OVR_DATA_OVERWRITTEN; /* DR register is overwritten withthe last conversion result in case of overrun */ 72. AdcHandle.Init.OversamplingMode = DISABLE; /* No oversampling */ 73. 74. /* Initialize ADC peripheral according to the passedparameters */ 75. HAL_ADC_Init(&AdcHandle); 76. if (HAL_ADC_Init(&AdcHandle) != HAL_OK) 77. { 78. Red_LED_On(); 79. } 80. 81. 82. /* ### - 2 - Start calibration############################################ */ 83. HAL_ADCEx_Calibration_Start(&AdcHandle,ADC_SINGLE_ENDED); 84. if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK) 85. { 86. Red_LED_On(); 87. } 88. 89. /* ### - 3 - Channel configuration######################################## */ 90. sConfig.Channel =ADCx_CHANNEL; /* Sampled channel number */ 91. sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel numberADCx_CHANNEL */ 92. sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock cycles unit) */ 93. sConfig.SingleDiff =ADC_SINGLE_ENDED; /* Single-ended input channel */ 94. sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */ 95. sConfig.Offset = 0; /* Parameter discarded becauseoffset correction is disabled */ 96. HAL_ADC_ConfigChannel(&AdcHandle,&sConfig); 97. if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) 98. { 99. Green_LED_Toggle(); 100. } 101. 102. /* ### - 4 - Start conversion in DMA mode################################# */ 103. HAL_ADC_Start_DMA(&AdcHandle,&aResultDMA, 1); 104. if (HAL_ADC_Start_DMA(&AdcHandle, &aResultDMA, 1) != HAL_OK) 105. { 106. Green_LED_Toggle(); 107. } 108. 109. 110. } adc.c 文件内容: 1. #include "main.h" 2. 3. /** @addtogroupSTM32L4xx_HAL_Examples 4. * @{ 5. */ 6. 7. /** @defgroup ADC_DMA_Transfer 8. * @{ 9. */ 10. 11. /* Private typedef-----------------------------------------------------------*/ 12. /* Private define------------------------------------------------------------*/ 13. /* Private macro -------------------------------------------------------------*/ 14. /* Private variables---------------------------------------------------------*/ 15. /* Private function prototypes-----------------------------------------------*/ 16. /* Private functions---------------------------------------------------------*/ 17. 18. /** @defgroup HAL_MSP_Private_Functions 19. * @{ 20. */ 21. 22. /** 23. * @brief ADC MSP Init 24. * @param hadc : ADC handle 25. * @retval None 26. */ 27. void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) 28. { 29. GPIO_InitTypeDef GPIO_InitStruct; 30. staticDMA_HandleTypeDef DmaHandle; 31. 32. /*##-1- Enable peripheralsand GPIO Clocks #################################*/ 33. /* ADC Periph clock enable*/ 34. ADCx_CLK_ENABLE(); 35. /* ADC Periph interface clockconfiguration */ 36. __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); 37. /* Enable DMA clock */ 38. DMAx_CHANNELx_CLK_ENABLE(); 39. 40. __HAL_RCC_GPIOA_CLK_ENABLE(); 41. 42. /*##- 2- Configureperipheral GPIO #########################################*/ 43. /* ADC Channel GPIO pinconfiguration */ 44. GPIO_InitStruct.Pin =ADCx_CHANNEL_PIN; 45. GPIO_InitStruct.Mode =GPIO_MODE_ANALOG_ADC_CONTROL; 46. GPIO_InitStruct.Pull =GPIO_NOPULL; 47. HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT,&GPIO_InitStruct); 48. /*##- 3- Configure DMA#####################################################*/ 49. 50. /***********************Configure DMA parameters ***************************/ 51. DmaHandle.Instance = DMA1_Channel1; 52. DmaHandle.Init.Request = DMA_REQUEST_0; 53. DmaHandle.Init.Direction = DMA_PERIPH_TO_MEMORY; 54. DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; 55. DmaHandle.Init.MemInc = DMA_MINC_ENABLE; 56. DmaHandle.Init.PeriphDataAlignment =DMA_PDATAALIGN_WORD; 57. DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; 58. DmaHandle.Init.Mode = DMA_CIRCULAR; 59. DmaHandle.Init.Priority = DMA_PRIORITY_MEDIUM; 60. 61. /* Deinitialize & Initialize the DMA for new transfer */ 62. HAL_DMA_DeInit(&DmaHandle); 63. HAL_DMA_Init(&DmaHandle); 64. 65. /* Associate the DMA handle*/ 66. __HAL_LINKDMA(hadc,DMA_Handle, DmaHandle); 67. 68. /* NVIC configuration forDMA Input data interrupt */ 69. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1,0); 70. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); 71. } 72. 73. /** 74. * @brief ADC MSPDe-Initialization 75. * This function frees the hardwareresources used in this example: 76. * - Disable the Peripheral's clock 77. * - Revert GPIO to their default state 78. * @param hadc: ADC handlepointer 79. * @retval None 80. */ 81. void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc) 82. { 83. 84. /*##-1- Reset peripherals##################################################*/ 85. ADCx_FORCE_RESET(); 86. ADCx_RELEASE_RESET(); 87. /* ADC Periph clock disable 88. (automatically reset allADC's) */ 89. __HAL_RCC_ADC_CLK_DISABLE(); 90. 91. /*##-2- Disable peripheralsand GPIO Clocks ################################*/ 92. /* De-initialize the ADCChannel GPIO pin */ 93. HAL_GPIO_DeInit(ADCx_CHANNEL_GPIO_PORT,ADCx_CHANNEL_PIN); 94. } 95. 96. 97. 98. /** 99. * @brief Initializes the Global MSP. 100. * @param None 101. * @retval None 102. */ |
没有在while()循环里,ADC转换结果aResultDMA在循环里没有赋值给b变量。
评分
查看全部评分
怎么修改呢?我把 b=(float)aResultDMA*3300/4069; 放到while里,依然没起作用啊
老哥
/* ### - 4 - Start conversion in DMA mode ################################# */
HAL_ADC_Start_DMA(&AdcHandle, &aResultDMA, 1);
if (HAL_ADC_Start_DMA(&AdcHandle, &aResultDMA, 1) != HAL_OK)
{
Red_LED_On();
}
Green_LED_On();
楼主应该先确保读到ADC值,才能正确赋值给变量b。
ST官方代码应该多参考
我也是从example上找的ADC,我感觉我跟例子里写的都一样。尴尬了
楼主可以直接运行ST演示代码,读取正确ADC值后再运行自己的代码试试。