I now have the outline of the state machine which will do the job. Now I am going to write a program which will let me test the state machine behaviour. I am going to do something else which is very sensible, I am going to automate the test procedure as much as possible. The easier I make the testing the more chance there is that I will do it properly.
In the program on the right I have created an array which holds a series of "events". I can feed these into the state machine and watch what it does. Remember that I must have the DEBUG
enabled so that the LCD is driven.
The state machine and the test program are given in Exercise 9.1. Because I am using the LCD functions (plus a utility library that I am developing) you will see a new file appear in project Exercise 9.1 called utils.c, which is needed to make it work.
When you run the program you will get the following cryptic display:
.QB-BB-BB.BB BQ.
The program prints out the event identifier followed by the initial state and the final state. ".QB
" means a dot arrived, the system was in the QUIET
state and it moved to the BUSY
state. "-BB
" means a dash arrived, the system was in the BUSY
state and it stayed in the BUSY
state. The dot at the very end is the first dot of the letter I
, at which point the printing falls off the end of the display. You might like to consider the behaviour of the machine as if moves through the events in the test program.
/* here is a test sequence for */
/* the morse code reader. I am */
/* sending three characters */
unsigned char test_sequence [] =
{
/* P */
DOT_EVENT, DASH_EVENT, DASH_EVENT,
DOT_EVENT, SPACE_EVENT,
/* I */
DOT_EVENT, DOT_EVENT, SPACE_EVENT,
/* C */
DASH_EVENT, DOT_EVENT, DASH_EVENT,
DOT_EVENT, SPACE_EVENT,
/* end marker */
0x00
} ;
void main ( void )
{
unsigned char i ;
lcd_start () ;
for ( i=0 ;
test_sequence [i] != 0 ;
i = i + 1 )
{
do_morse ( test_sequence [i] ) ;
}
while (1) ;
}