20260324
This commit is contained in:
121
IAPV1.1/Driver/adc/adc.c
Normal file
121
IAPV1.1/Driver/adc/adc.c
Normal file
@ -0,0 +1,121 @@
|
||||
#include "adc.h"
|
||||
#include "stm32l1xx_adc.h"
|
||||
#include "bsp.h"
|
||||
//PB0-ADC8,PB1-ADC9
|
||||
__IO uint16_t VREFINT_CAL;
|
||||
__IO uint16_t VREFINT_DATA;
|
||||
__IO float VDDA_VAL;
|
||||
|
||||
|
||||
void ADC15_Init(void)
|
||||
{
|
||||
ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
ADC_InitTypeDef ADC_InitStructure;
|
||||
/*----------------- ADC1 configuration with DMA enabled --------------------*/
|
||||
/* Enable the HSI oscillator */
|
||||
RCC_HSICmd(ENABLE);
|
||||
|
||||
ADC_CommonInitStructure.ADC_Prescaler=ADC_Prescaler_Div4;
|
||||
ADC_CommonInit(&ADC_CommonInitStructure);
|
||||
|
||||
|
||||
/* 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);
|
||||
|
||||
/* Check that HSI oscillator is ready */
|
||||
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 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);//<2F>ڲ<EFBFBD>ͨ<EFBFBD><CDA8>17ʹ<37><CAB9>
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE);
|
||||
VREFINT_CAL = *(__IO uint16_t *)(0X1FF800F8);
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>ADCֵ
|
||||
//ch:ͨ<><CDA8>ֵ 0~3
|
||||
u16 Get_Adc(u8 ch)
|
||||
{
|
||||
PWR_CTRLADC_H;//<2F><><EFBFBD><EFBFBD>ADC<44><43>Դ
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ADC<44>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
||||
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_384Cycles ); //ADC1,ADCͨ<43><CDA8>
|
||||
|
||||
ADC_SoftwareStartConv(ADC1); //ʹ<><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ADC1<43><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//<2F>ȴ<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
return ADC_GetConversionValue(ADC1); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ADC1<43><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD>ο<EFBFBD><CEBF><EFBFBD>ѹ<EFBFBD><D1B9>ADCֵ
|
||||
float Get_Adc_Ref(u8 ch)
|
||||
{
|
||||
float ADC_V;
|
||||
u16 adc_ch;
|
||||
PWR_CTRLADC_H;//<2F><><EFBFBD><EFBFBD>ADC<44><43>Դ
|
||||
// Get_Adc(ch);
|
||||
VREFINT_DATA=Get_Adc(17);
|
||||
adc_ch=Get_Adc(ch);
|
||||
VDDA_VAL=(float)3*VREFINT_CAL/VREFINT_DATA;//<2F><>ȡVDDA<44><41>ѹ
|
||||
ADC_V=((float)adc_ch/4095)*VDDA_VAL;
|
||||
return ADC_V;
|
||||
}
|
||||
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.35;
|
||||
return temp_val;
|
||||
//return ((temp_val/times));
|
||||
}
|
||||
|
||||
void ADC15_POWERON(void)
|
||||
{
|
||||
PWR_CTRLADC_H;
|
||||
};
|
||||
void ADC15_POWEROFF(void)
|
||||
{
|
||||
PWR_CTRLADC_L;
|
||||
};
|
||||
|
||||
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.2;
|
||||
sprintf(data_common1.ADC_DATA,"%.3f,",ADC_VALUVE);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
37
IAPV1.1/Driver/adc/adc.h
Normal file
37
IAPV1.1/Driver/adc/adc.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef __ADC_H
|
||||
#define __ADC_H
|
||||
#include "general_type.h"
|
||||
#include "pbdata.h"
|
||||
//#include "stdint.h"
|
||||
//#define ADC_CH1 1 //ͨ<><CDA8>1
|
||||
//#define ADC_CH_TEMP 16 //<2F>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>
|
||||
|
||||
void Adc_Get_Voltage(void);
|
||||
u16 Get_Adc(u8 ch); //<2F><><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ͨ<EFBFBD><CDA8>ֵ
|
||||
float Get_Adc_Average(u8 ch,u8 times);//<2F>õ<EFBFBD>ij<EFBFBD><C4B3>ͨ<EFBFBD><CDA8>10<31>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>ֵ
|
||||
u16 Get_Adc2(u8 ch) ;
|
||||
extern float ADC_VALUVE;
|
||||
extern __IO uint16_t VREFINT_CAL;
|
||||
extern __IO uint16_t VREFINT_DATA;
|
||||
extern __IO float VDDA_VAL;
|
||||
void ADC15_Init(void);
|
||||
float Get_Adc_Ref(u8 ch);
|
||||
void ADC15_POWERON(void);
|
||||
void ADC15_POWEROFF(void);
|
||||
void ADC15_Process(void);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user