In electric circuit theory lab, we were assigned one of several projects using a
TI MSP430 microcontroller. I chose to make a simple Morse code generator. I programmed the microcontroller code using
Energia, which allows you to program the TI as if it were an Arduino.
This simplifies programming immensely, with functions for outputting square waves and blinking the onboard LED. In hindsight, if I were to re-do this project, I would use the
millis()function instead of
delay() to set the timing.
Components
 |
| Morse code generator schematic |
- TI Launchpad w/MSP430G2
- Piezo buzzer
Show Code
int const DOT = 100;
int const DASH = DOT*3;
uint const FREQ = 500;
char const DOT_CHAR = '0';
int const PIN = 8;
// Morse code message to transmit
String message = "Your message here";
void setup() {
Serial.begin(9600);
// Convert to lower case for easy operation
message.toLowerCase();
// Initialize onboard LED
pinMode(RED_LED, OUTPUT);
}
void loop() {
// Delay to allow Log to open
delay(2000);
// Iterate through message string and convert each character to Morse code
for (int i = 0; i < message.length(); i++) {
toMorseCode(message.charAt(i));
}
// Wait 5 seconds before repeating message
delay(5000);
Serial.println("");
}
void toMorseCode(char c) {
String sequence;
switch(c) {
case 'a':
sequence = "01";
break;
case 'b':
sequence = "1000";
break;
case 'c':
sequence = "1010";
break;
case 'd':
sequence = "100";
break;
case 'e':
sequence = "0";
break;
case 'f':
sequence = "0010";
break;
case 'g':
sequence = "110";
break;
case 'h':
sequence = "0000";
break;
case 'i':
sequence = "00";
break;
case 'j':
sequence = "0111";
break;
case 'k':
sequence = "101";
break;
case 'l':
sequence = "0100";
break;
case 'm':
sequence = "11";
break;
case 'n':
sequence = "10";
break;
case 'o':
sequence = "111";
break;
case 'p':
sequence = "0110";
break;
case 'q':
sequence = "1101";
break;
case 'r':
sequence = "010";
break;
case 's':
sequence = "000";
break;
case 't':
sequence = "1";
break;
case 'u':
sequence = "001";
break;
case 'v':
sequence = "0001";
break;
case 'w':
sequence = "011";
break;
case 'x':
sequence = "1001";
break;
case 'y':
sequence = "1011";
break;
case 'z':
sequence = "1100";
break;
case ' ':
break;
}
// Write out current letter
Serial.print(c);
// Now, translate sequence to blinks and audio. Iterate through String sequence, where a "0" is a dot and a "1" is a dash.
for (int i = 0; i < sequence.length(); i++) {
if (sequence.charAt(i) == DOT_CHAR) {
blink(DOT);
} else {
blink(DASH);
}
// Pause between dots or dashes
delay(DASH);
}
// Delay between letters is equal to two dashes
delay(DASH*2);
}
void blink(int duration) {
digitalWrite(RED_LED, HIGH);
tone(PIN, FREQ);
delay(duration);
digitalWrite(RED_LED, LOW);
noTone(PIN);
}