STM32F0 LL库 ADC读取与串口发送上位机实时显示

本文详细描述了如何在嵌入式系统中初始化ADC,包括GPIO配置、设置通道、时钟配置、校准流程以及数据读取并通过USART1发送的过程。
摘要由CSDN通过智能技术生成

ADC LL 初始化

void MX_ADC_Init(void)
{
  LL_ADC_InitTypeDef ADC_InitStruct = {0};
  LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* Peripheral clock enable */
  LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);

  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
  /**ADC GPIO Configuration
  PA0   ------> ADC_IN0
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /** Configure Regular Channel
  */
  LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_0);//
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  ADC_InitStruct.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;//
//  ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
//  ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
 // ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE;
  LL_ADC_Init(ADC1, &ADC_InitStruct);
  ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;//
 // ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;//
 // ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_LIMITED;
  ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;//
  LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
 // LL_ADC_REG_SetSequencerScanDirection(ADC1, LL_ADC_REG_SEQ_SCAN_DIR_FORWARD);
  LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_239CYCLES_5);//

}

先校准ADC,后打开ADC,顺序不能错

  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_ADC_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  
  
  LL_ADC_StartCalibration(ADC1);  				//开始校准
  while (LL_ADC_IsCalibrationOnGoing(ADC1)){};   //等待校准完成
  LL_ADC_Enable(ADC1);
  while (LL_ADC_IsActiveFlag_ADRDY(ADC1)==0){};
    
  while (1)
  {
	  
    Send();
	  
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

读取ADC数据,串口发送到山外多功能调试助手

void UART1Send(char byData)
{
	LL_USART_TransmitData8(USART1, byData);
	while(!LL_USART_IsActiveFlag_TC(USART1)){};
}

uint16_t GetADC1_0(void)
{
	LL_ADC_REG_StartConversion(ADC1);
	while(LL_ADC_IsActiveFlag_EOC(ADC1)==0){};
  LL_ADC_ClearFlag_EOC(ADC1);
	return LL_ADC_REG_ReadConversionData12(ADC1);
}

void Send(void)
{
	uint32_t dwTimer=0;
	uint16_t wADC=GetADC1_0();
	
	UART1Send(0x03);
	UART1Send(0xfc);
	UART1Send(wADC&0xff);
	UART1Send((wADC>>8)&0xff);
	UART1Send(0xfc);
	UART1Send(0x03);
	
	while(dwTimer<10000)
	{
	  dwTimer++;
	}
	
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值