Generating random and pre-recorded Morse code

A Microcontroller Version

Barely a month after the previous project ended, I was spending some time volunteering at the Temple, Texas, Railroad and Heritage museum.

Santa would be arriving by train shortly, so he needed an electronic elf to respond to messages for him while he was away from the North Pole (Figure 3). Unfortunately, I was fresh out of cassette tapes and MP3 players. Not wanting to receive coal in my stocking, I went to work on a microcontroller solution.

Figure 3: Telegraph exhibit at the Temple Railroad and Heritage Museum. A speed key, or "bug," sticks out of the traditional "straight" key. The paddle for a speed key is connected to a telegraph system by jamming it between the contacts of the straight key. Straight keys were provided by the railroad, a speed key had to be purchased personally.

The code in this version simply generates random dots, dashes, and spaces. Later, I will expand the code to play back pre-recorded messages and to transmit the characters received via a serial port.

Here I am using an ATtiny2313 CMOS 8-bit microcontroller [4] to drive the project. Atmel does sell even smaller chips, but this one was handy, and it has a UART (serial port), whereas the smaller ones do not. Figure 4 shows the circuit diagram.

Figure 4: The ATtiny2313 Morse driver. Note that the resistor-transistor-relay circuit is identical to the comparator version.

Microcontrollers

All microcontroller functions are controlled by registers. The majority of these registers are simply single-bit flags packed into 8-bit values as a single character. Other registers are set by you and then cleared when a process within the chip completes.

This project uses three registers, DDRB, the GPIO direction register for port B, PORTB, the output control register for port B, and PINB, the input register for port B. These registers are duplicated for Port A, and on larger chips might extend all the way to F or G.

A chip's special features are also mapped to these same GPIO pins. More registers dedicated to the feature switch the pin between GPIO and a timer, UART, ADC, or whatever peripheral you might be using.

In the sections that follow, I will supply a block-by-block breakdown of the entire program in the microcontroller (see also Listing 1).

Listing 1

morse.c

01 #include <inttypes.h>
02 #include <avr/io.h>
03 #include <avr/interrupt.h>
04 #include <avr/sleep.h>
05 #include <util/delay.h>
06
07 const int iDotLength = 75;
08
09 void dot()
10 {
11    PORTB |= 1;
12    _delay_ms ( iDotLength );
13    PORTB &= ~1;
14 }
15
16 void dash()
17 {
18    PORTB |= 1;
19    _delay_ms ( iDotLength );
20    _delay_ms ( iDotLength );
21    _delay_ms ( iDotLength );
22    PORTB &= ~1;
23 }
24
25 void space()
26 {
27    _delay_ms ( iDotLength );
28    _delay_ms ( iDotLength );
29    _delay_ms ( iDotLength );
30
31 }
32
33 int main()
34 {
35    volatile int iSymbol = 0;
36    volatile int iCount = 50;
37
38    DDRB = 1;
39    PORTB |= ( 1 << PB2 );
40
41    for ( ; ; )
42    {
43       iSymbol = rand() % 3;
44
45       if ( iCount != 0 )
46       {
47          switch ( iSymbol )
48          {
49             case 0:dot();break;
50             case 1:dash();break;
51             case 2:space();break;
52          }
53          _delay_ms ( iDotLength );
54          iCount -= 1;
55       }
56
57       if ( ( ( PINB >> PB2 ) & 1 ) == 0 )
58       {
59          iCount = 50;
60       }
61
62    }
63 }

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content