TheLegace

adc

May 20th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. #include "adc.h"
  2.  
  3. extern volatile u16 temperature = 0;
  4. extern volatile u16 v_refint = 0;
  5. extern volatile u16 lisam_ADC3 = 0;
  6. extern volatile u16 lisam_adc2 = 0;
  7. u8 channel_array[4]; /* for injected sampling, 4 channels max, for regular, 16 max */
  8.  
  9. void timer2_setup(void)
  10. {
  11.     rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
  12.  
  13.     /* Time Base configuration */
  14.     timer_reset(TIM2);
  15.     timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT,
  16.         TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
  17.     timer_set_period(TIM2, 0xFF);
  18.     timer_set_prescaler(TIM2, 0x8);
  19.     timer_set_clock_division(TIM2, 0x0);
  20.     /* Generate TRGO on every update. */
  21.     timer_set_master_mode(TIM2, TIM_CR2_MMS_UPDATE);
  22.     timer_enable_counter(TIM2);
  23.     // printf("timer2_setup\r\n");
  24. }
  25.  
  26. void adc_setup(void)
  27. {
  28.     int i;
  29.  
  30.     rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC3EN);
  31.  
  32.     /* Make sure the ADC doesn't run during config. */
  33.     adc_off(ADC3);
  34.  
  35.     /* We configure everything for one single timer triggered injected conversion with interrupt generation. */
  36.     /* While not needed for a single channel, try out scan mode which does all channels in one sweep and
  37.      * generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one.
  38.      */
  39.     adc_enable_scan_mode(ADC3);
  40.     adc_set_single_conversion_mode(ADC3);
  41.     /* We want to start the injected conversion with the TIM2 TRGO */
  42.     adc_enable_external_trigger_injected(ADC3, ADC_CR2_JEXTSEL_TIM2_TRGO, ADC_CR2_EXTEN_FALLING_EDGE);
  43.     /* Generate the ADC3_2_IRQ */
  44.     adc_enable_eoc_interrupt_injected(ADC3);
  45.     adc_set_right_aligned(ADC3);
  46.     /* We want to read the temperature sensor, so we have to enable it. */
  47.     // adc_enable_temperature_sensor();
  48.     adc_set_sample_time_on_all_channels(ADC3, ADC_SMPR_SMP_28DOT5CYC);
  49.  
  50.     /* Select the channels we want to convert.
  51.      * 16=temperature_sensor, 17=Vrefint, 13=ADC3, 10=ADC2
  52.      */
  53.     channel_array[0] = 16;
  54.     channel_array[1] = 17;
  55.     channel_array[2] = 13;
  56.     channel_array[3] = 10;
  57.     adc_set_injected_sequence(ADC3, 4, channel_array);
  58.  
  59.     adc_power_on(ADC3);
  60.  
  61.     /* Wait for ADC starting up. */
  62.     for (i = 0; i < 800000; i++)    /* Wait a bit. */
  63.         __asm__("nop");
  64.  
  65.     // adc_reset_calibration(ADC3);
  66.     // while ((ADC_CR2(ADC3) & ADC_CR2_RSTCAL) != 0); //added this check
  67.     // adc_calibration(ADC3);
  68.     // while ((ADC_CR2(ADC3) & ADC_CR2_CAL) != 0); //added this check
  69.     // printf("adc_setup\r\n");
  70.     // adc_reset_calibration(ADC3);
  71.     // adc_calibration(ADC3);        
  72. }
  73.  
  74. // void adc_setup(void)
  75. // {
  76. //     int i;
  77. //  gpio_mode_setup(GPIOF, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3);
  78. //     rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC3EN);
  79.  
  80. //     /* Make sure the ADC doesn't run during config. */
  81. //     adc_off(ADC3);
  82.  
  83. //     /* We configure everything for one single conversion. */
  84. //     adc_disable_scan_mode(ADC3);
  85. //     adc_set_single_conversion_mode(ADC3);
  86. //     adc_disable_external_trigger_regular(ADC3);
  87. //     adc_set_right_aligned(ADC3);
  88. //     /* We want to read the temperature sensor, so we have to enable it. */
  89. //     // adc_enable_temperature_sensor();
  90. //     adc_set_sample_time_on_all_channels(ADC3, ADC_SMPR_SMP_28DOT5CYC);
  91. //     adc_power_on(ADC3);
  92.    
  93. //     /* Wait for ADC starting up. */
  94. //     for (i = 0; i < 800000; i++)    /* Wait a bit. */
  95. //             __asm__("nop");
  96.  
  97. //     //adc_reset_calibration(ADC3);
  98. //     //adc_calibration(ADC3);
  99. // }
  100.  
  101. void adc_isr(void)
  102. {
  103.     /* Clear Injected End Of Conversion (JEOC) */
  104.     ADC_SR(ADC3) &= ~ADC_SR_JEOC;
  105.     temperature = adc_read_injected(ADC3,1);
  106.     v_refint = adc_read_injected(ADC3,2);
  107.     lisam_ADC3 = adc_read_injected(ADC3,3);
  108.     lisam_adc2 = adc_read_injected(ADC3,4);
  109.     printf("ADC_ISR\r\n");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment