Skip to content

Month: April 2016

morse code in c

Well, its been a long time since I last wrote a blog post. I’ve set up a new site and moved over a few old posts, but I can’t seem to find the time to write up anything I’ve been working on.

A few weeks ago I passed my Technician, General, and Amateur Extra exams, so I am now officially a licensed amateur radio operator. I still don’t have a radio, but I did pick up two of the CW Pixie kits and built a 40m dipole.

 

pixie

Now that I can work with CW, I was thinking about using an arduino to encode text and transmit morse code and decode the tones coming in from the pixie. To get started, I knocked up a c program that converts the input arguments to morse code. For example:

./morse This is a test

(T) - (h) .... (i) .. (s) ... (i) .. (s) ... (a) .- (t) - (e) . (s) ... (t) -

With that working, I can start porting it over to the arduino and building out the rest of the code.


#include <stdio.h>
#include <string.h>

void decode(char c);
void dit(void);
void dah(void);
void space(void);

int main(int argc, char *argv[]) {
    if (argc < 1 ) {
        return 0;
    }

    for (int i = 1; i < argc; i++) {
        for (int j = 0; j < strlen(argv[i]); j++) {
            printf(" (%c) ", argv[i][j]);
            decode(argv[i][j]);
        }
    }
    printf("\n");
    return 0;
}


void decode(char c) {    
    switch(c) {
        case 'a': // .-
        case 'A':
            dit(); dah(); space();
            break;
        case 'b': // -...
        case 'B':
            dah(); dit(); dit(); dit(); space();
            break;
        case 'c': // -.-.
        case 'C':
            dah(); dit(); dah(); dit(); space();
            break;
        case 'd': // -..
        case 'D':
            dah(); dit(); dit(); space();
            break;
        case 'e': // .
        case 'E':
            dit(); space();
            break;
        case 'f': // ..-.
        case 'F':
            dit(); dit(); dah(); dit(); space();
            break;
        case 'g': // --.
        case 'G':
            dah(); dah(); dit(); space();
            break;
        case 'h': // ....
        case 'H':
            dit(); dit(); dit(); dit(); space();
            break;
        case 'i': // ..
        case 'I':
            dit(); dit(); space();
            break;
        case 'j': // .---
        case 'J':
            dit(); dah(); dah(); dah(); space();
            break;
        case 'k': // -.-
        case 'K':
            dah(); dit(); dah(); space();
            break;
        case 'l': // .-..
        case 'L':
            dit(); dah(); dit(); dit(); space();
            break;
        case 'm': // --
        case 'M':
            dah(); dah(); space();
            break;
        case 'n': // -.
        case 'N':
            dah(); dit(); space();
            break;
        case 'o': // ---
        case 'O':
            dah(); dah(); dah(); space();
            break;
        case 'p': // .--.
        case 'P':
            dit(); dah(); dah(); dit(); space();
            break;
        case 'q': // --.-
        case 'Q':
            dah(); dah(); dit(); dah(); space();
            break;
        case 'r': // .-.
        case 'R':
            dit(); dah(); dit(); space();
            break;
        case 's': // ...
        case 'S':
            dit(); dit(); dit(); space();
            break;
        case 't': // -
        case 'T':
            dah(); space();
            break;
        case 'u': // ..-
        case 'U':
            dit(); dit(); dah(); space();
            break;
        case 'v': // ...-
        case 'V':
            dit(); dit(); dit(); dah(); space();
            break;
        case 'w': // .--
        case 'W':
            dit(); dah(); dah(); space();
            break;
        case 'x': // -..-
        case 'X':
            dah(); dit(); dit(); dah(); space();
            break;
        case 'y': // -.--
        case 'Y':
            dah(); dit(); dah(); dah(); space();
            break;
        case 'z': // --..
        case 'Z':
            dah(); dah(); dit(); dit(); space();
            break;
        case '0': // -----
            dah(); dah(); dah(); dah(); dah(); space();
            break;
        case '1': // .----
            dit(); dah(); dah(); dah(); dah(); space();
            break;
        case '2': // ..---
            dit(); dit(); dah(); dah(); dah(); space();
            break;
        case '3': // ...--
            dit(); dit(); dit(); dah(); dah(); space();
            break;
        case '4': // ....-
            dit(); dit(); dit(); dit(); dah(); space();
            break;
        case '5': // .....
            dit(); dit(); dit(); dit(); dit(); space();
            break;
        case '6': // -....
            dah(); dit(); dit(); dit(); dit(); space();
            break;
        case '7': // --...
            dah(); dah(); dit(); dit(); dit(); space();
            break;
        case '8': // ---..
            dah(); dah(); dah(); dit(); dit(); space();
            break;
        case '9': // ----.
            dah(); dah(); dah(); dah(); dit(); space();
            break;
        case '.': // .-.-.-
            dit(); dah(); dit(); dah; dit(); dah(); space();
            break;
        case ',': // --..--
            dah(); dah(); dit(); dit(); dah(); dah(); space();
            break;
        case '?': // ..--..
            dit(); dit(); dah(); dah(); dit(); dit(); space();
            break;
        case '!': // ..--.
            dit(); dit(); dah(); dah(); dit(); space();
            break;
        case ':': // ---...
            dah(); dah(); dah(); dit(); dit(); dit(); space();
            break;
        case '"': // .-..-.
            dit(); dah(); dit(); dit(); dah(); dit(); space();
            break;
        case '\'': // .----.
            dit(); dah(); dah(); dah(); dah(); dit(); space();
            break;
        case '=': // -...-
            dah(); dit(); dit(); dit(); dah(); space();
            break;
        case ' ': 
            space(); space(); space(); space();
            break;
        default:
            break;
    }
}

void dit(void) {
    printf("%c", '.');
}

void dah(void) {
    printf("%c", '-');
}

void space(void) {
    printf("  ");
}

 
Comments closed