void adc_setup(void) { //ADC rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_ADC12EN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); //ADC gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); adc_off(ADC1); adc_set_clk_prescale(ADC_CCR_CKMODE_DIV2); adc_set_single_conversion_mode(ADC1); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); /* We want to read the temperature sensor, so we have to enable it. */ adc_enable_temperature_sensor(); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR1_SMP_61DOT5CYC); uint8_t channel_array[16]; channel_array[0]=16; // Vts (Internal temperature sensor channel_array[0]=1; //ADC1_IN1 (PA0) adc_set_regular_sequence(ADC1, 1, channel_array); adc_set_resolution(ADC1, ADC_CFGR_RES_12_BIT); adc_power_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); }
/** Configure and enable RCC for peripherals (ADC1, ADC2, Timer) */ static inline void adc_init_rcc( void ) { #if USE_AD1 || USE_AD2 || USE_AD3 /* Timer peripheral clock enable. */ rcc_periph_clock_enable(RCC_TIM_ADC); #if defined(STM32F4) adc_set_clk_prescale(ADC_CCR_ADCPRE_BY2); #endif /* Enable ADC peripheral clocks. */ #if USE_AD1 rcc_periph_clock_enable(RCC_ADC1); #endif #if USE_AD2 rcc_periph_clock_enable(RCC_ADC2); #endif #if USE_AD3 rcc_periph_clock_enable(RCC_ADC3); #endif /* Time Base configuration */ timer_reset(TIM_ADC); timer_set_mode(TIM_ADC, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); /* timer counts with ADC_TIMER_FREQUENCY */ uint32_t timer_clk = timer_get_frequency(TIM_ADC); timer_set_prescaler(TIM_ADC, (timer_clk / ADC_TIMER_FREQUENCY) - 1); timer_set_period(TIM_ADC, ADC_TIMER_PERIOD); /* Generate TRGO on every update (when counter reaches period reload value) */ timer_set_master_mode(TIM_ADC, TIM_CR2_MMS_UPDATE); timer_enable_counter(TIM_ADC); #endif // USE_AD1 || USE_AD2 || USE_AD3 }
void adc_init (void) { rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); //PA1 joint_1 gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO2); //PA2 joint_2 gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3); //PA3 joint_3 gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); //PC1 joint_4 gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO2); //PC2 joint_5 gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO5); //PC5 joint_6 adc_set_clk_prescale(ADC_CCR_ADCPRE_BY2); adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); adc_set_sample_time(ADC1, ADC_CHANNEL1, ADC_SMPR_SMP_3CYC); //joint_1 adc_set_sample_time(ADC1, ADC_CHANNEL2, ADC_SMPR_SMP_3CYC); //joint_2 adc_set_sample_time(ADC1, ADC_CHANNEL3, ADC_SMPR_SMP_3CYC); //joint_3 adc_set_sample_time(ADC1, ADC_CHANNEL11, ADC_SMPR_SMP_3CYC); //joint_4 adc_set_sample_time(ADC1, ADC_CHANNEL12, ADC_SMPR_SMP_3CYC); //joint_5 adc_set_sample_time(ADC1, ADC_CHANNEL15, ADC_SMPR_SMP_3CYC); //joint_6 adc_set_multi_mode(ADC_CCR_MULTI_INDEPENDENT); adc_power_on(ADC1); //nvic_enable_irq(NVIC_ADC_IRQ); //adc_enable_eoc_interrupt(ADC1); //adc_disable_eoc_interrupt(ADC1); }
/** Configure and enable RCC for peripherals (ADC1, ADC2, Timer) */ static inline void adc_init_rcc( void ) { #if USE_AD1 || USE_AD2 || USE_AD3 uint32_t timer; volatile uint32_t *rcc_apbenr; uint32_t rcc_apb; #if defined(USE_AD_TIM4) timer = TIM4; rcc_apbenr = &RCC_APB1ENR; rcc_apb = RCC_APB1ENR_TIM4EN; #elif defined(USE_AD_TIM1) timer = TIM1; rcc_apbenr = &RCC_APB2ENR; rcc_apb = RCC_APB2ENR_TIM1EN; #else timer = TIM2; rcc_apbenr = &RCC_APB1ENR; rcc_apb = RCC_APB1ENR_TIM2EN; #endif /* Timer peripheral clock enable. */ rcc_peripheral_enable_clock(rcc_apbenr, rcc_apb); #if defined(STM32F4) adc_set_clk_prescale(ADC_CCR_ADCPRE_BY2); #endif /* Enable ADC peripheral clocks. */ #if USE_AD1 rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); #endif #if USE_AD2 rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC2EN); #endif #if USE_AD3 rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC3EN); #endif /* Time Base configuration */ timer_reset(timer); timer_set_mode(timer, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); #if defined(STM32F1) timer_set_period(timer, 0xFF); timer_set_prescaler(timer, 0x8); #elif defined(STM32F4) timer_set_period(timer, 0xFFFF); timer_set_prescaler(timer, 0x53); #endif //timer_set_clock_division(timer, 0x0); /* Generate TRGO on every update. */ timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE); timer_enable_counter(timer); #endif // USE_AD1 || USE_AD2 || USE_AD3 }
/*--------------------------------------------------------------------*/ void adc_setup(void) { rcc_periph_clock_enable(RCC_ADC1); rcc_periph_clock_enable(RCC_GPIOA); nvic_enable_irq(NVIC_ADC_IRQ); /* Set port PA1 for ADC1 to analogue mode. */ gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); adc_power_on(ADC1); uint8_t channel[1] = { ADC_CHANNEL1 }; adc_set_regular_sequence(ADC1, 1, channel); adc_set_clk_prescale(ADC_CCR_ADCPRE_BY2); adc_enable_scan_mode(ADC1); adc_set_continuous_conversion_mode(ADC1); adc_set_sample_time(ADC1, ADC_CHANNEL1, ADC_SMPR_SMP_3CYC); adc_set_multi_mode(ADC_CCR_MULTI_INDEPENDENT); adc_set_dma_continue(ADC1); adc_enable_dma(ADC1); adc_enable_overrun_interrupt(ADC1); }