标题: STM32L05系列单片机ADC功能 hal库源程序 [打印本页]

作者: wysh1983    时间: 2022-6-23 10:43
标题: STM32L05系列单片机ADC功能 hal库源程序
STM32L05系列ADC功能,精确到1毫伏。可以编译直接用,通过串口1将数据发出,显示的是实时的电压值。

单片机源程序如下:
  1. #include "main.h"
  2. #include "adc.h"
  3. #include "usart.h"
  4. #include "gpio.h"

  5. #define  led_on   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  6. #define  led_off  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);

  7. extern  unsigned char flag;
  8. extern  unsigned char sec_flag;
  9.   uint16_t value;
  10.         uint8_t valuel,valueh;
  11.         uint16_t dianya;
  12.         


  13. void SystemClock_Config(void);

  14. int main(void)
  15. {

  16.   HAL_Init();


  17.   SystemClock_Config();

  18.   MX_GPIO_Init();
  19.   MX_ADC_Init();
  20.   MX_USART1_UART_Init();
  21.   CalibrateADC();
  22.   HAL_ADC_Start(&hadc);
  23.         
  24.   while (1)
  25.   {
  26.                 value=(HAL_ADC_GetValue(&hadc)-0x1e);
  27.                 //valueh=(HAL_ADC_GetValue(&hadc)&0xFf00)>>8;/////取得AD转换结果高8位
  28.                 //valuel=HAL_ADC_GetValue(&hadc)&0x00ff;      //////////取得AD转换结果低8位
  29.                 dianya=((uint16_t)value*3300/4096);
  30.                
  31.                 if(flag)
  32.                 {
  33.                         USART1_Send_Byte(dianya/1000+0x30);
  34.                         USART1_Send_Byte(0X2E);         
  35.                         USART1_Send_Byte((dianya/100)%10+0x30);
  36.                         USART1_Send_Byte((dianya/10)%10+0x30);
  37.                         USART1_Send_Byte(dianya%10+0x30);
  38.                         USART1_Send_Byte(0X56);
  39.                         
  40.     // USART1_Send_Byte(valueh);
  41.                         //USART1_Send_Byte(valuel);
  42.                         flag=0;
  43.                         HAL_ADC_Start(&hadc);
  44.   }
  45.                 if(dianya>=2500)   ///电压大于等于2.5V
  46.                 {
  47.                         led_on;
  48.                 }
  49.                 else
  50.                 {
  51.                         led_off;
  52.                 }
  53.   }
  54. }

  55. /**
  56.   * @brief System Clock Configuration
  57.   * @retval None
  58.   */
  59. void SystemClock_Config(void)
  60. {
  61.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  62.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  63.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  64.   /** Configure the main internal regulator output voltage
  65.   */
  66.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  67.   /** Initializes the CPU, AHB and APB busses clocks
  68.   */
  69.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  70.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  71.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  72.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  73.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  74.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_3;
  75.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  76.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  77.   {
  78.     Error_Handler();
  79.   }
  80.   /** Initializes the CPU, AHB and APB busses clocks
  81.   */
  82.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  83.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  84.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  85.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  86.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  87.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  88.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  89.   {
  90.     Error_Handler();
  91.   }
  92.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  93.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  94.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  95.   {
  96.     Error_Handler();
  97.   }
  98. }

  99. /* USER CODE BEGIN 4 */

  100. /* USER CODE END 4 */

  101. /**
  102.   * @brief  This function is executed in case of error occurrence.
  103.   * @retval None
  104.   */
  105. void Error_Handler(void)
  106. {
  107.   /* USER CODE BEGIN Error_Handler_Debug */
  108.   /* User can add his own implementation to report the HAL error return state */

  109.   /* USER CODE END Error_Handler_Debug */
  110. }

  111. #ifdef  USE_FULL_ASSERT
  112. /**
  113.   * @brief  Reports the name of the source file and the source line number
  114.   *         where the assert_param error has occurred.
  115.   * @param  file: pointer to the source file name
  116.   * @param  line: assert_param error line source number
  117.   * @retval None
  118.   */
  119. void assert_failed(uint8_t *file, uint32_t line)
  120. {
  121.   /* USER CODE BEGIN 6 */
  122.   /* User can add his own implementation to report the file name and line number,
  123.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  124.   /* USER CODE END 6 */
  125. }
  126. #endif /* USE_FULL_ASSERT */
复制代码

Keil代码下载 hal库:
代码.7z (2.42 MB, 下载次数: 28)







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1