Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "adc.h"
- extern volatile u16 temperature = 0;
- extern volatile u16 v_refint = 0;
- extern volatile u16 lisam_ADC3 = 0;
- extern volatile u16 lisam_adc2 = 0;
- u8 channel_array[4]; /* for injected sampling, 4 channels max, for regular, 16 max */
- void timer2_setup(void)
- {
- rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
- /* Time Base configuration */
- timer_reset(TIM2);
- timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT,
- TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
- timer_set_period(TIM2, 0xFF);
- timer_set_prescaler(TIM2, 0x8);
- timer_set_clock_division(TIM2, 0x0);
- /* Generate TRGO on every update. */
- timer_set_master_mode(TIM2, TIM_CR2_MMS_UPDATE);
- timer_enable_counter(TIM2);
- // printf("timer2_setup\r\n");
- }
- void adc_setup(void)
- {
- int i;
- rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC3EN);
- /* Make sure the ADC doesn't run during config. */
- adc_off(ADC3);
- /* We configure everything for one single timer triggered injected conversion with interrupt generation. */
- /* While not needed for a single channel, try out scan mode which does all channels in one sweep and
- * generates the interrupt/EOC/JEOC flags set at the end of all channels, not each one.
- */
- adc_enable_scan_mode(ADC3);
- adc_set_single_conversion_mode(ADC3);
- /* We want to start the injected conversion with the TIM2 TRGO */
- adc_enable_external_trigger_injected(ADC3, ADC_CR2_JEXTSEL_TIM2_TRGO, ADC_CR2_EXTEN_FALLING_EDGE);
- /* Generate the ADC3_2_IRQ */
- adc_enable_eoc_interrupt_injected(ADC3);
- adc_set_right_aligned(ADC3);
- /* We want to read the temperature sensor, so we have to enable it. */
- // adc_enable_temperature_sensor();
- adc_set_sample_time_on_all_channels(ADC3, ADC_SMPR_SMP_28DOT5CYC);
- /* Select the channels we want to convert.
- * 16=temperature_sensor, 17=Vrefint, 13=ADC3, 10=ADC2
- */
- channel_array[0] = 16;
- channel_array[1] = 17;
- channel_array[2] = 13;
- channel_array[3] = 10;
- adc_set_injected_sequence(ADC3, 4, channel_array);
- adc_power_on(ADC3);
- /* Wait for ADC starting up. */
- for (i = 0; i < 800000; i++) /* Wait a bit. */
- __asm__("nop");
- // adc_reset_calibration(ADC3);
- // while ((ADC_CR2(ADC3) & ADC_CR2_RSTCAL) != 0); //added this check
- // adc_calibration(ADC3);
- // while ((ADC_CR2(ADC3) & ADC_CR2_CAL) != 0); //added this check
- // printf("adc_setup\r\n");
- // adc_reset_calibration(ADC3);
- // adc_calibration(ADC3);
- }
- // void adc_setup(void)
- // {
- // int i;
- // gpio_mode_setup(GPIOF, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3);
- // rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC3EN);
- // /* Make sure the ADC doesn't run during config. */
- // adc_off(ADC3);
- // /* We configure everything for one single conversion. */
- // adc_disable_scan_mode(ADC3);
- // adc_set_single_conversion_mode(ADC3);
- // adc_disable_external_trigger_regular(ADC3);
- // adc_set_right_aligned(ADC3);
- // /* We want to read the temperature sensor, so we have to enable it. */
- // // adc_enable_temperature_sensor();
- // adc_set_sample_time_on_all_channels(ADC3, ADC_SMPR_SMP_28DOT5CYC);
- // adc_power_on(ADC3);
- // /* Wait for ADC starting up. */
- // for (i = 0; i < 800000; i++) /* Wait a bit. */
- // __asm__("nop");
- // //adc_reset_calibration(ADC3);
- // //adc_calibration(ADC3);
- // }
- void adc_isr(void)
- {
- /* Clear Injected End Of Conversion (JEOC) */
- ADC_SR(ADC3) &= ~ADC_SR_JEOC;
- temperature = adc_read_injected(ADC3,1);
- v_refint = adc_read_injected(ADC3,2);
- lisam_ADC3 = adc_read_injected(ADC3,3);
- lisam_adc2 = adc_read_injected(ADC3,4);
- printf("ADC_ISR\r\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment