想请各位大佬,看看原因出在哪里,怎么修改。(现在LCD上显示00.0,没数字,感觉是ADC转化写错了,并没有转换) main.c 文件(其中部分代码来自网络,只是学习目的,没有其他目的。) 1. #include "stm32l4xx.h" 2. #include "stm32l476g_discovery.h" 3. #include "adc.h" 4. #include "dma.h" 5. #include "stm32l476g_discovery_glass_lcd.h" 6. #include "stdlib.h" 7. #include "stdio.h" 8. //#include "main.h" 9. 10. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc); 11. /* USER CODE BEGIN 0 */ 12. uint32_t uhADCxConvertedValue[1]; 13. uint32_t oldvalue[1]; 14. unsigned long shijian=0; 15. volatile uint8_t dmaflage=0; 16. ADC_HandleTypeDef hadc1; 17. void SystemClock_Config(void) 18. { 19. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 20. RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 21. // MSI is enabled after System reset, activate PLL withMSI as source 22. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; 23. RCC_OscInitStruct.MSIState = RCC_MSI_ON; 24. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; 25. RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; 26. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 27. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; 28. RCC_OscInitStruct.PLL.PLLM = 1; 29. RCC_OscInitStruct.PLL.PLLN = 40; 30. RCC_OscInitStruct.PLL.PLLR = 2; 31. RCC_OscInitStruct.PLL.PLLP = 7; 32. RCC_OscInitStruct.PLL.PLLQ = 4; 33. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 34. { 35. // Initialization Error 36. BSP_LED_On(LED_RED); 37. while(1); 38. } 39. // Select PLL as system clock source and configure theHCLK, PCLK1 and PCLK2 40. //clocks dividers 41. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); 42. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 43. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 44. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 45. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 46. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct,FLASH_LATENCY_4) != HAL_OK) 47. { 48. // Initialization Error 49. while(1); 50. } 51. } 52. 53. /* USER CODE END 0 */ 54. int main(void) 55. { 56. /* USER CODE BEGIN 1 */ 57. uint16_t ad1=0; //ad2=0; 58. /* USER CODE END 1 */ 59. /* MCU 60. Configuration----------------------------------------------------------*/ 61. /* Reset of all peripherals, Initializes the Flashinterface and the Systick. */ 62. HAL_Init(); 63. /* Configure the system clock */ 64. SystemClock_Config(); 65. /* Initialize all configured peripherals */ 66. //MX_GPIO_Init(); 67. BSP_LCD_GLASS_Init(); 68. BSP_LED_Init(LED_RED); 69. BSP_LED_Init(LED_GREEN); 70. DMA_conf(); 71. ADC1_conf(); 72. /* USER CODE BEGIN 2 */ 73. #if DEBUG_ENABLE 74. debugInit(115200); 75. #endif 76. int a1,b1,c1; 77. char a[1]; 78. char b[1]; 79. char c[1]; 80. char d[]="M"; 81. char e[]="P"; 82. char f[]="S"; 83. shijian=HAL_GetTick(); 84. //ADC_Cmd(ADC1,ENABLE); 85. HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&uhADCxConvertedValue,1); 86. //ADC_SOFTWARESTARTCONVCMD(ADC1,ENABLE); 87. if(uhADCxConvertedValue!=0){ //printf("HAL_ADC_Start_DMA\r\n"); 88. BSP_LED_On(LED_RED); 89. HAL_Delay(100); 90. BSP_LED_On(LED_GREEN); 91. HAL_Delay(100); 92. //if(uhADCxConvertedValue[1]==oldvalue[1]) 93. BSP_LED_Off(LED_RED); 94. HAL_Delay(100); 95. dmaflage=1; 96. } 97. while (1) 98. { 99. if(HAL_GetTick()-shijian>=500) 100. { 101. if(dmaflage==1) 102. { 103. dmaflage=0; 104. //ad1 = uhADCxConvertedValue[0]; 105. //ad2 = uhADCxConvertedValue[1]; 106. /*printf("ad1=%d,Vad1=%0.2\r\n",ad1,ad1*3.3/4096);//UNIT IS FV 107. ad1 = (ad1*330)/4096; //扩大100倍 ,3.3V, 4096=12bit 108. printf("V100ad1=%d\r\n",ad1); 109. HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&uhADCxConvertedValue,2); 110. ad1 = 0;*/ 111. ad1 = (float)uhADCxConvertedValue[0]*330/4069; 112. 113. a1=(ad1/100 ); 114. b1=(ad1%100/10); 115. c1=(ad1%100%10/1); 116. itoa (a1, a, 10); 117. itoa (b1, b, 10); 118. itoa (c1, c, 10); 119. BSP_LCD_GLASS_DisplayChar(a,POINT_OFF,DOUBLEPOINT_OFF,LCD_DIGIT_POSITION_1); 120. BSP_LCD_GLASS_DisplayChar(b,POINT_ON,DOUBLEPOINT_OFF,LCD_DIGIT_POSITION_2); 121. BSP_LCD_GLASS_DisplayChar(c,POINT_OFF,DOUBLEPOINT_OFF,LCD_DIGIT_POSITION_3); 122. BSP_LCD_GLASS_DisplayChar(d,POINT_OFF,DOUBLEPOINT_OFF,LCD_DIGIT_POSITION_4);//M 123. BSP_LCD_GLASS_DisplayChar(e,POINT_OFF,DOUBLEPOINT_OFF,LCD_DIGIT_POSITION_5);//P 124. BSP_LCD_GLASS_DisplayChar(f,POINT_OFF,DOUBLEPOINT_OFF,LCD_DIGIT_POSITION_6);//S 125. 126. } 127. shijian=HAL_GetTick(); 128. } 129. } 130. } 131. 132. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) 133. { 134. dmaflage=1; 135. //--------- HAL_ADC_Stop_DMA(&hadc1); 136. } adc.c 文件内容: 1. #include "stm32l4xx.h" 2. #include "stm32l476g_discovery.h" 3. #include "adc.h" 4. #include "stm32l4xx_hal_adc.h" 5. ADC_HandleTypeDef hadc1; 6. DMA_HandleTypeDef hdma_adc1; 7. 8. ADC_ChannelConfTypeDef sConfig; 9. 10. void ADC1_conf(void) 11. { 12. HAL_Init();// HAL Init 13. /**Configure the global features of the ADC (Clock,Resolution, Data,Alignment and number of conversion) */ 14. hadc1.Instance = ADC1; 15. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; 16. hadc1.Init.Resolution = ADC_RESOLUTION_12B; 17. hadc1.Init.ScanConvMode = ENABLE; //upward 18. hadc1.Init.ContinuousConvMode = ENABLE; 19. hadc1.Init.DiscontinuousConvMode = DISABLE; 20. hadc1.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START; //software trigger 21. hadc1.Init.ExternalTrigConvEdge =ADC_EXTERNALTRIGCONVEDGE_RISING; //rising edge 22. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; //< Data right alignment 23. hadc1.Init.NbrOfConversion = 2; /* Thisparameter must be a number between Min_Data = 1 and Max_Data = 16. 24. Note:This parameter must be modified when no conversion is on going on regular group(ADC disabled, 25. orADC enabled without continuous mode or external trigger that could launch aconversion). */ 26. hadc1.Init.DMAContinuousRequests = ENABLE; 27. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; 28. /**Configure for the selected ADC regular channel itscorresponding rank in the sequencer and its sample time. */ 29. sConfig.Channel = ADC_CHANNEL_5; 30. sConfig.Rank = 1; 31. sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5; // Sampling time12.5 ADC clock cycles at ADC resolution 12 bits. 32. HAL_ADC_ConfigChannel(&hadc1,&sConfig); 33. /**Configure for the selected ADC regular channel itscorresponding rank in the sequencer and its sample time. */ 34. sConfig.Channel = ADC_CHANNEL_3; 35. sConfig.Rank = 2; 36. HAL_ADC_ConfigChannel(&hadc1,&sConfig); 37. 38. } 39. 40. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) 41. { 42. GPIO_InitTypeDef GPIO_InitStruct; 43. if(hadc->Instance==ADC1) 44. { 45. /* Peripheral clock enable */ 46. __HAL_RCC_ADC_CLK_ENABLE(); 47. /**ADC1 GPIO Configuration PA0-WKUP ------> ADC1_IN5*/ 48. GPIO_InitStruct.Pin = GPIO_PIN_0; //input port 49. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL; //////analog input MODE GPIO_MODE_ANALOG; 50. GPIO_InitStruct.Pull = GPIO_NOPULL; //no pull 51. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 52. /* Peripheral DMA init*/ 53. hdma_adc1.Instance = DMA1_Channel1; 54. hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY; 55. hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE; 56. hdma_adc1.Init.MemInc = DMA_MINC_ENABLE; 57. hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; 58. hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; 59. hdma_adc1.Init.Mode = DMA_CIRCULAR; 60. hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM; 61. HAL_DMA_Init(&hdma_adc1); 62. 63. __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1); 64. } 65. } 66. 67. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) 68. { 69. if(hadc->Instance==ADC1) 70. { 71. /* Peripheral clock disable */ 72. __HAL_RCC_ADC_CLK_DISABLE(); 73. 74. /**ADC1 GPIO Configuration 75. PA0-WKUP ------> ADC1_IN5 PC14 ------> ADC1_IN3 */ 76. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0); //PA0 //HAL_GPIO_DeInit(GPIOC, GPIO_PIN_14);//PC14 77. /* Peripheral DMA DeInit*/ 78. HAL_DMA_DeInit(hadc->DMA_Handle); 79. } 80. } dma.c内容: 1. //#include"stm32l4xx.h" 2. //#include"stm32l476g_discovery.h" 3. #include "dma.h" 4. void DMA_conf(void) 5. { 6. /* DMA controller clock enable */ 7. __HAL_RCC_DMA1_CLK_ENABLE(); 8. // __HAL_RCC_DMA2_CLK_ENABLE(); 9. /* NVIC configuration for DMA Input datainterrupt */ 10. //Nested Vectored InterruptController (NVIC) 11. //set the priority of NVIC 12. HAL_NVIC_SetPriority(1,5,1);//DMA1_Channel1_IRQn,RCC_IRQn, 0WWDG_IRQn : stm32l476xx.h 13. HAL_NVIC_EnableIRQ(1);//DMA1_Channel1_IRQn 14. } |
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分