本帖最后由 年轻人叫小马 于 2016-6-14 23:34 编辑
030 ADC 采用单次模式,搞了一天了不知道什么问题。各位帮忙看看
通道1采出来偏小0.5v左右,通道0 也有偏小。0和1单独采正确
#include "adc.h"
void ADC1_Config(void)
{
ADC_InitTypeDef ADC_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//使能GPIOA时钟
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//使能ADC1时钟
RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2) ;
/* Configure PA.0 as analog input */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStruct); //设置PA0为模拟输入,PA0对应的是通道0 ADC总共19个通道,16个外部通道,一个温度,一个电压,还有一个自己内部的Vbat通道;
/* ADC1 DeInit */
ADC_DeInit(ADC1);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStruct);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
// ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; //12位的分辨率
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;//ENABLE; //循环采样,意思就是在整个程序可以被进行多次转换,单次转换的话整个程序生命周期只能被触发一次
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//软件触发
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;// 右对齐
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward ;//ADC_ScanDirection_Backward;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_55_5Cycles);// 设置采样通道,一定要把通道号和引脚的串口号对应起来
ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_55_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1); //adc校准
ADC_Cmd(ADC1, ENABLE); // 使能adc
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular software Start Conv */
ADC_StartOfConversion(ADC1); // 开始ADC转换
}
//ADCData[Num]=ADC_GetConversionValue(ADC1); // 在实际程序中可以用这个函数进行采样
uint16_t Adc_Switch(uint32_t ADC_Channel)
{
// ADC1->CHSELR = 0 ;
// ADC1->CHSELR = (uint32_t)ADC_Channel; //????
// /* ADC1 regular Software Start Conv */
// ADC_StartOfConversion(ADC1); //????
// while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; //????
// ADCx->CHSELR = (uint32_t)ADC_Channel;
ADC_ChannelConfig(ADC1, ADC_Channel, ADC_SampleTime_55_5Cycles);// 设置采样通道,一定要把通道号和引脚的串口号对应起来
/* ADC Calibration */
// ADC_GetCalibrationFactor(ADC1); //adc校准
ADC_Cmd(ADC1, ENABLE); // 使能adc
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular software Start Conv */
ADC_StartOfConversion(ADC1); // 开始ADC转换
//while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ;
return ADC_GetConversionValue(ADC1) ;
}
unsigned int GetADC_Channel_0(void)
{
unsigned int DataValue;
// uint8_t i;
// uint32_t sum;
// for(i=0;i<100;i++)
// {
DataValue=Adc_Switch(ADC_Channel_0);
// sum+=DataValue;
// }
// DataValue=sum/100;
return DataValue;
}
unsigned int GetADC_Channel_1(void)
{
unsigned int DataValue;
// uint8_t i;
// uint32_t sum;
// for(i=0;i<100;i++)
// {
DataValue=Adc_Switch(ADC_Channel_1);
// sum+=DataValue;
// }
// DataValue=sum/100;
return DataValue;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC_Mode_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE);
ADC_DeInit(ADC1);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_ScanDirection=ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection=ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
// ADC1->CFGR1 |= (uint32_t)ADC_CFGR1_OVRMOD;
ADC_ChannelConfig(ADC1, ADC_Channel_2, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
ADC_StartOfConversion(ADC1);
}
void ADC_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADCresults;
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize=AD_Count*2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ClearITPendingBit(DMA1_IT_TC1);
DMA_ITConfig(DMA1_Channel1, DMA1_IT_TC1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_Init(&NVIC_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC_on(void)
{
ADC_GPIO_Init();
ADC_DMA_Init();
ADC_Mode_Init();
}
{
u8 i;
u32 AD_Data1=0;
u32 AD_Data2=0;
for(i=2;i<AD_Count;i++)
{
AD_Data1 += ADCresults[i][0];
AD_Data2 += ADCresults[i][1];
}
AD_Data1 = AD_Data1 / (AD_Count-2);
AD_Data2 = AD_Data2 / (AD_Count-2);
adc_top=(u8)(AD_Data1>>4); //pa2
adc_bot=(u8)(AD_Data2>>4); //pa3
}
void DMA1_Channel1_IRQHandler()
{
if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)
flag_ADCend=1;
DMA_ClearITPendingBit(DMA1_IT_TC1);
}
我试试
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab;
这里应该不需要&吧
一周热门 更多>