Initial Release
This commit is contained in:
5
adc/CMakeLists.txt
Normal file
5
adc/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
if (NOT PICO_NO_HARDWARE)
|
||||
add_subdirectory(adc_console)
|
||||
add_subdirectory(hello_adc)
|
||||
add_subdirectory(joystick_display)
|
||||
endif ()
|
||||
12
adc/adc_console/CMakeLists.txt
Normal file
12
adc/adc_console/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
add_executable(adc_console
|
||||
adc_console.c
|
||||
)
|
||||
|
||||
target_link_libraries(adc_console pico_stdlib hardware_adc)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(adc_console)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(adc_console)
|
||||
|
||||
100
adc/adc_console/adc_console.c
Normal file
100
adc/adc_console/adc_console.c
Normal file
@@ -0,0 +1,100 @@
|
||||
#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 (int 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
adc/hello_adc/CMakeLists.txt
Normal file
11
adc/hello_adc/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
add_executable(hello_adc
|
||||
hello_adc.c
|
||||
)
|
||||
|
||||
target_link_libraries(hello_adc pico_stdlib hardware_adc)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(hello_adc)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(hello_adc)
|
||||
30
adc/hello_adc/hello_adc.c
Normal file
30
adc/hello_adc/hello_adc.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/gpio.h"
|
||||
#include "hardware/adc.h"
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
printf("ADC Example, measuring GPIO26\n");
|
||||
|
||||
adc_init();
|
||||
|
||||
// Make sure GPIO is high-impedance, no pullups etc
|
||||
adc_gpio_init(26);
|
||||
// Select ADC input 0 (GPIO26)
|
||||
adc_select_input(0);
|
||||
|
||||
while (1) {
|
||||
// 12-bit conversion, assume max value == ADC_VREF == 3.3 V
|
||||
const float conversion_factor = 3.3f / (1 << 12);
|
||||
uint16_t result = adc_read();
|
||||
printf("Raw value: 0x%03x, voltage: %f V\n", result, result * conversion_factor);
|
||||
sleep_ms(500);
|
||||
}
|
||||
}
|
||||
11
adc/joystick_display/CMakeLists.txt
Normal file
11
adc/joystick_display/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
add_executable(joystick_display
|
||||
joystick_display.c
|
||||
)
|
||||
|
||||
target_link_libraries(joystick_display pico_stdlib hardware_adc)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(joystick_display)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(joystick_display)
|
||||
40
adc/joystick_display/joystick_display.c
Normal file
40
adc/joystick_display/joystick_display.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/adc.h"
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
adc_init();
|
||||
// Make sure GPIO is high-impedance, no pullups etc
|
||||
adc_gpio_init(26);
|
||||
adc_gpio_init(27);
|
||||
|
||||
while (1) {
|
||||
adc_select_input(0);
|
||||
uint adc_x_raw = adc_read();
|
||||
adc_select_input(1);
|
||||
uint adc_y_raw = adc_read();
|
||||
|
||||
// Display the joystick position something like this:
|
||||
// X: [ o ] Y: [ o ]
|
||||
const uint bar_width = 40;
|
||||
const uint adc_max = (1 << 12) - 1;
|
||||
uint bar_x_pos = adc_x_raw * bar_width / adc_max;
|
||||
uint bar_y_pos = adc_y_raw * bar_width / adc_max;
|
||||
printf("\rX: [");
|
||||
for (int i = 0; i < bar_width; ++i)
|
||||
putchar( i == bar_x_pos ? 'o' : ' ');
|
||||
printf("] Y: [");
|
||||
for (int i = 0; i < bar_width; ++i)
|
||||
putchar( i == bar_y_pos ? 'o' : ' ');
|
||||
printf("]");
|
||||
sleep_ms(50);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user