Base Lab 4 Donw

This commit is contained in:
Devoalda 2023-09-17 22:48:14 +08:00
parent fb5cef67e5
commit f67b58493c
2 changed files with 121 additions and 0 deletions

49
lab_4/CMakeLists.txt Normal file
View File

@ -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)

72
lab_4/lab_4.c Normal file
View File

@ -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 <stdio.h>
#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();
}
}