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_regular(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; }
int main(void) { uint16_t temp; clock_setup(); gpio_setup(); adc_setup(); usart_setup(); while (1) { adc_start_conversion_regular(ADC1); while (!(adc_eoc(ADC1))); temp=adc_read_regular(ADC1); gpio_port_write(GPIOE, temp << 4); my_usart_print_int(USART2, temp); } return 0; }
/*--------------------------------------------------------------------*/ int main(void) { uint32_t i,j; cntr=4; clock_setup(); gpio_setup(); adc_setup(); dma_setup(); /* Start of the ADC. Should continue indefinitely with DMA in circular mode */ adc_start_conversion_regular(ADC1); while (1) { /* Blink the LED (PB8, PB9) on the board. */ uint32_t count = 500*v[1]; gpio_toggle(GPIOD, GPIO12); for (i = 0; i < count; i++) __asm__("nop"); gpio_toggle(GPIOD, GPIO13); for (i = 0; i < count; i++) __asm__("nop"); } return 0; }
int main(void) { uint16_t temp; adc_setup(); usart_setup(); while (1) { adc_start_conversion_regular(ADC1); while (!(adc_eoc(ADC1))); temp = adc_read_regular(ADC1); my_usart_print_int(USART1, temp); int i; for (i = 0; i < 800000; i++) { /* Wait a bit. */ __asm__("nop"); } } return 0; }
void adc_gather() { adc_start_conversion_regular(ADC1); }
uint16_t battery_sense_read() { adc_start_conversion_regular(BAT_SENSE_ADC); while (!adc_eoc(BAT_SENSE_ADC)); return adc_read_regular(BAT_SENSE_ADC); }
/** * Initialize analog to digital converter */ void adc_init(adc_callback_t half_transfer_callback, adc_callback_t transfer_complete_callback) { /* Reset adc_state. */ adc_state.dma_transfer_error_counter = 0; adc_state.half_transfer_callback = half_transfer_callback; adc_state.transfer_complete_callback = transfer_complete_callback; /* Initialize peripheral clocks. */ rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC2EN); /* Initialize the ADC input GPIO. */ /* WARNING: this code is written to work with strip. On the strip * hardware we are lucky and all the ADC channels are on the same bank * so we can initialize all of them in one go. This code will need to be * changed/improved if we ever have to support hardware that has the * ADC's spread over more then one bank. */ gpio_set_mode(ADC_BANK, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC_PORT_U_VOLTAGE | ADC_PORT_V_VOLTAGE | ADC_PORT_W_VOLTAGE | ADC_PORT_V_BATT | ADC_PORT_CURRENT); /* Configure DMA for data aquisition. */ /* Channel 1 reacts to: ADC1, TIM2_CH3 and TIM4_CH1 */ dma_channel_reset(DMA1, DMA_CHANNEL1); dma_set_peripheral_address(DMA1, DMA_CHANNEL1, (uint32_t)&ADC1_DR); dma_set_memory_address(DMA1, DMA_CHANNEL1, (uint32_t)adc_state.raw_data); dma_set_number_of_data(DMA1, DMA_CHANNEL1, ADC_RAW_SAMPLE_COUNT/2); dma_set_read_from_peripheral(DMA1, DMA_CHANNEL1); dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL1); dma_enable_circular_mode(DMA1, DMA_CHANNEL1); dma_set_peripheral_size(DMA1, DMA_CHANNEL1, DMA_CCR_PSIZE_32BIT); dma_set_memory_size(DMA1, DMA_CHANNEL1, DMA_CCR_MSIZE_32BIT); dma_set_priority(DMA1, DMA_CHANNEL1, DMA_CCR_PL_VERY_HIGH); dma_enable_half_transfer_interrupt(DMA1, DMA_CHANNEL1); dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL1); dma_enable_transfer_error_interrupt(DMA1, DMA_CHANNEL1); dma_enable_channel(DMA1, DMA_CHANNEL1); /* Configure interrupts in NVIC. */ nvic_set_priority(NVIC_DMA1_CHANNEL1_IRQ, 0); nvic_enable_irq(NVIC_DMA1_CHANNEL1_IRQ); /* Disable ADC's. */ adc_off(ADC1); adc_off(ADC2); /* Enable dualmode. */ adc_set_dual_mode(ADC_CR1_DUALMOD_RSM); /* Dualmode regular only. */ /* Configure the adc channels. */ adc_config(ADC1, adc1_channel_array); adc_config(ADC2, adc2_channel_array); /* Start converting. */ adc_start_conversion_regular(ADC1); }