本程序采用STM32F103C8T6的3通道adc采集,其中为PA1(ADC1,ch,1,),PA2(ADC1,ch,2,),PA3(ADC1,ch,1,)
adc.c文件
#include "adc.h"
#include "delay.h"
extern int adcr,adcg,adcb;
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_NbrOfChannel=1;
ADC_InitStructure.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1,ch,1,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ch,2,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ch,3,ADC_SampleTime_1Cycles5);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
int get_adc(u8 dex)
{
int over;
while(1)
{
switch(dex)
{
case 0x01:
over = adcr;
break;
case 0x02:
over = adcg;
break;
case 0x03:
over = adcb;
break;
}
return over;
}
}
adc.h文件
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
void Adc_Init(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch,u8 times);
int get_adc(u8 dex);
#endif
main.c文件
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "adc.h"
#include "led.h"
int main(void)
{
u8 t=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
Adc_Init();
LED_Init();
while(1)
{
if(t%10==0)
{
adcr=Get_Adc_Average(ADC_Channel_1,10);
adcg=Get_Adc_Average(ADC_Channel_2,10);
adcb=Get_Adc_Average(ADC_Channel_3,10);
printf("adc1:%d adc2:%d adc3:%dn",adcr,adcg,adcb);
}
delay_ms(10);
t++;
if(t==20)
{
t=0;
}
}
}
本程序采用STM32F103C8T6的3通道adc采集,其中为PA1(ADC1,ch,1,),PA2(ADC1,ch,2,),PA3(ADC1,ch,1,)
adc.c文件
#include "adc.h"
#include "delay.h"
extern int adcr,adcg,adcb;
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_NbrOfChannel=1;
ADC_InitStructure.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1,ch,1,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ch,2,ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1,ch,3,ADC_SampleTime_1Cycles5);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
int get_adc(u8 dex)
{
int over;
while(1)
{
switch(dex)
{
case 0x01:
over = adcr;
break;
case 0x02:
over = adcg;
break;
case 0x03:
over = adcb;
break;
}
return over;
}
}
adc.h文件
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
void Adc_Init(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch,u8 times);
int get_adc(u8 dex);
#endif
main.c文件
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "adc.h"
#include "led.h"
int main(void)
{
u8 t=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
Adc_Init();
LED_Init();
while(1)
{
if(t%10==0)
{
adcr=Get_Adc_Average(ADC_Channel_1,10);
adcg=Get_Adc_Average(ADC_Channel_2,10);
adcb=Get_Adc_Average(ADC_Channel_3,10);
printf("adc1:%d adc2:%d adc3:%dn",adcr,adcg,adcb);
}
delay_ms(10);
t++;
if(t==20)
{
t=0;
}
}
}
举报