Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- uint16_t vref=0;
- uint16_t vref_calibrated = 0;
- uint16_t delta_vref = 0;
- uint32_t wait=0;
- int main(void)
- {
- /* Enable peripheral clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- /* configure ADC */
- adc_config();
- // Read internal voltage reference calibrated value
- vref_calibrated = (*(uint16_t*)0x1FFFF7BA);
- while(1)
- {
- // Start conversion for ADC1
- ADC_StartOfConversion(ADC1);
- // Wait until conversion is done
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
- // Read Vrefint value
- vref = ADC1->DR;
- // Calculate the difference between Vrefint measured and factory calibrated
- delta_vref = vref - vref_calibrated;
- // Add some delay
- for(wait = 0; wait < 0x0FFFFF; wait++);
- }
- }
- void adc_config(void)
- {
- ADC_DeInit(ADC1); // De-init ADC1 peripheral
- adc_struct.ADC_Resolution = ADC_Resolution_12b; // 12 bit resolution
- adc_struct.ADC_ContinuousConvMode = DISABLE; // single conversion mode
- adc_struct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
- adc_struct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
- adc_struct.ADC_DataAlign = ADC_DataAlign_Right;
- adc_struct.ADC_ScanDirection = ADC_ScanDirection_Upward;
- ADC_Init(ADC1, &adc_struct);
- // Convert Vrefint channel
- ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint, ADC_SampleTime_239_5Cycles);
- // Enable internal voltage reference channel
- ADC_VrefintCmd(ENABLE);
- // Enable ADC1 peripheral
- ADC_Cmd(ADC1, ENABLE);
- // wait until ADC1 peripheral is enabled
- while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
- }
Advertisement
Add Comment
Please, Sign In to add comment