Guest User

Untitled

a guest
Oct 29th, 2015
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. uint16_t vref=0;
  4. uint16_t vref_calibrated = 0;
  5. uint16_t delta_vref = 0;
  6. uint32_t wait=0;
  7.  
  8. int main(void)
  9. {
  10. /* Enable peripheral clock */
  11. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  12.  
  13. /* configure ADC */
  14. adc_config();
  15.  
  16. // Read internal voltage reference calibrated value
  17. vref_calibrated = (*(uint16_t*)0x1FFFF7BA);
  18.  
  19. while(1)
  20. {
  21. // Start conversion for ADC1
  22. ADC_StartOfConversion(ADC1);
  23. // Wait until conversion is done
  24. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  25. // Read Vrefint value
  26. vref = ADC1->DR;
  27. // Calculate the difference between Vrefint measured and factory calibrated
  28. delta_vref = vref - vref_calibrated;
  29. // Add some delay
  30. for(wait = 0; wait < 0x0FFFFF; wait++);
  31. }
  32. }
  33.  
  34. void adc_config(void)
  35. {
  36. ADC_DeInit(ADC1); // De-init ADC1 peripheral
  37.  
  38. adc_struct.ADC_Resolution = ADC_Resolution_12b; // 12 bit resolution
  39. adc_struct.ADC_ContinuousConvMode = DISABLE; // single conversion mode
  40. adc_struct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  41. adc_struct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
  42. adc_struct.ADC_DataAlign = ADC_DataAlign_Right;
  43. adc_struct.ADC_ScanDirection = ADC_ScanDirection_Upward;
  44. ADC_Init(ADC1, &adc_struct);
  45.  
  46. // Convert Vrefint channel
  47. ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint, ADC_SampleTime_239_5Cycles);
  48.  
  49. // Enable internal voltage reference channel
  50. ADC_VrefintCmd(ENABLE);
  51.  
  52. // Enable ADC1 peripheral
  53. ADC_Cmd(ADC1, ENABLE);
  54.  
  55. // wait until ADC1 peripheral is enabled
  56. while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
  57. }
Advertisement
Add Comment
Please, Sign In to add comment