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