73 lines
1.6 KiB
C
73 lines
1.6 KiB
C
|
|
/**
|
|
* 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();
|
|
}
|
|
}
|