void adc_setup() { rcc_periph_clock_enable(RCC_ADC1); /* Make sure the ADC doesn't run during config. */ adc_off(ADC1); /* We configure everything for one single conversion. */ adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); adc_power_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); /* Select the channel we want to convert. 16=temperature_sensor. */ uint8_t channel_array[16]; channel_array[0] = 0; adc_set_regular_sequence(ADC1, 1, channel_array); /* Start the conversion directly (not trigger mode). */ adc_start_conversion_direct(ADC1); }
/** * Configure a specific adc. */ void adc_config(uint32_t adc, const uint8_t const *channel_array) { adc_enable_scan_mode(adc); adc_set_continuous_conversion_mode(adc); adc_set_right_aligned(adc); adc_enable_external_trigger_regular(adc, ADC_CR2_EXTSEL_SWSTART); adc_set_sample_time_on_all_channels(adc, ADC_SAMPLE_TIME); adc_enable_dma(adc); adc_power_on(adc); { int i; /* Wait a bit for the adc to power on. */ for (i = 0; i < 800000; i++) { __asm("nop"); } } adc_reset_calibration(adc); adc_calibration(adc); adc_set_regular_sequence(adc, ADC_RAW_SAMPLE_COUNT/2, (uint8_t *)channel_array); }
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"); }
static void adc_setup(void) { rcc_periph_clock_enable(RCC_ADC); rcc_periph_clock_enable(RCC_GPIOA); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); adc_power_off(ADC1); adc_set_clk_source(ADC1, ADC_CLKSOURCE_ADC); adc_calibrate_start(ADC1); adc_calibrate_wait_finish(ADC1); adc_set_operation_mode(ADC1, ADC_MODE_SCAN); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); adc_enable_temperature_sensor(); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPTIME_071DOT5); adc_set_regular_sequence(ADC1, 1, channel_array); adc_set_resolution(ADC1, ADC_RESOLUTION_12BIT); adc_disable_analog_watchdog(ADC1); adc_power_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) { /* Wait a bit. */ __asm__("nop"); } }
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; }
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; }
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; }
static uint16_t read_adc_naiive(uint8_t channel) { uint8_t channel_array[16]; channel_array[0] = channel; adc_set_regular_sequence(ADC1, 1, channel_array); adc_start_conversion_direct(ADC1); while (!adc_eoc(ADC1)); uint16_t reg16 = adc_read_regular(ADC1); return reg16; }
/** * NOTE this is a state machine, but it expects to run often enough for millis() * @param machine * @param res */ void jack_run_task(volatile struct jacks_machine_t *machine, struct jacks_result_t *res) { res->ready = false; if (!jack_connected(machine->jack)) { return; } switch (machine->step) { case jack_machine_step_off: // is it time to do a reading yet? if (millis() - 3000 > machine->last_read_millis) { printf("switching power on: channel %u\n", (unsigned int) machine->jack->val_channel); gpio_set(machine->jack->power_port, machine->jack->power_pin); machine->step = jack_machine_step_powered; machine->step_entry_millis = millis(); } break; case jack_machine_step_powered: // have we been powered up long enough yet? if (millis() - machine->jack->power_on_time_millis > machine->step_entry_millis) { printf("power stable!\n"); machine->step = jack_machine_step_ready; // not really necessary... machine->step_entry_millis = millis(); } else { printf("."); } break; case jack_machine_step_ready: // TODO - this should actually start a dma sequence and go to a next step // that decimates/averages and finally returns. // ok! do a few readings and call it good adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); //adc_set_single_channel(ADC1, machine->jack->val_channel); adc_set_regular_sequence(ADC1, 1, (u8*)&(machine->jack->val_channel)); adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_SWSTART); adc_start_conversion_regular(ADC1); printf("ok, doing reading on channel!\n"); while(!adc_eoc(ADC1)) { ; } res->ready = true; res->value = adc_read_regular(ADC1); machine->last_value = res->value; machine->last_read_millis = millis(); gpio_clear(machine->jack->power_port, machine->jack->power_pin); machine->step = jack_machine_step_off; break; } return; }
float voltage_measure (uint32_t adc,uint8_t channel) { uint8_t channels[16]; float voltage; channels[0] = channel; adc_set_regular_sequence(adc, 1, channels); adc_start_conversion_regular(adc); gpio_toggle (GPIOD,GPIO15); while (!adc_eoc(adc)); voltage=adc_read_regular(adc)*(VREF/ADC_CONVERSION_FACTOR); return voltage; }
void battery_setup() { gpio_mode_setup(BAT_STAT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, BAT_STAT1_PORT | BAT_STAT2_PORT | BAT_PG_PORT); gpio_mode_setup(BAT_SENSE_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, BAT_SENSE_PIN); adc_off(BAT_SENSE_ADC); adc_disable_scan_mode(BAT_SENSE_ADC); adc_set_single_conversion_mode(ADC1); adc_set_sample_time(ADC1, ADC_CHANNEL10, ADC_SMPR_SMP_15CYC); uint8_t channels[] = {ADC_CHANNEL10}; adc_set_regular_sequence(BAT_SENSE_ADC, 1, channels); adc_power_on(BAT_SENSE_ADC); }
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; }
/*--------------------------------------------------------------------*/ 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); }
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; }
static void platform_init_adc() { /* Set up DMA for the ADC */ nvic_enable_irq(NVIC_DMA2_STREAM0_IRQ); nvic_set_priority(NVIC_DMA2_STREAM0_IRQ, 64); /* Set up ADC */ for (int i = 0; i < NUM_SENSORS; ++i) { if (config.sensors[i].method == SENSOR_ADC) { adc_pins[i] = config.sensors[i].pin; gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, (1<<adc_pins[i])); } } adc_off(ADC1); adc_enable_scan_mode(ADC1); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_15CYC); adc_power_on(ADC1); adc_disable_dma(ADC1); adc_enable_dma(ADC1); adc_clear_overrun_flag(ADC1); adc_set_dma_continue(ADC1); dma_stream_reset(DMA2, DMA_STREAM0); dma_set_priority(DMA2, DMA_STREAM0, DMA_SxCR_PL_HIGH); dma_set_memory_size(DMA2, DMA_STREAM0, DMA_SxCR_MSIZE_16BIT); dma_set_peripheral_size(DMA2, DMA_STREAM0, DMA_SxCR_PSIZE_16BIT); dma_enable_memory_increment_mode(DMA2, DMA_STREAM0); dma_set_transfer_mode(DMA2, DMA_STREAM0, DMA_SxCR_DIR_PERIPHERAL_TO_MEM); dma_enable_circular_mode(DMA2, DMA_STREAM0); dma_set_peripheral_address(DMA2, DMA_STREAM0, (uint32_t) &ADC1_DR); dma_set_memory_address(DMA2, DMA_STREAM0, (uint32_t) adc_dma_buf); dma_set_number_of_data(DMA2, DMA_STREAM0, NUM_SENSORS); dma_enable_transfer_complete_interrupt(DMA2, DMA_STREAM0); dma_channel_select(DMA2, DMA_STREAM0, DMA_SxCR_CHSEL_0); dma_enable_stream(DMA2, DMA_STREAM0); adc_set_regular_sequence(ADC1, NUM_SENSORS, adc_pins); }
static void setup_stm32f1_peripherals(void) { rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C1EN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); /* GPIO pin for I2C1 SCL, SDA */ /* VESNA v1.0 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6); gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO7); */ /* VESNA v1.1 */ AFIO_MAPR |= AFIO_MAPR_I2C1_REMAP; gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, TDA_PIN_SCL); gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, TDA_PIN_SDA); /* GPIO pin for TDA18219 IRQ */ gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, TDA_PIN_IRQ); /* GPIO pin for TDA18219 IF AGC */ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, TDA_PIN_IF_AGC); /* Set to lowest gain for now */ gpio_clear(GPIOA, TDA_PIN_IF_AGC); /* GPIO pin for AD8307 ENB */ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, TDA_PIN_ENB); /* ADC pin for AD8307 output */ gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, TDA_PIN_OUT); /* Setup I2C */ i2c_peripheral_disable(I2C1); /* 400 kHz - I2C Fast Mode */ i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_24MHZ); i2c_set_fast_mode(I2C1); /* 400 kHz */ i2c_set_ccr(I2C1, 0x14); /* 300 ns rise time */ i2c_set_trise(I2C1, 0x08); i2c_peripheral_enable(I2C1); /* Make sure the ADC doesn't run during config. */ adc_off(ADC1); /* We configure everything for one single conversion. */ adc_disable_scan_mode(ADC1); adc_set_single_conversion_mode(ADC1); adc_enable_discontinous_mode_regular(ADC1); adc_disable_external_trigger_regular(ADC1); adc_set_right_aligned(ADC1); adc_set_conversion_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); adc_on(ADC1); /* Wait for ADC starting up. */ int i; for (i = 0; i < 800000; i++) /* Wait a bit. */ __asm__("nop"); adc_reset_calibration(ADC1); adc_calibration(ADC1); uint8_t channel_array[16]; /* Select the channel we want to convert. */ if(TDA_PIN_OUT == GPIO0) { channel_array[0] = 0; } else if(TDA_PIN_OUT == GPIO2) { channel_array[0] = 2; } adc_set_regular_sequence(ADC1, 1, channel_array); }