#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include <cstdio>
const uint pwm_pin_out = 0; // GPIO pin for sending PWM signal
const uint pwm_pin_in = 1; // GPIO pin for receiving PWM signal
int main() {
stdio_init_all();
// Initialize PWM for sending
pwm_init(0, 0, true);
pwm_set_wrap(pwm_pin_out, 65535);
// Initialize PWM for receiving
pwm_config config = pwm_get_default_config();
pwm_config_set_wrap(&config, 65535);
pwm_init(0, &config, true);
while (1) {
// Send PWM signal with 50% duty cycle
pwm_set_chan_level(pwm_pin_out, 0, 32768); // 50% duty cycle (32768 out of 65535)
// Read PWM signal
uint32_t pulse_width = pwm_get_counter_mask_blocking(0);
uint32_t wrap = pwm_get_wrap(0);
// Calculate duty cycle
float duty_cycle = (float)pulse_width / (float)wrap;
// Print duty cycle and pulse width
printf("Duty Cycle: %.2f%%\n", duty_cycle * 100);
printf("Pulse Width: %lu\n", pulse_width);
// Pause before next cycle
sleep_ms(1000);
}
return 0;
}