STM32CubeMX--STM32L0系列ADC读取电压+LPUART串口通讯

因课题需求 所以选择了STM公司这款低功耗STM32L0系列芯片,之前买了一块学习板自己学习了一下,后来用自己项目的板子进行了调试,会持续更新的~~
刚开始是自己看官网的例程,学习库知识,后来知道了STM32CubeMX芯片配置工具,目前发现还挺好用的,基本的配置会自动生成代码,比如时钟,初始化等。

ADC+LPUART串口通讯
程序介绍:给ADC的第四通道一个电压,通过AD转换,输出到串口打印信息“The current AD value = ****** V”
调试过程中出现的问题
因为用的内置AD,调试过程不复杂,基本一次就通了,但是串口调试的问题主要是会出现乱码情况,还有16进制显示全都是00 00 00,关于这些问题大多数都是由于波特率不匹配或者是波特率和晶振频率不匹配等。

1.STM32CubeMX
STM32CubeMX--STM32L0系列ADC读取电压+LPUART串口通讯_第1张图片
STM32CubeMX--STM32L0系列ADC读取电压+LPUART串口通讯_第2张图片
STM32CubeMX--STM32L0系列ADC读取电压+LPUART串口通讯_第3张图片
STM32CubeMX里面时钟图还是比较清晰直观的,比较容易上手,从图中可以看出这款芯片的LPUART是低功耗的,并且用的内部时钟(选择LPUART也是因为项目需求,UART类似),而ADC是内部有专用时钟,用HSI16M时钟工作

2.代码部分

#include "main.h"
#include 

ADC_HandleTypeDef hadc;

UART_HandleTypeDef hlpuart1;

ADC_ChannelConfTypeDef        sConfig;

static uint32_t adc_conv_var;
float temp;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_LPUART1_UART_Init(void);
static void MX_ADC_Init(void);
static void GET_ADC_(void);


int main(void)
{
  
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  
  MX_ADC_Init();
	
	GET_ADC_();
	
	if((HAL_ADC_GetState(&hadc) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC)
	{
		adc_conv_var =HAL_ADC_GetValue(&hadc);
		temp=(float)adc_conv_var*(3.3/4096);	
	}		
  
	MX_LPUART1_UART_Init();
  printf("\r\n The current AD value = %f V \r\n",temp); 
  
  while (1)
  {
   
  }
}
//printf函数重定向
int fputc(int ch, FILE *f)
{
	uint8_t temp1[1]={ch};
	HAL_UART_Transmit(&hlpuart1, temp1, 1,0xFFFF);
	return ch;
}
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** 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_5;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** 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_MSI;
  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_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
  PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
}

/**
  * @brief ADC Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC_Init(void)
{
  ADC_ChannelConfTypeDef sConfig = {0};

  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }
 
  if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) !=  HAL_OK) //启动校验功能
  {
    Error_Handler();
  }
}
static void GET_ADC_(void)
{
	sConfig.Channel      = ADC_CHANNEL_4;               /* Channel to be converted */
  sConfig.Rank         = ADC_RANK_CHANNEL_NUMBER;    // 设置通道
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  
	if (HAL_ADC_Start(&hadc) != HAL_OK)
  {
    Error_Handler();
  }
	HAL_ADC_PollForConversion(&hadc, 10);
}
/**
  * @brief LPUART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_LPUART1_UART_Init(void)
{
  hlpuart1.Instance = LPUART1;
  hlpuart1.Init.BaudRate = 128000;
  hlpuart1.Init.WordLength = UART_WORDLENGTH_7B;
  hlpuart1.Init.StopBits = UART_STOPBITS_1;
  hlpuart1.Init.Parity = UART_PARITY_NONE;
  hlpuart1.Init.Mode = UART_MODE_TX_RX;
  hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin : PB13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF2_MCO;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

STM32CubeMX--STM32L0系列ADC读取电压+LPUART串口通讯_第4张图片
欢迎大家批评指正和交流啊~

你可能感兴趣的:(STM32)