找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4315|回复: 4
收起左侧

STM32的ADC多通道采样源代码+Proteus仿真

[复制链接]
ID:674330 发表于 2020-11-30 12:48 | 显示全部楼层 |阅读模式
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
51hei图片20201130124522.png
单片机源程序如下:
  1. #include "main.h"

  2. /* Private includes ----------------------------------------------------------*/
  3. /* USER CODE BEGIN Includes */
  4. #include <stdio.h>
  5. /* USER CODE END Includes */

  6. /* Private typedef -----------------------------------------------------------*/
  7. /* USER CODE BEGIN PTD */

  8. /* USER CODE END PTD */

  9. /* Private define ------------------------------------------------------------*/
  10. /* USER CODE BEGIN PD */
  11. /* USER CODE END PD */

  12. /* Private macro -------------------------------------------------------------*/
  13. /* USER CODE BEGIN PM */

  14. /* USER CODE END PM */

  15. /* Private variables ---------------------------------------------------------*/
  16. ADC_HandleTypeDef hadc1;

  17. UART_HandleTypeDef huart1;

  18. /* USER CODE BEGIN PV */

  19. /* USER CODE END PV */

  20. /* Private function prototypes -----------------------------------------------*/
  21. void SystemClock_Config(void);
  22. static void MX_GPIO_Init(void);
  23. static void MX_USART1_UART_Init(void);
  24. static void MX_ADC1_Init(void);
  25. /* USER CODE BEGIN PFP */

  26. /* USER CODE END PFP */

  27. /* Private user code ---------------------------------------------------------*/
  28. /* USER CODE BEGIN 0 */


  29. /* USER CODE END 0 */

  30. /**
  31.   * @brief  The application entry point.
  32.   * @retval int
  33.   */
  34. int main(void)
  35. {
  36.   /* USER CODE BEGIN 1 */
  37.     uint8_t i;
  38.     uint32_t ad_value[3] = {0};
  39.   /* USER CODE END 1 */

  40.   /* MCU Configuration--------------------------------------------------------*/

  41.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  42.   HAL_Init();

  43.   /* USER CODE BEGIN Init */

  44.   /* USER CODE END Init */

  45.   /* Configure the system clock */
  46.   SystemClock_Config();

  47.   /* USER CODE BEGIN SysInit */

  48.   /* USER CODE END SysInit */

  49.   /* Initialize all configured peripherals */
  50.   MX_GPIO_Init();
  51.   MX_USART1_UART_Init();
  52.   MX_ADC1_Init();
  53.   /* USER CODE BEGIN 2 */
  54.     for(i=0;i<3;i++)
  55.     {
  56.         HAL_ADC_Start(&hadc1);
  57.         HAL_ADC_PollForConversion(&hadc1,0xffff);
  58.         ad_value<i>=HAL_ADC_GetValue(&hadc1);
  59.     }
  60.     HAL_ADC_Stop(&hadc1);
  61.     HAL_Delay(500);
  62.   /* USER CODE END 2 */

  63.   /* Infinite loop */
  64.   /* USER CODE BEGIN WHILE */
  65.   while (1)
  66.   {
  67.         for(i=0;i<3;i++)
  68.         {
  69.             HAL_ADC_Start(&hadc1);
  70.             HAL_ADC_PollForConversion(&hadc1,0xffff);
  71.             ad_value<i>=HAL_ADC_GetValue(&hadc1);
  72.             printf("ch:%d--%d--",i,ad_value<i>);
  73.         }
  74.         printf("\r\n");
  75.         
  76.         HAL_ADC_Stop(&hadc1);
  77.         HAL_Delay(500);
  78.         
  79.     /* USER CODE END WHILE */

  80.     /* USER CODE BEGIN 3 */
  81.   }
  82.   /* USER CODE END 3 */
  83. }

  84. /**
  85.   * @brief System Clock Configuration
  86.   * @retval None
  87.   */
  88. void SystemClock_Config(void)
  89. {
  90.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  91.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  92.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  93.   /** Initializes the RCC Oscillators according to the specified parameters
  94.   * in the RCC_OscInitTypeDef structure.
  95.   */
  96.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  97.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  98.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  99.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  100.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  101.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  102.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  103.   {
  104.     Error_Handler();
  105.   }
  106.   /** Initializes the CPU, AHB and APB buses clocks
  107.   */
  108.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  109.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  110.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  111.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  112.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  113.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  114.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  115.   {
  116.     Error_Handler();
  117.   }
  118.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  119.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  120.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  121.   {
  122.     Error_Handler();
  123.   }
  124. }

  125. /**
  126.   * @brief ADC1 Initialization Function
  127.   * @param None
  128.   * @retval None
  129.   */
  130. static void MX_ADC1_Init(void)
  131. {

  132.   /* USER CODE BEGIN ADC1_Init 0 */

  133.   /* USER CODE END ADC1_Init 0 */

  134.   ADC_ChannelConfTypeDef sConfig = {0};

  135.   /* USER CODE BEGIN ADC1_Init 1 */

  136.   /* USER CODE END ADC1_Init 1 */
  137.   /** Common config
  138.   */
  139.   hadc1.Instance = ADC1;
  140.   hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  141.   hadc1.Init.ContinuousConvMode = DISABLE;
  142.   hadc1.Init.DiscontinuousConvMode = ENABLE;
  143.   hadc1.Init.NbrOfDiscConversion = 1;
  144.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  145.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  146.   hadc1.Init.NbrOfConversion = 3;
  147.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  148.   {
  149.     Error_Handler();
  150.   }
  151.   /** Configure Regular Channel
  152.   */
  153.   sConfig.Channel = ADC_CHANNEL_1;
  154.   sConfig.Rank = ADC_REGULAR_RANK_1;
  155.   sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
  156.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  157.   {
  158.     Error_Handler();
  159.   }
  160.   /** Configure Regular Channel
  161.   */
  162.   sConfig.Channel = ADC_CHANNEL_2;
  163.   sConfig.Rank = ADC_REGULAR_RANK_2;
  164.   sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
  165.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  166.   {
  167.     Error_Handler();
  168.   }
  169.   /** Configure Regular Channel
  170.   */
  171.   sConfig.Channel = ADC_CHANNEL_3;
  172.   sConfig.Rank = ADC_REGULAR_RANK_3;
  173.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  174.   {
  175.     Error_Handler();
  176.   }
  177.   /* USER CODE BEGIN ADC1_Init 2 */

  178.   /* USER CODE END ADC1_Init 2 */

  179. }

  180. /**
  181.   * @brief USART1 Initialization Function
  182.   * @param None
  183.   * @retval None
  184.   */
  185. static void MX_USART1_UART_Init(void)
  186. {

  187.   /* USER CODE BEGIN USART1_Init 0 */

  188.   /* USER CODE END USART1_Init 0 */

  189.   /* USER CODE BEGIN USART1_Init 1 */

  190.   /* USER CODE END USART1_Init 1 */
  191.   huart1.Instance = USART1;
  192.   huart1.Init.BaudRate = 9600;
  193.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  194.   huart1.Init.StopBits = UART_STOPBITS_1;
  195.   huart1.Init.Parity = UART_PARITY_NONE;
  196.   huart1.Init.Mode = UART_MODE_TX_RX;
  197.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  198.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  199.   if (HAL_UART_Init(&huart1) != HAL_OK)
  200.   {
  201.     Error_Handler();
  202.   }
  203.   /* USER CODE BEGIN USART1_Init 2 */

  204.   /* USER CODE END USART1_Init 2 */

  205. }

  206. /**
  207.   * @brief GPIO Initialization Function
  208.   * @param None
  209.   * @retval None
  210.   */
  211. static void MX_GPIO_Init(void)
  212. {
  213.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  214.   /* GPIO Ports Clock Enable */
  215.   __HAL_RCC_GPIOA_CLK_ENABLE();
  216.   __HAL_RCC_GPIOB_CLK_ENABLE();

  217.   /*Configure GPIO pin Output Level */
  218.   HAL_GPIO_WritePin(GPIOB, LED0_Pin|LED1_Pin, GPIO_PIN_RESET);

  219.   /*Configure GPIO pin : KEY0_Pin */
  220.   GPIO_InitStruct.Pin = KEY0_Pin;
  221.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  222.   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  223.   HAL_GPIO_Init(KEY0_GPIO_Port, &GPIO_InitStruct);

  224.   /*Configure GPIO pins : LED0_Pin LED1_Pin */
  225.   GPIO_InitStruct.Pin = LED0_Pin|LED1_Pin;
  226.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  227.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  228.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  229.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  230.   /* EXTI interrupt init*/
  231.   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  232.   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

  233. }

  234. /* USER CODE BEGIN 4 */
  235. int fputc(int ch,FILE *f)
  236. {
  237.     uint8_t temp[1]={ch};
  238.     HAL_UART_Transmit(&huart1,temp,1,2);
  239.     return ch;
  240. }
  241. /* USER CODE END 4 */

  242. /**
  243.   * @brief  This function is executed in case of error occurrence.
  244.   * @retval None
  245.   */
  246. void Error_Handler(void)
  247. {
  248.   /* USER CODE BEGIN Error_Handler_Debug */
  249.   /* User can add his own implementation to report the HAL error return state */

  250.   /* USER CODE END Error_Handler_Debug */
  251. }

  252. #ifdef  USE_FULL_ASSERT
  253. /**
  254.   * @brief  Reports the name of the source file and the source line number
  255.   *         where the assert_param error has occurred.
  256.   * @param  file: pointer to the source file name
  257.   * @param  line: assert_param error line source number
  258.   * @retval None
  259.   */
  260. void assert_failed(uint8_t *file, uint32_t line)
  261. {
  262.   /* USER CODE BEGIN 6 */
  263.   /* User can add his own implementation to report the file name and line number,
  264.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  265.   /* USER CODE END 6 */
  266. }
  267. #endif /* USE_FULL_ASSERT */

复制代码

全部资料51hei下载地址:
ADC.7z (354.63 KB, 下载次数: 163)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:883177 发表于 2021-2-7 11:08 | 显示全部楼层
下载下来,怎么编译不过,有两个报错
回复

使用道具 举报

ID:883177 发表于 2021-2-7 11:15 | 显示全部楼层
很好的例程,直接使用你的HEX是可以仿真的,但是我的KEIL编不过有两个错误,不知道哪里问题
回复

使用道具 举报

ID:88756 发表于 2021-2-19 22:36 | 显示全部楼层
谢谢咯!继续努力
回复

使用道具 举报

ID:440028 发表于 2021-4-19 21:38 | 显示全部楼层
这是用HAL库开发的程序
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表