Sei sulla pagina 1di 9

Engineering 6806

Project Design Labs in Electrical/Computer Engineering

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.

2 A Basic PIC Circuit

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

Figure 1: Basic PIC circuit.


3 MPLAB Setup
To setup the environment:
1. Project->New (call it main.prj)
2. Click Change. Select 16F877 from the drop down box. Click OK. Ignore warning.
3. Change Language Tool Suite to CCS. Click OK when warning appears.
4. Click on main.hex in the list. Then click Node Properties.
5. Click on the PCM checkbox. Click OK.
6. Click OK to close project settings window.
7. File->New
8. Type in code (See section 6)
9. File->Save As, main.c
10. Click Project, Edit Project and then Add Node. Select your file: main.c.

To setup the CCS compiler:


1. Click Project – Install Language Tool.
2. Choose CCS from the drop down box.
3. Click browse and locate the CCSC.EXE compiler (usually C:\PICC)
4. Click Build All.

To Build:
1. Click Project. - Build All.
2. The hex file is now in the directory where you created the project.

4 Downloading to the PIC

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.

1. Start Hyperterminal. (Usually Start – Programs – Accessories – Communication –


HyperTerminal).
2. In the new connection dialog type in a name for your connection (ie. PIC
Connection).
3. Ensure the right COM port is selected in the drop down box.
4. When the properties dialog appears, ensure that the baud rate is 9600, 8 data bits,
parity is none, 1 stop bit and none for flow control.

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

Figure 2: A simple "Hello world" program.

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.

8 Analog Inputs – Using the SharpGPD12 Range Sensor

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.

(a) Clock = 4 MHz


RTCC_DIV_2
(b) Clock = 20MHz
RTCC_DIV_128
Counter initialized to 147
(c) Clock = 20 MHz
RTCC_DIV_2
Counter initialized to 184
10 Using the Devantech SRF04 Ultrasonic Sensor

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.

Figure 6: Devantech SRF04 connection diagram.

Each time a sensor is sampled the following sequence of steps is required:

(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.

Which method would you prefer and why?

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.

11 Controlling Motor Speed – Pulse Width Modulation

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.

Figure 8: H-Bridge circuit


The H-Bridge takes a DC supply voltage and provides 4-quadrant control to a load
connected between two pairs of power switching transistors. Because the switches allow
current to flow bi-directionally, the voltages across the load (A-B) can be of either
polarity. For example consider the circuit in Figure 8. When switches A1 and B2 are
closed current will flow through the output from A to B. Similarly when switches B1 and
A2 are closed current will flow from B to A.
Figure 9: Controlling the switches on the h-bridge can reverse Motor.

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.

Figure 10: LMD18200 inputs.

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

Potrebbero piacerti anche