'****************************************************************
'* Name : HARDWARE.INC *
'* Notes : Hardware setup for Micro-Bot examples *
'****************************************************************
@ DEVICE HS_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF
DEFINE OSC 20 ' We're using a 20MHz crystal
DEFINE NO_CLRWDT 1 ' Do not auto insert watchdog timer reset code
'** Turn 16F62x Comparators OFF **'
CMCON = 7 ' Comparators = OFF, PortA = digital I/O
VRCON = 0 ' Internal A/D Vref disabled
' ** Infrared LED Control Constants ** '
Led_On CON 0 ' Led_On = logic 0 which turns IR LED on
Led_Off CON 1 ' Led_Off turns IR LED off
No CON 1 ' If IR Detector Output = 1, then No object detected
Yes CON 0 ' If IR Detector Output = 0, then Yes object detected
'** Setup PortB **'
' PortB.3 = PWM output. PortB.6 & B.7 unused
TRISB = %00000100 ' Set PortB TRIS register
Left_Led VAR PortB.0 ' PortB.0 = left IRLED output
Right_Led VAR PortB.1 ' PortB.1 = right IRLED output
IR_Detector VAR PortB.2 ' PortB.2 = IR detector input
Left_Servo VAR PortB.4 ' PortB.4 = left servo output
Right_Servo VAR PortB.5 ' PortB.5 = right servo output
Left_Servo = 0 ' Left servo control pin = 0
Right_Servo = 0 ' Right servo control pin = 0
Left_Led = Led_Off ' Left IRLED = OFF
Right_Led = Led_Off ' Right IRLED = OFF
'** Setup PortA **'
' PortA.4 & A.5 unused
TRISA = %00001011 ' Set PortA TRIS register
Right_CDS VAR PORTA.0 ' Right side CDS photocell
Left_CDS VAR PORTA.1 ' Left side CDS photocell
Trigger VAR PortA.2 ' PortA.2 = SRF04 trigger pin
Echo VAR PortA.3 ' PortA.3 = SRF04 echo pin
Left_Raw VAR WORD ' Raw left CDS reading
Right_Raw VAR WORD ' Raw right CDS reading
Width VAR WORD ' Pulse width from SRF04
Distance VAR WORD ' Converted distance value
Trigger = 0 ' PULSOUT = the inverse of the pin
'** IR Flag Bit Variables **'
Left_IR VAR BIT ' Flag bit for left IR object detection
Left_IR = No ' Initialize to 1 [no object detected] on power-up
Right_IR VAR BIT ' Flag bit for right IR object detection
Right_IR = No ' Initialize to 1 [no object detected] on power-up
'** Define Constants **'
ForwardLeft CON 800 ' 200 For 4MHz
ReverseLeft CON 600 ' 100 For 4MHz
ForwardRight CON 600 ' 100 For 4MHz
ReverseRight CON 800 ' 200 For 4MHz
TRUE CON 12 ' CCP1CON value for PWM ON
FALSE CON 0 ' CCP1CON value for PWM OFF
'** G.P. Working Variables **'
X VAR BYTE ' GP working variable
Loops VAR BYTE ' GP working variable
ServoTime VAR BYTE ' Time to move servos in motor control sub-routines
Loops = 0 ' Start with Loops variable clear
'** Setup For 40KHz PWM IRLED Drive **'
CCPR1L = 25 ' 63=50%, 25=20%, 94=75% PWM Duty-Cycle
PR2 = 124 ' Set PWM for approximately 40KHz
T2CON = 4 ' Timer2 ON + 1:1 prescale
CCP1CON = FALSE ' Start with hardware PWM = OFF
GOTO Begin ' Jump to beginning routine
Check_Left: ' Call here to check for left obstacle
CCP1CON = TRUE ' ON 40KHz PWM
Right_Led=Led_Off : PAUSE 5 ' OFF right LED / pause
Left_Led=Led_On : PAUSE 5 ' ON left LED / pause
Left_IR = IR_Detector ' Read IR detector output
Left_Led=Led_Off ' OFF Left LED
CCP1CON = FALSE ' OFF 40KHz PWM
RETURN ' Return to calling routine
Check_Right: ' Call here to check for right obstacle
CCP1CON = TRUE ' ON 40KHz PWM
Left_Led=Led_Off : PAUSE 5 ' OFF left LED / pause
Right_Led=Led_On : PAUSE 5 ' ON right LED / pause
Right_IR = IR_Detector ' Read IR detector output
Right_Led=Led_Off' OFF Right LED
CCP1CON = FALSE ' OFF 40KHz PWM
RETURN ' Return to calling routine
Ping:
Trigger=0 ' Pulsout = compliment of initial value
PULSOUT Trigger, 6 ' Output >10 uS trigger pulse
PULSIN Echo, 1, Width ' Measure distance to target
Distance = Width / 74 ' Convert to inches
RETURN
|