PIC16F84 Based Morse Code Reader

by Lawrence Foltzer, KE6UDL, photonsu@sonic.net

So you want to become a HAM, or you've got one of those no-code licences, but like me feel somewhat lacking,... not having obtaining that age old badge of proficiency that differientiated the HAM from other radio operators. However, like thousands and thousands of others, you have trouble learning the code. The problem for most people is the non-real time nature of the process, i.e., writing down the last character while listening to, and decoding the signature of the next character. Furthermore, when you make a mistake, the entire process collapses as your mind tries to perform error correction, trying to fill in the missing blanks, causing you to miss even more characters.

One way out of this delimma is to remove the burden of writing down the characters altogether during the process of building up your code speed. But to do this you need a device that copies and displays the code in parallel with you, which is what the stand-alone device described in this article is designed to perform.

The decoder is designed for code speeds ranging from about 6 words per minute (WPM) to greater than 36 WPM. The rate adaptive algorithm responds quickly to code speed changes, so you can copy both halves of a QSO, even when the parties transmit at different rates.

 

The Hardware

The schematic of the decoder is shown in figures 1a and 1b. It consists of four major pieces, all powered from a set of four (4) AA cells. The first piece, the front-end, is composed of an electret microphone and a common emitter transistor amplifier. This building block provides a wireless hookup to your radio receiver or code practice oscillator. The 15Kohm resistor biasing the electret may have to be changed to a different value, depending on the requirements of the electret you use. In addition to providing gain, the transistor amplifier also acts as a first level bandpass filter. Its band edges are determined by the size of the coupling capacitors, and the feedback capacitor between Q1's base and collector terminals.

Figure 1 b.

 


The second functional block is a narrowband PLL based tone detector, consisting of a tunable NE567 PLL tone decoder. There's nothing radical here, the circuit is right out of the manufacturers data sheet, and employs hysteresis for chatter prevention to, in effect, debounce the decoded signal. The small signal, narrowband detection capability of this block enables one to easily discriminate one signal from another, even when the signal you are copying is substantially smaller that the adjacent channel interference, as long as there is 100Hz or so frequency separation between them. The output of the detector is a one-zero pattern replicating the DIT-DAH sequence of the received signal. This output drives both an input to the PIC16F84 microcontroller and an LED which is used as a receiver tuning aid. More on that later.

The third functional block is the PIC16F84 microcontroller (CPU). Its function is to measure the duration of the one-zero input string from the tone decoder, and translate the pattern into DITs, DAHs, symbol spaces, character spaces, or word spaces. The CPU also perfoms input signal debounce, just in case the front-end missed something. This feature was one I found absolutely essential for the robust operation of the decoder under varying signal conditions.

The CPU also has the task of code speed adaptation, which it performs by performing a running average on the various components of the signal in real time. The symbol averages are then used to compute time threshold levels for correct symbol interpretation. As each of the symbols are received, a "code word" is assembled and used to lookup/convert to its ASCII equivalent character for display. The CPU also drives an LED in synchronism with the input. While this feature was used initially as a debug aid, it also serves as a tuning aid, and verifies that the CPU is receiving what the front-end sent. Finally, the CPU interfaces to the LCD line display, sending ASCII characters to it and monitoring LCD status.

The final building block is the LCD display. I used a surplus display that uses an Hitachi HD44780 LCD controller based interface. You can purchase a similiar OPTREX unit through DigiKey Electronics, P/N DCM-117A, for about $12.00, or Hitachi display I used from me for $8.00, while my limited supply lasts.

In the prototype implementation of the CPU, figure 1a, I used a crystal to generate the CPU clock since I had a sizeable stash of crystals in my junk box. But one could just as easily use the RC oscillator configuration since the PIC16F84 spends most of its time sitting around anyway, and could easily do the job running at 1MHz.

Construction

I built my decoder as two separate pieces, and recommend this approach for two reasons.

1) It allows you to distance the CPU and LCD away from your receiver, minimizing digital noise coupling the the receiver.

2) It keeps the CPU and LCD clock noise away from the sensitive front-end of the electret amplifier and PLL.

The front end was constructed using point-to-point wiring on a 2.0" by 2.5" piece of perf-board. I wrapped the edges with adhesive backed copper tape to provide a convenient place to ground components. The 10Kohm variable resistor off pin 5 of the NE567 provides frequency adjustment capability so you can tune it to the frequency of a code practice oscillator, or to a comfortable pitch to copy signals off the airwaves. A three (3) wire interface connects the front-end to the CPU/LCD assembly, carrying the signal, power and ground.

The CPU was also point-to-point wired on a 2" by 2.5" perf-board. I decided to interconnect the CPU and LCD using a 14 pin (2 * 7) plug and socket arrangement I made up for easy connect and disconnect. I used machined pin and collet type sockets and pins for this connection. A three pin header socket was used to make the connection to the front-end consisting of power, ground, and the decoder output.

All the parts used in this design are readily available through many suppliers. The total cost of the unit is less than $40.00, depending on how you choose to package the assemblies. Subtotals for the functional blocks are roughly: $4.00 for the front-end parts, $12.00 for the LCD display, and $15.00 for a preprogrammed PIC16F84 from the author, and $3.00 for miscellaneous items.

If you choose to order a preprogrammed PIC16F84 from me, please specify -HS for the 4.9152MHz crystal controlled version, or -RC for the RC oscillator version. Send $15.00 for the CPU plus $3.00 S&H to:

Lawrence Foltzer
P.O. Box 488
Occidental, Ca. 95465
Your order will be sent US Mail. Continental USA orders only.

The PIC16F84 Assembler/Machine Code

Download the source code

The firmware for this project is shown in Listing 1. This code was written in Microchip's native assemby language, and only took 365 instruction to implement. To help follow the flow, read the pseudocode at the end of the listing. You will find other important design details at the end of the listing also.

The heart of the code is two running average buffers that keep tabs on the length of the four previous DIT and DAH interval samples. With these short buffers, speed adaptation appears almost instantanous. But I must confess that I didn't start out with this approach. In my first implemetation, I averaged DITs and DAHs in the same buffer, since over time there would roughly be an equal number of DITs and DAHs, and the average, therefore, would be (2 DITs long) right in the middle and be the perfect symbol discriminator. But when you copy sequences like "he is 55 .....", the symbol decision threshold becomes corrupted quickly unless the buffer depth is quite large. But a larger buffer means a slow code speed adaptation rate, so I quickly abandoned that approach in favor of the dual buffer approach.

The three subroutines at the end of the code drive the LCD display interface. Perhaps you will find uses for them in other PIC related projects.

DIT DAH DIT DIT DAH DAH DAH DAH DAH DAH DAH DIT DAH DAH !

DAH DIT DAH DIT DAH DIT DIT DIT DIT DIT DIT DAH DAH DIT DIT DIT DAH DIT DIT

DAH DIT DAH
 

Listing 1

;**********************************************************************
;       Filename:       picmrsrc.asm
;       Date:   Wednesday, December 30, 1998 10:28
;       File Version:   
;       Author: Lawrence Foltzer
;       Size:           365 bytes
;**********************************************************************
        list    p=16F84 ; list directive to define processor
        #include        <p16F84.inc>        ; processor specific variable definitions

        __CONFIG        _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC
; timing based on 1.12MHz RC oscillator configuration using 5.1Kohm + 100pf

;       __CONFIG        _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
; timing based on 1.12MHz RC oscillator configuration using 5.1Kohm + 100pf
;**********************************************************************
;***** application specific equates
E       equ     .4
R_W     equ     .5
RS      equ     .6
code_in equ     .7      ; PORTB,7 (also ICSPDATA and tone LED)
busy    equ     .3
BusyChk equ     B'00100000'
FuncSet41       equ     0x22    ; get its attention
FuncSet42       equ     0x20    ; get it in right mode
DisplayOn       equ     0x0c
EntryMode       equ     0x06
InitDdRam       equ     0x27
Ddra4Input      equ     B'00001111'
led     equ     .4      ; RA4 runs LED
Ddrb4Input      equ     B'10001111'
Ddrb4Output     equ     B'10000000'
ReadCntrl       equ     B'10100000'
pad1    equ     0x68    ; = 104
noise   equ     0x02    ; 16 milliseconds or less is noise
Ithres  equ     0x80
tabsize equ     .4
; optest        equ     .16

;**********************************************************************
;***** ram allocation
        ORG     0x0c

temp_w  res     1
temp_status     res     1

flags   res     1
; "flags" bit assignment follows
DitDah  equ     7       ; set (1) for DIT, clear (0) for DAH
overflow        equ     6       ; slow code can cause counter overflow

PortaImage      res     1
PortbImage      res     1

timecnt res     1       ; this is increment in the ISR
period  res     1       ; and transferred here on edge detection
thres   res     1       ; computed symbol type decision threshold

codeword        res     1       ; 1/0 representation of mickey morse

ditptr  res     1       ; pointer to ditvals buffer
ditsum  res     1
ditave  res     1
ditvals res     tabsize

dahptr  res     1       ; pointer th dahvals buffer
dahsum  res     1
dahave  res     1
dahvals res     tabsize

stack   res     1       ; available RAM
;**********************************************************************
        org     0x000
boot    goto    init
; ********************************************************************************
; this IRQ is entered every 4 milliseconds
        org     0x004
ISR     movwf   temp_w
        movf    STATUS,W
        movwf   temp_status
        incf    timecnt,1
        btfss   STATUS,Z
        goto    isrjmp
        bsf     flags,overflow
        decf    timecnt,1
isrjmp  movlw   0x74    ; 116
        movwf   TMR0
        bcf     INTCON,T0IF
exit1   movf    temp_status,W
        movwf   STATUS
        swapf   temp_w,1
        swapf   temp_w,W
        retfie
; ********************************************************************************
; morse tables follow ISR to keep them in 1st page, hopefully.
ditab   clrf    PCLATH
        movf    codeword,W
        andlw   B'00111111'
        addwf   PCL,1
        retlw   " "
        retlw   "e"
        retlw   "a"
        retlw   "i"
        retlw   "w"
        retlw   "r"
        retlw   "u"
        retlw   "s"
        retlw   "j"
        retlw   "p"
        retlw   0x5f
        retlw   "l"
        retlw   0x5f
        retlw   "f"
        retlw   "v"
        retlw   "h"
        retlw   "1"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x7c    ; is "|" rather than "\" end of message
        retlw   0x5f
        retlw   0x5f    ; _ = wait
        retlw   "2"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   "3"
        retlw   "!"     ; acknowledge
        retlw   "4"
        retlw   "5"
        retlw   0x5f
        retlw   "'"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   "."
        retlw   0x5f
        retlw   0x5f
        retlw   0x22    ; quotation mark
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   "_"
        retlw   "?"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   "<"     ; end of work
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f

dahtab  clrf    PCLATH
        movf    codeword,W
        andlw   B'00111111'
        addwf   PCL,1
        retlw   " "
        retlw   "t"
        retlw   "n"
        retlw   "m"
        retlw   "d"
        retlw   "k"
        retlw   "g"
        retlw   "o"
        retlw   "b"
        retlw   "x"
        retlw   "c"
        retlw   "y"
        retlw   "z"
        retlw   "q"
        retlw   0x5f
        retlw   0x5f
        retlw   "6"
        retlw   "="     ; double dash
        retlw   "/"     ; fraction bar
        retlw   0x5f
        retlw   0x5f
        retlw   ">"     ; starting signal
        retlw   "("
        retlw   0x5f
        retlw   "7"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   "8"
        retlw   0x5f
        retlw   "9"
        retlw   "0"
        retlw   0x5f
        retlw   "-"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   ";"
        retlw   0x5f
        retlw   0x5f
        retlw   ")"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   ","
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   ":"
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f
        retlw   0x5f

; ********************************************************************************
; next is for xtal clock
init    movlw   0x04    ; divide XTAL OSC by 4 and then 32 for 4ms ticks
        option          ; later, load TMR0 with 256-140 and let overflow
; next is for rc clock
;init   movlw   0x02    ; divide RC OSC by 4 and then 8 for ~4ms ticks
;       option          ; later, load TMR0 with 256-140 and let overflow

        movlw   Ddra4Input
        tris    PORTA
        bcf     PORTA,led       ; on initially
        movlw   Ddrb4Input
        tris    PORTB

        clrf    PortbImage
        clrf    PortaImage
dlp1    decfsz  PortaImage,1
        goto    dlp1
        decfsz  PortbImage,1
        goto    dlp1

        call    ChkBusy

        movlw   FuncSet41
        movwf   PORTB
        call    SendCmmd

        movlw   FuncSet42
        movwf   PORTB
        call    SendCmmd
        movlw   DisplayOn
        call    SendCmmd
        movlw   EntryMode
        call    SendCmmd
        movlw   InitDdRam
        call    SendCmmd

        movlw   .13
        movwf   ditptr
        movlw   ditptr
        movwf   FSR
ilp1    incf    FSR,1
        clrf    INDF
        decfsz  ditptr,1
        goto    ilp1

        movlw   0x18
        movwf   thres

        clrf    TMR0
        clrf    flags
        clrf    INTCON
        bsf     INTCON,T0IE
        bsf     INTCON,GIE
; ********************************************************************************

        clrf    PortbImage
        clrf    PortaImage
dlp1    decfsz  PortaImage,1
        goto    dlp1
        decfsz  PortbImage,1
        goto    dlp1

        call    ChkBusy

        movlw   FuncSet41
        movwf   PORTB
        call    SendCmmd

        movlw   FuncSet42
        movwf   PORTB
        call    SendCmmd
        movlw   DisplayOn
        call    SendCmmd
        movlw   EntryMode
        call    SendCmmd




start   btfss   PORTB,code_in
        goto    start   ; receiving a tone, exit on a space
        bsf     PORTA,led
; we are now receiving a space
start2  btfsc   PORTB,code_in
        goto    start2  ; now we wait for a tone to time
; detected the start of a dit or dah
        bcf     PORTA,led       ; light LED
        clrf    timecnt ; reset timer count
db1     movlw   noise   ; debounce (db) tone edge
        subwf   timecnt,W       ; timecnt - noise
        btfss   STATUS,C
        goto    db1
w84spc1 btfss   PORTB,code_in
        goto    w84spc1 ; tone still present
; tone stopped, but this is first so we don't know what it is. we assume it is a dah!
        movf    timecnt,W
&nbs        movwf   period
        clrf    timecnt
        bsf     PORTA,led
        movlw   0x01
        movwf   codeword        ; prep for 1st symbol
        movf    thres,W
        subwf   period,W        ; period - thres
        btfsc   STATUS,C
        goto    isadah
isadit  bsf     flags,DitDah
        call    avedit
        goto    mainlp
isadah  bcf     flags,DitDah
        call    avedah
; now we wait for a tone to start so we can see what kind of space just passed
mainlp  btfsc   PORTB,code_in
        goto    mainlp  ; space while set
        movf    timecnt,W
        movwf   period
        clrf    timecnt
; we are timing the current tone period now. so light the LED.
        bcf     PORTA,led       ; let there be light
; ********************************************************************************
; so how long was the space? symbol or char
        movf    thres,W
        subwf   period,W        ; period - thres
        btfss   STATUS,C
        goto    symbolspc
        goto    longspc
longspc rlf     thres,W
        subwf   period,W        ; period - thres
        btfss   STATUS,C
        goto    charspc
        goto    wordspc
; ********************************************************************************
symbolspc       movlw   noise
db3     subwf   timecnt,W       ; timecnt - noise
        btfss   STATUS,C
        goto    db3
db4     btfss   PORTB,code_in
        goto    db4     ; wait for the tone to end
        movf    timecnt,W
        movwf   period
        clrf    timecnt
        bsf     PORTA,led
        movf    thres,W
        subwf   period,W        ; period - thres
        btfsc   STATUS,C
        goto    itsadah
        goto    itsadit
; ********************************************************************************
itsadit call    avedit
        btfsc   flags,DitDah
        bsf     STATUS,C
        btfss   flags,DitDah
        bcf     STATUS,C
        rlf     codeword,1
        goto    mainlp
; ********************************************************************************
itsadah call    avedah
        btfss   flags,DitDah
        bsf     STATUS,C
        btfsc   flags,DitDah
        bcf     STATUS,C
        rlf     codeword,1
        goto    mainlp
; ********************************************************************************
charspc btfsc   flags,DitDah
        goto    ditstart1
;       goto    dahstart1
; ********************************************************************************
dahstart1       call    dahtab
        goto    printit
; ********************************************************************************
ditstart1       call    ditab
printit call    SendText        ; printit, send to LCD
        goto    db1
; ********************************************************************************
wordspc btfsc   flags,DitDah
        goto    ditstart2
;       goto    dahstart2
dahstart2       call    dahtab
doit    call    SendText
        movlw   0x20    ; space
        call    SendText
        goto    db1
ditstart2       call    ditab
        goto    doit
; ********************************************************************************
avedit  movf    ditptr,W
        addlw   ditvals
        movwf   FSR
        movf    INDF,W
        subwf   ditsum,1
        movf    period,W
        movwf   INDF
        addwf   ditsum,1
        rrf     ditsum,W
        movwf   ditave
        rrf     ditave,1
        movlw   0x3f
        andwf   ditave,1
        incf    ditptr,1
        movlw   0x03
        andwf   ditptr,1
        goto    makethres
; ********************************************************************************
avedah  movf    dahptr,W
        addlw   dahvals
        movwf   FSR
        movf    INDF,W
        subwf   dahsum,1
        movf    period,W
        movwf   INDF
        addwf   dahsum,1
        rrf     dahsum,w
        movwf   dahave
        rrf     dahave,1
        movlw   0x3f
        andwf   dahave,1
        incf    dahptr,1
        movlw   0x03
        andwf   dahptr,1
makethres       movf    ditave,W
        subwf   dahave,W
        movwf   thres
        bcf     STATUS,C
        rrf     thres,1
        movf    ditave,W
        addwf   thres,1
        return
; ********************************************************************************
SendText        clrf    PORTB
        bsf     PORTB,RS
        goto    Send1
SendCmmd        clrf    PORTB
Send1   movwf   PortbImage
        swapf   PortbImage,w
        andlw   0x0f
        iorwf   PORTB,1
        bsf     PORTB,E
        bcf     PORTB,E
        movlw   0xf0
        andwf   PORTB,1
        movf    PortbImage,w
        andlw   0x0f
        iorwf   PORTB,1
        bsf     PORTB,E
        bcf     PORTB,E
; ********************************************************************************
ChkBusy movlw   Ddrb4Input
        tris    PORTB
SampleAgain     movlw   ReadCntrl
        movwf   PORTB
        bsf     PORTB,E
        movf    PORTB,w
        movwf   PortbImage
        bcf     PORTB,E
        bsf     PORTB,E
        bcf     PORTB,E
        btfsc   PortbImage,busy
        goto    SampleAgain
        movlw   Ddrb4Output
        tris    PORTB
        return
;**********************************************************************
; this version is based on use of an NE567 tone decoder
; as input filter. decoder output is low when a tone is detected.
; advantages: immunity to adjacent channel signal, and amplitude variation.

; timing considerations, how long is a symbol?
; the reference symbol duration is the DIT!.
; a DAH symbol is 3 DITs long.
; the space between a pair of symbols is 1 DIT long.
; characters are groups of symbols and the spaces between them.
; the space between characters is 3 DITs long.
; the space between words is 5 DITs long.
; there are 50 symbol periods in the reference string: PARIS
; ". _ _ .   . _   . _ .   . .   . . .     "
; so at 12 WPM, we have 600 symbols in 60 seconds, ===> 0.1 sec / symbol
; @ 36 WPM, the shortest (DIT) symbol period is 33ms long.
; ********************************************************************************
; ********************************************************************************
; *****************************************


        end     

 

 


 [ About me | Acronyms  | CW | Data Sheets | Docs | Download | E-mail | HOME | Ham projects | Hobby circuits | Photo galery | PIC | QTH photos |
Sign in my guestbook | View my guestbook ]
 


© 2001 - YO5OFH, Csaba Gajdos