Skip to content
Snippets Groups Projects
Commit cfd4c0dd authored by cf2g21's avatar cf2g21
Browse files

Upload main.c

parent f9608e72
Branches
No related tags found
No related merge requests found
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "hardware/clocks.h"
const int FIRST_LED_PIN = 0;
const int LAST_LED_PIN = 28;
const int BUZZER_PIN = 18;
void sound_buzzer() {
gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(BUZZER_PIN);
uint16_t pwm_count = pwm_gpio_to_channel(BUZZER_PIN);
uint frequency = 200;
float divider = 1.0f;
uint32_t sys_clk_freq = clock_get_hz(clk_sys);
uint32_t wrap_value = sys_clk_freq / (divider * frequency);
while (wrap_value > 65535) {
divider *= 2.0f;
wrap_value = sys_clk_freq / (divider * frequency);
}
pwm_set_clkdiv(slice_num, divider);
pwm_set_wrap(slice_num, wrap_value);
uint32_t dutyCycle = wrap_value / 2;
pwm_set_chan_level(slice_num, pwm_count, dutyCycle);
pwm_set_enabled(slice_num, true);
sleep_ms(200); // Sound the buzzer for 200 milliseconds
pwm_set_enabled(slice_num, false);
}
bool valid_led(int pin) {
if (pin == 23 || pin == 24 || pin == 25) {
return false;
} else {
return true;
}
}
int main() {
// Use for debugging
stdio_init_all();
// Initialise LED pins as output
for (int pin = FIRST_LED_PIN; pin <= LAST_LED_PIN; pin++) {
if (valid_led(pin)) {
gpio_init(pin);
gpio_set_dir(pin, GPIO_OUT);
}
}
// Turn on all LEDs
for (int pin = FIRST_LED_PIN; pin <= LAST_LED_PIN; pin++) {
if (valid_led(pin)) {
gpio_put(pin, 1);
}
}
sleep_ms(1000);
// Turn off each LED one by one each second
for (int pin = FIRST_LED_PIN; pin <= LAST_LED_PIN; pin++) {
if (valid_led(pin)) {
gpio_put(pin, 0);
sleep_ms(1000);
}
}
sound_buzzer();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment