Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 Introduction
This lab will introduce you to the PIC micro-controller as well as the various types of
sensors that will be used in the project.
This part of the lab requires you to build and test a basic PIC circuit. This circuit (Figure
1) also includes a RS-232 transceiver to allow communications with a desktop PC.
Construct this circuit using the components supplied and then verify the crystal is
oscillating correctly.
VCC U1
1k
1 40
2 MCLR*/Vpp RB7/PGD 39
VCC 3 RA0/AN0 RB6/PGC 38
4 RA1/AN1 RB5 37
5 RA2/AN2/Vref- RB4 36 VCC
1p 6 RA3/AN3/Vref+ RB3/PGM 35
7 RA4/T0CKI RB2 34
8 RA5/AN4/SS* RB1 33 1p
9 RE0/RS*/AN5 RB0/INT 32
10 RE1/WR*/AN6 Vdd 31
11 RS2/CS*/AN7 Vss 30
12 Vdd RD7/PSP7 29
Y1 13 Vss RD6/PDP6 28
14 OSC1/CLKIN RD5/PSP5 27
15 OSC2/CLKOUT RD4/PSP4 26
18pF 18pF 16 RC0/T1OSO/T1CKI RC7/RX/DT 25
CRYSTAL 17 RC1/T1OSI/CCP2 RC6/TX/CK 24
18 RC2/CCP1 RC5/SDO 23
19 RC3/SCK/SCL RC4/SDI/SDA 22
20 RD0/PSP0 RD3/PSP3 21
RD1/PSP1 RS2/PSP2
PIC16F877 U2
13 12
8 R1IN R1OUT 9
R2IN R2OUT
11 14
10 T1IN T1OUT 7
T2IN T2OUT
1
0.1u 3 C1+
4 C1-
5 C2+
2 C2-
0.1u 6 V+
V-
1
6
2
7
3
8
4
9
5
MAX202
R2
R
P2
VCC
CONNECTOR DB9
0.1u 0.1u
To Build:
1. Click Project. - Build All.
2. The hex file is now in the directory where you created the project.
To download your code (HEX file) to the PIC using the bootloader do the following:
1. Ensure nothing is using the COM port that your PIC is connected to (ie.
Hyperterminal).
2. Start the PIC Downloader program.
3. Click Search and locate your HEX file.
4. Ensure that the COM port is correct and that the speed is set to 19200.
5. Click the Write button and then power on your PIC. The program should
download the HEX file.
6. You can now reset your PIC and your program should run.
5 Configuring Hyperterminal
In order to talk to the PIC through your serial port, Hyperterminal needs to be properly
configured.
6 “Hello World”
With a working PIC circuit, write a simple program that prints hello world on a
HyperTerminal display when your circuit is reset. Figure 2 illustrates a basic PIC
program. Use the CCS manual to gain an understanding of the pre-processor commands.
Pre-processor
commands
Main program
body
7 Digital I/O
Using a simple switch and a light emitting diode write a program that turns on the LED
when the switch is in the “on” position and turns off the LED when the switch is in the
“off” position. Figure 3 illustrates a sample program that will accomplish this task.
Familiarize yourself with the various options for reading and writing to digital inputs and
outputs. Also familiarize yourself with the commands for setting ports to input or output.
Figure 3: A simple program demonstrating digital I/O.
Using the SharpGPD12 sensor build a circuit that will sample the analog output of the
sensors and print it back to your screen. A sample program is shown in Figure 4. Collect
a series of readings by placing an obstacle at known distance from the sensor and record
the sensor readings. Plot the voltage vs. distance curve for the sensor.
NOTE: Place a 100uF capacitor across power and ground when connecting the Sharp
sensor.
Figure 4: Sample program for perform A/D conversions on the PIC.
9 PIC Timing
This task requires the use of timing and interrupts. Timing on the PIC can be a little
tricky. The following section describes in detail how to do timing on the PIC.
The PIC has a number of timers that can be used to perform timing operations. For this
example assume we are using Timer_0 or RTCC. The timer is incremented at a rate
((CLOCK)/4)/RTCC_DIV counts/sec. So if you are using a 20 MHz crystal with
RTCC_DIV set to 128 the clock will increment every ((20,000,000)/4)/128 = 39062.5
times per second (~2.56us).
When using an interrupt driven by RTCC the interrupt will occur each time the counter
overflows (256 increments). Refer to Figure 5. How often will a message be output to
your display? Load this program onto your circuit to verify.
Figure 5: Sample-timing program.
There are several ways of varying the timer duration. The first method is by varying the
pre-scalar. The pre-scalar can be set to: RTCC_DIV_2, RTCC_DIV_4, RTCC_DIV_8,
RTCC_DIV_16, RTCC_DIV_32, RTCC_DIV_64, RTCC_DIV_128, RTCC_DIV_256.
This allows you to make coarse adjustments to your timer. The second method is by
setting the count value of your timer to a predefined value. For example if the counter is
initialized to 246 then it will only require 10 more counts to overflow.
Exercises: Calculate the timing interval for each of the following combinations of
settings. Write a program that prints a message to your display for each count to verify.
Ultrasonic sensors are one of the most widely used sensors for mobile robots. By
measuring the time-of-flight (TOF) fairly accurate measurements can be made for the
distance to the nearest obstacle. The sensor used in this lab is the Devantech SRF04. The
connection diagram is shown in Figure 6 and the timing diagram for this sensor is
illustrated in Figure 7.
(1) Pulse the input for a minimum of 10us. This can be done by setting a digital
output connected to the sensor input to high for 10us
EITHER:
(2) Either poll a digital input connected to the Echo output until a value goes low
indicating a return pulse. Note that if the signal remains high for 36 ms then no
obstacle is detected…OR
(3) Use an interrupt to detect the Echo Pulse Output.
Write a program that samples the ultrasonic sensor and displays the value to the screen.
Hints:
(1) The time of flight for sound is approximately 1cm/29 us. By designing a 29 us
timer each increment of the timer is equivalent to one cm of travel.
(2) Remember that the TOF is round-trip thus the distance to the obstacle is half of
TOF.
Figure 7: Devantech SRF04 timing diagram.
Controlling the speed of the motors will be very important for this project. The PIC
micro-controller has two built in pulse width modulators that can be used to drive an h-
bridge allowing you to vary the speed of the motor. By varying the duty-cycle of the
pulse width modulated signal the average voltage seen by the motor cab be varied thus
varying the speed.
Fortunately H-Bridges are very popular and several pre-packaged chips are available that
simplify motor control. The H-Bridge used on this project is the LMD18200. It simply
requires two inputs, a digital input for setting the direction and a pulse width modulated
input for controlling speed as shown in Figure 9.
Familiarize yourself with the following commands and write a simple program to vary
the PWM output on the PIC.
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 127, 1);
set_pwm1_duty(value);
Links
CCS Compiler Documentation
www.ccsinfo.com/piccmanual3.zip