[mw_shl_code=c,true] #include "adc.h"
#include "delay.h"
int normal;
float inpid=0.0;
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1,ENABLE ); //????ADC1?¨???±??
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //?è??ADC·????ò×?6 72M/6=12,ADC×??ó?±??????????14M
//PA1 ×÷???????¨??????????
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //????????????
GPIO_Init(GPIOC, &GPIO_InitStructure);
ADC_DeInit(ADC1); //????ADC1,?????è ADC1 ???????????÷???è???±????
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC?¤×÷????:ADC1??ADC2?¤×÷??????????
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //????×????¤×÷?????¨??????
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //????×????¤×÷??????×???????
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //×??????í??????????????·?????
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC??????????
ADC_InitStructure.ADC_NbrOfChannel = 1; //???ò???????ò×?????ADC?¨????????
ADC_Init(ADC1, &ADC_InitStructure); //?ù??ADC_InitStruct?????¨???????????????èADCx???????÷
ADC_Cmd(ADC1, ENABLE); //???????¨??ADC1
ADC_ResetCalibration(ADC1); //??????????×?
while(ADC_GetResetCalibrationStatus(ADC1)); //??????????×??á??
ADC_StartCalibration(ADC1); //????AD??×?
while(ADC_GetCalibrationStatus(ADC1)); //??????×??á??
//ADC_SoftwareStartConvCmd(ADC1, ENABLE); //???????¨??ADC1???í??×???????????
}
//????ADC??
//ch:?¨???? 0~3
u16 Get_Adc(u8 ch)
{
//?è?????¨ADC?????ò×é?¨?????????ò???????ù?±??
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC?¨??,???ù?±????239.5????
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //???????¨??ADC1???í??×???????????
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//????×????á??
return ADC_GetConversionValue(ADC1); //·???×??ü????ADC1???ò×é??×????á??
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
void adc_pid(void)
{
int i;
u8 temp1[6]={35,75,115,135,175,215};
//u8 temp1[5]={35,75,115,175,215};
//u8 temp2[6]={0,50,100,150,200,250};
u16 s[6]={0};
float temp,sum=0.0,adc[6];
temp=Get_Adc_Average(ADC_Channel_10,10);//???®?????ù
adc[0]=(float)temp*(3.3/4096);
printf("\nADC[0]:%f\n",adc[0]);
temp=Get_Adc_Average(ADC_Channel_11,10);
adc[1]=(float)temp*(3.3/4096);
//adc[1]=temp;
printf("\nADC[1]:%f\n",adc[1]);
temp=Get_Adc_Average(ADC_Channel_12,10);
adc[2]=(float)temp*(3.3/4096);
//adc[2]=temp;
printf("\nADC[2]:%f\n",adc[2]);
temp=Get_Adc_Average(ADC_Channel_13,10);
adc[3]=(float)temp*(3.3/4096);
printf("\nADC[3]:%f\n",adc[3]);
temp=Get_Adc_Average(ADC_Channel_14,10);
adc[4]=(float)temp*(3.3/4096);
printf("\nADC[4]:%f\n",adc[4]);
temp=Get_Adc_Average(ADC_Channel_15,10);
adc[5]=(float)temp*(3.3/4096);
printf("\nADC[5]:%f\n",adc[5]);
}
int main(void)
{
// lMotor_Speed=250;
// rMotor_Speed=250;
// //TIM2_PWM_Init();
NVIC_Configuration();
delay_init();
uart_init(115200);
Adc_Init();
// OLED_Init();
while(1)
{
adc_pid(); }[/mw_shl_code]
[mw_shl_code=c,true]仿照原子哥的例程写了一段AD取值 串口返回
但是我用电压表测引脚电压是0 烧进去mini板的例程 例程很正常 那我这是哪儿错了?[/mw_shl_code]