#include "adc.h"
#include "lcd.h"
#include "delay.h"
#include "sys.h"
__IO uint16_t adc_value_final[ADC_CH_NUM] ={0};
__IO uint16_t adc_value[ADC_CH_NUM * ADC_CONV_COUNT] = {0};
/*多通道独立DMA采集模式*/
void ADC1_CH10to15_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1, DMA2 and GPIO clocks ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2 Stream0 channel0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t)&ADC1->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_value;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADC_CONV_COUNT*ADC_CH_NUM;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
/* Configure ADC1 Channel10to15 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion =ADC_CH_NUM;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel10to15 configuration *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_480Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_480Cycles);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
}
/*
进行一次AD数据采集
*/
void ADC_Sampling(void)
{
u8 i,j;
uint16_t sum;
ADC_SoftwareStartConv(ADC1);/* Start ADC Software Conversion */
while(RESET == DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0));
for(i=0; i<ADC_CH_NUM; i++)
{
sum=0;
for(j=0; j<ADC_CONV_COUNT; j++)
sum+=adc_value[j*ADC_CONV_COUNT+i];
adc_value_final
=sum/ADC_CONV_COUNT;
}
}
/****
获取指定通道的值
*/
int ADC_GetChannelValue(u8 ch)
{
if(ch<=0 || ch >ADC_CH_NUM) return -1;
else
return adc_value_final[ch-1];
}/*ADC值显示*/
void Display_ADC1Voltage(void)
{
u16 adcx;
float temp;
u16 i;
for(i=0; i<ADC_CH_NUM; i++)
{
adcx=adc_value_final;
switch(i)
{
case 0:
LCD_ShowxNum(134,130,adcx,4,16,0); //显示ADCC采样后的原始值
temp=(float)adcx*(3.3/4096); //获取计算后的带小数的实际电压值,比如3.1111
adcx=temp; //赋值整数部分给adcx变量,因为adcx为u16整形
LCD_ShowxNum(134,150,adcx,1,16,0); //显示电压值的整数部分,3.1111的话,这里就是显示3
temp-=adcx; //把已经显示的整数部分去掉,留下小数部分,比如3.1111-3=0.1111
temp*=1000; //小数部分乘以1000,例如:0.1111就转换为111.1,相当于保留三位小数。
LCD_ShowxNum(150,150,temp,3,16,0X80); //显示小数部分(前面转换为了整形显示),这里显示的就是111.
break;
case 1:
LCD_ShowxNum(134,170,adcx,4,16,0); //显示ADCC采样后的原始值
temp=(float)adcx*(3.3/4096); //获取计算后的带小数的实际电压值,比如3.1111
adcx=temp; //赋值整数部分给adcx变量,因为adcx为u16整形
LCD_ShowxNum(134,190,adcx,1,16,0); //显示电压值的整数部分,3.1111的话,这里就是显示3
temp-=adcx; //把已经显示的整数部分去掉,留下小数部分,比如3.1111-3=0.1111
temp*=1000; //小数部分乘以1000,例如:0.1111就转换为111.1,相当于保留三位小数。
LCD_ShowxNum(150,190,temp,3,16,0X80); //显示小数部分(前面转换为了整形显示),这里显示的就是111.
break;
case 2:
LCD_ShowxNum(134,210,adcx,4,16,0); //显示ADCC采样后的原始值
temp=(float)adcx*(3.3/4096); //获取计算后的带小数的实际电压值,比如3.1111
adcx=temp; //赋值整数部分给adcx变量,因为adcx为u16整形
LCD_ShowxNum(134,230,adcx,1,16,0); //显示电压值的整数部分,3.1111的话,这里就是显示3
temp-=adcx; //把已经显示的整数部分去掉,留下小数部分,比如3.1111-3=0.1111
temp*=1000; //小数部分乘以1000,例如:0.1111就转换为111.1,相当于保留三位小数。
LCD_ShowxNum(150,230,temp,3,16,0X80); //显示小数部分(前面转换为了整形显示),这里显示的就是111.
break;
case 3:
LCD_ShowxNum(134,250,adcx,4,16,0); //显示ADCC采样后的原始值
temp=(float)adcx*(3.3/4096); //获取计算后的带小数的实际电压值,比如3.1111
adcx=temp; //赋值整数部分给adcx变量,因为adcx为u16整形
LCD_ShowxNum(134,270,adcx,1,16,0); //显示电压值的整数部分,3.1111的话,这里就是显示3
temp-=adcx; //把已经显示的整数部分去掉,留下小数部分,比如3.1111-3=0.1111
temp*=1000; //小数部分乘以1000,例如:0.1111就转换为111.1,相当于保留三位小数。
LCD_ShowxNum(150,270,temp,3,16,0X80); //显示小数部分(前面转换为了整形显示),这里显示的就是111.
break;
default:
break;
}
}
}
一周热门 更多>