Files
SM-1000M/RTX5_20220316/Driver/adc/adc.c
2026-04-23 10:50:18 +08:00

165 lines
5.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "adc.h"
#include "stm32l1xx_adc.h"
#include "bsp.h"
__IO uint16_t VREFINT_CAL;
__IO uint16_t VREFINT_DATA;
__IO float VDDA_VAL;
extern float ADCADCADFC;
/*
*********************************************************************************************************
* 函 数 名: ADC15_Init()
* 功能说明: ADC初始化
* 输 入 :无
* 输 出 :无
*********************************************************************************************************
*/
void ADC15_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/*----------------- ADC1 configuration with DMA enabled --------------------*/
/* Enable the HSI oscillator */
RCC_HSICmd(ENABLE);
/* Check that HSI oscillator is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* Enable GPIOB clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* Configure PB.14 (ADC Channe20) in analog mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* ADC1 configuration */
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_Resolution=ADC_Resolution_12b;//12位
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE);//内部通道17使能
ADC_Cmd(ADC1, ENABLE);
VREFINT_CAL = *(__IO uint16_t *)(0X1FF800F8);
}
/*
*********************************************************************************************************
* 函 数 名: Get_Adc()
* 功能说明: 获取ADC的采样值
* 输 入 通道ch
* 输 出 ADC采样值
*********************************************************************************************************
*/
u16 Get_Adc(u8 ch)
{
PWR_CTRLADC_H;//打开ADC电源
//设置指定ADC的规则组通道一个序列采样时间
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_384Cycles ); //ADC1,ADC通道
ADC_SoftwareStartConv(ADC1); //使能指定的ADC1的软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果
}
/*
*********************************************************************************************************
* 函 数 名: Get_Adc_Ref()
* 功能说明: 获取ADC的采样值采样值加入内部参考电压
* 输 入 通道ch
* 输 出 ADC采样值
*********************************************************************************************************
*/
float Get_Adc_Ref(u8 ch)
{
float ADC_V;
u16 adc_ch;
PWR_CTRLADC_H;//打开ADC电源
// Get_Adc(ch);
VREFINT_DATA=Get_Adc(17);
adc_ch=Get_Adc(ch);
VDDA_VAL=(float)3*VREFINT_CAL/VREFINT_DATA;//获取VDDA电压
ADC_V=((float)adc_ch/4095)*VDDA_VAL;
return ADC_V;
}
/*
*********************************************************************************************************
* 函 数 名: Get_Adc_Average()
* 功能说明: 获取ADC的采样平均值
* 输 入 通道ch 采用次数 times
* 输 出 ADC采样均值
*********************************************************************************************************
*/
float Get_Adc_Average(u8 ch,u8 times)
{
float temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
//temp_val+=Get_Adc_Ref(ch);
temp_val+=Get_Adc(ch);
delay_ms(10);
}
temp_val=((temp_val/times)/4095)*3.3;
return temp_val;
//return ((temp_val/times));
}
/*
*********************************************************************************************************
* 函 数 名: ADC15_POWERON()
* 功能说明: 打开ADC采集电压通道
* 输 入 :无
* 输 出 :无
*********************************************************************************************************
*/
void ADC15_POWERON(void)
{
PWR_CTRLADC_H;
};
/*
*********************************************************************************************************
* 函 数 名: ADC15_POWEROFF()
* 功能说明: 关闭ADC采集电压通道
* 输 入 :无
* 输 出 :无
*********************************************************************************************************
*/
void ADC15_POWEROFF(void)
{
PWR_CTRLADC_L;
};
/*
*********************************************************************************************************
* 函 数 名: ADC15_Process()
* 功能说明: ADC采集流程
* 输 入 :无
* 输 出 :无
*********************************************************************************************************
*/
void ADC15_Process(void)
{
float ADC_VALUVE;
ADC_VALUVE=Get_Adc_Average(15,10);
delay_ms(500);
ADC_VALUVE=Get_Adc_Average(15,10);
ADC_VALUVE*=(float)4.73774;
//ADC_VALUVE += 0.8;
ADCADCADFC = ADC_VALUVE;
sprintf(data_common1.ADC_DATA,"%.3f,",ADC_VALUVE);
}
/***************************** END OF FILE *********************************/