From f67b58493c2486a1229fb94de533ce79ad4bd7d4 Mon Sep 17 00:00:00 2001 From: Devoalda Date: Sun, 17 Sep 2023 22:48:14 +0800 Subject: [PATCH] Base Lab 4 Donw --- lab_4/CMakeLists.txt | 49 ++++++++++++++++++++++++++++++ lab_4/lab_4.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 lab_4/CMakeLists.txt create mode 100644 lab_4/lab_4.c diff --git a/lab_4/CMakeLists.txt b/lab_4/CMakeLists.txt new file mode 100644 index 0000000..4bdb304 --- /dev/null +++ b/lab_4/CMakeLists.txt @@ -0,0 +1,49 @@ +# Generated Cmake Pico project file + +cmake_minimum_required(VERSION 3.13) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) + +set(PICO_BOARD pico_w CACHE STRING "Board type") + +# Pull in Raspberry Pi Pico SDK (must be before project) +include(pico_sdk_import.cmake) + +if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0") + message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}") +endif() + +project(lab_4 C CXX ASM) + +# Initialise the Raspberry Pi Pico SDK +pico_sdk_init() + +# Add executable. Default name is the project name, version 0.1 + +add_executable(lab_4 lab_4.c ) + +pico_set_program_name(lab_4 "lab_4") +pico_set_program_version(lab_4 "0.1") + +pico_enable_stdio_uart(lab_4 1) +pico_enable_stdio_usb(lab_4 1) + +# Add the standard library to the build +target_link_libraries(lab_4 + pico_stdlib) + +# Add the standard include files to the build +target_include_directories(lab_4 PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required +) + +# Add any user requested libraries +target_link_libraries(lab_4 + hardware_pwm + hardware_adc + ) + +pico_add_extra_outputs(lab_4) + diff --git a/lab_4/lab_4.c b/lab_4/lab_4.c new file mode 100644 index 0000000..52f3f30 --- /dev/null +++ b/lab_4/lab_4.c @@ -0,0 +1,72 @@ + +/** + * To configure a PWM signal at 20 Hz with a 50% duty cycle + * on GP2 and feed it into an ADC at GP26 while + * sampling the ADC every 25 ms, + * you must use the Raspberry Pi Pico and its Pico C SDK. + * + * You will need to use a jumper wire to connect GP2 to GP26. + * You may use a timer interrupt. + */ + +#include +#include "pico/stdlib.h" +#include "hardware/pwm.h" +#include "hardware/adc.h" + +#define PWM_PIN 2 +#define PWM_FREQ 20 +#define PWM_DUTY 0.5 + +#define ADC_PIN 26 +#define ADC_FREQ 25 + +void setup () { + gpio_set_function(PWM_PIN, GPIO_FUNC_PWM); + uint slice_num = pwm_gpio_to_slice_num(PWM_PIN); + + pwm_set_wrap(slice_num, 100); + // Setup PWM with 20 Hz frequency and 50% duty cycle + pwm_set_chan_level(slice_num, PWM_CHAN_A, PWM_DUTY * 100); + pwm_set_enabled(slice_num, true); + pwm_set_clkdiv(slice_num, 1); + + // Set up ADC + adc_init(); + adc_gpio_init(ADC_PIN); + adc_select_input(0); +} + +bool repeating_timer_callback (struct repeating_timer *t) { + // Read ADC value + uint16_t result = adc_read(); + uint64_t time = time_us_64(); + printf("%02d:%02d:%02d:%03d -> ADC value: %d\n", + (int) (time / 3600000000), + (int) (time / 60000000) % 60, + (int) (time / 1000000) % 60, + (int) (time / 1000) % 1000, + result + ); + + return true; +} + +int main () { + stdio_init_all(); + setup(); + + struct repeating_timer timer; + + // Setup timer interrupt + add_repeating_timer_ms(ADC_FREQ, + repeating_timer_callback, + NULL, + &timer + ); + + while (true) + { + tight_loop_contents(); + } +}