Professional Documents
Culture Documents
GRADUATION THESIS
MICROCONTROLLER BASED AUTOMOBILE
PREPARED BY
EREN ERDAL AKSOY
2000514011
SUPERVISED BY
Assistant Prof. Dr. MUSTAFA GÖK
JUNE 2005
ADANA
2
To my parent
3
ABSTRACT
CONTENTS
1. INTRODUCTION................................................................................................................ 1
1.1 Microcontrollers......................................................................................................... 1
1.2 Microcontrollers Versus Microprocessors............................................................... 1
1.3 Types of Microcontrollers.......................................................................................... 2
1.4 Programming PIC Microcontrollers......................................................................... 2
2. HARDWARE....................................................................................................................... 3
2.1 Minimum Circuitry for 16F877................................................................................. 3
2.2 Power Supply.............................................................................................................. 4
2.3 Motor Control............................................................................................................. 5
2.4 LCD.............................................................................................................................. 7
2.5 Sensors......................................................................................................................... 8
2.6 Battery Control........................................................................................................... 10
2.7 Serial Communication................................................................................................ 11
3. SOFTWARE......................................................................................................................... 14
3.1 PIC C for Microcontroller........................................................................................ 14
3.2 User Interface............................................................................................................. 21
4. CONCLUSION..................................................................................................................... 26
5.REFERENCES ...................................................................................................................... 27
5
1. INTRODUCTION
Preparation of classical control circuit is very difficult with using transistor, diode,
resistors, capacitors, coil, op-amp and other integrated circuits. To use of these
components in the circuits can increase the cost and occupies places and may decrease
the efficiency. In addition, while setting up these components on the circuit, they can be
affected by temperature and mechanical pressure. Instead of these elements
microcontrollers are preferred. The microcontrollers are so widespread in modern
electronic systems. They are used in a wide number of electronic systems such as:
In this study PIC 16F877 microcontroller has been used for controlling DC motors,
battery, LCD, etc… Flexibility which is the most important advantage of the
microcontrollers aided about rewriting and improving the programs.
1.1 MICROCONTROLLERS
Microcontroller is a computer on a chip that is programmed to perform almost any
control, sequencing, monitoring and display function. Because of its relatively low cost,
it is a natural choice for design. It performs many of the functions traditionally done by
simple logic circuitry, sequential control circuits, timers or a small microcomputer.
2. HARDWARE
2.1 MINIMUM CIRCUITRY FOR 16F877
In this thesis PIC 16F877 microcontroller is used. Because it has 8K Flash Program
Memory, 368 bytes RAM, Timers, 10 bits Analog to Digital Converters, 33 I/O ports.
The minimum circuitry for 16F877 is indicated below. 4MHz Crystal and 22pF
capacitors are used for Oscillator. Port 1 is the Master Clear input to reset the PIC.
8
Base points of all transistors are connected to the PIC microcontroller. According to the
H-bridge theory, two of transistor bases are ON and others are OFF for each motor.
Also two of the transistors are PNP (BD140) and others are NPN (BD139). BD140 and
BD139 are power transistors that mean they have higher beta β according to the normal
transistors. All resistances are 430 Ω, in this circuit. The motor and transistor
connections to the PIC 16F877 are indicated below.
11
2.4 LCD
LCD (Liquid Crystal Display) is used for informing the user. There is information about
the movements of the automobile, on LCD. 2x8 LCD which has 14 pins is used in this
thesis. With descriptions, 14 pins of LCD are shown in the table, below. 5V is supplied to
the 2nd pin. The 1st Pin is connected to the ground. Also the 3rd pin is used to adjust the
contrast of the LCD. So 2KΩ can be connected to the 3rd pin.
2.5 SENSORS
The automobile designed in this thesis can detect a black line on white surface. For this
process Reflective Optical Sensor (CNY70) is used. CNY70 has a compact construction
where the emitting light source and the detector are arranged in the same direction to
sense the presence of an object by using the reflective IR beam from the object. The
operating wavelength is 950 nm. The detector consists of a phototransistor and diode
which are indicated below.
The infrared-emitting diode radiates. If there is any object in front of the sensor, then
the phototransistor detects it because of the reflected wave. White color absorbs the light
but black color does not. According to this phenomenon, there will be a current flow
while the sensor detects a black line on the white surface. Then this current will be used
in a comparator circuit to get a high output voltage. This output voltage acts as a pulse
for microcontroller to operate the motors. So according to the color the motors will be
ON or OFF. This method can be used for different aims. For example our automobile
can follow a black line on white surface or it can move in a bounded area.
The sensor circuit is indicated below.
13
There will be always a current flow on the infrared-emitting diode. When the
phototransistor detects an object, the current flow will be on the collector also. This
current is increased by connecting a transistor as a Darlington. In this circuit op-amp
(LM741) acts as a comparator. It has constant 4.4V on the 3rd pin which is reference
voltage. When phototransistor is off, the voltage on the 2nd pin is zero and the output of
the comparator will be zero. So the red led will be turn on. When phototransistor is on
the voltage will increase over 4.4 V and the output of the comparator will be 5V. So the
red led will be turn off and also this output will trigger the microcontroller for changing
the directions of the motors. In this thesis three sensor circuits are used. Each sensor’s
outputs are connected to the microcontrollers as a three different inputs. The sensor
circuit is connected to the microcontroller by a flex cable as an external device.
The connection of the sensor circuit to the PIC 16F877 is indicated below.
14
There are two sizes of Serial Ports. They are the D-Type
25 pin connector and the D-Type 9 pin connector both of
which are male on the back of the PC, thus we will
require a female connector on our device. Below is a
table of pin connections for the 9 pin D-Type connectors.
RS-232 communication is asynchronous. That is a clock signal is not sent with the data.
Each word is synchronized using it's start bit, and an internal clock on each side, keeps
tabs on the timing.
The diagram above shows the expected waveform from the UART when using the
common 8N1 format. 8N1 signifies 8 Data bits, No Parity and 1 Stop Bit. A transmission
starts with a start bit which is (Logic 0). Then each bit is sent down the line, one at a
time. The LSB (Least Significant Bit) is sent first. A Stop Bit (Logic 1) is then appended
to the signal to make up the transmission. The above diagram is only relevant for the
signal immediately at the UART. RS-232 logic levels uses +3 to +25 volts to signify a
"Space" (Logic 0) and -3 to -25 volts for a "Mark" (logic 1). Any voltage in between
these regions (i.e. between +3 and -3 Volts) is undefined. Therefore this signal is put
through a "RS-232 Level Converter". This is the signal present on the RS-232 Port of
any computer, shown below.
Max232 also includes two receivers and two transmitters in the same package. This is
handy in many cases when we only want to use the Transmit and Receive data Lines.
We don't need to use two chips, one for the receive line and one for the transmit line.
The pin connections of PIC16F877 microcontroller, max232 and computer are indicated
above. All the capacitances used in this circuit are equal to 1 µF. Also a serial cable is
used to connect this circuit to the computer. This serial cable is approximately 3 meters
and has 2 female connectors. Hence we must use a male connector for this circuit.
18
3. SOFTWARE
3.1 PIC C FOR MICROCONTROLLER
For programming PIC16F877 microcontroller CCS PIC C is used. PIC C is a kind of C
program developed for PIC microcontrollers. So microcontroller is programmed
according to C logic and rules. The pre-processors and functions used in this program
are explained below.
Pre-Processors
#include < filename > filename is a valid PC filename. It maybe type of PIC or
LCD. Text from the specified file is used at this point of the
compilation.
#fuses options This directive defines what fuses should be set in the part
when it is programmed. This directive does not affect the
compilation.
#use rs232 ( ) This directive tells the compiler the baud rate and pins used
for serial I/O. Also it takes effect until another RS232
directive is encountered. This directive enables use of built-in
functions such as GETC, PUTC, and PRINTF.
output_x ( value ) value is a 8 bit int. This function outputs an entire byte to a
port. PIC 16F877 has 5 ports (port A, B, C, D, E). x is the
port name.
19
set_tris_x ( value ) value is an 8 bit integer with each bit representing a bit of the
I/O port. Each bit in the value represents one pin. 1 indicates
the pin is input and 0 indicates it is output. x is the port
name.
Delay Functions
Delay_ms ( time ) time varies between 0-255. This function will create code to
perform a delay of the specified length. Time is specified in
milliseconds.
setup_adc ( mode ) mode is Analog to digital mode. The valid options vary
depending on the device.
set_adc_channel ( chan ) chan is the channel number to select. Channel numbers start
at 0 and are labeled in the data sheet AN0, AN1.This function
specifies the channel to use for the next READ_ADC call.
read_adc ( ) This function will read the digital value from the analog to
digital converter. setup_adc(), setup_adc_ports() and
set_adc_channel() should be made sometime before this
function is called. The range of the return value depends on
the type of the microcontroller.
LCD Functions
lcd_init ( ) This function initializes LCD.
This is the PIC 16F877 source code of automobile. After compiling these codes, we get
.hex file which will be downloaded to PIC 16F877.
//__________________________________________________________________________
//_________________PIC16F877 Microcontroller Based Automobile_________________
//__________________________________________________________________________
//_________________________Pre-Processors____________________________________
//__________________________________________________________________________
//__________________________________________________________________________
//___________________________Battery Control_________________________________
//__________________________________________________________________________
void battery (void)
{
float battery_value;
setup_port_a( AN0 ); //to define the 1st bit of portA as analog and other as digital input.
setup_adc( ADC_CLOCK_INTERNAL ); //4MHz crystal is used as a clock…
set_adc_channel( 0 ); //means which bit is used as input for reading analog value…
void line_follow (void) //receives the sensors outputs as inputs and control the
//motors according to these inputs…
{
lcd_putc("\f LINE ");
lcd_putc("\n FOLLOW ");
24
battery();
output_b(0x00); //firstly, stops all motors…
while(TRUE)
{
if(input(pin_e0)) //controls the first sensor output…
output_b(0x33); //drives motors forward…
else if(input(pin_e1)) //controls the second sensor output…
output_b(0x3C); //changes motor directions to turn right…
else if(input(pin_e2)) //controls the third sensor output…
output_b(0xCC); //changes motor directions to turn left…
else
output_b(0x00); //if all sensor outputs are low, then motor stops…
if(kbhit() && (getc()=='S' )) break; //waits until receiving char ‘S’ from PC…
}
}
void main()
{
unsigned char selection;
lcd_init(); //for initializing LCD…
battery(); //controls the battery value and sends it to the PC…
while(TRUE)
{
while(!kbhit()); //waits until receiving data from PC…
selection=getc(); //receiving data from PC…
switch(selection)
{
case 'F': forward(); break; //forward movement…
25
Components of MSComm
CommPort Defines number of serial communication port. COM1,
COM2, COM3, etc…
Other Functions
Timer Writes the date and hour on the user form.
According to these functions and components, the user interface is programmed and
designed. The program is indicated blow.
‘__________________________________________________________________________
‘________________________The User Interface__________________________________
‘__________________________________________________________________________
Private Sub Form_Load()
MSComm1.Settings = "9600, N, 8, 1" ‘Baud rate is 9600, no parity bit is used,
‘only one stop bit is used, data length is 8 bits…
MSComm1.CommPort = 2 ‘COM2 is selected…
MSComm1.InBufferSize = 1 ‘for receiving only one bit data…
MSComm1.RThreshold = 1 ‘jumps to subprogram after receiving one bit data...
MSComm1.PortOpen = True ‘opens the port…
Timer1.Interval = 1 ‘timing interval is 1 ms…
Timer1.Enabled = True ‘timer is enabled…
Text1 = "select a control method, please..."
End Sub
End If
End Sub
End Sub
‘__________________________________________________________________________
‘__________________________________________________________________________
CONCLUSION
REFERENCES
[1] PIC 16F877 Data Sheet, Microchip Technology Inc., 2001. www.microchip.com
[3] Prof. Dr. Doğan İbrahim, “PIC ve PC iletişim Projeleri”, 2004, Bileşim yayınevi.
[4] www.antrak.org.tr
[5]www.endtas.com
[6] www.robot.metu.edu.tr