Hello again...
I've been using PBP for a while now, I've blinked many many LED's, Swiched lights on and off with my PC and a PIC via RS232, and now I have a robot which follows a black line.
Essentially I have a PIC 16F84A on a 4x3 keypad, I've tried getting it to communicate via RS232 with my Robot (PIC16F877).
Heres the Keypads code...
Code:
INCLUDE "modedefs.bas"
' Declare Variables...
Col1 VAR PORTA.0
Col2 VAR PORTA.1
Col3 VAR PORTA.2
Row1 VAR PORTB.4
Row2 VAR PORTB.5
Row3 VAR PORTB.6
Row4 VAR PORTB.7
LED var PORTB.1
SERTX var PORTB.2
SERoK var PORTB.3
Key var word
' Setup Ports
input row1 : input row2 : input row3 : input row4
output col1 : output col2 : output col3 : output SERTX
' Setup Var
key = 20
' Program Start
Boot:
high led
pause 500
low led
pause 100
high led
pause 100
low led
pause 2000
high serok
goto main
Main:
gosub ScanKeys
if key != 20 then gosub transmit
goto main
ScanKeys:
high col1
pause 50
if row1 = 1 then Key = 1
if row2 = 1 then Key = 4
if row3 = 1 then Key = 7
if row4 = 1 then Key = 10
low col1
high col2
pause 50
if row1 = 1 then Key = 2
if row2 = 1 then Key = 5
if row3 = 1 then Key = 8
if row4 = 1 then Key = 0
low col2
high col3
pause 50
if row1 = 1 then Key = 3
if row2 = 1 then Key = 6
if row3 = 1 then Key = 9
if row4 = 1 then Key = 11
low col3
return
Transmit:
gosub flash
serout sertx,4,[55,key]
pause 50
key = 20
low led
return
Flash:
high led
pause 50
low led
pause 50
high led
pause 50
return
end
Ok, so the PIC16F84A raises SERoK to logic 1 which tells the PIC16F877A to accept serial communication.
PIC16F877A Code:
Code:
INCLUDE "modedefs.bas"
' Define LCD registers and bits
Define LCD_DREG PORTA
Define LCD_DBIT 0
Define LCD_RSREG PORTA
Define LCD_RSBIT 4
Define LCD_EREG PORTA
Define LCD_EBIT 5
DEFINE LCD_LINES 4
'Define Aliases
RHF var PORTB.0
RHB VAR PORTB.1
LHF VAR PORTB.2
LHB VAR PORTB.3
LineCom var PORTB.4
LEDS var PORTB.5
SERoK var PORTB.7
SERRX var PORTB.6
'Define Vars
Dat var byte
ADCON1 = 7
'Begin program
Boot:
dat = 20
Pause 500 ' Wait for LCD to startup
gosub display
low leds
repeat
Lcdout $FE, $C0, "!! Insert Ctrl !!"
Lcdout $FE, $94, "Waiting..."
pause 100
until serok = 1
goto modeselect
ModeSelect:
Lcdout $FE, $C0, "!! Select Mode !!"
Lcdout $FE, $94, "Waiting..."
serin SERRX,4,[55],Dat
pause 50
Discon:
Lcdout $FE, $94, "Pls Discon..."
if serok = 0 then GoMode
goto discon
GoMode:
if dat <> 20 then
select case dat
case 20
goto boot
case 2
goto linefollow
end select
endif
goto boot
Display:
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "PIC-Bot... v1.0"
Lcdout $FE, $94, "Pls Wait..."
Pause 1000
return
'Check for controler
ChkCtrl:
if serok = 1 then modeselect
return
'Line Follow loop
LineFollow:
Lcdout $FE, $C0, "~~ LiNe FoLlOw ~~"
Lcdout $FE, $94, "...Scan Line..."
low RHF
low LHF
PWM LHF,60,18
pause 100
if linecom = 0 then gosub moveleft
low RHF
low LHF
PWM RHF,60,18
pause 100
if linecom = 0 then gosub moveright
gosub chkctrl
goto linefollow
MoveLeft:
Lcdout $FE, $94, "Correct Left"
repeat
low RHF
low LHF
PWM RHF,60,20
PWM LHb,60,20
pause 100
until linecom = 1
pause 50
low RHF
low LHF
PWM RHF,60,18
return
MoveRight:
Lcdout $FE, $94, "Correct Right"
repeat
low RHF
low LHF
PWM LHF,60,20
PWM rHb,60,20
pause 100
until linecom = 1
pause 50
low RHF
low LHF
PWM LHF,60,18
return
end
Now the Robot waits until the keypad unit is pluged in, then it waits until the digit 2 is sent serially (by pressing 2 on the keypad), then it askes you to dicconnect the keypad unit (SERoK changes from logic 1 to logic 0) and it executes the program IF 2 was pressed.
Bookmarks