char sampleADC(void) { char res = 0x0; CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_VrefintCmd(ENABLE); delay_10us(3); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_6Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_0, ENABLE); delay_10us(3); ADC_SoftwareStartConv(ADC1); while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0); res = (char)ADC_GetConversionValue(ADC1); ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ //ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_0, DISABLE); return res; }
/** * @brief Read ADC1 * @caller several functions * @param None * @retval ADC value */ u16 ADC_Supply(void) { uint8_t i; uint16_t res; /* Enable ADC clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); /* de-initialize ADC */ ADC_DeInit(ADC1); /*ADC configuration ADC configured as follow: - Channel VREF - Mode = Single ConversionMode(ContinuousConvMode disabled) - Resolution = 12Bit - Prescaler = /1 - sampling time 9 */ ADC_VrefintCmd(ENABLE); delay_10us(3); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, ENABLE); delay_10us(3); /* initialize result */ res = 0; for(i=8; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* de-initialize ADC */ ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, DISABLE); return (res>>3); }
void adc_init(void) { ADC_Init(ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1); ADC_VrefintCmd(ENABLE); ADC_SamplingTimeConfig(ADC1,ADC_Group_SlowChannels,ADC_SamplingTime_4Cycles); ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); ADC_Cmd(ADC1,ENABLE); };
/** * @brief ADC configuration * @note * @retval */ void ADC_Cfg(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_Cmd(ADC1, DISABLE); ADC_VrefintCmd(ENABLE); ADC_GetCalibrationFactor(ADC1);//校准 ADC1->CHSELR |= ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL17 ; ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; ADC_Cmd(ADC1, ENABLE); /* GPIOA Clocks enable */ RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE); /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); }
/** * @brief ADC1 channel with DMA configuration * @param None * @retval None */ void ADC1_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* Configure ADC Channel11 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 4; 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); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 1 with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_55_5Cycles); /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles); ADC_TempSensorCmd(ENABLE); /* Convert the ADC1 Vref with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles); ADC_VrefintCmd(ENABLE); /* Convert the ADC1 Vbat with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_55_5Cycles); ADC_VbatCmd(ENABLE); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
void adc_init(void) { ADC_InitTypeDef ADC_InitStructure; { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = BATTERYPIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(BATTERYPORT, &GPIO_InitStructure); } RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); { DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x40012440; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcarray; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 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); } ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); ADC_DMACmd(ADC1, ENABLE); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, BATTERY_ADC_CHANNEL , ADC_SampleTime_239_5Cycles); ADC_VrefintCmd(ENABLE); ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); ADC_StartOfConversion(ADC1); DMA_Cmd(DMA1_Channel1, ENABLE); // reference is measured a 3.3v, we are powered by 2.8, so a 1.17 multiplier // different vccs will translate to a different adc scale factor, // so actual vcc is not important as long as the voltage is correct in the end vref_cal = 1.17857f * (float) ( adcref_read ((adcrefcal *) 0x1FFFF7BA) ); }
/** * @brief main entry point. * @par Parameters None * @retval void None * @par Required preconditions: None */ void main(void) { uint8_t PayloadLength, data_sensor, *bufMessage; /* deinit I/O ports */ DeInitClock(); DeInitGPIO(); /* Select HSI as system clock source */ #ifdef USE_HSI CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); #else CLK_SYSCLKSourceSwitchCmd(ENABLE); /* Select 2MHz HSE as system clock source */ CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_4); CLK_HSICmd(DISABLE); #endif // Initializes the LCD glass LCD_GLASS_Init(); /* LED button init: GPIO set in push pull */ GPIO_Init( LED_GPIO_PORT, LED_GPIO_PIN, GPIO_Mode_Out_PP_Low_Fast); // set to 0 GPIOE->ODR &= ~LED_GPIO_PIN; /* USER button init: GPIO set in input interrupt active mode */ GPIO_Init( BUTTON_GPIO_PORT, USER_GPIO_PIN, GPIO_Mode_In_FL_IT); EXTI_SetPinSensitivity(EXTI_Pin_7, EXTI_Trigger_Falling); //* Init Bar on LCD all are OFF BAR0_OFF; BAR1_OFF; BAR2_OFF; BAR3_OFF; enableInterrupts(); //* At power on VDD diplays bufMessage = NDEFmessage; if (EEMenuState > STATE_TEMPMEAS) EEMenuState = STATE_CHECKNDEFMESSAGE; FLASH_Unlock(FLASH_MemType_Data ); state_machine = EEMenuState ; delayLFO_ms (1); if (EEInitial == 0) { User_WriteFirmwareVersion (); EEInitial =1; } while (1) { switch (state_machine) { case STATE_VREFF: // measure the voltage available at the output of the M24LR04E-R Vref_measure(); delayLFO_ms (2); //turn on led GPIO_SetBits(GPIOE, GPIO_Pin_6); break; case STATE_VBIO: //measure the output voltage of biosensor through Pin 7 Port E CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_VrefintCmd(ENABLE); delay_10us(3); GPIO_DeInit(GPIOE); GPIO_Init(GPIOE,GPIO_Pin_7 ,/*GPIO_Mode_In_FL_No_IT*/GPIO_Mode_In_PU_No_IT); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single,ADC_Resolution_12Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_3, ENABLE); delay_10us(3); // Important delay res = 0; res_2 = 0; i=0; for(i=8; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* de-initialize ADC*/ ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ ADC_SchmittTriggerConfig(ADC1, ADC_Channel_3, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_3, DISABLE); res = res>>3; P_VREFINT_Factory = VREFINT_Factory_CONV_ADDRESS; #ifdef VREFINT_FACTORY_CONV if ((*P_VREFINT_Factory>VREFINT_Factory_CONV_MIN ) && (*P_VREFINT_Factory<VREFINT_Factory_CONV_MAX )) { /* If the value exists: Adds the hight byte to FullVREF_FACTORY */ FullVREF_FACTORY = VREFINT_Factory_CONV_MSB; FullVREF_FACTORY += *P_VREFINT_Factory; res_2 = (float)(FullVREF_FACTORY*VDD_FACTORY); res_2 /= res; } else { res_2 = (VREF/res) * ADC_CONV; // usally res>>3 } #else /* We use the theorcial value */ res_2 = (VREF/res) * ADC_CONV; #endif /* Vdd_appli in mV */ res_2*= 1000L; convert_into_char (res_2, tab); /* To add unit and decimal point */ tab[5] = 'V'; tab[4] = ' '; tab[1] |= DOT; /* To add decimal point for display in volt */ tab[0] = ' '; LCD_GLASS_DisplayStrDeci(tab); delayLFO_ms (2); //LCD_GLASS_DisplayString("V BIO"); break; case STATE_CHECKNDEFMESSAGE: // read the NDEF message from the M24LR04E-R EEPROM and display it if it is found if (User_ReadNDEFMessage (&PayloadLength) == SUCCESS) User_DisplayMessage (bufMessage,PayloadLength); // User_DisplayMessageActiveHaltMode (PayloadLength); else User_DisplayMessage(ErrorMessage,20); break; case STATE_TEMPMEAS: // read the ambiant tempserature from the STTS751 User_GetOneTemperature (&data_sensor); // display the temperature User_DisplayOneTemperature (data_sensor); delayLFO_ms (2); break; break; /* for safe: normaly never reaches */ default: LCD_GLASS_Clear(); LCD_GLASS_DisplayString("Error"); state_machine = STATE_VREFF; break; } } }