Morse Code Communication Device

Device for Hearing Impaired using LPC1768

Karthik M Dani
Darshan S K

Introduction

In this presentation, we’ll walk through a C program designed for the LPC17xx microcontroller. The program converts a given input string to its Morse code equivalent and blinks an LED to represent the dots and dashes of Morse code.

Includes and Definitions

  • #include "LPC17xx.h": Includes the header file for the LPC17xx microcontroller series.
  • #define statements: Define constants for Morse code timing:
    • DOT_DURATION: Duration of a dot.
    • DASH_DURATION: Duration of a dash.
    • SYMBOL_GAP: Gap between symbols (dots and dashes).
    • CHAR_GAP: Gap between characters.
#include "LPC17xx.h"

#define DOT_DURATION 200   // Duration of a dot in milliseconds
#define DASH_DURATION 600  // Duration of a dash in milliseconds
#define SYMBOL_GAP 200     // Gap between symbols in milliseconds
#define CHAR_GAP 600       // Gap between characters in milliseconds

Morse Code Mapping Array

  • morse_code array: Holds Morse code strings for characters A-Z and 0-9.
const char* morse_code[] = {
    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",  // A-J
    "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",    // K-T
    "..-", "...-", ".--", "-..-", "-.--", "--..",                         // U-Z
    "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."  // 0-9
};

GPIO Initialization and LED Control Functions

GPIO Initialization

  • GPIO_Init(): Configures P2.0 as an output pin for the LED.
void GPIO_Init() {
    LPC_GPIO2->FIODIR |= (1 << 0);  // Set P2.0 as output (assuming LED is connected to P2.0)
}

LED Control Functions

  • LED_On(): Turns the LED on by setting P2.0 high.
  • LED_Off(): Turns the LED off by clearing P2.0.
void LED_On() {
    LPC_GPIO2->FIOSET = (1 << 0);
}

void LED_Off() {
    LPC_GPIO2->FIOCLR = (1 << 0);
}

Delay Function

  • delay_ms(uint32_t ms): Creates a delay of the specified number of milliseconds using a busy-wait loop.
void delay_ms(uint32_t ms) {
    uint32_t count = SystemCoreClock / 1000 * ms;
    while (count--) {
        __NOP();
    }
}

Morse Code Output Function

  • output_morse_code(const char* code): Outputs Morse code by blinking the LED:
    • Blinks for dots (.) and dashes (-).
    • Turns the LED off and waits for SYMBOL_GAP between symbols.
void output_morse_code(const char* code) {
    while (*code) {
        if (*code == '.') {
            LED_On();
            delay_ms(DOT_DURATION);
        } else if (*code == '-') {
            LED_On();
            delay_ms(DASH_DURATION);
        }
        LED_Off();
        delay_ms(SYMBOL_GAP);
        code++;
    }
}

Morse Code Lookup Function

  • get_morse_code(char c): Returns the Morse code string for a given character i,e.. uppercase letters (A-Z) and digits (0-9).
const char* get_morse_code(char c) {
    if (c >= 'A' && c <= 'Z') {
        return morse_code[c - 'A'];
    } else if (c >= '0' && c <= '9') {
        return morse_code[c - '0' + 26];
    }
    return "";
}

Main Function

Variable Declarations

  • Variables:
    • input[]: Example input string.
    • char* p: Pointer to iterate over the input string.
int main() {
    char input[] = "HELLO WORLD 123";  // Example input
    char* p;

System and GPIO Initialization

  • SystemInit(): Initializes the system.
  • GPIO_Init(): Initializes GPIO for the LED.
    SystemInit();
    GPIO_Init();

Morse Code Conversion Loop

  • Conversion Loop: Iterates over each character in the input string:
    • Handles spaces with CHAR_GAP.
    • Converts and outputs Morse code for each character using get_morse_code() and output_morse_code().
    for (p = input; *p; p++) {
        if (*p == ' ') {
            delay_ms(CHAR_GAP);  // Gap between words
        } else {
            const char* code = get_morse_code(*p);
            output_morse_code(code);
            delay_ms(CHAR_GAP);  // Gap between characters
        }
    }

Thank you!