
CIRCBot
Part 4: Serial, Sonar and Servos Continued
In Bascom, one method of sending serial data is
the PRINT
command. To send the string "Hello World" to the other
microcontroller the command is:
PRINT "Hello World"
To receive serial data, the INPUT or
INKEY commands
are used. INPUT is used to receive a line of data that is terminated
with a line feed character. INKEY is used to receive characters one at a
time. Our program uses the INKEY command to receive data.
Byterecvd = INKEY()
Byterecvd has the value 0 (zero) if no
character was received. If a character was received, Byterecvd contains the
ASCII code.
The speed that serial data is sent &
received is called the BAUD rate. We are using 9600 but you can use
faster or slower speeds to suit your need.
Let's look at the Primary Serial Test
Code (an underscore at the end of a line means the text wraps to the next
line):
'CIRCBot
Primary MCU Serial Test Code
'Load into the lower
Mega48
$regfile
=
"m48def.dat"
$baud
=
9600
$crystal
=
8000000
$hwstack
=
32
$swstack
=
32
$framesize
=
32
The statements above tells the compiler
(Bascom) important information about the microcontroller - which chip,
what frequency and what baud rate. HWStack, SWTack and Framesize are
memory buffers used by the compiler. All of these settings are
accessible through the Options menu. But by putting these statements in
your program, anyone looking at your code will know what the correct
settings are.
Dim
Char As
String
*
1 ,
Mystr As
String
*
16 ,
Byterecvd As
Byte
Dim Dist
As
Byte
,
Count As
Byte
,
Timeout As
Word
The DIM statements define or dimension
the variables we are using in the program.
Config Lcdpin =
Pin ,
Db4 =
Portc.0
,
Db5 =
Portc.1
,
Db6 =
Portc.2
,
Db7 =
Portc.3
,
E =
Portd.4
,
Rs_ =
Portd.3
Config
Lcd
=
16 *
1a 'Type of LCD
Display
These two lines configure the LCD
display. If you have a 16x2 display, change the second line to read
"16 * 2". For a 20x4 display, change it to "20 *
4".
Cls
'Clear the LCD
Display
Cursor
Off
'Turn off the
Blinking Cursor
Wait
1
Lcd
"Starting..."
Wait
5
The CLS command clears the LCD
and puts the cursor in home position, at the beginning of the line. Cursor
Off command removes the blinking cursor from the LCD display. The Wait
statement cause the program to pause for the number of seconds
specified. The LCD command prints the text on the LCD
display, similar to the Print command used to send data out the serial
port.
Enable
Interrupts
Enable Interrupts is a global
flag for the microcontroller, enabling all interrupts. Think of it as a
master on/off switch for interrupts.
Count =
1
Timeout =
0
Do
Timeout =
0
If
Count =
1 Then
Print
"100"
'This number
represents pointing straight ahead
If
Count =
2 Then
Print
"65"
'This number
represents pointing right
If
Count =
3 Then
Print
"135"
'This number
represents pointing left
The If statements allow us to
send different messages based on the value of Count. When Count = 1 is
true, the IF statement will execute the Print statement that
follows Then.
Mystr =
""
Do
Byterecvd =
Inkey()
'Look for data sent
back to us
As we discussed above, the Inkey
statement is used to look for data coming from the serial port. When no
data is available, Inkey returns a zero value.
Select
Case
Byterecvd 'Decide
what to do based on the value of Byterecvd
Case
0
'No
data available
Incr
Timeout
Waitms
1
If
Timeout >
500 Then
Timeout =
0
Dist =
0
Exit
Do
End
If
Case
10
'Linefeed,
got our data
Timeout =
0
Dist =
Val(mystr)
Exit
Do
Case
13
'Carriage
Return, ignore
Timeout =
0
Case
Else
Char =
Chr(byterecvd)
'Convert the Data
into a string
Mystr =
Mystr +
Char '
Timeout =
0
End
Select
The Select Case statement lets
us run blocks of code based on the value of a variable - Byterecvd in this
case. As the text data is received, each byte is checked using the Select
Case statement above. There are three special values that we want to
handle differently than the rest:
0 (zero) - No data present,
increment a timeout value so we don't get stuck in an endless loop here
13 - Carriage Return. each text message ends with a Carriage
Return character and a Line Feed character. The Carriage Return
character isn't used but we don't want it appended to the string
variable. By calling it out separately, no action is taken when Carriage
Return is detected.
10 - Line Feed. This character represents the end of a text
message. This tells us to stop looking for characters and process the
message we received.
Loop
Cls
'Clear the LCD
Lcd
"DIST: "
;
Dist
Wait
2
Incr
Count
If
Count >
3 Then
Count =
1
Loop
When Line Feed is detected, it causes the program to
exit the loop via the Exit Do command. The command following Loop
is executed next, in this case it is CLS. The LCD command is
used to display the message we received from the Secondary
microcontroller. The message is the distance detected by the Sonar Sensor.
|