+ All Categories
Home > Documents > 40474363 Micro Controller Based on an Automobile

40474363 Micro Controller Based on an Automobile

Date post: 10-Apr-2018
Category:
Upload: praveen-ganiger
View: 216 times
Download: 0 times
Share this document with a friend

of 32

Transcript
  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    1/32

    1

    THE UNIVERSITY OF UKUROVA

    ENGINEERING & ARCHITECTURE FACULTY

    ELECTRICAL & ELECTRONICS ENGINEERING DEPARTMENT

    GRADUATION THESIS

    MICROCONTROLLER BASED AUTOMOBILE

    PREPARED BY

    EREN ERDAL AKSOY

    2000514011

    SUPERVISED BY

    Assistant Prof. Dr. MUSTAFA GK

    JUNE 2005

    ADANA

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    2/32

    2

    To my parent

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    3/32

    3

    ABSTRACT

    The Microcontrollers are very popular in electronics and computer controlled

    systems overall in the world. Remote control of a system by using computers is applied

    to lots of processes. It is very important about reducing the time loss and also cost of

    employees.

    This thesis presents the controlling of a microcontroller based system using

    computer. First of all microcontrollers are introduced. The main differences between

    microcontrollers and microprocessors are discussed with reasons. Also, the reasons of

    why PICs (Peripheral Interface Controller) are the most popular type of the

    microcontrollers are explained. The hardware and software parts of microcontroller

    based automobile are clarified comprehensively. The motor drivers, battery control

    units, sensor circuits for following a line and displaying units are achieved by using PIC

    16F877. One of the most important parts of this thesis is the communication between

    PIC microcontroller and computer, so communication protocol is described in depth.

    PIC C is used for programming PIC microcontroller and Visual Basic is used for

    programming the computer. The thesis concludes with explanations of these programs.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    4/32

    4

    CONTENTS

    1. INTRODUCTION................................................................................................................ 11.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................................................................................. 32.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

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    5/32

    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 decreasethe 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:

    Engine management systems in automobiles.

    Electronic measurement instruments (such as multimeters and oscilloscopes)

    Televisions, radios, CD players, tape recording equipment.

    Robotics technology, etc

    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.

    1.2 MICROCONTROLLERS VERSUS MICROPROCESSORS

    Microcontroller differs from a microprocessor in many ways. First and the most

    important is its functionality. In order for a microprocessor to be used, other

    components such as memory, or components for receiving and sending data must be

    added to it. In short that means that microprocessor is the very heart of the computer.

    On the other hand, microcontroller is designed to be all of that in one. No other external

    components are needed for its application because all necessary peripherals are already

    built into it. Thus, we save the time and space needed to construct devices.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    6/32

    6

    1.3 TYPES OF MICROCONTROLLERS

    There are lots of different types of microcontrollers. For example, Intel microcontrollers

    (8051), Motorola microcontrollers (68HC11), etc... PIC (Peripheral Interface Controller)

    microcontrollers from Microchip are very popular microcontrollers. PICs are easilyprogrammable cheap microcontrollers. PIC is the name for the Microchip

    microcontroller (MCU) family, consisting of a microprocessor, I/O ports, timer(s) and

    other internal, integrated hardware. The main advantages are low external part count, a

    wide range of chip sizes (from 8-pin up), great availability of compilers and source code

    and easy programming. Flash-type devices are reprogrammable in-circuit, while OTP

    versions are very cheap to use at the final stage. A wide range of simple programmer

    hardware and software is downloadable from the net. With a PIC microcontroller you

    can do things that would be difficult to do with discrete logic. Besides its power, you can

    easily "change your mind" with a microcontroller. The PIC isn't for every project. It

    won't replace a PC, or even a larger processor. But for many jobs it is just the right size,

    inexpensive, and doesn't require much in the way of support hardware.

    1.4 PROGRAMMING PIC MICROCONTROLLERS

    There are nowadays many tools for making programs for PICs. Microchip providesMPLAB, a free assembler programming environment. Even when you do not want to

    use assembler you should get MPLAB, if only for the build-in simulator. Various

    companies sell C compilers for PICs. High-Tech C provides PICC-Lite, a free demo

    version of their compiler. PICC-Lite targets an number of 16F chips, some with

    limitations. There are also compilers for other languages (for example JAL). In this

    thesis CCS PIC C is used for programming PIC microcontroller. After writing and

    compiling the codes, a different program is used to download the compiled file (.hex file)

    into the PIC microcontroller.

    http://www.microchip.com/http://www.microchip.com/
  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    7/32

    7

    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/8/2019 40474363 Micro Controller Based on an Automobile

    8/32

    8

    2.2 POWER SUPPLY

    The voltage range is form 3V to 5V for 16F877. High sink and source currents are

    25mA. So a voltage regulator is used for supplying constant 5V to microcontroller. The

    regulator circuit is indicated below. C1 is 330 F, C2 is 10 F and C3 is 0.1 F. A diodeis used to prevent the reverse current. The input voltage range of 7805 is 7~9V, but

    output voltage is constant 5V. NiMH Batteries are used because their currents can be

    increased up to 2100 mA. Hence we can get enough power for drive motors,

    microcontroller, LCD at the same time.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    9/32

    9

    2.3 MOTOR CONTROL

    The movable part of the automobile consists of two dc motors. These motors must be

    driven through any direction. A very popular circuit for driving DC motors is called an

    H-bridge. It's called that because it looks like the capital letter 'H' on classic schematics.The great ability of an H-bridge circuit is that the motor can be driven forward or

    backward at any speed, optionally using a completely independent power source. To

    power the motor, two switches that are diagonally opposed must be turned on.

    In the picture to the right,

    think that the high side left

    and low side right switches

    are turned on. The current

    flow is shown in red. The

    current flows and the motor

    begins to turn in a "positive"

    direction.

    If the high side right and low

    side left transistors are turned

    on, then the current flow is on

    the other direction which is

    drown in green. So the motor

    turns in the opposite direction

    according to the previous one.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    10/32

    10

    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.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    11/32

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

    1 Vss, Gnd, 0V 8 Data 1

    2 Vcc, +5V 9 Data 2

    3 Contrast 10 Data 3

    4 Register Select 11 Data 4

    5 Read/Write 12 Data 5

    6 Enable 13 Data 6

    7 Data 0, LSB 14 Data 7, MSB

    According to the program,

    PIC16F877 sends 4 bit data to

    the LCD. Because of the type of

    LCD, we used 4 bit data.

    The maximum data length can

    be 8 bit.

    The connection of LCD to PIC 16F877 is indicated below.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    12/32

    12

    2.5 SENSORS

    The automobile designed in this thesis can detect a black line on white surface. For thisprocess Reflective Optical Sensor (CNY70) is used. CNY70 has a compact constructionwhere 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. Theoperating wavelength is 950 nm. The detector consists of a phototransistor and diodewhich are indicated below.

    The infrared-emitting diode radiates. If there is any object in front of the sensor, thenthe phototransistor detects it because of the reflected wave. White color absorbs the lightbut black color does not. According to this phenomenon, there will be a current flowwhile the sensor detects a black line on the white surface. Then this current will be usedin a comparator circuit to get a high output voltage. This output voltage acts as a pulsefor 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 automobilecan follow a black line on white surface or it can move in a bounded area.The sensor circuit is indicated below.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    13/32

    13

    There will be always a current flow on the infrared-emitting diode. When thephototransistor detects an object, the current flow will be on the collector also. Thiscurrent 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 3 rd pin which is referencevoltage. 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 onthe voltage will increase over 4.4 V and the output of the comparator will be 5V. So thered led will be turn off and also this output will trigger the microcontroller for changingthe directions of the motors. In this thesis three sensor circuits are used. Each sensorsoutputs are connected to the microcontrollers as a three different inputs. The sensorcircuit 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.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    14/32

    14

    2.6 BATTERY CONTROL

    The automobile has 8 NiMH batteries as a power supply. It is needed to control thebatteries and warn the user when they are low. PIC 16F877 has 8 different 10 bit Analogto Digital Converters (ADC). The analog input charges a sample and hold capacitor.

    The output of the sample and hold capacitor is the input into the converter. Theconverter then generates a digital result of this analog level via successiveapproximation. The A/D conversion of the analog input signal results in a corresponding10-bit digital number. In this thesis only one input (RA0) of the microcontroller is usedfor analog to digital conversion. Two 10K resistors are connected to the PIC 16F877as shown below. So, as shown below the analog input is 2,5V. Also the 5V can beconnected to the microcontroller directly. But the current could be high and thendamage the microcontroller.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    15/32

    15

    2.7 SERIAL COMMUNICATION

    The electrical specifications of the serial port are contained in the EIA (ElectronicsIndustry Association) RS232C standard. It states many parameters such as,

    A "Space" (logic 0) will be between +3 and +25 Volts. A "Mark" (Logic 1) will be between -3 and -25 Volts. The region between +3 and -3 volts is undefined. An open circuit voltage should never exceed 25 volts. (In Reference to GND) A short circuit current should not exceed 500mA. The driver should be able to

    handle this without damage.

    There are two sizes of Serial Ports. They are the D-Type25 pin connector and the D-Type 9 pin connector both of

    which are male on the back of the PC, thus we willrequire a female connector on our device. Below is atable of pin connections for the 9 pin D-Type connectors.

    Pin No. Abbreviation Full Name Function

    Pin 3 TD Transmit Data Serial Data Output (TXD)

    Pin 2 RD Receive Data Serial Data Input (RXD)Pin 7 RTS Request To Send This line informs the Modem that

    UART is ready to exchange data.Pin 8 CTS Clear To Send This line indicates that the

    Modem is ready to exchange data.Pin 6 DSR Data Set Ready This tells UART that the modem

    is ready to establish a link.Pin 5 SG Signal Ground GroundPin 1 CD Carrier Detect When the modem detects a

    "Carrier" from the modem at theother end of the phone line, thisline becomes active.

    Pin 4 DTR Data TerminalReady

    This is the opposite of DSR. Thistells the Modem that the UART isready to link.

    Pin 9 RI Ring Indicator Goes active when modem detectsa ringing signal from the PSTN.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    16/32

    16

    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, keepstabs on the timing.

    The diagram above shows the expected waveform from the UART when using thecommon 8N1 format. 8N1 signifies 8 Data bits, No Parity and 1 Stop Bit. A transmissionstarts with a start bit which is (Logic 0). Then each bit is sent down the line, one at atime. The LSB (Least Significant Bit) is sent first. A Stop Bit (Logic 1) is then appendedto the signal to make up the transmission. The above diagram is only relevant for thesignal 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 betweenthese regions (i.e. between +3 and -3 Volts) is undefined. Therefore this signal is putthrough a "RS-232 Level Converter". This is the signal present on the RS-232 Port ofany computer, shown below.

    PIC microcontrollers which weuse require either TTL orCMOS logic levels. Thereforethe first step to connecting themicrocontroller to the RS-232port is to transform the RS-232levels back into 0 and 5 Volts.This is achieved by RS-232Level Converters. The commonlevel converter is the MAX-

    232. The connection of theMax232 to the computer isindicated on the right. RX/TXPins of the Max232 isconnected to RX/TX pins of themicrocontroller in order.Max232 includes a ChargePump, which generates +10Vand -10V from a single 5vsupply.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    17/32

    17

    Max232 also includes two receivers and two transmitters in the same package. This ishandy 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 indicatedabove. All the capacitances used in this circuit are equal to 1 F. Also a serial cable isused to connect this circuit to the computer. This serial cable is approximately 3 metersand has 2 female connectors. Hence we must use a male connector for this circuit.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    18/32

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

    #use delay( clock=speed ) speed is a constant 1-100000000 (1 Hz to 100 MHz). This

    function tells the compiler the speed of the processor and

    enables the use of the delay function: delay_ms ( ).

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

    Discrete I/O Functions

    input ( pin ) This function reads the pin. Pins are defined in the devices .h

    file. This function returns the state of the indicated pin. If the

    pin is low, then the function returns 0, otherwise 1.

    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.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    19/32

    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.

    A/D Conversion Functions

    setup_adc_ports ( value ) value is a constant defined in the devices .h file. It can be a

    pin of any port. This function sets up the ADC pins to be

    analog, digital or a combination.

    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 onthe type of the microcontroller.

    RS232 I/O Functions

    getc ( ) This function waits for a character to come in over the RS232

    RCV pin and returns the character.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    20/32

    20

    Printf ( string ) string is a constant string or an array of characters null

    terminated. This function outputs a string of characters to

    the standard RS-232 pins.

    kbhit ( ) If the RS232 is under software control this function returns

    TRUE if the start bit of a character is being sent on the

    RS232 RCV pin. If the RS232 is hardware this function

    returns TRUE is a character has been received and is waiting

    in the hardware buffer for getc() to read.

    LCD Functions

    lcd_init ( ) This function initializes LCD.

    lcd_putc ( char ) This function will display char on LCD.

    Lcd_goto_xy ( x, y ) Set write position on LCD

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    21/32

    21

    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____________________________________

    //__________________________________________________________________________

    #include //type of microcontroller

    #include //needed for using LCD

    #fuses XT, NOWDT, NOPROTECT, NOLVP //type of oscillator, etc

    #usedelay(clock=4000000) //4MHz crystal is used in oscillator

    #use rs232(baud=9600,parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8)

    //Baud rate, parity bits, number of receiving, transmitting pins, and length of data

    //__________________________________________________________________________

    //___________________________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

    delay_ms(100); //delay 100ms for reading analog data

    battery_value=((2,5*read_adc())/256); //This is the real battery value. Because of having

    //10 bit AD Converter and 4MHz crystal, the accuracy is (210

    -1)/4=256. Also the reference

    //voltage is 2.5V.

    if(battery_value=2.3)

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    22/32

    22

    printf("H"); // H means High

    else

    printf("N"); // N means normal...

    }

    void open_lcd (void) // displaying slide writings

    {

    int x;

    lcd_putc("\f"); //clears display

    for(x=8;x>0;x--)

    { lcd_gotoxy(x,1);

    lcd_putc("CUKUROVA"); //puts character on LCD

    lcd_gotoxy(x,2);

    lcd_putc(" UNI. ");

    delay_ms(500);

    }}

    void forward (void)

    {

    output_b(0x33); //causes motors turn clockwise direction

    lcd_putc("\f MOVING ");

    lcd_putc("\nFORWARD ");

    }

    void back (void)

    {

    output_b(0xCC); //causes motors turn counterclockwise directionlcd_putc("\f MOVING ");

    lcd_putc("\n BACK ");

    }

    void right (void)

    {

    output_b(0x3C); //causes the motors turns opposite directions according to each other.lcd_putc("\fTURNING ");

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    23/32

    23

    lcd_putc("\n RIGHT ");

    }

    void left (void)

    {

    output_b(0xC3); //causes the motors turns opposite directions according to each other.

    lcd_putc("\fTURNING ");

    lcd_putc("\n LEFT ");

    }

    void stop (void)

    { output_b(0x00); //stops all motors

    lcd_putc("\f STOP ");

    delay_ms(2000);

    lcd_putc("\fCUKUROVA");

    lcd_putc("\n UNI. "); }

    void manual (void) //to control the automobile on computer

    {

    battery(); //send the battery value to the PC

    lcd_putc("\f MANUAL ");

    lcd_putc("\nCONTROL ");

    }

    void exit (void)

    {

    output_b(0x00); //stops all motorsopen_lcd();

    }

    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 ");

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    24/32

    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

    elseif(input(pin_e1)) //controls the second sensor output

    output_b(0x3C); //changes motor directions to turn right

    elseif(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()

    {

    unsignedchar selection;

    lcd_init(); //for initializing LCD

    battery(); //controls the battery value and sends it to the PC

    set_tris_b(0x00); //sets port B as an output for motors

    output_b(0x00); //causes all bits of port B being low

    open_lcd();

    while(TRUE)

    {

    while(!kbhit()); //waits until receiving data from PC

    selection=getc(); //receiving data from PC

    switch(selection)

    {case 'F': forward(); break; //forward movement

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    25/32

    25

    case 'B': back(); break; //backward movement

    case 'R': right(); break; //turning right

    case 'L': left(); break; //turning left

    case 'S': stop(); break; //stops the automobile

    case 'M': manual(); break; //controlling the motors on PC

    case 'X': line_follow(); break; //following a line

    case 'E': exit(); break; //exit

    }

    }

    }

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    26/32

    26

    3.2 USER INTERFACE

    Programming computer for serial communication is as important as programming PIC

    microcontroller. In this thesis serial programming of computer is achieved by using

    MSComm component of Visual Basic. MSComm is used for any type of adjustmentsabout serial ports. The basic functions of MSComm and Visual Basic used in this

    program are clarified below.

    Components of MSComm

    CommPort Defines number of serial communication port. COM1,

    COM2, COM3, etc

    PortOpen Activates serial port.

    RThreshold Defines the number of input character which causes

    program jumping OnComm subprogram.

    Settings Defines features of serial port. Adjusts of Baud rate, length

    of data, number of stop bits, etc

    Input Reads data from input buffer.

    InBufferSize Adjusts the input buffer length.

    comEvReceive Returns 1 while receiving data from serial port.

    Output Sends data from computer by serial port.

    Other Functions

    Timer Writes the date and hour on the user form.

    Text Receives data from the user.

    MsgBox Gives a message to the user.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    27/32

    27

    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

    Private Sub MSComm1_OnComm()

    Dim newdata As String

    IfMSComm1.CommEvent = comEvReceive Then receives battery value from PIC.

    newdata = MSComm1.Input

    Ifnewdata = "L" Then If the PIC sends char L,

    Text3 = "Low" then the user is informed that battery is Low

    Text4 = ""

    Text5 = ""

    ElseIfnewdata = "H" Then If the PIC sends char H,Text3 = "" then the user is informed that battery is High

    Text4 = ""

    Text5 = "High"

    Else

    Text3 = ""

    Text4 = "Normal" otherwise the user is informed that battery is Normal

    Text5 = ""End If

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    28/32

    28

    End If

    End Sub

    PrivateSub manual_Click()

    IfMSComm1.PortOpen = False Then If port is close,

    MSComm1.PortOpen = True then, opens the port

    End If

    MSComm1.Output = "M" sends char M to the PIC for manual control

    Text1 = "use manual control buttons, please..."

    End Sub

    PrivateSub linefollow_Click()

    IfMSComm1.PortOpen = False Then If port is close,

    MSComm1.PortOpen = True then, opens the port

    End If

    MSComm1.Output = "X" sends char X to the PIC for following a line

    Text1 = "press stop button to exit..."

    End Sub

    PrivateSub exit_Click()

    IfMSComm1.PortOpen Then If port is open,

    MSComm1.Output = "E" sends char E to the PIC to exit

    MSComm1.PortOpen = False then, closes the port

    End If

    End

    End Sub

    Private Sub forward_Click()

    IfMSComm1.PortOpen Then If port is open,

    MSComm1.Output = "F" sends char F for forward moving

    Text1 = "moving forward..."

    Else otherwise warns the user

    MsgBox ("press manual control, please...")End If

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    29/32

    29

    End Sub

    PrivateSub right_Click()

    IfMSComm1.PortOpen Then If port is open,

    MSComm1.Output = "R" sends char R for turning right

    Text1 = "turning right..."

    Else otherwise warns the user

    MsgBox ("press manual control, please...")

    End If

    End Sub

    Private Sub left_Click()

    IfMSComm1.PortOpen Then If port is open,

    MSComm1.Output = "L" sends char L for turning left

    Text1 = "turning left..."

    Else otherwise warns the user

    MsgBox ("press manual control, please...")

    End If

    End Sub

    PrivateSub back_Click()

    IfMSComm1.PortOpen Then If port is open,

    MSComm1.Output = "B" sends char B for moving backward

    Text1 = "moving backward..."

    Else otherwise warns the user

    MsgBox ("press manual control, please...")End If

    End Sub

    Private Sub stop_Click()

    IfMSComm1.PortOpen Then If port is open,

    MSComm1.Output = "S" sends char S to stop

    Text1 = "stop..."Else otherwise warns the user

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    30/32

    30

    MsgBox ("press manual control, please...")

    End If

    End Sub

    PrivateSub Timer1_Timer ()

    Text2 = Now timer is on

    End Sub

    __________________________________________________________________________

    __________________________________________________________________________

    The compiled form of the user interface is indicated below.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    31/32

    31

    CONCLUSION

    In this paper, PIC16F877 microcontroller based automobile was designed and

    programmed. This automobile has lots of functionalities. It can be controlled using serialport. For implementing this property, a user interface was designed with Visual Basic.

    This automobile can control its battery and warns the user. LCD is used for informing

    the user about all movements of the automobile. Another property is following a black

    line on a white surface. Because of the flexibility of PIC microcontroller, some properties

    can be improved by modifying the program. For example this automobile can move in a

    bounded area after changing some codes. Also, this thesis can be used for different

    applications in industry. After adding a robot arm on this automobile, a dangerousprocess can be done in safety.

  • 8/8/2019 40474363 Micro Controller Based on an Automobile

    32/32

    32

    REFERENCES

    [1] PIC 16F877 Data Sheet, Microchip Technology Inc., 2001. www.microchip.com

    [2] Interfacing the Serial/RS232 Port v5.0, http://www.senet.com.au/~cpeacock

    [3] Prof. Dr. Doan brahim, PIC ve PC iletiim Projeleri, 2004, Bileim yaynevi.

    [4] www.antrak.org.tr

    [5]www.endtas.com

    [6] www.robot.metu.edu.tr

    http://www.microchip.com/http://www.senet.com.au/~cpeacockhttp://www.antrak.org.tr/http://www.antrak.org.tr/http://www.senet.com.au/~cpeacockhttp://www.microchip.com/

Recommended