[McHUG] Morse Generator in C for PC
Rich Mitchell
geobra at att.net
Sat Mar 8 15:31:04 EST 2008
Here is the PC version of Pete's Morse generator. I did the table in hex manually so may have screwed up some characters. But I can vouch for the characters in "Hello World" hi hi. I used a mask to check the bits retrieved from the table. I ran it in Visual Studio so just printed the character and the dits and dahs out to the screen. Hope to use similar code in the Butterfly but with the do...s calling my piezo service.
Rich
// Ascii2Morse
#include <stdio.h>
#include <string.h>
unsigned char MorseTable[] = {
0x01, 0x6B, 0x52, 0x80, 0x89, 0x80, 0x28, 0x5E, 0x26, 0x6D,
0x45, 0x2A, 0x73, 0x61, 0x55, 0x32,
0x3F, 0x2F, 0x27, 0x23, 0x21, 0x20, 0x30, 0x38, 0x3C, 0x3E,
0x78, 0x6A, 0x80, 0x31, 0x80, 0x4C, 0x5A,
0x05, 0x18, 0x1A, 0x0C, 0x02, 0x12, 0x0E, 0x10, 0x04, 0x17,
0x0D, 0x14, 0x07, 0x06, 0x0F, 0x16, 0x1D, 0x0A, 0x08, 0x03,
0x09, 0x11, 0x0B, 0x19, 0x1B, 0x1C,
0x80, 0x80, 0x80, 0x80, 0x4D
};
void processString (char[], int);
unsigned char getMorseCode(char);
void showCode (unsigned char);
void doDash(void);
void doDot(void);
void doSpace(void);
int main (void) {
processString ("Hello world", 11);
return 0;
}
void processString (char s[], int len) {
int i;
unsigned char c;
printf ("The string is: %s \n", s);
for (i = 0; i < len; i++) {
c = getMorseCode(s[i]);
showCode(c);
}
}
unsigned char getMorseCode(char ascii) {
unsigned char morseCode = 0x80;
if (ascii >= 97 && ascii <= 122) {
ascii -= 32;
}
printf (" %c ", ascii);
if (ascii >= 32 && ascii <= 95) {
morseCode = MorseTable[ascii - 32];
}
return morseCode;
}
void showCode (unsigned char morseCode) {
unsigned char mask;
int bitPos;
int bitOn;
int bitSend = 0;
for (bitPos = 7; bitPos >= 0; bitPos --) {
mask = (1 << bitPos);
bitOn = morseCode & mask;
if (bitSend) {
if (bitOn) {
doDash();
} else {
doDot();
}
}
if (bitOn) {
bitSend = 1;
}
}
doSpace();
}
void doDash() {
printf("dah ");
}
void doDot() {
printf("dit ");
}
void doSpace() {
printf(" \n");
}
--
McHUG - Physical Computing ;)
MicroController Ham User Group
More information about the McHUG
mailing list