|
采集PA0-PA5上的模拟值,各端口均无电压时,就显示除了PA0,1-5均有电压,能正常测量PA0电压
程序如下:
- #include "adc/ADC2.h"
- #include <stdio.h>
- /*定义数组变量ADC_ConvertedValue[2],
- __IO uint16_t ADC_ConvertedValue[7];
- void ADC1_conf(void)
- {
- ADC1_GPIO_Config();
- ADC1_Mode_Config();
-
- }
- static void ADC1_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable DMA clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- /* Enable ADC1 and GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA,ENABLE);
-
- 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(GPIOA, &GPIO_InitStructure);
- }
- /* 函数名:ADC1_Mode_Config
- * 描述 :配置ADC1的工作模式为MDA模式
- * 输入 : 无
- * 输出 :无
- * 调用 :内部调用
- */
- static void ADC1_Mode_Config(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- /* DMA channel1 configuration */
- DMA_DeInit(DMA1_Channel1);
- /*定义DMA外设基地址,即为存放转换结果的寄存器*/
- //DMA_InitStructure.DMA_PeripheralBaseAddr =ADC1_DR_Address; /*定义内存基地址*/
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA外设ADC基地址
- DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue;
- /*定义AD外设作为数据传输的来源*/
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
-
- DMA_InitStructure.DMA_BufferSize = 6;
-
- /*设定寄存器地址固定*/
- 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工作再循环缓存模式*/
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
- /*设定DMA选定的通道软件优先级*/
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
- /* Enable DMA channel1 */
- DMA_Cmd(DMA1_Channel1, ENABLE);
- /*设置ADC工作在独立模式*/
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- /*规定AD转换工作在扫描模式,即对多个通道采样*/
- ADC_InitStructure.ADC_ScanConvMode = ENABLE ;
- /*设定AD转化在连续模式*/
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- /*不使用外部促发转换*/
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- /*采集的数据在寄存器中以右对齐的方式存放*/
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- /*设定要转换的AD通道数目*/
- ADC_InitStructure.ADC_NbrOfChannel = 6;
- ADC_Init(ADC1, &ADC_InitStructure);//根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
- /*配置ADC时钟,为PCLK2的8分频,即9MHz*/
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- /*配置ADC1的通道11为55.5个采样周期 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_4 ,5, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_55Cycles5);
- /* Enable ADC1 DMA */
- ADC_DMACmd(ADC1, ENABLE);
- /* Enable ADC1 */
- ADC_Cmd(ADC1, ENABLE);
- /*复位校准寄存器 */
- ADC_ResetCalibration(ADC1);
- /*等待校准寄存器复位完成 */
- while(ADC_GetResetCalibrationStatus(ADC1));
- /* ADC校准 */
- ADC_StartCalibration(ADC1);
- /* 等待校准完成*/
- while(ADC_GetCalibrationStatus(ADC1));
- /* 由于没有采用外部触发,所以使用软件触发ADC转换 */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- u16 GetVdy(u16 advalue)
- {
- //return (u16)( ADCConvertedValue * 330 / 4096*1000);
- return (u16)(ADC_ConvertedValue[advalue]);
- }
- void G_shuchu(void)
- {
- printf(" ADC_ConvertedValue [0]:%d\r\n", ADC_ConvertedValue[0]);
- printf(" ADC_ConvertedValue [1]:%d\r\n", ADC_ConvertedValue[1]);
- printf(" ADC_ConvertedValue [2]:%d\r\n", ADC_ConvertedValue[2]);
- printf(" ADC_ConvertedValue [3]:%d\r\n", ADC_ConvertedValue[3]);
- printf(" ADC_ConvertedValue [4]:%d\r\n", ADC_ConvertedValue[4]);
- printf(" ADC_ConvertedValue [5]:%d\r\n", ADC_ConvertedValue[5]);
-
- printf(" ADC_GetConversionValue:%d\r\n", ADC_GetConversionValue(ADC1));
- //printf(" ADC_ConvertedValue [3]:%d\r\n", ADC_ConvertedValue[3]);
- }
复制代码
|
|