FlashMyPico.com
log in about discover docs scratchpad
This website requires a Chrome-based browser in order to access the WebUSB API. Without it, you may not be able to flash your device.

adc_console
for Raspberry Pi Picoβ„’ 2, submitted by flashmypico

Loading...

#include <stdio.h> #include "pico/stdlib.h" #include "hardware/gpio.h" #include "hardware/adc.h" #define N_SAMPLES 1000 uint16_t sample_buf[N_SAMPLES]; void printhelp() { puts("\nCommands:"); puts("c0, ...\t: Select ADC channel n"); puts("s\t: Sample once"); puts("S\t: Sample many"); puts("w\t: Wiggle pins"); } void __not_in_flash_func(adc_capture)(uint16_t *buf, size_t count) { adc_fifo_setup(true, false, 0, false, false); adc_run(true); for (size_t i = 0; i < count; i = i + 1) buf[i] = adc_fifo_get_blocking(); adc_run(false); adc_fifo_drain(); } int main(void) { stdio_init_all(); adc_init(); adc_set_temp_sensor_enabled(true); // Set all pins to input (as far as SIO is concerned) gpio_set_dir_all_bits(0); for (int i = 2; i < 30; ++i) { gpio_set_function(i, GPIO_FUNC_SIO); if (i >= 26) { gpio_disable_pulls(i); gpio_set_input_enabled(i, false); } } printf("\n===========================\n"); printf("RP2040 ADC and Test Console\n"); printf("===========================\n"); printhelp(); while (1) { char c = getchar(); printf("%c", c); switch (c) { case 'c': c = getchar(); printf("%c\n", c); if (c < '0' || c > '7') { printf("Unknown input channel\n"); printhelp(); } else { adc_select_input(c - '0'); printf("Switched to channel %c\n", c); } break; case 's': { uint32_t result = adc_read(); const float conversion_factor = 3.3f / (1 << 12); printf("\n0x%03x -> %f V\n", result, result * conversion_factor); break; } case 'S': { printf("\nStarting capture\n"); adc_capture(sample_buf, N_SAMPLES); printf("Done\n"); for (int i = 0; i < N_SAMPLES; i = i + 1) printf("%03x\n", sample_buf[i]); break; } case 'w': { printf("\nPress any key to stop wiggling\n"); int i = 1; gpio_set_dir_all_bits(-1); while (getchar_timeout_us(0) == PICO_ERROR_TIMEOUT) { // Pattern: Flash all pins for a cycle, // Then scan along pins for one cycle each i = i ? i << 1 : 1; gpio_put_all(i ? i : ~0); } gpio_set_dir_all_bits(0); printf("Wiggling halted.\n"); break; } case '\n': case '\r': break; case 'h': printhelp(); break; default: printf("\nUnrecognised command: %c\n", c); printhelp(); break; } } }
This firmware image was imported from the pico-examples repository. 


Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
   disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Homepage: https://github.com/raspberrypi/pico-examples
Repository: https://github.com/raspberrypi/pico-examples/tree/master/adc/adc_console

β“˜ Tips