[McHUG] Ascii2Morse and the Hamfest
Rich Mitchell
geobra at att.net
Tue Oct 21 08:51:36 EDT 2008
McHUGgers,
The video splitter came in yesterday but I haven't had chance to play with it. I am including the sketch code for Pete's Ascii to Morse program below. Later I'll put the library code on the reflector. If you copy and paste the code into a sketch it should work. I added a couple more comments since I last tested it, but hopefully did it correctly.
Pat, if you are going to be at the board meeting, maybe you could put the PICetSat museum piece in the club station so we can use it Sunday. Steve, if you haven't finished making the give away CD perhaps I can send you the Library code for Ascii2Morse to add to it.
Rich, N3III
//sketch code below this line ========================
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* Ascii2Morse Pete's Ascii to Morse program
* translated into Arduino. The key to the program
* is the look up table. The program looks at it as
* binary. For example 0x05 = 00000101. The rule is:
* The first 1 says now we start. After that each 1
* is a dash and each 0 is a dot. So our example
* would be dot dash or A. 0x3F = 00111111 or 5 dashes,
* the number 0.
* How do you find the right code in the table? The ascii
* code is a number. The A is decimal 65. Pete's table
* starts with decimal 32, so subtract 31 from the ascii code,
* in this case 65 - 31 = 34. Count 34 into the table and you
* come to the value 0x05, or 00000101 - dot, dash
* This sketch only sends Hello world, but when we turn
* it into a library it will send whatever we ask it to.
* * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* Declaration section
* * * * * * * * * * * * * * * * * * * * * * * * * */
// Ascii to Morse lookup table
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
};
int pin = 13; // pin that will flash the Morse Code
int time = 300; // milliseconds for a dit
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* Setup section
* * * * * * * * * * * * * * * * * * * * * * * * * */
void setup() {
pinMode(pin, OUTPUT); // set pin to output
}
/* * * * * * * * * * * * * * * * * * * * * * * * * *
* Loop section
* * * * * * * * * * * * * * * * * * * * * * * * * */
void loop () {
processString ("Hello world", 11); // calls the control routine
// with string and length. Keeping this simple helps when
// you convert it into a library.
}
// Control routine
void processString (char s[], int len) {
int i; // pointer through the string
unsigned char c; // hold hex that will be turned into dots and dashes
for (i = 0; i < len; i++) { // go through each ascii character
c = getMorseCode(s[i]); // convert ascii to morse as hex
showCode(c); // cause the LED to flash
}
}
// translate routine
unsigned char getMorseCode(char ascii) {
unsigned char morseCode = 0x80; // untranslated characters
if (ascii >= 97 && ascii <= 122) { // if ascii is lower case
ascii -= 32; // translate to uppercase
}
if (ascii >= 32 && ascii <= 95) { // is ascii translatable??
morseCode = MorseTable[ascii - 32]; // then go translate it
}
return morseCode; // hex representation of morse code
}
// display routine
void showCode (unsigned char morseCode) {
unsigned char mask;
int bitPos; // where we are in the hex character
int bitOn; // is it a 1 or a 0?
int bitSend = 0; // indicates the first 1 not reached
for (bitPos = 7; bitPos >= 0; bitPos --) { // go thru all 8 bits
mask = (1 << bitPos); // set mask so bit position is 1, rest are 0
bitOn = morseCode & mask; // bit on or off??
if (bitSend) { // have we passed the first 1?
if (bitOn) { // yes, then a 1 must be a dash
doDash();
} else { // yes, then a 0 must be a dot
doDot();
}
}
if (bitOn) { // have we reached a 1?
bitSend = 1; // then indicate we have, since we never turn it off
// only first 1 counts.
}
}
doSpace(); // need a space at the end of the letter
}
void doDash() {
digitalWrite(pin, HIGH); // turn LED on
delay(time * 3); // wait for dash - three counts
digitalWrite(pin, LOW); // turn LED off
delay(time); // wait for letter space - one count
}
void doDot() {
digitalWrite(pin, HIGH); // turn LED on
delay(time); // wait for dot - one count
digitalWrite(pin, LOW); // turn LED off
delay(time); // wait for letter space - one count
}
void doSpace() {
delay(time * 3); // wait for word space - three count
}
// code ends here
More information about the McHUG
mailing list