PIC Experimenter's Board Manual
Peek Command (PICBasic Compiler)
We can also use the Peek Command to check the status of any input line. The advantages of the Peek command are as follows. Using Peek, one can read the five I/O lines of Port A (or the eight I/O lines of Port-B) at once. This increases the versatility of the PIC chip and allows our program to be more concise (less convoluted) shorter and easier to read.
To emphasize the points lets rewrite our last programs using the Peek Command. This program uses the same schematic.
Symbol TRISB = 134
Symbol PortB = 6
for B0 = 0 to 127
poke PortB, B0
if bit7 = 0 then loop2
if bit7 = 1 then loop1
'Set TRISB to 134
'Set PortB to 6
'Set port B pins (1..6) output, Pin 7 input
'Place B0 value at port to light LED's
'pause counting or it's too fast to see
'Check button status
'if sw1 is closed jump to loop2
' Next B0 value
'Second loop Not Counting
'turn off all LED's
'Check button status, if open jump back
'if sw1 is open jump to loop 1
The variable B0 is performing double duty. First it is holding our current counting number 0 through 127. The numbers 0 to 127 require 7 bits, of the variable B0 (bit0-bit6). This leaves the eighth bit (bit7) available for use. We use bit7 to check the status of the switch. If it's open its value will be a binary 1 and closed its equal to binary 0.
The Command PEEK, is followed by a memory address, then a comma, then a storage variable.
PEEK Address, Var
As it's name implies, the Peek command allows one to view (or peek at) the contents of a specified memory address. Typically the memory address "peeked at" is one of the PIC microcontrollers registers. The "peeked" valued is stored in a variable "VAR" defined in the command.
In this program we Peeked at the one input line on Port B:
The Peek command can reads an entire byte (8-bits) at once. Or as in this case, only the upper bit (bit7) of the peeked value is relevant. (The rest of the bits are holding our counting number that's outputted to the LED's).
Peeking Memory using the PICBasic Pro Compiler
Looking at (or peeking at) a memory loacation is much easier using the PICBasic Pro compiler. Simply assign a variable to the memory location. As an example on can look at (peek) PortB with the following command in PICBasic Pro:
B0 = PortB
The variable BO will now contain the value in PortB.
Basic INPUT and OUTPUT Commands
In our programs we directly wrote (using the POKE command) to the PIC microcontroller TRIS registers (A or B) to set various pins to be either Input or Output lines. By poking the TRIS register we are able to configure the 8 pins to Port B at once. In addition and more important we can configure the five open pins on Port A as well.
However, the PicBasic Compiler has two basic commands for making pins either input or output lines. The commands are INPUT and OUTPUT. Unfortunately these two commands only work on Port B pins.
Makes the specified pin an input line. Only the pin number itself, i.e. 0 to 7, is specified (i.e. NOT Pin0.)
INPUT 2 'Makes Pin2 an input line.
The opposite of the input command is the output command.
Makes the specified pin an output line. Only the pin number itself, i.e. 0 to 7, is specified (i.e. NOT Pin0.)
OUTPUT 0 'Makes Port B, pin 0 an output
The above examples are intended for use with PICBasic only. PICBasic Pro has both the INPUT and OUTPUT commands as well, but the naming convention of the pins is a bit different. With PICBasic Pro, you can use the INPUT and OUTPUT commands to set any pin on any register to an input or output (as opposed to the regular PICBasic, which only allows you to set the state of port B).
In PICBasic Pro, the pin is specified in the following format:
For example, port B pin 0 would be denoted:
You could use this in an input/output command like so:
input PortB.0 ' Make port B, pin 0 an input
output PortB.2 ' Make port B, pin 2 an output
input PortA.0 ' Make port A, pin 0 an input
For more information, experiments and projects, check out the new Pic Microontroller book. Coming Soon.