Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include "pico/stdlib.h"
- #include "hardware/pwm.h"
- #include <string.h>
- #ifndef PICO_DEFAULT_LED_PIN
- #warning blink example requires a board with a regular LED
- #else
- #define LED 25
- #define SND 15
- #define BASE 100
- //above is base speed of single . unit of morse in milliseconds
- void blink_dit() {
- gpio_put(LED, 1);
- pwm_set_gpio_level(SND, 15000);
- sleep_ms(BASE);
- gpio_put(LED, 0);
- pwm_set_gpio_level(SND, 0);
- sleep_ms(BASE);
- return;
- }
- void blink_dat() {
- gpio_put(LED, 1);
- pwm_set_gpio_level(SND, 15000);
- sleep_ms(BASE * 3);
- gpio_put(LED, 0);
- pwm_set_gpio_level(SND, 0);
- sleep_ms(BASE);
- return;
- }
- void output(char *a) {
- int i = 0;
- while (a[i] != '\0') {
- if (a[i] == '.') blink_dit();
- if (a[i] == '-') blink_dat();
- if (a[i] == 'S') sleep_ms(BASE); //represents silence one base unit long, or one .
- i++;
- }
- sleep_ms(BASE * 3);
- return;
- }
- void morse(char *a) {
- int i = 0, j = strlen(a);
- for (i = 0; i != j; i++) {
- if (a[i] == 'A' || a[i] == 'a') output(".-");
- if (a[i] == 'B' || a[i] == 'b') output("-...");
- if (a[i] == 'C' || a[i] == 'c') output("-.-.");
- if (a[i] == 'D' || a[i] == 'd') output("-..");
- if (a[i] == 'E' || a[i] == 'e') output(".");
- if (a[i] == 'F' || a[i] == 'f') output("..-.");
- if (a[i] == 'G' || a[i] == 'g') output("--.");
- if (a[i] == 'H' || a[i] == 'h') output("....");
- if (a[i] == 'I' || a[i] == 'i') output("..");
- if (a[i] == 'J' || a[i] == 'j') output(".---");
- if (a[i] == 'K' || a[i] == 'k') output("-.-");
- if (a[i] == 'L' || a[i] == 'l') output(".-..");
- if (a[i] == 'M' || a[i] == 'm') output("--");
- if (a[i] == 'N' || a[i] == 'n') output("-.");
- if (a[i] == 'O' || a[i] == 'o') output("---");
- if (a[i] == 'P' || a[i] == 'p') output(".--.");
- if (a[i] == 'Q' || a[i] == 'q') output("--.-");
- if (a[i] == 'R' || a[i] == 'r') output(".-.");
- if (a[i] == 'S' || a[i] == 's') output("...");
- if (a[i] == 'T' || a[i] == 't') output("-");
- if (a[i] == 'U' || a[i] == 'u') output("..-");
- if (a[i] == 'V' || a[i] == 'v') output("...-");
- if (a[i] == 'W' || a[i] == 'w') output(".--");
- if (a[i] == 'X' || a[i] == 'x') output("-..-");
- if (a[i] == 'Y' || a[i] == 'y') output("-.--");
- if (a[i] == 'Z' || a[i] == 'z') output("--..");
- if (a[i] == '0') output("-----");
- if (a[i] == '1') output(".----");
- if (a[i] == '2') output("..---");
- if (a[i] == '3') output("...--");
- if (a[i] == '4') output("....-");
- if (a[i] == '5') output(".....");
- if (a[i] == '6') output("-....");
- if (a[i] == '7') output("--...");
- if (a[i] == '8') output("---..");
- if (a[i] == '9') output("----.");
- if (a[i] == '.') output(".-.-.-SSSSSSS");
- if (a[i] == '?') output("..--..SSSSSSS");
- if (a[i] == ' ') output("SSS");
- }
- return;
- }
- int main() {
- int i = 0;
- uint slice_snd;
- sleep_ms(5000);
- gpio_init(LED);
- gpio_init(SND);
- gpio_set_function(SND, GPIO_FUNC_PWM);
- gpio_set_dir(LED, GPIO_OUT);
- pwm_config config = pwm_get_default_config();
- slice_snd = pwm_gpio_to_slice_num(SND);
- pwm_init(slice_snd, &config, true);
- morse("HAVING FUN WITH PULSE WIDTH MODULATION");
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment