robotics experiment with pic microcontroller l 1

Upload: david-miranda

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    1/88

    Rob otics expe riment with PIC m icroc ontrollerl1

    Robotics experiment withPIC microcontroller

    based-on Robo-PICA robot kit

    3rd Edition

    (C) Innova tive Experiment Co.,Ltd .

    INNOVATIVE EXPERIMENT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    2/88

    2lRob otics expe rime nt w ith PIC m ic roc ont roller

    Contents

    Chapter 1 Part list of Robo-PICA and Introduce software tools...............5

    1.1 Robo-PICA pa rt list

    1.2 Hand tools for ma king rob ot kit

    1.3 Softw are d eve lop ment tools for Rob ot prog ramm ing

    Chapter 2 RBX-877V2.0 Robot Controller board...................................25

    2.1 Tec hnica l features

    2.2 Circuit description

    Ac tivity 1 : Write p rograms for testing RBX-877 V2.0 Controller board

    Chapter 3 Building Robo-PICA kit..............................................................35

    Activity 2 : Make the Robo-PICA

    Chapter 4 Simple robot s programming control...................................45

    Ac tivity 3 : Simp le moveme nt c ontrol

    Ac tivity 4 : Speed control of Rob o-PICA

    Chapter 5 Contactless object detection...............................................57

    5.1 PIC16F8875s A/ D c onve rter

    5.2 ADC reg iste r

    5.3 ADC c onfiguration

    5.4 A/ D Conversion p roc ed ure

    5.5 GP2D120 : 4 to 30cm. Infrared distance sensor

    Ac tivity 5 : Rea ding the Ana log signa l

    Ac tivity 6 : Testing GP2D120

    Ac tivity 7 : Conta c tless ob jec t d etec tion robo t

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    3/88

    Rob otics expe riment with PIC m icroc ontrollerl3

    Chapter 6 Line following mission..............................................................71

    6.1 Infrared reflector sensor

    Activity 8 : Reading the Line tracking sensor

    Ac tivity 9 : Moves follow the b lack line

    Chapter 7 Remote control experiment...................................................79

    7.1 38kHz Infrared receiver module

    7.2 Infrared remote control 4 channels

    Activity 10 : Reading Remote control data

    Ac tivity 11 : IR control Rob o-PICAs movem ent

    Appendix A : Activating the License Key

    of mikroC compiler................................................................87

    E mikroC is registered trademark of mikroElektronika (www.mikroe.com).PIC and PICkit2TM a re reg istered trad em a rks of Mic roc hip Tec hnolog y

    (www.microchip.com).

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    4/88

    4lRob otics expe rime nt w ith PIC m ic roc ont roller

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    5/88

    Robotics experiment with PIC microcontrollerl5

    Chapter 1Part list of Robo-PICA and

    Introduce software tool

    1.1 Robo-PICA part list

    There a re 2 groups :

    1.1.1 Mechanical parts

    1.1.2 Electronic parts

    1.1.1 Mechanical parts

    Motor Gearbox Uses

    a 4.5V (9V max.) and

    180 mA DC motor with

    a ratio of 48:1; torque

    4kg/cm; comes w i th

    the mounting.

    Many sizes of Screw and

    Nut

    (Sc rew : 3x6mm.,3x10mm.,3x15mm.,3x25mm. and

    3x35mm., 3mm. nuts), Flat

    hea d sc rews and Thumb

    sc rew s.

    Set of Plastic Spacers

    (length : 3mm., 15mm.

    and 25 mm.)

    Hex Standoffs : 3x30mm.

    Track wheel set - includes 3-

    leng ths of Track wheel, many

    support wheels and

    sprockets, axels and shaft

    bases

    The Plate set and 4-types

    of the color-mixed Plastic

    Joiner (10 of Straight

    Joiner, 10 of Right-angle

    Joiner, 10 of Ob tuse

    Joiner and 3/5/12 Holes

    straight joiners)

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    6/88

    6lRobotics experiment with PIC microcontroller1.1.2 Electronic parts

    ZX-03

    Infrared Reflector

    (x2)

    ZX-01

    Switch inp ut

    (x2)

    ZX-IRM

    38kHz Infrared Receiver

    1.2 Tools for making the robot kit

    Cutter plierA sha rp -tipp ed

    hobby kni fe or

    Handy CutterPhilips Sc rewdriver

    Computer

    Insta ll Wind o w s98SE o r

    higher and has both RS-232

    seria l port and Parallel port

    GP2D120

    4 to 30cm. Infrared

    Distance sensor

    ER-4

    Infrared

    Remote Control

    RBX-877V2.0 PIC16F887 Robot Experiment Board

    USB Programmer board

    with ICD2 cable

    USB cable

    4 of AA batteries

    (Rechargable battery is

    recommended

    - not include this kit)

    ZX- POTH

    Potentiometer (x1)

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    7/88

    Robotics experiment with PIC microcontrollerl7

    1.3 Software development tools for Robot programming

    The RoboPICA kit uses the PIC Mic rcontroller PIC16F887. Builders c an w rite the

    c ontrolled p rog ram in a ssembly, BASIC and C langua ge . Only BASIC and C prog ram

    language requires the use of a compiler software.

    However in this kit all examples are in C language with mikroC compiler from

    mikroElektronika (mikroE : www.mikroe .com ). The Rob o-PICA robot kit c an use this com -

    piler as well.

    The dem o version of Mikro C c om piler is used for this robot kit. Builders who nee d

    to develop the advance program will need to purchase the full version from MikroE at

    the ir web iste . The d emo version o f mikroC c an be do wn load ed f rom h t tp : / /

    ww w.mikroe .co m. How eve r in the Rob o-PICA rob ot kit, this softwa re is in the b und led

    CD-ROM. You must download the mikroC manual latest version from mikroElektronika

    web site. This build ing manua l does not desc ribe a ll the instruc tions.

    Ano the r one too ls is PIC m icroc ontroller prog ramme r sof tware. The Rob o-PICA

    provides a USB prog ramm er. Its func tion is c om patible Microc hip s PICkit2TM program-

    mer. The softw are can use PICkit2TM programming software. Free downlaod the latest

    version at www.microchip.com.

    1.3.1 mikroC Compiler (Demo version)

    1.3.1.1 Overview

    mikroC is a powerful, feature rich development tool for PICmicros. It is designed

    to provide the customer with the easiest possible solution for developing applications

    for embedded systems, without compromising performance or control.

    mikroC provides a suc c essful ma tc h fea turing h ighly advanc ed IDE, ANSI c om -

    pliant compiler, broad set of hardware libraries, comprehensive documentation, and

    plenty of ready-to-run examples.

    mikroC allows you to quickly develop and deploy complex applications:

    l Write your C source code using the highly advanced Code Editor

    l Use the included mikroC libraries to dramatically speed up the development:

    data acquisition, memory, displays, conversions, communications

    Spec ial thanks : All info rma tion about m ikroC Co mp iler and PICkit2 Programm ing softwa re are

    referenc ed from owner web site a nd d oc umentation (www.mikroe.co m a nd w ww.mic roc hip.com ).

    Thanks for all free and op en-source d eve lope me nt tools. User who nee d the full fea tures of m ikroC

    compiler can purchase on-line at www.mikroe.com.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    8/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    9/88

    Robotics experiment with PIC microcontrollerl9(A) Install of the Microsoft .NET Framework

    First thing to do is to insta ll the Mic rosoft.NET Framew ork. Selec t from the

    folder PICkit 2 Setup v2.01 dotNET

    dotnetfx in the bundled CD-ROM. Double-click atdotnetfx.exe file. After complete, install the PICkit2TM Prog ramm ing Softw are by double-

    click at PICkit2Setup.msi file. The softw are insta lla tion will sta rt.

    (B) Microsoft .NET Framework is installed ready

    User c an insta ll the PICkit2TM Prog ram ming Softwa re by ente r to folder PICkit

    2 Setup v2.01x in the bund led CD-ROM of Rob o-PICA kit. Doub le-c lick at PICkit2Setup.msi

    file. The softw are insta lla tion will sta rt.

    1.3.2.1.2 Install from the internet.

    Visit the Microchip website at www.microchip.com. Selec t Development tools

    webpage and enter to PICkit 2 Programmer/ Debuggerwebpage.

    (A) Install of the Microsoft .NET Framework

    For user who have not insta ll Mic rosoft .NET Framew ork, they will need to

    install it first via downloading the file from topic PICkit2V2.01 Install with .NET Frame-

    work. You will get the PICkit 2 Setup v2.01 dotNET.zip file (version number may vary).

    Extract this file and store it in the folder PICkit 2 Setup v2.01 dotNET. Enter to this folder

    and into the dotnetfx folder. Double-click at dotnetfx.exe file to start Microsoft .NET

    Frame wo rk insta lla tion. After this is c om pleted , insta ll the Pickit2TM Prog ramm ing Soft -

    ware by double-clicking on the PICkit2Setup.msi file. THe software insta lla tion will sta rt.

    (B) Microsoft .NET Framework is installed ready

    Users who ha ve Mic rosoft .NET Frame wo rk alrea dy insta lled c an d ow n-

    loa d the setup file from PICkit2V2.01 Installheader. You w ill get file PICkit 2 Setup v2.01.zip

    (version number may be vary) Extract this file and store in the folder PICkit 2 Setup v2.01.

    Enter to this folder and double-click on the PICkit2Setup.msi file to start the software

    installation.

    After run the installation setup file ; PICkit2Setup.msi. Click on the accept button

    on each step and follow the installation progress until it is finished.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    10/88

    10lRob ot ics expe riment with PIC m icroc ontroller1.3.2.2 Using PICkit2TM Programming Software

    1.3.2.2.1 Testing hardwa re connection

    (1) Connect the USB cable b etween the prog ramm er and Comp uters USB port.

    Open the software Pickit2TM Prog ramming Softwa re b y ente ring the Sta rt All programs

    Microchip Pickit 2 V201. The ma in window will appea r as show n in figure 1-1.

    (2) On successful connnection, the message PICkit 2 found and connected will

    appea r in the Sta tus box.

    Figure 1-1 : The screen of Pickiit2TM Programming Software

    Status boxCommand menu

    Progress bar

    Short cutbutton

    Inform thedestinationof HEX file

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    11/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    12/88

    12lRob ot ics expe riment with PIC m icroc ontroller1.3.2.2.2 Command menu description

    FILE

    Import File Import a hex file for programming

    Export File Export a hex file read from a device

    Exit Exit the program (duplicated with the Quit button)

    DEVICE FAMILY

    Baseline (12-bit Core) Configures the programming software for baseline Flash

    dev ices

    Mid-range - Configures the programming software for 14-bit core flash de-

    vices. The dev ice s in this rang e inc lude PIC12F6xx and 16F6xx, 7x, 7xx, 8x, 8xx. When

    selec ted , softw are w ill chec k the c onnec tion ta rge t a t ICD2 and ICSP terminal. If found

    the correct device, device number will appear at Device line in Midrange Configura-

    tion box. Click the OK button to continue. For RBX-877V2.00 board would be use this

    group chip because the controller board provides PIC16F887; it is mid-range PIC

    microcontroller.

    PIC18F - Configures the programming software for PIC18F core flash devices.

    PIC18F_J_- Configures the programming software for PIC18FxxJxx low voltage

    devices.

    PIC24 - Configures the programming software for 16-bit core devices; PIC24FJxx.

    dsPIC30 - Configures the programming software for 16-bit core devices; dsPIC30Fxx.

    dsPIC33 - Configures the programming software for 16-bit core devices;

    dsPIC33Fxx.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    13/88

    Robotics experiment with PIC microcontrollerl13PROGRAMMER

    Read Device Reads the program memory, data EEPROM memory, ID loca-

    tions, and Configuration bits.

    Write Device Writes the program memory, data EEPROM memory, ID loca-

    tions, and Configuration bits.

    Verify Verifies the program memory, data EEPROM memory, ID locations and Con-

    figuration bits read from the target MCU against the code stored in the programming software.

    Erase Performs a bulk erase o f the ta rge t MCU. OSCCAL and band gap va l-

    ues are p reserved (PIC12F629/ 675 and PIC16F630/ 676 only).

    Blank Check Performs a blank check of program memory, data EEPROM

    memory, ID locations and Configuration bits.

    Verify on Write - Verifies program memory, data EEPROM memory, ID locations

    and Configuration bits read from the target MCU against the code stored in the pro-

    gramming software with word per word.

    Full Erase (OSCCAL and BG erased) Performs a bulk erase inc luding the OSCCAL

    and Band Gap (BG) va lues (PIC12F629/ 675 and PIC16F630/ 676 only).

    Regenerate OSCCAL Regenerates the O SCC AL va lue (o nly for PIC12F629/

    675 and PIC16F630/676). The AUX line must b e connec ted to the RA4/T1G p in.

    Set Band Gap Calibration Value Sets the b and ga p value. Write on PICkit Button - Set for supp orting of p rog ram ming the ta rge t mic roc on-

    troller witth PROG RAM switch on the USB programmer bo ard.

    TOOLS

    Enable Code Protect Enables code protection for Flash program memory.

    Enable Data Protect Enables code protection for EEPROM data memory.

    Set OSCCAL - Sets the OSCCAL va lue for alignm ent internal c loc k freq uenc y.

    Target VDD Source Pow er ta rget from the USB Programme r.

    Auto-Detect: Selec t to USB prog ramm er turn on o r off the supply vo ltage

    to target microcontroller automatically (not suggess to use this option).

    Forced PICkit2: Set the prog ramm er to supply the suitab le vo ltage to ta r-

    get microc ontroller. After selec t, LED at Targer po sition will light a nd a t VDD PICkit2 box

    on screen will check atr On position. User can adjust the supply voltage from selection

    box in the right-hand (not suggess to use this option).

    Forced Target: Select to inform the softwa re knows ab out the ta rge t has

    voltag e a pplied . Suggess to use th is option fo r safty operation. Also in this option, user

    must apply the supply voltage to the target PIC microcontroller.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    14/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    15/88

    Robotics experiment with PIC microcontrollerl15(b) Access only Flash program memory

    Click at Enabled box in EEPROM data border to remove the mark. At

    EEPROM data border will show Preserve device EEPROM data on write in red message.It means the EEPROM data memory will be protected. User can access only Flash pro-

    gram memo ry. See the illlustration b elow.

    1.3.2.3 Updating the USB Programmer Firmware

    To upd ate the p rogrammer firmware Operating System, complete the following steps.

    (1) Download the la test PICkit 2 Operat ing System from the Microc hip we b site

    a t w ww .mic roc hip .co m. Bec a use the Rob o-PICA s USB p rog ra mm er is c om pa tible

    Microchips PICkit2TM programmer.

    (2) From the menu, select Tools Download PICKit 2 OS Firmware , as shown in

    figure below

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    16/88

    16lRob ot ics expe riment with PIC m icroc ontroller(3) Brow se to the d irec tory whe re the la test Op erating System c od e was saved ,

    Selec t the PK2*.hex file and click on the Open button as shown in figure below.

    (4) The p rog ress of the O S update will be d isp layed in the sta tus bar of the p ro-

    gramming softwa re and the Busy LED on the USB Microcontroller Programm er will flash.

    When the update completes successfully, the status bar will display Operating System

    Verified and the Busy LED will go out. The op erating system update is then com plete .

    1.3.2.4 Short cut button

    The PICkit2TM Programming Software has 7 short cut b uttons as follow s :

    (1) Read : Read data from target MCU.

    (2) Write : Write or program the code into target MCU.

    (3) Verify : Verify programming.

    (4) Erase : Erase data in target MCU.

    (5) Blank Check : Check blank data in target MCU.

    (6) Import Hex File + Write Device : Open the HEX file a nd prog ram into ta rge t MCU

    automatically

    (7) Read Device + Export Hex File : Read device and save as the HEX file automatically.

    1.3.2.5 ICD2 cable assignmentThe USB Prog ram mer comes with an ICD2 ca ble for connec ting be tween the p ro-

    grammer and the targe t boa rd. The w ire assignment of this cable is shown below.

    Vpp

    GND

    PGCPGM

    PGD

    Vdd

    Top view

    ~~

    ~~

    VppVddGND

    PGCPGD

    PGM

    VppVdd GND

    PGCPGD

    PGM

    $

    ICD2 cableICD2 jack RBX-877 V2.0 Robot Controller board

    USB

    Programmerside

    Vpp

    GND

    PGCPGM

    PGD

    Vdd

    ICD2 jack pin assignment

    on target board

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    17/88

    Rob otics expe riment with PIC m icroc ont rollerl17

    1.4 Programming devleopment for Robo-PICA

    The summa ry of steps of p rog ram the Rob o-PICA rob ot kit a re as follow s :

    1. Create the C p rojec t file w ith m ikroC IDE softwa re.

    2. Compile the project file.

    3. If any error occurs, edit the C program to fix the error and compile the

    project file until all are correct.

    4. The HEX file would b e c rea ted a fter the c om pila tion is completed .

    5. Op en the PICkit2TM Prog ramm ing softw are. Connec t the USB prog ramm er

    with USB port and connec t the ICD2 ca ble b etw een the USB Prog ramm er and the RBX-

    877 V2.0 Controller board at ICD2 jack.

    6. Download the HEX file to the RBX-877 V2.0 Controller board of Robot-

    PICA.

    7. Run the p rog ram and chec k the ha rdwa re o pe rat ion. If it is not c orrec t,

    go back to edit the C program, compile and download again. Do these steps unitl the

    operation are completed.

    1.5 Getting Start

    From here, we will desc ribe abo ut the g etting sta rt o f programming d evelopment

    for the Rob o-PICA. This rob ot kit is controlled by the RBX-877 V2.0 Rob ot Controller boa rd.

    The hea rt of this controller bo ard is PIC16F887 chip. The p rog ramm ing develop ment in-

    c ludes 2 ma in step s as C prog ramm ing d eve lop ment and Downloa d the HEX file to

    microcontroller.

    The C prog ramm ing d eve lop ment will be using m ikroC IDE inc luded C c ompiler

    and the other provides support tools and libraries. Howeve r this kit w ill work with the demo

    version. You can purchase the full version for more programming at www.mikroe.com.

    You c an de velop the C projec t file a nd test the op eration o f the Rob o-PICA s hard-

    wa re from these p roc ed ures be low.

    1.5.1 Insta ll the mikroC softwa re too ls following the instruction manua l. See this doc u-

    ment in Rob o-PICA s CD-ROM o r downloa d the upda te d oc ument from www.mikroe.c om.

    1.5.2 Insta ll the PICkit2 Program ming software for USB programmer.

    1.5.3 Op en the mikroC IDE by c lic king at Sta rt Programs Mikroelktronika

    mikroC mikroC . The m a in window w ill appea r. The Figure 1-1 shows the ma in windo w o f

    mikroC IDE and the imp ortant c ompo nents.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    18/88

    18lRob otics expe rime nt w ith PIC m icroc ontroller

    Target microcontroller

    device

    Complie button

    Clock frequency ofmicrocontroller

    Program editor area

    Output window

    Figure 1-1 : The main window of mikroC IDE and the important components

    need to know.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    19/88

    Rob otics expe riment with PIC m icroc ont rollerl191.5.4 Create the new project file by entering to menu Project and select New

    Project...

    1.5.5 The New Project window will app ea r. You must set the impo rtant parameter

    as follows :

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    20/88

    20lRob otics expe rime nt w ith PIC m icroc ontroller(a ) Projec t Name : Put the projec t na me . mikroC IDE will crea te the folder to

    support your projec t file which inc ludes the C sourcec od e. For examp le isBlink_LEDproject

    file.

    (b ) Project Path : Selec t the location of your projec t file. Click the Browse

    butto n to select the loc at ion. For examp le is D:\ ROBO-PICA

    (c ) Description : Put the informa tion of your porjec t file. For example is Robo

    PICA Code Blinking LED on RB3

    (d ) Device : Selec t the target mic roc ont roller. For the Rob o-PICA kit a nd

    RBX-877V2.0 Controller board must select to PIC16F887

    (e) Clock : Selec t the c loc k freq uenc y for the ta rge t mic roc ontroller. For the

    Robo-PICA kit a nd RBX-877V2.0 Controller boa rd use 20MHz c loc k. Put the va lue 020.000000.

    (f) Device Flags : Set the c onfiguration for the targe t mic roc ontroller. Developer

    can set very ea sy by Default button . The Default will set the 3 ma in configurations

    as follows :

    High Speed Osc illa tor enabled (HS_OSC)for 10MHz and abo ve c loc k

    frequency.

    Watchdog timer disabled (WDT_OFF)

    Low Voltage Programming disabled (LVP_OFF)

    After the configuration is being set, c lick on the OK button. mikroC IDE would

    c lose the New Project window a nd c reate the Blink_LED.C file with the blank editor area

    for writing the C p rog ram .

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    21/88

    Rob otics expe riment with PIC m icroc ont rollerl21

    void main()

    {TRISB.F3=0; // Set RB3 as Outputwhile(1) // Infinite Loop{

    PORTB.F3=0; // LED_ONDelay_ms(500);PORTB.F3=1; // LED_OFFDelay_ms(500);

    }}

    Listing 1-1 : Blinking LED test code

    1.5.6 Type the C p rogram follow ing the Listing 1-1.

    1.5.7 Click on the Build Project button or Ctrl+F9 for co mp iling the projec t file.

    1.5.8 Observe the error message at the Output window. If all is correct, it would

    show the size o f usage p rog ram mem ory of th is file a nd Suc cess message.

    After that, you will get the HEX file; Blink_LED.HEX for downloading to the

    Robo-PICAs controller board; RBX-877V2.0.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    22/88

    22lRob otics expe rime nt w ith PIC m icroc ontroller1.5.9 Put 4 of AA batteries into battery holder of the RBX-877V2.0 Controller board.

    1.5.10 Co nnec t the USB programer with PCs USB port

    1.5.11 Connec t the ICD2 ca ble be tween the USB programme r and the RBX-877V2.0

    Controller board.

    1.5.12 Turn-on the power to the RBX-877 V2.0 Controlller board.

    1.5.13 Open the PICkit2TM Programming software.

    1.5.14 If a ll connec tions are c orrec t, the PICkit2TM Prog ramm ing softw are will chec k

    the target microontroller automaticcaly and show PIC16F887 is found.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    23/88

    Rob otics expe riment with PIC m icroc ont rollerl231.5.15 Selec t the HEX fle which require prog ram to microc ontroller by entering menu

    File Import Hex. The op en HEX file window will ap pea r. Selec t to D:\ ROBO-PICA\ Blink_LED

    for selec ting the Blink_LED.hex

    1.5.16 Click on the Write button to dow nload HEX file to the RBX-877 V2.0 Controlller

    board.

    1.5.17 Observe the result a t RB3 LED on the RBX-877 V2.0 Controlller board.

    RB3 LED of the RBX-877V2.0 Controller board blinks always.

    INNOVATIVE EXPERIMENT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    24/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    25/88

    Robotics experiment with PIC microcontrollerl25

    Robo-PICA robotic kit is c ontrolled by the RBX-877 V2.0 (PIC16F887 Robot Expe ri-

    ment b oa rd). The ma in microc ontroller is the PIC16F887. Figure 2-1 shows op erating d ia-

    gram of RBX-877 board. In this chapter will present the operation of RBX-877 board and

    some example experiment. Builders must read and test all experiments for building and

    programming the robot in next chapter.

    ICD2 Programminginterface

    UART Serial interface

    Programmable Analoginput and DigitalInput/Output port

    IC bus interface

    Driving RC servomotor circuit

    Driving DC motorcircuit

    DrivingPiezo speaker

    LCD moduleinterface

    +5V Switchingpower supply

    Batterysupply(AA x4)

    Low Batterystatus circuit

    PIC16F887microcontroller

    +5V

    20MHz clock circuit

    +5V

    Figure 2-1 : RBX-877 V2.0 Robot controller boards block diagram

    Chapter 2RBX-877V2.0

    Robot Controller Board

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    26/88

    26lRob otics expe riment with PIC m icroc ontroller

    2.1 Technical features

    l Controlled by PIC16F887 Microc ontroller with 8Kword memory. Run with 20MHz c lock

    l Download the program via ICD2 jack.

    l LCD16x2 display with LED back light and jumper to on/off control

    l Piezo speaker

    l a LED monitor

    l Drive 2-DC motors 4.5V to 6V and 3-RC Servo mo tors (in range 4.8 to 6V)

    l 9-Prog ram ma ble po rts supp ort all ana log inout and d igtial input/ outp ut

    l I2C bus port

    l UART port fo r inte rfac ing the serial d ev ice suc h a s RS-232 transc eiver, XBee

    module and Bluetooth.

    l Supp ly voltage from 4 of AA ba tteries (Rec hargable b at tery is rec ommended)

    l 2.375 x 6.25 Inches size

    Figure 2-2 : RBX-877 V2.0 Robot controller board layout

    ICD2 in-system programming jack

    Battery terminal

    POWERswitch

    DC motor output

    (connect RC2, RD0, RD1

    and RC1, RD2, RD3)

    Servo motor output

    (RC5, RB4 and RB5)Interrupt switch

    (RB0/ INT)

    Programma ble I/ O port

    (RA0-RA3, RA5, RE0-RE2)

    LCD connector

    (RD2, RD3, RD4-RD7)

    Piezo speaker

    (RC0 )

    LED monitor

    (RB3)

    I2C connector

    (RC3 and RC4)

    PIC16F887 microcontroller

    UART connector

    (RC6 and RC7)

    Interrupt port

    RA4 switch

    LOW BAT.indicator

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    27/88

    Robotics experiment with PIC microcontrollerl27

    2.2 RBX-877 V2.0 board circuit description

    2.2.1 Microcontroller circuit

    The heart of this board is the PIC16F887 microc ontroller. The 20MHz ceramic reso-

    nator, CR1 is used to make the 20MHz clock for PIC16F887.

    2.2.2 Power supply

    The RBX-877 V2.0 bo ard conta ins a step-up switching p ow er supply to supp ly +5V

    regulated for PIC16F887. Although the level of battery will decrease when driving the

    motor. This switc hing p ow er supply c irc uit will ma intain the +5V for microc ontroller until

    battery voltage level down to 1.5V

    S1 is on-off switc h to supp ly the vo ltag e from batte ries to RBX-877 V2.0 board . R3,

    D1 and ZD1 ard used to limit the input voltage to IC2 not over 5.1V

    IC1 is a switching power supply IC, NCP1450-5.0. It can support input voltage 1.5

    to 4.2V range for regulating +5V supply voltage. ZD2 is used to limit output voltage of

    NCP1450-5.0 not over +5V.

    2.2.3 In-System Programming circuit

    The RBx-877 V2.0 boa rd req uire In-system programming via ICD2 or ISP c onnec -

    to r. The USB p rog ramme r wh ich is bund led in the Rob o-PICA kit w ill c onnec t to ICD2 jac k of the RBX-877 V2.0 controller boa rd. It use the supp ly voltage from USB port o f

    computer.

    The program ming signal will send to RB6 and RB7 pin of PIC16F887. The high volt-

    age programming is sent to MCLR pin. All programming status would be show on the

    PICkit2 Programming software on computers monitor. After programming complete,

    this controller can work suddenly.

    2.2.4 Display circuit

    Character display : The RBX-877 V2.0 boa rd provides LCD m od ule c onnec tor. It

    supports 16 characters 2 lines LCD. PIC16F877s RD4 to RD7 pin are assigned to D4 to D7

    data p ins, RD3 to E p in and RD2 to RS p in for selec tion d ata mod e. VR1 is used to c ontrast

    adjustment of LCD screen. In case using Back-light LCD, it provides a jumper to control

    the LED back-light of LCD.

    LED monitor : RBX-877 V2.0 board ha s a g eneral purpose LED. They are c on-

    nected to RB3 of PIC16F887 microcontroller via a current limited resistor.

    Sound output: RBX-877 V2.0 board has a sound driver circuit. Connect RC0 pin to

    a piezo speaker via a capacitor 10F. This c irc uit c an d rive aud io frequenc y signa l.

    However the piezo speaker has the resonance frequency of range 1kHz to 3kHz.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    28/88

    28lRob otics expe riment with PIC m icroc ontroller

    Figure 2-3 : Schematic diagram of RBX-877 V2.0 Robot Controller board

    23

    18

    37RB4

    RD0

    RD1

    RB1

    RB2

    RC4/SDA

    RC3/SCL

    RC6/TxD

    RC7/RxD

    CR1Ceramic

    Resonator

    20MHz

    13 14

    9

    2

    7

    1

    15

    10

    16

    13 12 5 4

    IC4

    L293D

    1A

    2A

    12EN

    4A

    3A

    Vcc1

    34EN

    1Y

    2Y

    4Y

    3Y

    3

    6

    14

    11

    R112k2

    LED4DIR. #A

    LED5DIR. #B

    K18A-K18BMOTOR-A

    K19A-K19BMOTOR-B

    K17AN7/RE2

    K16AN6/RE1

    K15AN5/RE0

    K14AN4/RA5

    K9AN3/RA3

    K13AN2/RA2

    R12

    2k2

    DIRECT

    INVERT

    DIRECT

    INVERT

    +

    +

    +

    +8

    +Vm

    +VmK8

    SERVO1

    38RB5

    +VmK9

    SERVO2

    35

    34

    20

    19

    6

    +5V

    24RC5

    +VmK10

    SERVO3

    +5V

    R54k7

    SW2

    RESET

    R61k

    RB6

    +5V

    11 32

    1

    RB740

    39

    MCLR

    +5V

    25

    26

    +5V

    RC2/CCP1 17

    RC1/CCP216

    R30.47

    ZD15.1V

    L1

    10H

    CE

    OUTGND4

    5

    D2MBRA340T3

    Q1

    FDS6680A

    ZD25.6V

    IC2

    NCP14505.0

    +5V

    1

    2

    C4

    1000F

    6.3V

    C5

    0.01F

    50V

    R4

    1k

    LED2ON

    Vpp

    GND

    PGCJ1ICD2

    PGD

    K12AN1/RA1

    K11AN0/RA0

    RE2/AN7

    RE1/AN6

    RE0/AN5

    RA5/AN4

    RA3/AN3

    RA2/AN2

    RA1/AN1

    RA0/AN02

    3

    4

    5

    7

    8

    9

    10

    K5TxD/RC6

    K6RxD/RC7

    K3SDA/RC4

    K4SCL/RC3

    +5V

    +5V

    R84k7

    R74k7

    RA4

    RB0/INTK7

    INT/RB0

    +5V

    R9150

    DSP1LCD 16x2 (back light option)

    4

    6

    RS

    E

    D7 D6 D5 D4 D3 D2 D1 D0 R/W

    +5V

    BLA

    +V

    Vo

    GNDBLK

    JP1BACK LIGHT

    ON

    14 13 12 11 10 9 8 7 5 15 2

    3

    1

    16

    R5

    47

    VR110k

    BRIGHTNESS

    RD2

    RD3

    21

    22

    RD7

    RD6

    RD5

    RD4

    33

    36RB3

    R10510

    LED3RB3

    27

    28

    29

    30

    RC0

    SP1PIEZO

    15

    12

    31

    PGD

    +5V

    Vpp

    +5V

    K2ISP

    IC3PIC16F887

    C6

    0.1F50V

    C7

    10F 50V

    C9

    0.1F 50V

    C8

    0.1F 50V

    C2

    0.1/50V

    +Vm

    K1DC input

    +4.8~6Vdc

    S1POWER

    C1100F

    10V

    D11N5819

    +

    - R11k

    LED1LOW-BAT.

    IC1KIA7042

    1

    3

    2

    GND

    +V

    RESET

    R247k

    C3100F

    10V

    PGC

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    29/88

    Robotics experiment with PIC microcontrollerl292.2.5 Programmable port

    The RBX-877 V2.0 board p rovides 9-prog ramm able multipurpose p orts. It inc ludes

    RA0-RA3, RA5, RE0-RE2 and RB0 pin. All port pin can p rogram to 3 func tions as

    (1) Analog input - to get analog signal to A/D converter circuit inside

    microcontroller. Input voltage range is 0 to 5V. Converter resolution is 10-bit.

    (2) Digital input - to get digital signal from digital device and switch.

    (3) Digital output - to d rive d igital signa l log ic 0 and 1 to external device.

    In default all port will be set to analog input port.

    On RBX-877 V2.0 board p rovides a ll ports in 3-pin JST connec to r. Eac h c onnec tor

    includes +5V and GND.

    2.2.6 UART port for serial data wired/ wireless communication

    Builders can make the serial data communication from RBX-877 V2.0 board to

    c omputer s RS-232 serial port and many wireless serial device suc h as XBEE module and

    Blueto oth. PIC16F887 mic rocontroller p rovide s RC6 a nd RC7 p in UART mod ule p ort p in

    for this purpose. Seria l signa l from PIC16F887s are c onnec ted to 2 free JST connec tors

    for support all serial device.

    2.2.7 DC motor driver circuit

    The RBX-877 V2.0 boa rd use IC4, L293D H-bridg e moto r driver IC a re used fo r

    d riving 2 channel DC m otors. The suitab le motor is 4.5-6V 100 to 200mA or up to 400mA.

    Motor A speed is controlled by RD0 with RD1 pin and enable by RC2 port. Motor

    B speed is controlled by RB1 with RB2 pin and enable by RC1. LED4 and LED5 are bi-color

    LED. They a re used for showing the motor outp ut sta tus.

    Voltage is supplied to L293D includes +5V supply voltage and Motor supply volt-

    age (+Vm). The +Vm is c onc entrated d irec t from ba tteries for powerful driving.

    2.2.8 RC servo motor driver circuit

    The RBX-877 V2.0 Controller bo a rd p rovid es 3 port p ins fo r RC servo moto rs. It

    inc ludes RB4, RB5 and RC5 . RC servo moto r supply comes from system ba ttery. This driver

    c annot support high-current and high p ow er RC servo m otor. The suitab le RC servo

    motor is 4.8 to 6V motor and need current consumption about 100-200mA.

    2.2.9 I2C connector

    A way to expansion of RBX-877 V2.0 board is using a I2C bus connector. Many

    external devic e need I2C bus protocol such as Real-time clock, memory, A/D and D/A

    c onve rter, Port expa nsion dev ice and etc . RC3/ SCL and RC4/ SDA of PIC16F887 a reconnected to I2C bus connector includes +5V supply and GND. No any pull-up resistor

    are connected to theses port. User must provides them at the external devices.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    30/88

    30lRob otics expe riment with PIC m icroc ontroller

    Activity 1

    Write programs for testingRBX-877 V2.0 Controller board

    Procedure

    For all activities of the programming development for Robo-PICA robot kit have

    the summary of steps are as follows :

    1. Create the C project file with mikroC IDE software.

    2. Compile the project file.

    3. If any error happens, edit the C program to fix the error and compile the

    project file until all are correct.

    4. The HEX file would b e c rea ted after the c om pila tion is c omplete d.

    5. Connec t the USB prog ramm er with USB port and c onnec t the ICD2 ca b le

    between the USB Programmer and the RBX-877 V2.0 Controller boa rd at ICD2 jac k.

    Robo-PICARRobotRPICRMicroRobotic

    ICD2

    4.8-6V

    BATT

    ON

    M-1

    M-2

    RESET

    RA4 RB0RB0/INT RA0 RA1 RA2 RA3 RA5 RE0 RE

    RC3

    SCL

    RC4

    SDA

    RC6

    TxD

    RC7

    RxD

    LCD

    DOWNLOAD

    POWER

    ON

    #1

    #2

    #3 S

    ERVO

    S+

    ICD2 cableUSB Programmer

    USB port

    6. Open the PICkit2TM Programming software and checking the connection.

    7. Download the HEX file to the PIC16F887 on the RBX-877 V2.0 Controller

    board of Robot-PICA.

    8. Run the p rog ram a nd chec k the hardw are op eration. If it is not c orrec t,go back to edit the C program, compile and download again. Do these steps unitl the

    operation are complete.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    31/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    32/88

    32lRob otics expe riment with PIC m icroc ontrollerActivity 1-2Reading digital data via switch and driving sound

    See the figue A1-2, it shows the schema tic of the switch input o f the RBX-877 V2.0

    Controller boa rd. The switc h tested in this ac tivity is the RA4-switch. If switch is not p ressed ,DATA p oint a s log ic 1 from pull-up resisto r 10k. If switc h is pressed, DATA p oint w ill

    connec t to ground . It c auses DATA p oint is logic 0. PIC16F887 will drive a sound fol-

    lowing the activated swtich at RA4 pin.

    Reading switch input programming

    The easiest way to c hec k this switc h being p ressed in C p rog ram of m ikroC c om-

    piler is looping and check with IF command. If switch is being pressed, the program will

    jump to the following condition. In writing the program, you must select the port that

    interface the switch first.

    Figure A1-2 : RA4 Switch input schematic of the RBX-877 V2.0 Controller

    board

    6

    +5V

    R54k7

    SW2RESET

    R61k

    +5V

    11 32

    1MCLR

    +5V

    R84k7

    R74k7

    RA4

    RB0/INTK7

    INT/RB0

    +5V

    R9150 33

    RC0

    SP1

    PIEZO

    15

    12

    31

    1+!

    21+$.&&%

    C6

    0.1F

    50V

    C7

    10F 50V

    CR1

    Ceramic

    Resonator

    20MHz

    13 14

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    33/88

    Robotics experiment with PIC microcontrollerl33

    Listing A1-3 : The C program of reading digital value from the Switch input

    at RA4 pin to control the sound generation of Piezo speaker at RC0 pin. The

    operation is similar the door chime.

    Testing

    A1.2.1 Write the Listing A1-3. Comp ile a nd downloa d the c od e to RBX-877 boa rd.

    A1.2.2 Press the switch at RA4 and observe the operation of the Piezo speaker on the

    RBX-877 V2.0 Robot Controller board.

    Listen sound from the piezo speaker following the switch pressing.

    void main(){Sound_Init(&PORTC, 0); // Init Soundwhile(1){if (!PORTA.F4) // Test RA4 keypresssound_play(250,50); // 2kHz sound ON RC0

    }}

    Activity 1-3 Show message on LCD moduleThe RBX-877 V2.0 Rob ot Co ntroller bo ard p rov ides the c onnec to r to interfac e

    LCD moud ule. The sc hematic d iag ram is shown in the Figure A1-3. User must use this

    information to define in the C program for mikroC compiler knows the port pin that use

    in this interface.

    When interfac ing, you wil req uire 6 po rt p ins which inc ludes the RD2 for RS p in of

    LCD module, RD3 for E pin and RD4 to RD7 for data pin D4 to D7 in 4-bit interface mode.

    The R/ W pin of LCD is c onne c ted to g round for only writing a ll da ta to LCD. With this

    c onnec tion, help d evelope rs to m ake the C cod e for interfac ing the LCD module ea sier.Because you can use the LCD built-in function of mikroC compiler; Lcd_Init(&PORTD).

    Testing

    A1.3.1 Write the Listing A1-4. Compile and download the code to RBX-877 V2.0 Robot

    Controller board.

    A1.3.2 Observe the operation.

    At LCD module show message Innovative on the upper line and Experiment on

    the lower line. If need to use the back-light LED, put jumper at LCD backlight position.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    34/88

    34lRob otics expe riment with PIC m icroc ontroller

    Listing A1-4 : The C program for displaying message on LCD module of

    RBX-877 V2.0 Robot Controller board

    Figure A1-3 : LCD interface schematic of RBX-877 board

    +5V

    R54k7

    SW24-5-6

    R61k

    +5V

    11 32

    1MCLR

    DSP1LCD 16x2 (back light option)

    4

    6

    RS

    E

    D7 D6 D5 D4 D3 D2 D1 D0 R/W

    +5V

    BLA

    +V

    Vo

    GNDBLK

    JP1BACK LIGHT

    ON

    14 13 12 11 10 9 8 7 5 15 2

    3

    1

    16

    R13

    47

    VR1

    10kBRIGHTNESS

    RD2

    RD3

    21

    22

    RD7

    RD6

    RD5

    RD427

    28

    29

    30

    1+!

    21+$.&&%

    C6

    0.1F50V

    CR1

    Ceramic

    Resonator

    20MHz

    13 14

    12

    31

    char *text1 = "Innovative";char *text2 = "Experiment";

    void main(){

    Lcd_Init(&PORTD);Lcd_Cmd(LCD_CURSOR_OFF);while(1){

    Lcd_Out(1,1,text1);Lcd_Out(2,1,text2);

    Delay_ms(5000);Lcd_Cmd(LCD_CLEAR);Delay_ms(500);

    }}

    INNOVATIVE EXPERIMENT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    35/88

    Rob ot ics expe riment with PIC m ic roc ont rollerl35

    This c hapter desc ribes about how to build ing the Rob o-PICA rob ot kit. The features

    of Robo-PICA robot kit are as follows :

    l Driving with DC motor gearboxes and Track whee l

    l Controlled by PIC16F887 microcontroller

    l 8KWords prog ram memory

    l Re-prog ramm able a t least 10,000 times for flash p rog ram memo ry

    l Support many types of sensor and dete c tor such as

    ZX-01 Switch input boa rd for attacking detec tion,

    ZX-03 Infrared Reflector for line tracking and area,

    ZX- IRM Infrared receiver module for remote controlling,

    GP2D120 Infrared distance sensor,

    SRF05 Ultrasonic sensor,

    CMPS03 Digita l com pass,

    Memsic2125 Accelerometer sensor

    and more...

    l Provides Character LCD moduel 16x2 and LED status for displaying the robot

    operation.

    Chapter 3Building Robo-PICA kit

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    36/88

    36lRob otics expe rime nt w ith PIC m ic roc ont roller

    Activity 2

    Make the Robo-PICA

    Figure A2-1 : Shows the parts for making a Robo-PICA.

    30mm. Hexstandoffs x 3

    Long angled shaft base x 2

    Thumb screw x 3

    3mm. spacer x 2

    Metal axel x 3

    Hub x 6 Main sprocketwheel x 2

    Medium support wheel x 2

    3-hold Straightjoiner x 2

    10-joint track wheel x 4

    30-joint track wheel x 2Right angle joiners x 3

    3x10mm. Screw x 153x15mm. Screw x 1

    3mm. Nut x 11

    2mm. Wood screw x 2

    DC motor gearboxwith mounting x 2

    Infrared reflectorwith cable x 2

    GP2D120 x 1

    Large support wheel x 2

    RBX-877 PIC16F887 controller

    board

    Short angled shaftbase x 2

    Universal Plate x 1

    Obtuse joiners x 3

    Straight joiners x 3

    38kHz receivermodule x 1

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    37/88

    Rob ot ics expe riment with PIC m ic roc ont rollerl37A2.1 Fix 2 of DC mo tor gea rboxes at the base. Turn the extrude side o f the right gea rbox

    out side shown in Figure A2-2. Tighten the 3x10mm. sc rews from botto m side to fix this

    gearbox. Lea ve the inside ho le o f the left g ea rbo x. Do not t ighten the sc rew.

    A2.2 Insert the main sprocket to the gearboxs shaft and fix with 2mm. Wood screw. Do

    both DC gearboxes.

    A2.3 Put up side do wn. Atta ch the Long ang led shaft ba se w ith the ba se a t the spec ific

    position a s shown in the figure A2-6. Tighte n the 3x10mm . sc rew to a leave ho le from step

    A2.1.Next, tight a 3x10mm . sc rew and 3mm. nut to fix the sec ond hole of the Long angled

    shaft base as shown in the figure A2-6.

    Turn the extrude side to outsideand tight a screw from bottom side

    Top side

    3x10mm. screw

    3x10mm. screw

    Bottom side

    Leave thegearboxs hole

    Figure A2-2

    Figure A2-3

    Top side

    Main sprocket

    2mm. Wood screw

    Figure A2-4

    Figure A2-5

    Hole for tightening a 3x10mm.screw and 3 mm. nut

    Bottom side

    3x10mm. screw

    Figure A2-6

    Long angledshaft base

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    38/88

    38lRob otics expe rime nt w ith PIC m ic roc ont rollerA2.4 Attache the rest of Long angled shaft base w ith a base b y inserted the 3x10mm. screws

    from top side through the hole and tighten with 3mm. nuts following the Figure A2-7.

    A2.5 Turn the b ase over. Atta c h 2 of the Short ang led sha ft bases at the front o f the robot s

    base a s shown in the Figure A2-8 by inserted the 3x10mm. sc rews from b ottom side throug h

    the shaft b ases holes and tighten w ith 3mm. nuts. Tighten the sc rew on the inside ho le.

    Lea ve the outside holes.

    A2.6Fix a Hexagonal standofff at bottom side of base by put upside down and tight a

    3x10mm. screw through a left corner hole and the Right angle joiner.

    3mm. nut3mm. nut

    3mm. nut

    Bottom side

    Long angledshaft base

    Figure A2-7

    Turn the bottom side up

    Rightgearbox

    Top side ofthe base

    A left cornerback side holeof the base

    Right anglejoiner

    Hexagonal standoff

    3x10mm. screw

    Figure A2-8

    3mm. nut

    Leave holes

    3mm. nut

    Top side

    Top side

    Short angledshaft base

    Figure A2-9

    3x10mm. screw

    Bottom side

    Right angle joiner

    30mm. Hexagonalstandoff

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    39/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    40/88

    40lRob otics expe rime nt w ith PIC m ic roc ont roller

    The example shown above is only a sample to show you the standard type of

    track width used. You can of course assemble your own track length based on

    your own requirements for your robot.

    A2.9 Crea te tw o trac k belts by put ting the d ifferent size trac ks tog ethe r. One trac k would

    consist of the fo llow ing : One 30-joint trac k and two 10-joint trac ks. Connec t a ll trac ks

    toge ther. Take one end and connec t it to the other end of the trac k to form one c om-

    plete loop. Repeat the steps to make two track sets. If the track is too tight or loose, you

    can either adjust the length of the track or adjust the position of the short angled shaft

    ba se until the trac k has a good fit.

    A2.10 Attach the tracks to the supporting wheels of the robot.

    Figure A2-12

    Figure A2-13

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    41/88

    Rob ot ics expe riment with PIC m ic roc ont rollerl41A2.11 Attach the RBX-877 V2.0 controller board on top of robots chasis. Please fix the

    bo ard w ith the Power swtc h a t the side where the DC mo tor gea rbo xes are. Sec ure w ith

    3 Thumb sc rews at the end s.

    Thumbscrew

    Thumbscrews

    Straight joiner

    Obtuse joiner 3x15mm. screw

    A2.12 Attach a ZX-IRM 38kHz Receiver module sensor board with the Obtuse joiner using

    3x15mm . sc rew and 3mm. nut. Insert a Straight joiner at another end of the Ob tuse joiner.

    JST3AA-8 sensor cable

    Figure A2-14

    Figure A2-15

    A2.13 Attach a Right angle joiner at the center hole of the back side (Power switch side)

    of the RBX-877 V2.0 controller board by a 3x10mm . sc rew and 3mm. nut fo r a tta ching the

    ZX-IRM sensor board.

    Right angle joiner

    3x10mm. screw

    Figure A2-16

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    42/88

    42lRob otics expe rime nt w ith PIC m ic roc ont rollerA2.14 Connect the ZX-IRM structure from step A2.12 to the Right angle joiner on the RBX-877

    V2.0 controller boa rd from step A2.13. Plug in the Zx-IRM sensor c able to RB0/INT connector.

    ZX-IRM

    RB0

    A2.15 Plug the DC m oto r gearboxes cab le to Motor connec tors. The right motor is con-nected to the white M-2 output and left motor is connected to the black M-1 output.

    However the motors pole (white or black connector) can be changed depending on

    the programming and mission. Normally, refer from the motor outputs indicator, if both

    light green, it means the forward movement and both light red mean the backward

    movement. You can change later if the operation incorrect.

    M-1 motor

    M-2 motor

    Figure A2-17

    Figure A2-18

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    43/88

    Rob ot ics expe riment with PIC m ic roc ont rollerl43A2.16 Install the ZX-03 Infrared reflector sensor board at the bottom of the robots chasis.

    Attach the sensor with the end hole o f the 3-hole Straight joiner by inserted the 3x10mm .

    screw through the sensor board, 3mm. plastic spacer, joiner and tighten with 3mm. nut.

    Install both side; left and right.

    A2.17 Atta ch a GP2D120 module with a Right a ng le joiner as shown in the Figure A2-21 by

    3x10mm. sc rew and 3mm. nut.

    GP2D120

    Right angle joiner

    Tighten 3x10mm. screwwith 3mm. nut

    A2.18 At the front o f rob ot, insert a 3x10mm . sc rew through a cente r hole p osition of the

    RBX-877V2.0 board and 3mm. nut from top side as shown in the Figure A2-22. Do not

    tighten. Next, Insert the GP2D120 structure from step A2.17 between a sc rew and control-

    ler boa rd ( see the Figure A2-23). Tighte n the sc rew to fix a ll together.

    Infrared reflector sensors

    3mm. spacer

    3mm. nut

    Figure A2-19

    Figure A2-20

    Figure A2-21

    Figure A2-22 Figure A2-23

    Insert theGP2D120structureand tightenthe screwto fix it.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    44/88

    44lRob otics expe rime nt w ith PIC m ic roc ont rollerA2.19 Plug the GP2D120 cable to RA2 port, the left ZX-03 sensors cable to RA0 port and

    the right ZX-03 sensors cable to RA1 port.

    GP2D120

    Left ZX-03

    Right ZX-03

    ZX-IRM

    A2.20 Arrange all cables and check all connection carefully. Your Robo-PICA is

    now ready for programing.

    INNOVATIVE EXPERIMENT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    45/88

    Robotics experiment with PIC microcontrollerl45

    The first thing is to c ontrol rob ot Mo vement . The hea rt of this mo vement is DC

    motor c ircuit. In Rob o-PICA has DC motor gea rbox in d riving . The Figure 4-1 shows the

    DC motor circuit. PIC16F887 assigns 6 port pins to connect the DC motor driver circuit

    for driving 2 motors.

    The mo tor d riving mec hanism are d ivided into 4 types as follow s :

    (1) Clockwise motor driving

    (2) Anti-clockwies motor driving

    (3) Motors shaft is free

    (4) Motors shaft is locked or Braked

    Chapter 4Simple robot s programming control

    Figure 4-1 : The DC motor driver schematic of RBX-877 V2.0 board

    RD0

    RD1

    RB1

    RB2

    CR1

    Ceramic

    Resonator

    20MHz

    13 14

    9

    2

    7

    1

    15

    10

    16

    13 12 5 4

    IC4

    L293D

    1A

    2A

    12EN

    4A

    3A

    Vcc1

    34EN

    1Y

    2Y

    4Y

    3Y

    3

    6

    14

    11

    R11

    2k2LED4

    DIR. #A

    LED5DIR. #B

    K18A-K18BMOTOR-A

    K19A-K19BMOTOR-B

    R12

    2k2

    DIRECT

    INVERT

    DIRECT

    INVERT

    +

    +

    +

    +8

    +Vm

    35

    34

    20

    19

    +5V

    R54k7

    SW2RESET

    R61k

    +5V

    11 32

    1MCLR +5V

    RC2/CCP1 17

    RC1/CCP216

    12

    31

    IC3

    PIC16F887

    C60.1F

    50V

    C9

    0.1F 50V

    C8

    0.1F 50V

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    46/88

    46lRob ot ics expe riment with PIC m icroc ontroller

    Table 4-1 : Shows logic signal to control motor direction

    Motor operation

    Shaft free

    Shaft locked or Brake

    Clockwise turning

    anti-clockwise turning

    Shaft locked or Brake

    12EN/34EN pin 1A/3A pin 2A/4A pin

    0 X X

    1 0 0

    1 0 1

    1 1 0

    1 1 1

    X means logic "0" or "1"

    The hea rt of DC motor driver circuit is the L293D H-Bridge d river (ma y be rep lac ed

    by SN754410). In the Tab le 4-1 show s a ll the required signa ls to c ontrol the DC m otor

    driver circuit.

    L293D outputs connects to DC motor gearbox and provides LED status for motor

    supp ly voltage. If power is sup plied DIRECTLY, the LED will light up in Green. When it is

    opposite, if red LED lights up , it means the supply vo ltage is INVERTED. Builders can use

    the different color for defining direction. In other words, if red LED are turned on, the

    robot will be moving backwards. If the green LED are turned on, the robot will be mov-

    ing forwards.

    4.1 Motor library file

    For better performance and ease of programming, we make the library for driving

    and movement controls for the DC motors. It is the motor.hlibrary file. The souc e cod e o f

    this library is shown in Listing 4-1.

    You can use simple text editor t ocreate this library and save as .h file or open

    mikroC IDE to create this file. After that copy this library file to the library folder of mikroCsoftw are. The location is C:\ Program Files\ Mikroelektronika\ mikroC\ include. You must

    copy the motor.hfile to this folder. Because the complier will link to this folder for includ-

    ing any library.

    motor.h library file consists of many functions of movement control. Include :

    Motor_Init : Initial the micrococontroller port pin for interfacing the DC

    motor d river c ircuit.

    Change_Duty : Control the motors speed.

    Motor_A_FWD : Drive motor A (M-1 output) to forward direction (LED indi-

    cates of M-1 lights in green).

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    47/88

    Robotics experiment with PIC microcontrollerl47char motor_duty_= 127; // Defalt PWM 50%char motor_init_=0; // Status initial

    // *** Motor A *****// PD0 ====> 1A// PD1 ====> 1B// PC2 ====> 1E (PWM1)

    // *** Motor B *****// PB1 ====> 2A// PB2 ====> 2B// PC1 ====> 2E (PWM2)

    //****************************************************//********** Initial Motor Function ******************//****************************************************void Motor_Init()

    {if (motor_init_==0) // First time ?{

    motor_init_=1; // StatusANSELH.F0=0; // RB1 ==> Digital IOANSELH.F2=0; // RB2 ==> Digital IOTRISB.F1=0; // Motor B 2ATRISB.F2=0; // Motor B 2BTRISD.F0=0; // Motor A 1ATRISD.F1=0; // MOtor A 1BPwm1_Init(5000); // Initail PWM 1EPwm2_Init(5000); // Initail PWM 2E

    }

    }//****************************************************

    //****************************************************//********** Control Duty Cycle *********************//****************************************************void Change_Duty(char speed){if (speed != motor_duty_) // Check Same old speed{motor_duty_=speed; // Save for old speedPwm1_Change_Duty(speed); // Motor APwm2_Change_Duty(speed); // Motor B

    }}//****************************************************

    /********** Motor A Forward ********/void Motor_A_FWD(){

    Pwm1_Start();PORTD.F0 =0;PORTD.F1 =1;

    }/************************************/

    Listing 4-1 : The source code of motor.h library file for driving the DC motor(continue)

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    48/88

    48lRob ot ics expe riment with PIC m icroc ontroller

    Listing 4-1 : The source code of motor.h library file for driving the DC motor(continue)

    /********** Motor B Forward ********/void Motor_B_FWD(){

    Pwm2_Start();PORTB.F1 =0;PORTB.F2 =1;

    }/************************************/

    /********** Motor A Backward *******/void Motor_A_BWD(){

    Pwm1_Start();PORTD.F0 =1;PORTD.F1 =0;

    }/************************************/

    /********** Motor B Backward *******/void Motor_B_BWD(){

    Pwm2_Start();PORTB.F1 =1;PORTB.F2 =0;

    }/************************************/

    /********** Motor A Off ************/void Motor_A_Off(){

    Pwm1_Stop();PORTD.F0 =0;PORTD.F1 =0;

    }/************************************/

    /********** Motor B Off ************/void Motor_B_Off(){

    Pwm2_Stop();PORTB.F1 =0;PORTB.F2 =0;

    }/************************************/

    /********** Go Forward ************/void Forward(char speed){

    Motor_Init();Change_Duty(speed);Motor_A_FWD();Motor_B_FWD();

    }/************************************/

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    49/88

    Robotics experiment with PIC microcontrollerl49

    /********** Go Backward ************/void Backward(char speed){

    Motor_Init();Change_Duty(speed);Motor_A_BWD();Motor_B_BWD();

    }/************************************/

    /********** Spin Left *************/void S_Right(char speed){

    Motor_Init();Change_Duty(speed);Motor_A_FWD();Motor_B_BWD();

    }/************************************/

    /********** Spin Right ************/void S_Left(char speed){

    Motor_Init();Change_Duty(speed);Motor_A_BWD();Motor_B_FWD();

    }/************************************/

    /********** Stop Motor ************/void Motor_Stop(){

    Motor_Init();Change_Duty(0);Motor_A_Off();Motor_B_Off();

    }/************************************/

    Listing 4-1 : The source code of motor.h library file for driving the DC motor(final)

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    50/88

    50lRob ot ics expe riment with PIC m icroc ontrollerMotor_B_FWD : Drive motor B (M-2 output) to forward direction (LED indi-

    cates of M-2 lights in green).

    Motor_A_BWD : Drive motor A (M-1 output) to backward direction (LEDindicates of M-1 lights in red).

    Motor_B_BWD : Drive motor B (M-2 output) to backward direction (LED

    indicates of M-2 lights in red).

    Motor_A_off : Turn off or Stop mo tor A (M-1 output).

    Motor_B_off : Turn off o r Stop motor B (M-2 output).

    foward : Drives both DC motror to move the Robo-PICA forward.

    backward : Drives both DC motror to move the Robo-PICA backward.

    S_right : Drives both DC motror to spin the Robo-PICA in right direction.

    S_left : Drives both DC motror to spin the Robo-PICA in left direction.

    Motor_stop : Stop both DC mo tror.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    51/88

    Robotics experiment with PIC microcontrollerl51

    Robo-PICA moves forward or backward by driving both DC motor gearboxes in

    same direction and at the same time. If need to turn or rotate, below shows the method :

    1. Stop one motor and Drive another one If stop left motor and drive right motor,

    the robot will turn left. In the opposite direction, stop right motor and drive left motor.

    The robot will turn right. The spee d of movem ent is simila r. The p ivot turning p oint o f th is is

    a t the sta tiona ry trac k. See the Figure A3-1.

    Figure A3-1Turning method by stopping a motor and fix a wheel.

    Turning point Turning point

    Turning point Turning point

    Figure A3-2 Turning method by driving both motors in opposite direction.

    Activity 3

    Simple movement control

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    52/88

    52lRob ot ics expe riment with PIC m icroc ontroller

    Listing A3-1 The movement program demonstration of Robo-PICA

    2. Drive both motors in opposite direction If the left motor drives forward and

    right motor drives backward, the robot will rotate right direction. If its in the opposite

    d irec tion, the left m oto r drives backward a nd right mo tor d rives forwa rd . The robot w ill

    rotate left direction instead. In this method the speed of rotation will be increase 2 times

    and less fric tion. The turning point is center of robot bod y. See Figure A3-2.

    A3.1 Write p rog ram fo llow ing the Listing A3-1 then compile a nd dow nloa d to RBX-877

    V2.0 Rob ot Controller boa rd . Turn-off pow er switc h.

    #include void main(){Sound_Init(&PORTC, 0); // Init Sound

    while(1){Forward(255); // Call ForwardDelay_ms(2000);sound_play(100,50); // 1 kHz sound ON RC0

    S_Left(255); // Call Spin LeftDelay_ms(800);sound_play(100,50); // 1 kHz sound ON RC0

    Forward(255); // Call ForwardDelay_ms(2000);sound_play(100,50); // 1 kHz sound ON RC0

    S_Right(255); // Call Spin RightDelay_ms(800);sound_play(100,50); // 1 kHz sound ON RC0

    Forward(255); // Call ForwardDelay_ms(2000);sound_play(100,50); // 1 kHz sound ON RC0

    Backward(255); // Call BackwardDelay_ms(1000);sound_play(100,50); // 1 kHz sound ON RC0Motor_Stop; // Stop all

    }

    }

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    53/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    54/88

    54lRob ot ics expe riment with PIC m icroc ontroller

    Robo-PICA can control the speed movement by send the signal to the enable

    pin (EN) of motor driver IC, L293D. Refer the figure 4-1 (in this c hapte r), EN pin of L293D is

    connec ted to RC2/ CCP1 and RC1/ CCP2 port pins of PIC16F887. Both port p ins a re PWM

    output port. Builders can write the program to control the PWM output signal for adjust-

    ment m otor speed .

    PWM operationNorma l driving mo tor tec hnique is app ly the vo ltage to mo tor direc tly. The moto r

    works in full speed. Sometime this speed faster. Then the simp le me thod to control motor

    speed is control the voltage applied to motor. The p op ulate te chnique is PWM (pulse-

    width mod ula tion). This tec hnique will control the w id th o f the positive p ulse. The vo lt-

    age is applied to motor as average value. Ratio of positive pulse width and totally

    pulse width is called Duty cycle. Its unit is percentage (%)

    4.8V 4.8V

    2.4V

    Volt

    Time

    Volt

    Time

    50%

    duty cycle

    Average voltage = 2.4V

    Figure A4-1 : Shows average voltage output of PWM

    (A) Full volatge apply. (B) 50% duty cycle PWM(C) 75% duty cycle PWM (D) 25% duty cycle PWM

    Activity 4

    Speed control of Robo-PICA

    (A) (B)

    (C) (D)

    4.8V

    3.6V

    Volt

    Time

    4.8V

    1.2V

    Volt

    Time

    75%duty cycle

    Average voltage = 3.6V

    25%

    duty cycle

    Average voltage = 1.2V

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    55/88

    Robotics experiment with PIC microcontrollerl55

    For Robo-PICA, we prepare the speed control with PWM technique via soft-

    ware by the motor.hlibrary file. You can see detail in motor.h sourcecode in Listing 4-1

    (in this chapter). motor.h library has PWM function for supporting 2 PWM modules of

    PIC16F887 as fo llow s :

    Pwm1_Change_Duty(speed); // Motor A Duty

    Pwm2_Change_Duty(speed); // Motor B Duty

    You can put the required duty cycle value in (speed). The range is 0 to 255 for 0

    to 100% duty cycle.

    A4.1 Write the Listing A4-1. Com pile a nd dow nloa d the c od e to Rob o-PICA. Turn-off

    power switch.

    A4.2 Remove the downlaod cable from Robo-PICA.

    A4.3 Place the rob ot on the floor. Turn-on power to run the p rog ram. See the o peration.

    The Rob o-PICA rob ot will move fo rward fastest in 2 sec onds and sp in left

    5 sec ond . After that the rob ot will move forawa rd with fastest spe ed ag ain. The robot

    will move this routine all times.

    Listing A4-1 : The speed control program for Robo-PICA by using PWM.

    #include char i;void main()

    {Forward(255); // Motor Forwardwhile(1){

    Delay_ms(2000);Pwm1_Change_Duty(220); // Motor A 85% DutyPwm2_Change_Duty(255); // Motor B 100% DutyDelay_ms(5000);Pwm1_Change_Duty(255); // Motor A 100% Duty

    }}

    E The suitable PWM duty cycle value for driving the robot is more than 70%.If selec t the less va lue, the rob ot has not torque mo re enough for turningor rotation.

    INNOVATIVE EXPERIMENT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    56/88

    56lRob ot ics expe riment with PIC m icroc ontroller

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    57/88

    Rob otics expe rime nt w ith PIC m ic roc ont rollerl57

    The o ne of m ost important func tion of mo bile rob ot is interfac ing the sensors. Rob o-

    PICA can interface with many type of sensors. Because it has both digital and analog

    inputs. PIC16F887 the main microcontroller of Robo-PICA has many ports. We assign 9

    programmable port pins for supporting the analog and digital sensors. In addtion 2 types

    of seria l c oo munication p orts; UART and I2C bus.

    In this chapter, we will concentra te to interfac ing with angalog sensors. The Rob o-

    PICA kit provides 2 kinds of analog sensors ; GP2D120 the infrared distance sensor and

    ZX-03 Infrared Reflec to r sensors for line trac king a c tivities.

    5.1 PIC16F887s A/ D converter

    PIC16F887 microc ontt roller conta ins 14-cha nnel 10-bit ana log to d ig ita l co nverter

    module (ADC). All analog input p orts can b e c onfigured to digital input a nd o utput. They

    inc lude RA0 to RA3, RA5, RB0 to RB5 and RE0 to RE2.

    The Ana log -to-Dig ita l Converter (ADC) a llow s c onve rsion o f an analog input signa l

    to a 10-bit binary rep resentation of tha t signa l. This device uses ana log inputs, which are

    multiplexed into a single samp le and hold c irc uit. The output of the samp le a nd hold is

    c onnec ted to the input of the c onverter. The converter generates a 10-bit b inary result

    via suc cessive approximation a nd sto res the c onversion result into the ADC result reg isters

    (ADRESL and ADRESH).

    The ADC vo ltage reference is softwa re selec tab le to either VDD or a voltage ap-

    plied to the external reference pins.

    5.2 ADC register

    The important reg iste r of this mo dule a re ADCON0 and ADCON1 register. The

    ADCON0 is used to select the analog pin fucntion and ADCON1 is used to select the

    result data format and voltage reference.

    Chapter 5Contactless object detection

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    58/88

    58lRob otics expe rime nt w ith PIC m ic roc ont roller5.2.1 ADCON0 : A/ D Control register 0

    Deta il of each b it in ADCON0 reg ister is shown b elow.

    ADCS1 CHS1

    % $ # " !>EJ

    R/W-0

    ADCS0 CHS3 CHS2 CHS0GO/

    DONEADON

    R/W-0 R/W-0 R/W-0R/W-0 R/W-0R/W-0R/W-0

    bit 7 and 6 - ADCS1, ADCS0 : A/ D Conversion Cloc k Selec t b its

    00 = FOSC/2

    01 = FOSC/8

    10 = FOSC/ 32

    11 = FRC (c loc k derived from a ded icated internal osc illa tor = 500 kHz ma x)

    bit 5 to 2 - CHS3 to CHS0 : Ana log Channel Select bits

    0000 = AN0 (RA0 pin)

    0001 = AN1 (RA1 pin)

    0010 = AN2 (RA2 pin)

    0011 = AN3 (RA3 pin)

    0100 = AN4 (RA5 pin)

    0101 = AN5 (RE0 p in)

    0110 = AN6 (RE1 p in)

    0111 = AN7 (RE2 p in)

    1000 = AN8 (RB2 pin - reserve for DC moto r circ uit o f the RBX-877V2.0 Robot

    Controller b oa rd)

    1001 = AN9 (RB3 pin - reserve for LED monito r of the RBX-877V2.0 Robot

    Controller b oa rd)

    1010 = AN10 (RB1 pin - reserve for DC moto r circ uit o f the RBX-877V2.0 Robot

    Controller b oa rd)

    1011 = AN11 (RB4 pin - reserve for servo motor output of the RBX-877V2.0

    Robot Controller board)

    1100 = AN12 (RB0 pin - alternative func tion wih External Inte rrput a nd Swtich

    of the RBX-877V2.0 Rob ot Controller boa rd)

    1101 = AN13 (RB5 pin - reserve for servo motor output of the RBX-877V2.0

    Robot Controller board)

    1110 = CVREF

    1111 = Fixed Ref (0.6 vo lt fixed referenc e)

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    59/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    60/88

    60lRob otics expe rime nt w ith PIC m ic roc ont roller5.2.3 ANSEL : Analog Select register

    The ANSEL reg ister is used to configure the Input mod e o f an I/O p in to analog.

    Sett ing the a pprop ria te ANSEL b it high w ill c ause a ll d igita l rea ds on the p in to be read as0 and allow analog functions on the pin to operate correctly.

    Deta il of ea ch b it in ANSEL reg ister is shown b elow .

    ANS7 ANS3

    % $ # " !bit

    R/W-1

    ANS6 ANS5 ANS4 ANS2 ANS1 ANS0

    R/W-1 R/W-1 R/W-1R/W-1 R/W-1R/W-1R/W-1

    bit 7 to 0 - ANS7 to ANS0 : Analog Selec t b its

    Analog select between analog or digital function on pins AN or RE2,

    RE1, RE0, RA5, RA3, RA2, RA1 and RA0 respe c tive ly.

    1 = Analog input. Pin is assigned as analog input (d efa ult).

    0 = Dig ital I/ O. Pin is assigned to port or spec ial func tion.

    5.2.4 ANSELH : Analog Select High register

    The ANSELH reg ister is used to configure the Input m od e of an I/O p in to analog .

    Sett ing the approp ria te ANSELH bit high w ill ca use a ll digital rea ds on the p in to be rea d

    as 0 and allow ana log functions on the pin to operate c orrec tly. The p ort pins which a re

    controlled by this reg iste r consists of AN8 to AN13 (RB2, RB3, RB1, RB4, RB0 and RB5).

    Deta il of each b it in ANSELH registe r is shown below.

    - ANS11

    % $ # " !bit

    R/W-1

    - ANS13 ANS12 ANS10 ANS9 ANS8

    R/W-1 R/W-1 R/W-1U-0 R/W-1R/W-1U-0

    bit 7 and 6 - Unimplemented: Rea d as 0

    bit 5 to 0 - ANS13 to ANS8 : Analog Selec t b its

    Ana log selec t b etw een ana log or digita l function on p ins AN or RB5,

    RB0, RB4, RB1, RB3 and RB2 respec tive ly.

    1 = Analog input. Pin is assigned as analog input.

    0 = Dig ital I/ O. Pin is assigned to port or spec ial func tion.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    61/88

    Rob otics expe rime nt w ith PIC m ic roc ont rollerl61

    5.3 ADC configurationFor using the ADC module of PIC16F887 microcontroller the following functions

    must b e conside red :

    lPort configurationlChannel selectionlADC voltage referenc e select ionlADC conversion clock sourcelResults forma tt ing

    5.4.1 Port configuration

    The ADC c an be used to c onvert bo th analog and digital signa ls. When c onvert-

    ing analog signals, the I/O pin should be configured for analog by setting the associated

    TRIS and ANSEL bits.

    5.4.2 Channel selection

    The CHS b its of the ADCON0 reg ister dete rmine which c hannel is c onnec ted to the

    samp le and ho ld c irc uit. When c hanging c hannels, a d elay is req uired befo re sta rting the

    next conversion.

    5.4.3 ADC Voltage referenceThe VCFG b its of the ADCON0 reg ister provide indep end ent control of the positive

    and nega tive voltage referenc es. The positive voltage referenc e c an b e either Vdd or

    an external voltage source . Likewise, the nega tive voltag e reference c an be e ither Vss or

    an external voltage source.

    For the RBX-877V2.0 Robot Controller board will select the positive reference to

    +5V and negative reference at ground or Vss.

    5.4.4 Conversion Clock

    The source of the conve rsion c lock is softwa re selec table via the ADCS bits of the

    ADCON0 register. There a re four possible c lock op tions:

    lFOSC/ 2 : for 20MHz c lock, TAD = 100nslFOSC/ 8 : for 20MHz c lock, TAD = 400nslFOSC/ 32 : for 20MHz c lock, TAD = 1.6slFRC (dedicated internal oscillator) : TAD = 2 to 6s

    The time to c omplete one b it c onve rsion is defined as TAD. One full 10-b it c onve r-

    sion requires 11 TAD periods.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    62/88

    62lRob otics expe rime nt w ith PIC m ic roc ont roller5.4.5 Result formatting

    The A/ D converter result will sto re in a pa ir of regete r; ADRESH:ADRESL. They will

    keep the d a ta forma t follow ing the selec tion o f ADFM b it.

    If Left justified is selected (ADFM = 0), ADRESH register keeps 8 upper bits

    and ADRESL register keeps 2 lower bits.

    If Right justified is selected (ADFM = 1), ADRESH register keeps 2 upper bits

    and ADRESL register keeps 8 lower bits.

    b b b b b b b b b b 0 0 0 0 0 0

    10-bit Result

    # " ! ' & % $ # " !

    ADFM = 0 Left justified result

    b b b b b b b b b b0 0 0 0 0 0

    10-bit Result

    # " ! ' & % $ # " !

    ADFM = 1 Right justified result

    The result da ta forma t from ADFM bit selection of ADCON1 register

    5.5 A/ D Conversion procedure

    This is an exam ple p roc ed ure fo r using the ADC to perform an Ana log -to-Digita l

    conversion p f PIC16F887 microc ontroller:

    1. Configure Port:

    l Disable pin output driver

    l Configure pin as analog by setting ANSEL or ANSELH register

    2. Configure the ADC mod ule (b y sett ing ADCON0 reg ister) :

    l Selec t ADC c onve rsion c loc k

    l Configure voltage reference

    l Select ADC input channel

    l Selec t result forma t by set ting ADCON1 register

    l Turn on ADC mo dule

    3. Configure ADC interrupt (optional):

    l Clear ADC interrupt flag

    l Enable ADC interrupt

    l Enable peripheral interrupt

    l Enable global interrupt.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    63/88

    Rob otics expe rime nt w ith PIC m ic roc ont rollerl634. Wait the required acquisition time.

    5. Sta rt c onversion by set ting the GO/ DONE bit in ADCON0 reg ister.

    6. Wait for ADC c onversion to complete b y one of the follow ing:

    l Polling the GO/DONE bit

    l Waiting for the ADC interrupt (interrupts ena bled )

    7. Read ADC Result. The result da ta will sto re in ADRESH and ADRESL reg iste r.

    8. Clear the ADC interrupt flag (req uired if interrup t is ena bled ).

    5.6 GP2D120 : 4 to 30cm. Infrared distance sensorOne o f the spec ial sensors in rob ot ics is the Infrared Dista nc e sensor. Som e p eo ple

    c all it the IR Range r. With the GP2D120 mod ule, it ad ds the d istanc e m ea suring and Ob -

    stacle detection using infrared light feature to your robot. Your Robo-PICA robot can

    avoid obstacles without having to make any physical contact.

    5.6.1 GP2D120 features

    l Uses Infrared light reflection to measure range

    l Can measure a rang e from 4 to 30 cm.

    l 4. 5 to 5 V pow er supply and 33mA e lec tric c urrent

    l

    The outp ut vo ltage range is 0.4 to 2.4V whe n supplied by +5V

    Infrared LED transmitter Infrared Receiver

    GNDVout Vcc

    /2,

    4 8 12 16 20 24 28 3200

    0.4

    0.8

    1.2

    1.6

    2.0

    2.4

    2.8

    Output voltage (V)

    Distance (cm)

    Figure 5-1 : GP2D120 pin assignment, operation and characteristic curve

    1st measure 2nd measure

    Not stable 1st output 2nd output n output

    n measure

    38.39.6ms

    5 ms

    Measurement

    Vout

    Supply

    * Use Kodak R-27 gray-white

    paper. The white side has a

    90% reflection rate, made

    from a material that reflects

    light for range measurement.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    64/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    65/88

    Rob otics expe rime nt w ith PIC m ic roc ont rollerl65

    L F

    A X=

    Therefo re, L eq ua ls

    F AL

    X

    =

    Thus, the d ista nc e va lue from the photo transisto rs will be sent to the Signa l Eva lua-

    tion Module before it is changed to voltage, resulting in a change of voltage according

    to the measured distance.

    5.6.3 Reading GP2D120 with A/ D converter

    The GP2D120s outp ut voltage w ill c hang e a coording to the d etec tion d istance.

    For example, Vout 0.5V is eq ual 26cm. distanc e and Vout 2V is eq ual 6cm. d istanc e. The

    table 5-1 shows the summary of GP2D120s Vout and Distance relation.

    For interfacing with A/D converter module within microcontroller, the result is raw

    data from the A/ D conversion. The user will need to use the softw are to c onve rt the raw

    data to the exac t distanc e. You can c alculate the app roxima te d istanc e from the formular

    below.

    #8

    '"4

    +=

    Thus, R as Distance in Centimetre unit

    V as Digital data from A/D conversion

    For example, see the Tab le 5-1. The raw da ta from conversion is 307. It is eq ua l

    8cm. distance.

    Warning for the signal cable of the GP2D120

    The GP2D120 module has a different pin arrangement then that of the MicroCamp board,

    even though it looks similar. Therefore, a special signal cable has already been connected

    to the GP2D120 module. The user just needs to connect the other end of the cable to the

    connection points of the MicroCamp board. DO NOT remove the cable from the module,

    and do not replace it with signal cables from other sensor modules.

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    66/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    67/88

    Rob otics expe rime nt w ith PIC m ic roc ont rollerl67

    This ac tivity introduc es the simp le experime nt about rea ding the a na log signa l.

    The simp le Variab le resistor or Potentiomete r is used to analog voltage source and p lug

    into the analog input port of PIC16F887. You make the simple C code to read the result

    from ADC module inside PIC16F887 to display on LCD module.

    A5.1 Write the Listing A5-1. Compile and download the code to RBX-877 V2.0 Robot Con-

    troller board.

    A5.2 Plug the ZX-POTH; potentiometer sensor to RA3 port pin of RBX-877 V2.0 Robot Con-

    troller board.

    A5.3 Run the p rog ram. Ad just the potentiomete r and see the result a t LCD sc reen on the

    RBX-877 V2.0 Robot Controller board.

    LCD m od ule shows messageSENSOR1 = xxx (xxx as 0 to 1023).

    Activity 5

    Reading the analog signal

    /***********************************/

    /***** Show ADC from RA1 to LCD ***//***********************************/char data_[6];int x;void main(){

    Delay_ms(1000);Lcd_Init(&PORTD);ANSEL = 0xFF; // PORTA ==> AnalogTRISA = 0xFF; // PORTA ==> inputLcd_Cmd(LCD_CURSOR_OFF); // LCD cursor offLcd_Out(1,1,"SENSOR1 = "); // Show TextADCON0=0b11001101; // Select Analog1 RC_Mode and ADONwhile(1){

    ADCON0.GO=1;while(ADCON0.GO);x= (ADRESH*4)+(ADRESL/64);WordToStr(x,data_);Lcd_Out(1,10,data_);Delay_ms(100);

    }}

    Listing A5-1 : The ADC demonstration program for reading the sensor value

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    68/88

    68lRob otics expe rime nt w ith PIC m ic roc ont roller

    A6.1 Write the Listing A6-1. Compile and download the code to Robo-PICA.

    A6.2 Plug the GP2D120 sensor to RA2 port p in of Rob o-PICA. (ready from build ing ac tivty 3).

    A6.3 Run the p rog ram. Place som e objec t a t the front o f GP2D120 mod ule. Ob serve the

    LCD operation.

    A6.4 Adjust the distance of the object from GP2D120 sensor and observe the result.

    From testing, you will found the GP2D120 can de tec t the ob jec t in range 4 to 30cm.

    Activity 6

    Testing GP2D120

    int Adc;char txt[6];

    void Read_Adc(){ADCON0=0b11001001; // Select Analog2 RC_Mode and ADONADCON0.GO=1; // Start Convertwhile(ADCON0.GO); // Wait Until Convert Complete

    Adc=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> Adc}

    void main(){Delay_ms(1000);Lcd_Init(&PORTD); // Initial LCDLcd_Cmd(LCD_CURSOR_OFF); // LCD Cursor OFFLcd_Out(1,1,"Raw Data= "); // Show Fisrt Line Textwhile(1){Read_Adc();WordToStr(Adc,txt); // Convert To Show on LCDLcd_Out(1,10,txt);if (Adc

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    69/88

    Rob otics expe rime nt w ith PIC m ic roc ont rollerl69

    A7.1 Write the Listing A7-1. Compile and download the code to Robo-PICA.

    A7.2 Turn-off power and unp lug the dow nloa d c ab le from Rob o-PICA.

    Activity 7

    Contactless object detection robot

    /**********************************************************//***** Robot with Object Detector *************************//**********************************************************/

    #include int Adc; // Save analog Datachar Txt[6]; // Save String

    void Read_Adc(){ADCON0=0b11011101; // Select Analog2 RC_Mode and ADONADCON0.GO=1; // Start Convertwhile(ADCON0.GO); // Wait Until Convert Complete

    Adc=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> Adc}

    void main()

    { Delay_ms(1000); // Start up DelayANSELH.F4=0; // RBO ==> Digital IOANSEL=0xFF;TRISA=0xFF;Lcd_Init(&PORTD); // Initial LCDLcd_Cmd(LCD_CURSOR_OFF); // LCD Cursor OFFwhile(PORTB.F0); // Wait Key Presswhile(1){Read_Adc(); // Read Analog 2WordToStr(Adc,Txt); // Convert Data to stringLcd_Out(1,1,Txt); // Show on LCDif (Adc>300) // if Detect object in range{

    Backward(255);Delay_ms(500); // Backword and turn leftS_left(255);Delay_ms(400);

    }else{

    Forward(255); // Object out of range FORWARD}

    }}/**********************************************************/

    Listing A7-1 : The C program of the contactless object detection robot

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    70/88

    70lRob otics expe rime nt w ith PIC m ic roc ont rollerA7.3 Plac e the rob ot on the floor. Turn-on the power and observe its op eration.

    A7.4 Try to p lac e a ny ob jec t a t the front o f the robo t a nd see its op eration.

    The robo t will chec k the distance o f the ob jec t in 8cm. range. If not a ny obstac le,

    rob ot w ill move forwa rd continue. If found the objec t, it w ill move b ac kward, turn left a nd

    move forward again.

    INNOVATIVE EXPERIMENT

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    71/88

    Rob otics expe riment with PIC m icroc ontrollerl71

    Line trac king or follow ing is a pop ular ac tivity in any Rob otics ac tivity. The purpose

    of this ac tivity is to lea rn about how to interface ana log sensor. In Rob o-PICA rob ot kit, it

    c om es with a p a ir of Infrared reflec to r sensor for this ac tivity. Add senses to the Rob o-PICA

    so tha t it c an d ete c t a nd mo ve follow ing the line, b y using the IR Reflec tor Sensor. Two IR

    Reflec tor Sensors will be insta lled at the b ot tom of the Rob o-PICA so tha t it ca n detec t

    bo th white and blac k lines.

    6.1 ZX-03 Infrared Reflector

    The heart o f this sensor is TCRT5000 reflec tive ob jec t sensor. It is designed for c lose

    proximity infra red (IR) detec tion. There s an infra red d iod e b ehind its transparent b lue

    window and an infra red transistor be hind its blac k window. When the infra red emitted by

    the diode reflects off a surface and returns to the black window, it strikes the infrared

    transistors base, c ausing it to cond uc t current . The more infrared inc ident on the transistor sba se, the m ore current it c onducts.

    When used as an analog sensor, the ZX-03 can detect shades of gray on paper

    and distances over a short range if the light in the room remains constant.

    The suitab le d ista nc e from sensor to line or floor is during 3 to 8 mm. The outp ut

    vo ltag e is during 0.1 to 4.8V and d igita l va lue from 10-bit A/D converter is 20 to 1,000.

    Thus, ZX-03 will suitab le to apply to line tracking sensor.

    Figure 6-1 : ZX-03 Infrared Reflector information

    10k

    TCRT5000

    510

    +V

    GND

    OUT

    Infrared Reflector

    Signa l c onnec tor

    Chapter 6Line following mission

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    72/88

    72lRob otics expe riment with PIC mic roc ontroller

    Activity 8

    Reading the Line tracking sensor

    In build ing Rob o-PICA from ac tivity 2, both of ZX-03 Infrared ref lec tors are insta lled

    at the bo ttom of the robot b ody and connec ted to the sensors cab le to RA0 and RA1

    port p in. This ac tivity is to write the prog ram to rea d sensors va lue to d isp lay a t the LCD

    module of Robo-PICA.

    A8.1 Write the Listing A8-1. Compile and download the code to Robo-PICA.

    A8.2 Turn-off power and unp lug the d ownload cable from Rob o-PICA.

    A8.3 Place Robo-PICA on the white surface of the paper demonstration field which is

    bund led w ith Rob o-PICA kit. The p osition o f both ZX-03 sensors must above the white

    surface. Turn-on POWER switch. See the result a t LCD sc reen and rec ord it.

    A8.4 Place Rob o-PICA o n the b lack line o f the p aper demo nstration field . The p osition of

    both ZX-03 sensors must a bove the b lac k line. Turn-on POWER switch. See the result a t LCD

    sc reen and rec ord it.

    From testing can conclude the result as :

    The white surfac e rea ding is during 400 to 900

    The b lac k surfac e rea ding is during 0 to 150

    Thus, the refe renc e va lue for making dec ision is during 150 to 400. You can selec t

    the suitable value and make decision as :

    If the sensor reading value more than the reference value, sensor detect

    WHITE surface

    If the sensor reading value less than the reference value, sensor detectBLACK surface or line

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    73/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    74/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    75/88

  • 8/14/2019 Robotics Experiment With PIC Microcontroller l 1

    76/88

    76lRob otics expe riment with PIC mic roc ontroller

    Listing A9-1 :The C program of Line tracking robot

    /********************************************************//***** Track Black line Forward if cross line ***********//********************************************************/

    #include int Sensor0,Sensor1; // Save Analogchar Txt[6]; // Save convert to string

    void Read_Adc(){

    ADCON0=0b11000001; // Select Analog0 RC_Mode and ADONADCON0.GO=1; // Start Convertwhile(ADCON0.GO); // Wait Until Convert CompleteSensor0=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> sensor0ADCON0=0b11000101; // Select Analog1 RC_Mode and ADONADCON0.GO=1; // Start Convertwhile(ADCON0.GO); // Wait Until Convert Complete

    Sensor1=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> sensor1}

    void main(){

    Delay_ms(1000); // Start up DelayLcd_Init(&PORTD); // Initial LCDANSEL = 0xFF; // PORTA ==> AnalogTRISA = 0xFF; // PORTA ==> inputLcd_Cmd(LCD_CURSOR_OFF); // LCD cursor off

    while(1){

    Read_Adc();

    WordToStr(Sensor0,Txt); // Convert Sensor0 to stringLcd_Out(1,1,Txt); // and show on LCD

    WordToStr(Sensor1,Txt); // Convert Sensor1 to stringLcd_Out(2,1,Txt); // and show on LCD

    if ((Sensor0>500)&(Sensor1>500)) // Check All Sensor are whiteForward(255); // For