u16 adc_measure(void){ u8 channel_array[16]; u16 val,valLoaded; /* Select the channel we want to convert. 16=temperature_sensor. */ channel_array[0] = 6; adc_set_regular_sequence(ADC1, 1, channel_array); /* * Start the conversion directly (not trigger mode). */ adc_start_conversion_direct(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); val = ADC_DR(ADC1); return val; gpio_set(GPIOA, GPIO1); adc_start_conversion_direct(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); valLoaded = ADC_DR(ADC1); gpio_clear(GPIOA, GPIO5 | GPIO1); return val - valLoaded; }
void init_adc_sensor(){ // we will use ADC1 channel 0 for IR sensor & ADC1 channel 1 for laser's photoresistor uint8_t adc_channel_array[ADC_CHANNEL_NUMBER] = {0,1,6}; // Make sure the ADC doesn't run during config adc_off(ADC1); // enable ADC & PA0/PA1 clocking rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN | RCC_APB2ENR_IOPAEN); rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV4); gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0 | GPIO1); rcc_periph_clock_enable(RCC_DMA1); // enable DMA for ADC values storing // Configure ADC as continuous scan mode with DMA ADC1_CR1 = ADC_CR1_SCAN; // enable scan mode // set sample time on channels 1&2: 239.5 cycles for better results ADC1_SMPR2 = 0x3f; dma_channel_reset(DMA1, DMA_CHANNEL1); DMA1_CPAR1 = (uint32_t) &(ADC_DR(ADC1)); DMA1_CMAR1 = (uint32_t) ADC_value; DMA1_CNDTR1 = ADC_CHANNEL_NUMBER; DMA1_CCR1 = DMA_CCR_MINC | DMA_CCR_PSIZE_16BIT | DMA_CCR_MSIZE_16BIT | DMA_CCR_CIRC | DMA_CCR_PL_HIGH | DMA_CCR_EN; // continuous conv, enable ADC & DMA ADC1_CR2 = ADC_CR2_CONT | ADC_CR2_ADON | ADC_CR2_DMA; // set channels adc_set_regular_sequence(ADC1, ADC_CHANNEL_NUMBER, adc_channel_array); // reset calibration registers & start calibration ADC1_CR2 |= ADC_CR2_RSTCAL; while(ADC1_CR2 & ADC_CR2_RSTCAL); // wait for registers reset ADC1_CR2 |= ADC_CR2_CAL; while(ADC1_CR2 & ADC_CR2_CAL); // wait for calibration ends nvic_enable_irq(NVIC_ADC1_2_IRQ); ADC1_CR2 |= ADC_CR2_SWSTART; // turn on ADC - to do it we need set ADC_CR2_ADON again! ADC1_CR2 |= ADC_CR2_ADON; }
static uint16_t ADC_Measure(uint16_t ch) { uint16_t val; uint8_t channel_array[16]; /* Make sure the ADC doesn't run during config. */ // adc_off(TS_ADC); /* We configure everything for one single conversion. */ adc_disable_scan_mode(TS_ADC); adc_set_single_conversion_mode(TS_ADC); adc_disable_external_trigger_regular(TS_ADC); adc_set_right_aligned(TS_ADC); /* ADC regular channel14 configuration */ // adc_set_sample_time(TS_ADC, ch, ADC_SMPR_SMP_55DOT5CYC); adc_set_sample_time_on_all_channels(TS_ADC, ADC_SMPR_SMP_55DOT5CYC); // adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); // ADC_RegularChannelConfig(TS_ADC, ch, 1, ADC_SampleTime_55Cycles5); /* Enable ADC */ // ADC_Cmd(TS_ADC, ENABLE); adc_power_on(TS_ADC); delay(100); #if 1 /* Enable ADC reset calibaration register */ adc_reset_calibration(TS_ADC); // ADC_ResetCalibration(TS_ADC); /* Check the end of ADC reset calibration register */ // while(ADC_GetResetCalibrationStatus(TS_ADC)); /* Start ADC calibaration */ // ADC_StartCalibration(TS_ADC); // adc_calibration(TS_ADC); adc_calibrate_async(TS_ADC); /* Check the end of ADC calibration */ // while(ADC_GetCalibrationStatus(TS_ADC)); #endif /* Select the channel we want to convert. */ channel_array[0] = ch; adc_set_regular_sequence(TS_ADC, 1, channel_array); /* Start ADC Software Conversion */ // adc_start_conversion_regular(TS_ADC); adc_start_conversion_direct(TS_ADC); while(!adc_eoc(TS_ADC)); // val = adc_read_regular(TS_ADC); val = ADC_DR(TS_ADC); // adc_off(TS_ADC); return val; }
batt_state adc_testBatt(void){ u8 channel_array[16]; u16 val,valLoaded; gpio_set(GPIOA, GPIO5); delay_ms(25); /* Select the channel we want to convert. 16=temperature_sensor. */ channel_array[0] = 6; adc_set_regular_sequence(ADC1, 1, channel_array); /* * Start the conversion directly (not trigger mode). */ adc_start_conversion_direct(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); val = ADC_DR(ADC1); if(val < 0x0A3D){ return BATTCRIT; } val = val & 0xFFFC; gpio_set(GPIOA, GPIO1); delay_ms(25); adc_start_conversion_direct(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); valLoaded = ADC_DR(ADC1); gpio_clear(GPIOA, GPIO5 | GPIO1); valLoaded = valLoaded & 0xFFFC; // kontroluje pokles vetsi nez 0,75 V po zatizeni 15 Ohm if(val - valLoaded > 0x1E0){ return BATTLOW; } return BATTOK; }
uint16_t adc_read(uint8_t ch) { while (!(ADC_SR(ADC1) & ADC_SR_EOC)); uint16_t last_res = ADC_DR(ADC1); /* Select the channel we want to convert. 16=temperature_sensor. */ uint8_t channel_array[16]; channel_array[0] = ch; adc_set_regular_sequence(ADC1, 1, channel_array); /* Start the conversion directly (not trigger mode). */ adc_start_conversion_direct(ADC1); return last_res; }
int main(void) { u8 channel_array[16]; u16 temperature; rcc_clock_setup_in_hse_16mhz_out_72mhz(); gpio_setup(); usart_setup(); adc_setup(); gpio_clear(GPIOB, GPIO7); /* LED1 on */ gpio_set(GPIOB, GPIO6); /* LED2 off */ /* Send a message on USART1. */ usart_send(USART1, 's'); usart_send(USART1, 't'); usart_send(USART1, 'm'); usart_send(USART1, '\r'); usart_send(USART1, '\n'); /* Select the channel we want to convert. 16=temperature_sensor. */ channel_array[0] = 16; adc_set_regular_sequence(ADC1, 1, channel_array); /* * If the ADC_CR2_ON bit is already set -> setting it another time * starts the conversion. */ adc_on(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); temperature = ADC_DR(ADC1); /* * That's actually not the real temperature - you have to compute it * as described in the datasheet. */ my_usart_print_int(USART1, temperature); gpio_clear(GPIOB, GPIO6); /* LED2 on */ while(1); /* Halt. */ return 0; }
int main(void) { uint8_t channel_array[16]; uint16_t temperature; rcc_clock_setup_in_hse_16mhz_out_72mhz(); gpio_setup(); usart_setup(); adc_setup(); gpio_clear(GPIOB, GPIO7); /* LED1 on */ gpio_set(GPIOB, GPIO6); /* LED2 off */ /* Send a message on USART1. */ usart_send(USART1, 's'); usart_send(USART1, 't'); usart_send(USART1, 'm'); usart_send(USART1, '\r'); usart_send(USART1, '\n'); /* Select the channel we want to convert. 16=temperature_sensor. */ channel_array[0] = 16; adc_set_regular_sequence(ADC1, 1, channel_array); /* * Start the conversion directly (not trigger mode). */ adc_start_conversion_direct(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); temperature = ADC_DR(ADC1); /* * That's actually not the real temperature - you have to compute it * as described in the datasheet. */ my_usart_print_int(USART1, temperature); gpio_clear(GPIOB, GPIO6); /* LED2 on */ while(1); /* Halt. */ return 0; }
uint32_t adc_read_regular(uint32_t adc) { return ADC_DR(adc); }