SlideShare una empresa de Scribd logo
1 de 40
Intro to Robotics
       Moayad H. Almohaishi
Louisiana Tech MS computer science
 President of Louisiana Tech Robotic
                Club
ROBOTIC 2010



Video Deleted to reduce the file size. I will add it soon
to Youtube.
What you need to start
 MPLAB Programming IDE (need windows OS)

• http://www.microchip.com/stellent/idcplg?IdcService=S
 S_GET_PAGE&nodeId=1406&dDocName=en019469
 &part=SW007002

 MPLAB C compiler for PIC18 MCUs

• http://www.microchip.com/stellent/idcplg?IdcService=S
 S_GET_PAGE&nodeId=1406&dDocName=en534868

 PIC18F2520 Data Sheet

• http://www.microchip.com/wwwproducts/Devices.aspx
 ?dDocName=en010277
Programming tool
Parts
Prototype Board
Wires
PIC 18 Microprocessor
Capacitors
Resistors
Voltage regulator
LEDs
Battery
6-pins headers
Sensors
Parts
4Mhz Crystal
H-Bridge / Motor Controller
The Base
(Chassis)- Each
group will bring its
own robot base.
Note1: When buying
the base, put in your
mind the maze size.
and the way the base
move.
Note2: a sample of the
maze is available in the
second floor of
Nethken Hall
Board        Note: All the pins inside
               the shaded areas are
               connected together.
connectivity
Power Circuit

 Main      Voltage
Power      Regulat                     5 volt

source       or

                     Capacito
                                Resistor        LED
                        r


                     Ground
Power Circuit   Note: The same like
                the previous slide, just
                another perspective.
The Board
PIC 18 pins map

  Port A         Port B
Analog Input    Digital I/O

    Port A


  Port C        Port C
 Digital I/O   Digital I/O
Connecting PIC 18


 Resistor

  Capacit
    or


      Capacit
              Crystal
       ors




Note: Sometimes you will need to add a resistor between the Pin
10 and the crystal. for more information look at the PIC18F2520
Data Sheet.
Start Reset




Note: You need the Start/Reset sercut to give the micro
controller the signal to start/restart the program. You can add
a push button if you want a restart button
The Board
The H-Bridge



Why do we need the H-bridge ?
The H-Bridge




Note: As and ENs are
inputs from the Micro
Controller and Ys are
outputs to the motors.
H-Bridge Logic

1
             +


1

             -
0
H-Bridge Logic

0
             -


1

             +
1


1


0


0
Connecting The H-Bridge
               Pin
               C0
                                   Pin
               Pin
                                   C6
               C1
                     LM +   RM +




                     LM -   RM -
                                   Pin
               Pin
                                   C5
               C2
                                   Pin
Note: LM = Left                    C4
Motor, RM = Right
Motor
Complete Board
What You Learned


Robot requirements (Softwares and Hardwares)

How to make the power circuit

How to connect your PIC18 chip

How to connect the H-Bridge
MPLAB


Port A

Port A
Hello World - LED ON
/** I N C L U D E S **************************************************/
#include "p18f2520.h"


/** D E C L A R A T I O N S ******************************************/


void main (void)
{

    TRISC = 0b01111111; // PORTC bit 7 to output (0); bits 6:0 are inputs (1)

    LATCbits.LATC7 = 1;                 // Set LAT register bit 7 to turn on LED

    while (1)                                Note1: You need to
    ;                                        include the specific
                                             library of the PIC18 you
}                                            are using.
                                             Note2 : Use the                       Note3 : Use The
                                             register TRIS to define               function LAT to define
                                             the output pins and the               the output value of a
                                             input pins in the Port.               specific pin
Hello World - Blinking LED
/** I N C L U D E S **************************************************/
#include "p18f2520.h"
#include "delays.h"


/** D E C L A R A T I O N S ******************************************/


void main (void)
{

    TRISC = 0b01111111; // PORTC bit 7 to output (0); bits 6:0 are inputs (1)

     while (1){
     LATCbits.LATC7 = 1;
                K                       // Set LAT register bit 7 to turn on LED
    Delay1KTCYx(50);          // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz
     LATCbits.LATC7 = 0;                // Set LAT register bit 7 to turn on LED
    Delay1KTCYx(20);          // Delay 50 x 1000 (1k) = 50,000 cycles; 20ms @ 4MHz

}
                                                                          Note: To use the delay
    ;                                                                     function, you have to
                                                                          include the delay
}                                                                         libraryPut your
                                                                          Note:
                                                                          application inside the
                                                                          While(1) loop to make
                                                                          it run forever.
Directions
PORTC =   C7
          0    RM +
                C6    RM -
                       C5    RM E
                              C4    C3
                                    0    LM -
                                          C2    LM +
                                                 C1    LM E
                                                        C0
Directions
         PORTC =       0   RM +   RM -   RM E   0   LM -   LM +   LM E



int moveForward =      0    1      0      1     0    0      1      1



int moveBackward =                                          0
                       0    0      1      1     0    1             1



int turnLeft       =   0    1      0      1     0    0      1      0



int turnRight   =      0    1      0      0     0    0      1      1
Running The Robot
/** I N C L U D E S **************************************************/
#include "p18f2520.h"


/** D E C L A R A T I O N S ******************************************/


void main (void)
{
  int moveForward = 0b01010011;
  int moveBack = 0b00110101;
  int turnLeft = 0b01010010;
  int turnRight = 0b01000011;

    TRISC = 0b00000000; // All PORTC bits to output (0)
    PORTC = moveForward ;                  // set PORTC bits to 0b10100011

    while (1)
    ;                                                              Note : Use the register PORT to
                                                                   give or read a value from/to all the
}                                                                  pins in the specified Port.
                                                                   Note : PORTA, PORTB, PORTC
Timing
/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
#include "delays.h"

/** D E C L A R A T I O N S ******************************************/


void main (void)
{
  int moveForward = 0b01010011;
   int moveBack = 0b00110101;
   int turnLeft = 0b01010010;
   int turnRight = 0b01000011;

    TRISC = 0;       // All PORTC bits to output (0) (using the dismal system )
    PORTC = moveForward ;
           Delay1KTCYx(50);     // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz
    PORTC = turnLeft ;
           Delay1KTCYx(50);     // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz
    PORTC = turnRight ;
           Delay1KTCYx(50);     // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz
    PORTC = moveBack ;
           Delay1KTCYx(50);     // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz
    while (1)
    ;

}
                                                                          Note: Think about how
                                                                          this robot will be
                                                                          moving
What You Learned


How to start your first project

How to define the Input and the output ports

How to Time Control your output
Problem
        Robot with Touch Sensor
        Our Robot IR Sensor




Wall
IR Sensor
Operating Voltage 4.5v - 5.5v (red and black)
Distance 4cm - 30cm
Analog output (White Cable)



Note: The white cable
will go to the Micro
Controller. And the
Red to the Regulated
5v we created. and the
black to the ground.

Note2: The response
time for this IR Sensor
is 39ms. which is too
slow for a moving
robot.
IR Sensor
 Note: if the distance is
 less than 4cm the
 Sensor gives back
 wrong measuring.
Note2: Because the
response time for the
IR sensor is too
slow, you need to
detect the distance as
early as you can. and
put a safe fail back
technique
PIC 18 pins map

  Port A
Analog Input

    Port A
Connecting the IR Sensors
  Center Sensor (AN0)
   Left Sensor (AN1)
  Right Sensor (AN2)
Using The AD Converter

void setSensor(sensorChannel)
{
    switch (sensorChannel)
    {
         case 0:// channel AN0 for Central Sensor
                   ADCON0 = 1;
                   break;
         case 1: // channel AN1 for Right Sensor
                   ADCON0 = 5; //101
                   break;
         case 2: // channel AN2 for Left Sensor
                   ADCON0 = 9; //1001
                   break;
    }                                               Note2: We create the
}
                                                    Function SetSensor to
                                                    make it easer for us to
                                                    debug and understand
                                                    the code.
ADCON0


                                                        STATUS   ON/OFF
                         CHANNEL NUMBER BITS
                                                          BIT     BIT




Note2: if you need to understand more about the Analog to
Digital Converter’s registers read about it in the micro
controller Data Sheet.
void setupAD()
{
    ADCON1 = 12; //This sets the chip so that AN0, AN1, AN2 are analog. Instructions for this can be found in
the chip documentation. p 230
    ADCON0 = 0; //Make sure ADC settings are 0
    ADCON2 = 164; //this sets Right justified. Bit 6 is NC (0), Bits 5-0 adjust time (Bits 5-0 are 100100)
}

void enableAD() // You don’t need this function, we already set our ADON to 1 in the function SetSensor() I put it
just to explain what you can do.
{
                                                                                              Note: Enable the AD
    ADCON0bits.ADON = 1; //Enables the A/D converter                                          buy changing the pit
}                                                                                             ADON value to 1 and
void listenToAD()
                                                                                              disable it by changing
{                                                                                             the value to 0;
    ADCON0bits.GO_DONE = 1; //Like the start button for the converter.
    while(ADCON0bits.GO_DONE == 1) //Wait until the ADC is finished. GO_DONE will change to 0 and the
loop will end
    {
           //do nothing here
    }
}
How to use it in the main
                                       Note: The Values for FarRange and
int farRange = 70;                     CloseRange is just for explaining. You will
int closeRange = 150;                  need to figure out the best values for your
int centralSensor, rightSensor, leftSensor; robot.
                                       own

setSensor(0);
                                                 Note: The values from the AD will
      listenToAD();                              be stored in the Register ADRES,
                                                 if you need to compare the
        centralSensor = ADRES;                   distance from the sensors you
        if (centralSensor < farRange)            need to store the value to a
                                                 variable or your ADRES will be
        PORTC = moveBack;                        overwritten.
        if (centralSensor > farRange && SC < clsoeRange)
             PORTC = turnLeft;
   if (centralSensor > closeRange)
        PORTC = moveBackward;
What You learned


The Usage of Sensors

How to use Analog IR sensor

Making The right diction
                           Note: The Code I provided is not
                           meant to be running 100%, it is
                           explanatory code ONLY.

                           You will need to define your
                           functions and variables correctly.
SPECIAL THANKS

Robotic Club Advisor
   •Dr. Ben Choi

Robotic Club Vise President
   •James Hurst


 Our FaceBook Group : Louisiana Tech Robotic
                   Club

Más contenido relacionado

La actualidad más candente

Pic assembly launguage
Pic assembly launguagePic assembly launguage
Pic assembly launguage
kishore_435
 
Xcs 234 microprocessors
Xcs 234 microprocessorsXcs 234 microprocessors
Xcs 234 microprocessors
sweta suman
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
SARITHA REDDY
 
Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2
Ikhwan_Fakrudin
 
Chp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copyChp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copy
mkazree
 
Embedded system (Chapter 3) io_port_programming
Embedded system (Chapter 3) io_port_programmingEmbedded system (Chapter 3) io_port_programming
Embedded system (Chapter 3) io_port_programming
Ikhwan_Fakrudin
 
EMBEDDED SYSTEMS 2&3
EMBEDDED SYSTEMS 2&3EMBEDDED SYSTEMS 2&3
EMBEDDED SYSTEMS 2&3
PRADEEP
 
8051-mazidi-solution
8051-mazidi-solution8051-mazidi-solution
8051-mazidi-solution
ZunAib Ali
 

La actualidad más candente (20)

Pic assembly launguage
Pic assembly launguagePic assembly launguage
Pic assembly launguage
 
Xcs 234 microprocessors
Xcs 234 microprocessorsXcs 234 microprocessors
Xcs 234 microprocessors
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
 
Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2
 
8051 ports
8051 ports8051 ports
8051 ports
 
Ports 0f 8051
Ports 0f 8051Ports 0f 8051
Ports 0f 8051
 
Picmico
PicmicoPicmico
Picmico
 
Chp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copyChp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copy
 
Embedded system (Chapter 3) io_port_programming
Embedded system (Chapter 3) io_port_programmingEmbedded system (Chapter 3) io_port_programming
Embedded system (Chapter 3) io_port_programming
 
Class10
Class10Class10
Class10
 
8051 Instruction Set
8051 Instruction Set8051 Instruction Set
8051 Instruction Set
 
Unit iv microcontrollers final
Unit iv microcontrollers finalUnit iv microcontrollers final
Unit iv microcontrollers final
 
74hc4020
74hc402074hc4020
74hc4020
 
Soc
SocSoc
Soc
 
EMBEDDED SYSTEMS 2&3
EMBEDDED SYSTEMS 2&3EMBEDDED SYSTEMS 2&3
EMBEDDED SYSTEMS 2&3
 
8051-mazidi-solution
8051-mazidi-solution8051-mazidi-solution
8051-mazidi-solution
 
8051 Microcontroller
8051 Microcontroller8051 Microcontroller
8051 Microcontroller
 
Handling Asynchronous Events in MCUs
Handling Asynchronous Events in MCUsHandling Asynchronous Events in MCUs
Handling Asynchronous Events in MCUs
 
Atmega16
Atmega16Atmega16
Atmega16
 

Similar a Intro2 Robotic With Pic18

Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
Rashmi
 
8051microcontroller
8051microcontroller 8051microcontroller
8051microcontroller
manish080
 
Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
Spitiq
 

Similar a Intro2 Robotic With Pic18 (20)

Basics Of Embedded Systems
Basics Of Embedded SystemsBasics Of Embedded Systems
Basics Of Embedded Systems
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller
 
PIC18F458_Ritula Thakur.pptx.pdf
PIC18F458_Ritula Thakur.pptx.pdfPIC18F458_Ritula Thakur.pptx.pdf
PIC18F458_Ritula Thakur.pptx.pdf
 
Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVR
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
8051microcontroller
8051microcontroller 8051microcontroller
8051microcontroller
 
Microcontroller 8051 By Mitesh kumar
Microcontroller 8051 By Mitesh kumarMicrocontroller 8051 By Mitesh kumar
Microcontroller 8051 By Mitesh kumar
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
 
LCD_Example.pptx
LCD_Example.pptxLCD_Example.pptx
LCD_Example.pptx
 
Class7
Class7Class7
Class7
 
Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
 
8051 full ppt
8051 full ppt8051 full ppt
8051 full ppt
 
PIC16F877A C Programming.ppt
PIC16F877A C Programming.pptPIC16F877A C Programming.ppt
PIC16F877A C Programming.ppt
 
Introduction2_PIC.ppt
Introduction2_PIC.pptIntroduction2_PIC.ppt
Introduction2_PIC.ppt
 
Microcontroller 8051 gs
Microcontroller 8051 gsMicrocontroller 8051 gs
Microcontroller 8051 gs
 
Datasheet8951
Datasheet8951Datasheet8951
Datasheet8951
 
Introduction to PIC.pptx
Introduction to PIC.pptxIntroduction to PIC.pptx
Introduction to PIC.pptx
 
8155 GPPI
8155 GPPI8155 GPPI
8155 GPPI
 
P89v51rd2
P89v51rd2P89v51rd2
P89v51rd2
 

Intro2 Robotic With Pic18

  • 1. Intro to Robotics Moayad H. Almohaishi Louisiana Tech MS computer science President of Louisiana Tech Robotic Club
  • 2. ROBOTIC 2010 Video Deleted to reduce the file size. I will add it soon to Youtube.
  • 3. What you need to start MPLAB Programming IDE (need windows OS) • http://www.microchip.com/stellent/idcplg?IdcService=S S_GET_PAGE&nodeId=1406&dDocName=en019469 &part=SW007002 MPLAB C compiler for PIC18 MCUs • http://www.microchip.com/stellent/idcplg?IdcService=S S_GET_PAGE&nodeId=1406&dDocName=en534868 PIC18F2520 Data Sheet • http://www.microchip.com/wwwproducts/Devices.aspx ?dDocName=en010277
  • 5. Parts Prototype Board Wires PIC 18 Microprocessor Capacitors Resistors Voltage regulator LEDs Battery 6-pins headers Sensors
  • 6. Parts 4Mhz Crystal H-Bridge / Motor Controller The Base (Chassis)- Each group will bring its own robot base. Note1: When buying the base, put in your mind the maze size. and the way the base move. Note2: a sample of the maze is available in the second floor of Nethken Hall
  • 7. Board Note: All the pins inside the shaded areas are connected together. connectivity
  • 8. Power Circuit Main Voltage Power Regulat 5 volt source or Capacito Resistor LED r Ground
  • 9. Power Circuit Note: The same like the previous slide, just another perspective.
  • 11. PIC 18 pins map Port A Port B Analog Input Digital I/O Port A Port C Port C Digital I/O Digital I/O
  • 12. Connecting PIC 18 Resistor Capacit or Capacit Crystal ors Note: Sometimes you will need to add a resistor between the Pin 10 and the crystal. for more information look at the PIC18F2520 Data Sheet.
  • 13. Start Reset Note: You need the Start/Reset sercut to give the micro controller the signal to start/restart the program. You can add a push button if you want a restart button
  • 15. The H-Bridge Why do we need the H-bridge ?
  • 16. The H-Bridge Note: As and ENs are inputs from the Micro Controller and Ys are outputs to the motors.
  • 17. H-Bridge Logic 1 + 1 - 0
  • 18. H-Bridge Logic 0 - 1 + 1 1 0 0
  • 19. Connecting The H-Bridge Pin C0 Pin Pin C6 C1 LM + RM + LM - RM - Pin Pin C5 C2 Pin Note: LM = Left C4 Motor, RM = Right Motor
  • 21. What You Learned Robot requirements (Softwares and Hardwares) How to make the power circuit How to connect your PIC18 chip How to connect the H-Bridge
  • 23. Hello World - LED ON /** I N C L U D E S **************************************************/ #include "p18f2520.h" /** D E C L A R A T I O N S ******************************************/ void main (void) { TRISC = 0b01111111; // PORTC bit 7 to output (0); bits 6:0 are inputs (1) LATCbits.LATC7 = 1; // Set LAT register bit 7 to turn on LED while (1) Note1: You need to ; include the specific library of the PIC18 you } are using. Note2 : Use the Note3 : Use The register TRIS to define function LAT to define the output pins and the the output value of a input pins in the Port. specific pin
  • 24. Hello World - Blinking LED /** I N C L U D E S **************************************************/ #include "p18f2520.h" #include "delays.h" /** D E C L A R A T I O N S ******************************************/ void main (void) { TRISC = 0b01111111; // PORTC bit 7 to output (0); bits 6:0 are inputs (1) while (1){ LATCbits.LATC7 = 1; K // Set LAT register bit 7 to turn on LED Delay1KTCYx(50); // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz LATCbits.LATC7 = 0; // Set LAT register bit 7 to turn on LED Delay1KTCYx(20); // Delay 50 x 1000 (1k) = 50,000 cycles; 20ms @ 4MHz } Note: To use the delay ; function, you have to include the delay } libraryPut your Note: application inside the While(1) loop to make it run forever.
  • 25. Directions PORTC = C7 0 RM + C6 RM - C5 RM E C4 C3 0 LM - C2 LM + C1 LM E C0
  • 26. Directions PORTC = 0 RM + RM - RM E 0 LM - LM + LM E int moveForward = 0 1 0 1 0 0 1 1 int moveBackward = 0 0 0 1 1 0 1 1 int turnLeft = 0 1 0 1 0 0 1 0 int turnRight = 0 1 0 0 0 0 1 1
  • 27. Running The Robot /** I N C L U D E S **************************************************/ #include "p18f2520.h" /** D E C L A R A T I O N S ******************************************/ void main (void) { int moveForward = 0b01010011; int moveBack = 0b00110101; int turnLeft = 0b01010010; int turnRight = 0b01000011; TRISC = 0b00000000; // All PORTC bits to output (0) PORTC = moveForward ; // set PORTC bits to 0b10100011 while (1) ; Note : Use the register PORT to give or read a value from/to all the } pins in the specified Port. Note : PORTA, PORTB, PORTC
  • 28. Timing /** I N C L U D E S **************************************************/ #include "p18f45k20.h" #include "delays.h" /** D E C L A R A T I O N S ******************************************/ void main (void) { int moveForward = 0b01010011; int moveBack = 0b00110101; int turnLeft = 0b01010010; int turnRight = 0b01000011; TRISC = 0; // All PORTC bits to output (0) (using the dismal system ) PORTC = moveForward ; Delay1KTCYx(50); // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz PORTC = turnLeft ; Delay1KTCYx(50); // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz PORTC = turnRight ; Delay1KTCYx(50); // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz PORTC = moveBack ; Delay1KTCYx(50); // Delay 50 x 1000 (1k) = 50,000 cycles; 50ms @ 4MHz while (1) ; } Note: Think about how this robot will be moving
  • 29. What You Learned How to start your first project How to define the Input and the output ports How to Time Control your output
  • 30. Problem Robot with Touch Sensor Our Robot IR Sensor Wall
  • 31. IR Sensor Operating Voltage 4.5v - 5.5v (red and black) Distance 4cm - 30cm Analog output (White Cable) Note: The white cable will go to the Micro Controller. And the Red to the Regulated 5v we created. and the black to the ground. Note2: The response time for this IR Sensor is 39ms. which is too slow for a moving robot.
  • 32. IR Sensor Note: if the distance is less than 4cm the Sensor gives back wrong measuring. Note2: Because the response time for the IR sensor is too slow, you need to detect the distance as early as you can. and put a safe fail back technique
  • 33. PIC 18 pins map Port A Analog Input Port A
  • 34. Connecting the IR Sensors Center Sensor (AN0) Left Sensor (AN1) Right Sensor (AN2)
  • 35. Using The AD Converter void setSensor(sensorChannel) { switch (sensorChannel) { case 0:// channel AN0 for Central Sensor ADCON0 = 1; break; case 1: // channel AN1 for Right Sensor ADCON0 = 5; //101 break; case 2: // channel AN2 for Left Sensor ADCON0 = 9; //1001 break; } Note2: We create the } Function SetSensor to make it easer for us to debug and understand the code.
  • 36. ADCON0 STATUS ON/OFF CHANNEL NUMBER BITS BIT BIT Note2: if you need to understand more about the Analog to Digital Converter’s registers read about it in the micro controller Data Sheet.
  • 37. void setupAD() { ADCON1 = 12; //This sets the chip so that AN0, AN1, AN2 are analog. Instructions for this can be found in the chip documentation. p 230 ADCON0 = 0; //Make sure ADC settings are 0 ADCON2 = 164; //this sets Right justified. Bit 6 is NC (0), Bits 5-0 adjust time (Bits 5-0 are 100100) } void enableAD() // You don’t need this function, we already set our ADON to 1 in the function SetSensor() I put it just to explain what you can do. { Note: Enable the AD ADCON0bits.ADON = 1; //Enables the A/D converter buy changing the pit } ADON value to 1 and void listenToAD() disable it by changing { the value to 0; ADCON0bits.GO_DONE = 1; //Like the start button for the converter. while(ADCON0bits.GO_DONE == 1) //Wait until the ADC is finished. GO_DONE will change to 0 and the loop will end { //do nothing here } }
  • 38. How to use it in the main Note: The Values for FarRange and int farRange = 70; CloseRange is just for explaining. You will int closeRange = 150; need to figure out the best values for your int centralSensor, rightSensor, leftSensor; robot. own setSensor(0); Note: The values from the AD will listenToAD(); be stored in the Register ADRES, if you need to compare the centralSensor = ADRES; distance from the sensors you if (centralSensor < farRange) need to store the value to a variable or your ADRES will be PORTC = moveBack; overwritten. if (centralSensor > farRange && SC < clsoeRange) PORTC = turnLeft; if (centralSensor > closeRange) PORTC = moveBackward;
  • 39. What You learned The Usage of Sensors How to use Analog IR sensor Making The right diction Note: The Code I provided is not meant to be running 100%, it is explanatory code ONLY. You will need to define your functions and variables correctly.
  • 40. SPECIAL THANKS Robotic Club Advisor •Dr. Ben Choi Robotic Club Vise President •James Hurst Our FaceBook Group : Louisiana Tech Robotic Club