刚接触stm32,有许多不懂?
最近在弄stm32f429-discovery,adc单通道多路采集(非DMA),
我设置了8路PA0-PA7;通过USART1反馈回电压值;
在外面连了一个滑动变阻器,连得是stm32f429-discovery上的3V,万用表测得电压2.97V;
滑动 滑动变阻器,测试了PA0,PA1,PA5;
PA0数值从0.016左右到0.6左右变化
PA1数值从0.016左右到2.6左右变化,当滑动变阻器接近最大阻值时,数值突然跳到44左右;
PA5数值从0.016左右到55左右变化;
求解释呀?
adc配置函数:
[mw_shl_code=c,true]#include "adc.h"
#include "delay.h"
#include "stm32_tpdata.h"
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_480Cycles );
ADC_SoftwareStartConv(ADC1);
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<times;t++)
{
temp_val+=Get_Adc(ch); delay_ms(5);
}
return temp_val/times;
}
[/mw_shl_code]
主函数:
[mw_shl_code=c,true]#include "stm32f4xx.h"
#include "spi_config.h"
#include "usart_config.h"
#include "delay.h"
#include "adc.h"
#include <stdio.h>
int main(void)
{
int y=0;
int qq;
u16 adcx = 0;
float temp;
char buffer[7];
USART_Config();
Adc_Init();
USART_SendData(USART1,0xa0);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
while(1)
{
adcx=Get_Adc_Average(ADC_Channel_1,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_2,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_3,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_4,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_5,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_6,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_7,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
adcx=Get_Adc_Average(ADC_Channel_8,10);
temp=(float)adcx*(3.3/4096);
sprintf(buffer,"%f",temp);
for(y=0;y<7;y++)
{
USART_SendData(USART1,buffer[y]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_SendData(USART1,',');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,'
');
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}[/mw_shl_code]
一周热门 更多>