u16 Get_Adc(u8 ch) { ADC_RegularChannelConfig(ADC1,ch,1,ADC_SampleTime_239Cycles5); ADC_SoftwareStartConvCmd(ADC1,ENABLE); while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)); return ADC_GetConversionValue(ADC1); }
uint16_t readADC1(uint8_t channel) { ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_7Cycles5); // Start the conversion ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Wait until conversion completion while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // Get the conversion value return ADC_GetConversionValue(ADC1); }
/**************************************************************/ //程 序 名: ADC_Config() //开 发 者: chenhonglin //入口参数: 无 //功能说明: ADC配置 //**************************************************************/ void ADC_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ; // PC2 -> AD_SIG1 PC3-> AD_SIG2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); //ADC1 configuration //select continuous conversion mode ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //We will convert multiple channels ADC_InitStructure.ADC_ScanConvMode = ENABLE; //we will convert one time ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;// //select no external triggering ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //right 12-bit data alignment in ADC data register ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //2 channels conversion ADC_InitStructure.ADC_NbrOfChannel = 2; //load structure values to control and status registers ADC_Init(ADC1, &ADC_InitStructure); //configure each channel ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_41Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 2, ADC_SampleTime_41Cycles5); //Enable ADC1 ADC_Cmd(ADC1, ENABLE); //enable DMA for ADC ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 reset calibration register ADC_ResetCalibration(ADC1); //Check the end of ADC1 reset calibration register while (ADC_GetResetCalibrationStatus(ADC1)); //Start ADC1 calibration ADC_StartCalibration(ADC1); //Check the end of ADC1 calibration while (ADC_GetCalibrationStatus(ADC1)); }
/************************************************************************************** Func: 四线触摸端口配置 Time: 2014-6-18 Ver.: V1.0 Note; **************************************************************************************/ void Touch_ADC1_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; ADC_DeInit(ADC1); //将外设 ADC1 的全部寄存器重设为缺省值 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式 ADC_InitStructure.ADC_ScanConvMode =ENABLE; //模数转换工作在扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在连续转换模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发转换关闭 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐 ADC_InitStructure.ADC_NbrOfChannel =TOUCH_ADC_CHN; //顺序进行规则转换的ADC通道的数目 ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器 ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 2, ADC_SampleTime_239Cycles5); ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //复位指定的ADC1的校准寄存器 while(ADC_GetResetCalibrationStatus(ADC1)); //获取ADC1复位校准寄存器的状态,设置状态则等待 ADC_StartCalibration(ADC1); //开始指定ADC1的校准状态 while(ADC_GetCalibrationStatus(ADC1)); //获取指定ADC1的校准程序,设置状态则等待 }
u16 readADC1(u8 channel) { /* Start the conversion */ ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_1Cycles5); /* Wait until conversion completion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* Get the conversion value */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); return ADC_GetConversionValue(ADC1); }
void ad_init() { RCC_PeriphClockCmd(RCC_ADC1, ENABLE); RCC_PeriphClockCmd(RCC_GPIOC, ENABLE); init_GPIO(GPIOC, GPIO_Pin_0, GPIO_Mode_AN, GPIO_Fast_Speed, GPIO_OType_PP, GPIO_PuPd_NOPULL); initCommon_ADC(ADC_Mode_Independent, ADC_Prescaler_Div2, ADC_DMAAccessMode_Disabled, ADC_TwoSamplingDelay_5Cycles); init_ADC(ADC_Resolution_12b, DISABLE, ENABLE, ADC_ExternalTrigConvEdge_None, ADC_ExternalTrigConv_T1_CC1, ADC_DataAlign_Right, 1); ADC_Cmd(ADC1,ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_144Cycles); }
void Get_Adc(uint8_t ch) { //清空最近一次ADC转换的结果 ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5); ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束 g_usAdcValue = ADC_GetConversionValue(ADC1); sum_Adc=sum_Adc+g_usAdcValue; g_usAdcValue=0; }
static uint16_t adc( void ) { ADC_RegularChannelConfig( ADC1, ADC_CHANNEL, 1, ADC_SampleTime_239Cycles5 ); ADC_SoftwareStartConvCmd( ADC1, ENABLE ); while ( ADC_GetFlagStatus( ADC1, ADC_FLAG_EOC ) != SET ) ; uint16_t res; res = ADC_GetConversionValue( ADC1 ); return res; }
void ADC1_MODE_Config(void) { DMA_InitTypeDef DMA_InitStructure; //定义一个DMA结构体变量 ADC_InitTypeDef ADC_InitStructure; //定义一个ADC结构体变量 DMA_DeInit(DMA1_Channel1); //开启DMA1的第一通道 DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&ADC_ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA的转换模式为SRC模式,由外设搬移到内存 DMA_InitStructure.DMA_BufferSize = 1; //DMA缓存大小,1个 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //接收一次数据后,设备地址禁止后移 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //关闭接收一次数据后,目标内存地址后移 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //定义外设数据宽度为16位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMA搬移数据尺寸,HalfWord就是为16位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //转换模式,循环缓存模式。 DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级高 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //M2M模式禁用 DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); ADC_TempSensorVrefintCmd(ENABLE); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立的转换模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
static void ADC1_Mode_Config(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* DMA channel1 configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; 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_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, DISABLE); /* ADC1 configuration */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式 每个ADC独立工作 ADC_InitStructure.ADC_ScanConvMode = ENABLE; //使用扫描模式 scan位设置 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // cont位设置 连续转换模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ;//EXTSEL 选择启动规则通道组转换的外部事件 设置成有软件控制 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据对齐 由软件置位和清楚 这里设置成右对齐 ADC_InitStructure.ADC_NbrOfChannel = 1; //规则通道序列长度 这些位由软件定义在规则通道转换序列中的通道数目 1个转换 指定由多少个通道被转换 ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel11 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_55Cycles5); //转换时间是55.5个周期 /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
u16 Get_Adc(u8 ch) { //éè?????¨ADCμ?1??ò×éí¨μà£?ò???DòáD£?2é?ùê±?? ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADCí¨μà,2é?ùê±???a239.5?ü?ú ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ê1?ü???¨μ?ADC1μ?èí?t×a?????ˉ1|?ü while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//μè′y×a???áê? return ADC_GetConversionValue(ADC1); //·μ??×??üò?′?ADC11??ò×éμ?×a???á1? }
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规则组的转换结果 }
int main(void) { static unsigned int led_state = 0; RCC_ClocksTypeDef clockinfo; RCC_GetClocksFreq(&clockinfo); // regardless of clock speed this gives us 1000 ticks per second SysTick_Config(clockinfo.SYSCLK_Frequency / 1000); int blink_speed_ms = 400; setup_gpios(); setup_adc(); setup_usart(); setup_button_irqs(); kkputs("hello karl...\n"); uint64_t lasttime = millis(); while (1) { if (millis() - blink_speed_ms > lasttime) { if (led_state & 1) { switch_leds_on(); kkputs("O"); } else { switch_leds_off(); kkputs("o"); } led_state ^= 1; GPIO_TOGGLE(GPIOC, GPIO_Pin_3); lasttime = millis(); } if (button_pressed) { button_pressed = 0; kkputs("button was pressed!\n"); blink_speed_ms >>= 1; if (blink_speed_ms <= 50) { blink_speed_ms = 1000; } } // start and wait for adc to convert... ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_192Cycles); ADC_SoftwareStartConv(ADC1); while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0) ; uint16_t pot_val = ADC_GetConversionValue(ADC1); if (pot_val > 0x700) { GPIO_HIGH(GPIOA, GPIO_Pin_4); } else { GPIO_LOW(GPIOA, GPIO_Pin_4); } } }
void configureADC_Temp(void) { uint32_t ch_index; /* Enable ADC clock & SYSCFG */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Enable the internal connection of Temperature sensor and with the ADC channels*/ ADC_TempSensorVrefintCmd(ENABLE); /* Wait until ADC + Temp sensor start */ T_StartupTimeDelay = 1024; while (T_StartupTimeDelay--); /* Setup ADC common init struct */ ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; ADC_CommonInit(&ADC_CommonInitStructure); /* Initialise the ADC1 by using its init structure */ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // Set conversion resolution to 12bit ADC_InitStructure.ADC_ScanConvMode = ENABLE; // Enable Scan mode (single conversion for each channel of the group) ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // Disable Continuous conversion ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; // Disable external conversion trigger ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // Set conversion data alignement to right ADC_InitStructure.ADC_NbrOfConversion = ADC_CONV_BUFF_SIZE; // Set conversion data alignement to ADC_CONV_BUFF_SIZE ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular Temperature sensor channel16 and internal reference channel17 configuration */ for (ch_index = 1; ch_index <= MAX_TEMP_CHNL; ch_index++) { ADC_RegularChannelConfig(ADC1, ADC_Channel_16, ch_index, ADC_SampleTime_384Cycles); } ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 17, ADC_SampleTime_384Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 18, ADC_SampleTime_384Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 19, ADC_SampleTime_384Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 20, ADC_SampleTime_384Cycles); }
static void ADC3_Mode_Config() { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_DeInit(ADC1); //将外设 ADC1 的全部寄存器重设为缺省值 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式 ADC_InitStructure.ADC_ScanConvMode =ENABLE; //模数转换工作在扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在连续转换模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发转换关闭 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐 ADC_InitStructure.ADC_NbrOfChannel =ADC_CHN; //顺序进行规则转换的ADC通道的数目 ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5); ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //复位指定的ADC1的校准寄存器 while(ADC_GetResetCalibrationStatus(ADC1)); //获取ADC1复位校准寄存器的状态,设置状态则等待 ADC_StartCalibration(ADC1); //开始指定ADC1的校准状态 while(ADC_GetCalibrationStatus(ADC1)); //获取指定ADC1的校准程序,设置状态则等待 DMA_DeInit(DMA1_Channel1); //将DMA的通道1寄存器重设为缺省值 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA外设ADC基地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCDMA_Value[0][0]; //DMA内存基地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //内存作为数据传输的目的地 DMA_InitStructure.DMA_BufferSize = ADC_BUFSize*ADC_CHN; //DMA通道的DMA缓存的大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //数据宽度为16位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为16位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循环缓存模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x拥有高优先级 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输 DMA_Init(DMA1_Channel1, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道 ADC_SoftwareStartConvCmd(ADC1, ENABLE); DMA_Cmd(DMA1_Channel1, ENABLE); DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); ADC_DMACmd(ADC1, ENABLE); }
void SENSOR_ADC_Init(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); /* ADC Common configuration *************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_Interl; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 regular channel 12 configuration ************************************/ 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_Init(ADC2, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_3Cycles); ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (multi-ADC mode) ******************/ ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); /* Enable ADC **************************************************************/ ADC_Cmd(ADC1, ENABLE); ADC_Cmd(ADC2, ENABLE); /* Enable DMA2 Stream0 Transfer complete interrupt */ DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); /* Enable ADC1 DMA since ADC1 is the Master*/ ADC_DMACmd(ADC1, ENABLE); }
void ds_therm_init(void) { //Initialization RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOF, &GPIO_InitStructure); //Enable the ADC’s voltage regulator and wait for it to stabilize ADC_VoltageRegulatorCmd(ADC1, ENABLE); ADC_TempSensorCmd(ADC1, ENABLE); ds_delay_uS(10); //Initialize the parameters that are common to all of the A2D Channels ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_CommonInitStructure.ADC_Mode=ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC1, &ADC_CommonInitStructure); //Initialize the parameters specific to channel 10 ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); //Configure the specific ADC, channel, and timing ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_7Cycles5); //Enable the ADC and wait for it to become ready. ADC_Cmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); //Start the first conversion ADC_StartConversion(ADC1); }
void halInternalInitAdc(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; //Configure TEMP_ENABLE GPIO as push-pull output, defaulting to active/on GPIO_WriteBit(TEMP_ENABLE_PORT, TEMP_ENABLE_PIN, Bit_SET); GPIO_InitStructure.GPIO_Pin = TEMP_ENABLE_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(TEMP_ENABLE_PORT, &GPIO_InitStructure); //Configure TEMP_SENSE GPIO as analog input GPIO_InitStructure.GPIO_Pin = TEMP_SENSOR_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(TEMP_SENSOR_PORT, &GPIO_InitStructure); //Divide down the APB2 clock (72MHz) to get an ADC clock of 12MHz (<= 14MHz) RCC_ADCCLKConfig(RCC_PCLK2_Div6); //Configure ADC to be in independent, single conversion mode ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; 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_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(TEMP_SENSOR_ADC, &ADC_InitStructure); //Configure sample times to longest (recommended for temp measure) ADC_RegularChannelConfig(TEMP_SENSOR_ADC, TEMP_SENSOR_ADC_CHAN, 1, ADC_SampleTime_239Cycles5); //Enable ADC ADC_Cmd(TEMP_SENSOR_ADC, ENABLE); //Reset ADC calibration registers ADC_ResetCalibration(TEMP_SENSOR_ADC); //Wait for cal reset to complete while(ADC_GetResetCalibrationStatus(TEMP_SENSOR_ADC) == SET) {} //Calibrate the ADC ADC_StartCalibration(TEMP_SENSOR_ADC); //Wait for cal to complete while(ADC_GetCalibrationStatus(TEMP_SENSOR_ADC) == SET) {} //This driver does not use interrupts; it's purely polling. }
/******************************************************************************* * 函 数 名: * 功 能: * 参 数: * 返 回: *******************************************************************************/ uint16_t ADC1_GetValue(uint8_t channel) { //设置指定ADC的规则组通道,设置它们的转化顺序和采样时间 //ADC1,ADC通道3,规则采样顺序值为1,采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_239Cycles5 ); //使能指定的ADC1的软件转换启动功能 ADC_SoftwareStartConvCmd(ADC1, ENABLE); //等待转换结束 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )); //返回最近一次ADC1规则组的转换结果 return ADC_GetConversionValue(ADC1); }
void ADCManager_Structure(ADCManagerTypeDef *ADCManager) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; u8 i; defaultADCManager=ADCManager; defaultADCManager->getData=ADCManager_getData; defaultADCManager->startTransfer=ADCManager_startTransfer; //defaultADCManager->ADC_Channels_num=sizeof(defaultADCManager->ADC_Channels); RCC_APB2PeriphClockCmd(filterGPIO_RCC_ADC() | RCC_APB2Periph_ADC1 , ENABLE ); //使能ADC1通道时钟 RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M for(i=0;i<defaultADCManager->ADC_Channels_num;i++) { if(defaultADCManager->ADC_Channels[i]==ADC_Channel_16|defaultADCManager->ADC_Channels[i]==ADC_Channel_17) continue; GPIO_InitStructure.GPIO_Pin = filterPin_ADC(defaultADCManager->ADC_Channels[i]); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚 GPIO_Init(filterGPIO_ADC(defaultADCManager->ADC_Channels[i]), &GPIO_InitStructure); } ADC_DeInit(ADC1); //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模数转换工作在单通道模式 ADC_InitStructure.ADC_ContinuousConvMode = (FunctionalState)defaultADCManager->isContinus; //模数转换工作在单次转换模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件而不是外部触发启动 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = defaultADCManager->ADC_Channels_num; //顺序进行规则转换的ADC通道的数目 ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器 for(i=0;i<defaultADCManager->ADC_Channels_num;i++) { if(defaultADCManager->ADC_Channels[i]==ADC_Channel_16|defaultADCManager->ADC_Channels[i]==ADC_Channel_17) ADC_TempSensorVrefintCmd(ENABLE); ADC_RegularChannelConfig(ADC1, defaultADCManager->ADC_Channels[i], i+1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期 } ADC_DMA_Config(); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //使能复位校准 while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束 ADC_StartCalibration(ADC1); //开启AD校准 while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束 }
static void ConfigADC(void) { ADC_InitTypeDef ADC_InitStructure; /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = ADC_CHANNELS; ADC_Init(ADC1, &ADC_InitStructure); RCC_ADCCLKConfig(RCC_PCLK2_Div4); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_TIME); ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_TIME); ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_TIME); ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_TIME); ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_TIME); ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 6, ADC_TIME); }
void AudioFilter::initPeriph() { RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_ADC1, ENABLE); /* GPIO Init */ GPIO_InitTypeDef gpio_init; gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; gpio_init.GPIO_Mode = GPIO_Mode_AIN; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &gpio_init); gpio_init.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; gpio_init.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &gpio_init); /* ADC Init */ ADC_InitTypeDef adcInit; adcInit.ADC_ContinuousConvMode = DISABLE; adcInit.ADC_DataAlign = ADC_DataAlign_Right; adcInit.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; adcInit.ADC_Mode = ADC_Mode_Independent; adcInit.ADC_NbrOfChannel = 2; adcInit.ADC_ScanConvMode = DISABLE; ADC_Init(ADC1, &adcInit); ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_1Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_1Cycles5); ADC_Cmd(ADC1, ENABLE); /* ADC CALIBRATION */ ADC_ResetCalibration(ADC1); while (ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while (ADC_GetCalibrationStatus(ADC1)); ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
static inline uint16_t adc_read(analogin_t *obj) { // Get ADC registers structure address ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc); // Configure ADC channel switch (obj->pin) { case PA_0: ADC_RegularChannelConfig(adc, ADC_Channel_0, 1, ADC_SampleTime_7Cycles5); break; case PA_1: ADC_RegularChannelConfig(adc, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5); break; case PA_4: ADC_RegularChannelConfig(adc, ADC_Channel_4, 1, ADC_SampleTime_7Cycles5); break; case PB_0: ADC_RegularChannelConfig(adc, ADC_Channel_8, 1, ADC_SampleTime_7Cycles5); break; case PC_1: ADC_RegularChannelConfig(adc, ADC_Channel_11, 1, ADC_SampleTime_7Cycles5); break; case PC_0: ADC_RegularChannelConfig(adc, ADC_Channel_10, 1, ADC_SampleTime_7Cycles5); break; default: return 0; } ADC_SoftwareStartConvCmd(adc, ENABLE); // Start conversion while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion return(ADC_GetConversionValue(adc)); // Get conversion value }
//定时器触发的ADC配置 void adc_tim_trig_config(uint32_t period, uint32_t prescaler) { ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InitTypeDef ADC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); gpio_an_no_init(GPIOC, GPIO_Pin_5); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式 ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //两个采样阶段之间的延迟5个时钟 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //ADCCLK=PCLK2/4=84/4=21Mhz ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位模式 ADC_InitStructure.ADC_ScanConvMode = DISABLE; //关闭扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //关闭连续转换 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //外部触发上升沿 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐 ADC_InitStructure.ADC_NbrOfConversion = 1; //规则序列中有1个转换 ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_84Cycles ); //设置通道5采样顺序为1, 采样时间为84个周期 ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); ADC_Cmd(ADC1, ENABLE); nvic_config(ADC_IRQn, 2); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Period = period-1; TIM_TimeBaseStructure.TIM_Prescaler = prescaler-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_Pulse = period/2; TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_Cmd(TIM2, ENABLE); TIM_InternalClockConfig(TIM2); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_UpdateDisableConfig(TIM2, DISABLE); }
/*=====================================================================================================*/ void ADC_Config( void ) { DMA_InitTypeDef DMA_InitStruct; ADC_InitTypeDef ADC_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE); /* ADC_1 PA4 */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStruct); /* ADC DMA Init *************************************************************/ DMA_DeInit(DMA1_Channel1); DMA_InitStruct.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS; // Peripheral address DMA_InitStruct.DMA_MemoryBaseAddr = (u32)&ADC_DMA_Buf; // DMA內存地址 DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; // 內存作為數據傳輸的目的地 DMA_InitStruct.DMA_BufferSize = ADC_Sample*ADC_Channel; // DMA通道的DMA緩存的大小 DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外設地址寄存器不變 DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; // 內存地址寄存器遞增 DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 數據寬度為16位 DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 數據寬度為16位 DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; // 循環模式開啟,Buffer寫滿後,自動回到初始地址開始傳輸 DMA_InitStruct.DMA_Priority = DMA_Priority_High; // DMA通道x擁有高優先級 DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; // DMA通道x沒有設置為內存到內存傳輸 DMA_Init(DMA1_Channel1, &DMA_InitStruct); DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC Init *****************************************************************/ ADC_InitStruct.ADC_Mode = ADC_Mode_Independent; // ADC1 和 ADC2 工作在獨立模式 ADC_InitStruct.ADC_ScanConvMode = ENABLE; // 掃描模式 ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; // 連續轉換模式 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // 外部觸發關閉 ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; // ADC數據右對齊 ADC_InitStruct.ADC_NbrOfChannel = ADC_Channel; // 轉換ADC通道數目 ADC_Init(ADC1, &ADC_InitStruct); /* ADC Regular Config *******************************************************/ // 設置指定ADC的規則組通道,設置它們的轉化順序和采樣時間 // ADC1, ADC通道x, 規則采樣順序值為y, 采樣時間為239.5週期 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_55Cycles5); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
void adc_init( void ) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // enable DMA1 clock RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE ); // enable ADC1 clock RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE ); GPIO_InitStructure.GPIO_Pin = (uint16)(ADC_PIN_INT_BATT | ADC_PIN_EXT_BATT | ADC_PIN_PT100); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init( GPIOC, &GPIO_InitStructure ); adc_dma_config(); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; // ADC_InitStructure.ADC_NbrOfChannel = ADC_IX_MAX; ADC_Init( ADC1, &ADC_InitStructure ); /* ADC1 RegularChannelConfig */ // ADC_RegularChannelConfig( ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5 ); ADC_RegularChannelConfig( ADC1, ADC_Channel_11, 1, ADC_SampleTime_13Cycles5 ); // ADC_RegularChannelConfig( ADC1, ADC_Channel_12, 3, ADC_SampleTime_13Cycles5 ); /* Enable ADC DMA interface */ ADC_DMACmd( ADC1, ENABLE ); /* Enable ADC1 */ ADC_Cmd( ADC1, ENABLE ); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration( ADC1 ); /* Check the end of ADC1 reset calibration register */ while( ADC_GetResetCalibrationStatus( ADC1 ) ); /* Start ADC1 calibration */ ADC_StartCalibration( ADC1 ); /* Check the end of ADC1 calibration */ while( ADC_GetCalibrationStatus( ADC1 ) ); /* Start ADC1 conversion */ ADC_SoftwareStartConvCmd( ADC1, ENABLE ); } // end of adc_init()
uint32_t ADC_Conversion_Data(uint8_t channel_num){ /* ADC1 regular channel select configuration ******************************/ ADC_RegularChannelConfig(ADC1, channel_num, 1, ADC_SampleTime_15Cycles); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Wait for End of Conversion in ADC1*/ //ADC_ClearFlag(ADC1, ADC_FLAG_EOC); // clearing the flag while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)!=SET); // wait return ADC1->DR; }
uint32_t analogRead(uint32_t ulPin) { uint32_t ulValue = 0; uint32_t ulChannel; if (ulPin < A0) ulPin += A0; ulChannel = g_APinDescription[ulPin].ulADCChannelNumber ; if (ulPin >= PINS_COUNT || ulChannel == NONE ) { return -1; } pinMode(ulPin,AN_INPUT); #if defined (STM32F10X_HD) || (STM32F10X_MD) ADC_RegularChannelConfig(ADC1, g_APinDescription[ulPin].ulADCChannelNumber, 1, ADC_SampleTime_55Cycles5); #elif defined (STM32F40_41xxx) ADC_RegularChannelConfig(ADC1, g_APinDescription[ulPin].ulADCChannelNumber, 1, ADC_SampleTime_15Cycles); #endif //Start ADC1 Software Conversion #if defined (STM32F10X_HD) || (STM32F10X_MD) ADC_SoftwareStartConvCmd(ADC1, ENABLE); #elif defined (STM32F40_41xxx) ADC_SoftwareStartConv(ADC1); #endif // Wait until conversion completion // while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); // Read the value ulValue = ADC_GetConversionValue(ADC1); ulValue = mapResolution(ulValue, ADC_RESOLUTION, _readResolution); return ulValue; }
static void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 , ENABLE); // ADC1 configuration ADC_DeInit(ADC1); ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 3; ADC_Init(ADC1, &ADC_InitStructure); // ADC1 channel sequence ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 3, ADC_SampleTime_28Cycles5); // ADC2 configuration ADC_DeInit(ADC2); ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 3; ADC_Init(ADC2, &ADC_InitStructure); // ADC2 channel sequence ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 2, ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC2, ADC_Channel_17, 3, ADC_SampleTime_28Cycles5); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); // Calibrate ADC1 ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); // Enable ADC1 external trigger ADC_ExternalTrigConvCmd(ADC1, ENABLE); ADC_TempSensorVrefintCmd(ENABLE); // Enable ADC2 ADC_Cmd(ADC2, ENABLE); // Calibrate ADC2 ADC_ResetCalibration(ADC2); while(ADC_GetResetCalibrationStatus(ADC2)); ADC_StartCalibration(ADC2); while(ADC_GetCalibrationStatus(ADC2)); // Enable ADC2 external trigger ADC_ExternalTrigConvCmd(ADC2, ENABLE); }
/* ************************************************************ * 函数名称: ADCx_GetValue * * 函数功能: 获取一次ADCx的值 * * 入口参数: ADCx:ADC设备 * ch:通道 * * 返回参数: ADCx转换后的数字量 * * 说明: ADC_Channel_1~ADC_Channel_16 ************************************************************ */ unsigned short ADCx_GetValue(ADC_TypeDef *ADCx, unsigned char ch) { //设置指定ADC的规则组通道,一个序列,采样时间 ADC_RegularChannelConfig(ADCx, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期 ADC_SoftwareStartConvCmd(ADCx, ENABLE); //使能指定的ADC1的软件转换启动功能 while(!ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC )); //等待转换结束 return ADC_GetConversionValue(ADCx); //返回最近一次ADC1规则组的转换结果 }