⇑ FIGURE 3. Hardware setup.
Nuts & Volts website at www.nutsvolts.com Let’s step
through it section by section.
Two variables are created named “old” and “new.”
These will be used to store the LED number to light and
which one to turn off.
⇐ FIGURE 4. The
screen that sets up the
connection file name.
mand implemented is the
SEROUT command that
sends the statement;
“Enter which LED to light
(0-7):” out to the PC.
Anything within quotes in
the SEROUT command
will be sent as it’s seen.
In other words, the
SEROUT will automatically look up the ASCII
code for each character and send those codes out serially.
The 10 and 13 that follow are the ASCII number codes for
line feed ( 10) and carriage return ( 13). The results of this
command will be displayed on the Hyperterminal screen,
as shown in Figure 7.
old var byte
new var byte
serout portc.6, 2, [“Enter which LED to light (0-7): “, 10, 13]
Next, the ports are set up for their proper input and
output mode. Remember, on a PIC a “0” makes the pin an
output and a “1” makes the pin an input. In this project,
Port C pin 6 is the transmit pin and is made into an output.
Port C pin 7 is the receive pin so it’s made an input. The
rest of the port pins are also made into inputs since that
puts them in a safer high impedance mode.
TrisC = %10111111
All the LEDs are connected to Port B, so we make these
outputs. We could actually skip these steps since the
SEROUT and SERIN commands along with the HIGH and
LOW commands set the proper mode automatically, but it’s
best to learn this way of controlling the PIC in your programs.
The SERIN command comes next, and it just waits for
a response ASCII code to be sent from the PC user. One
thing I didn’t point out is the format. The pin being used
for SERIN or SEROUT follows the command and then a
number “2” follows that. The “2” represents True 9600 bits
per second in PICBasic Pro language. If you have the full
version of PICBasic Pro, you can also enter T9600 instead
of “2” when you include a definition file at the top of the
program. But the sample version doesn’t allow “includes,”
so you have to use the raw “2” value.
After the “2” in the SERIN line is the pound sign and
the variable “new.” What the pound sign does is tell SERIN
to receive the ASCII value and then convert it into the actual numerical value it represents. For example, the ASCII
code for the number “1” is actually 31h. When the PC user
chooses to light LED1, 31h is sent to the SERIN command
and is then converted to decimal value one.
TrisB = %00000000
serin portc.7, 2, #new
The main loop of code starts with the “main” label.
The first com-
⇓ FIGURE 5. Connection setup window.
Converting “new” to the numerical value “1” (binary
00000001) instead of the ASCII character “1”
(binary 00110001) allows us to directly use the
variable as the pin number in the HIGH command to drive the proper LED. Before we get to
that, though, the program sends the value
received back to the PC as the ASCII value by
putting the # sign in front of the variable. This
converts the contents of the variable “new” to
the ASCII code for sending it, but doesn’t
change the actual value of the contents of the
“new” variable. This is just a way to feedback to
the PC user the PIC received the proper value.
You can see that in Figure 7, also.
⇐ FIGURE 6. Shows the setup as 9600 bits per
second, eight data bits, no parity, and one stop bit.