I'm using serial comms to transmit data from a PIC16F84A to PIC16F877A.
F84A uses SEROUT and 877A uses Hardware USART, HSERIN. Both processors run @ 4 MHz with an XTAL oscillator. There is a distance of about 1.2 meters between the processors.
What would be an approprite baud for serial comms between these two peocessors? And could someone possibly give an example of what such PBP code might look like.
Essentially the F84A scans a keypad, stores the key pressed in a byte variable and sends this value to the F877A, the F877A then recieves this, stores the value in its own byte variable and acts on it accordingly.
I have managed to get this working, but I am seeing different values on my LCD (attached to the F877A) as to what were sent serially by the F84A. FOr example, sending a "1" would display a "10" and sending a "2" would display a "40" but sometimes this would change to a "60".
As a fairly novice user I assume I am missing something obveous, so I thought I would ask the experts for some advise.
Thank you in advance.
My F84A code is as follows:
My F877A code is as follows: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 byte
' Setup Ports (I know theres an easier way with the TRIS registers, but this makes it easier to understand)
input row1 : input row2 : input row3 : input row4
output col1 : output col2 : output col3 : output SERTX
' Setup Var
key = 20 ' Initialize "Key" var with "20".
' Program Start
Boot: ' Do the whole unnessesary light show
high led
pause 500
low led
pause 100
high led
pause 100
low led
pause 2000
high serok ' Tell the F877A you ready to be useful (finally).
goto main ' Start the important stuff.
Main:
gosub ScanKeys ' Scan the keypad.
if key <> 20 then gosub transmit ' If Key var is NOT = 20, transmit the value.
goto main ' Repeat the process.
ScanKeys:
high col1 ' Make Col1 logic 1 for checking.
pause 50 ' Debounce.
if row1 = 1 then Key = 1 ' Check for logic 1 on the rows, if found, thats ur numba!
if row2 = 1 then Key = 4
if row3 = 1 then Key = 7
if row4 = 1 then Key = 10
low col1
high col2 ' Same thing for Col2 if nothing is found on Col1.
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 ' And again for Col3.
if row2 = 1 then Key = 6
if row3 = 1 then Key = 9
if row4 = 1 then Key = 11
low col3
return ' Go back to main routine.
Transmit:
gosub flash ' Do another quick unessesary light show
serout sertx,T2400,[key] ' Send the "Key" value to the F877A
pause 50 ' Dont rush!
key = 20 ' Reset the "Key" var.
low led ' Stop the light show.
return ' Go back to main routine.
Flash:
high led ' The other unessesary light show.
pause 50
low led
pause 50
high led
pause 50
return
end
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 HSER Registers
' Set receive register to receiver enabled
DEFINE HSER_RCSTA 90h
' Set baud rate
DEFINE HSER_BAUD 2400
'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 ' Initialize Dat var with "20".
Pause 500 ' Wait for LCD to startup.
gosub display ' Go to Display subroutine.
low leds ' Turn on the decor!
repeat
Lcdout $FE, $C0, "!! Insert Ctrl !!" ' Ask user to plug in F84A Keypad
Lcdout $FE, $94, "Waiting..."
Lcdout $FE, $D4, "Dat = ",#Dat ' Display value of Dat variable.
pause 100 ' Dont rush.
until serok = 1 ' Wait until F84A is connected and ready.
goto modeselect ' Get user's commands.
ModeSelect:
Lcdout $FE, $C0, "!! Select Mode !!" ' Inform the user to select mode with kaypad.
Lcdout $FE, $94, "Waiting..."
Lcdout $FE, $D4, "Dat = ",#Dat ' Display value of Dat variable.
HSERIN 5000,Modeselect,[str Dat\1] ' Recieve keypad value serially from F84A.
Discon:
repeat
Lcdout $FE, $94, "Pls Discon..." ' Once value is recieved, ask user to disconnect F84A.
Lcdout $FE, $D4, "Dat = ",#Dat ' Display value of Dat variable.
until serok = 0 ' Wait until F84A is disconnected.
goto gomode ' Find out what to do with the value recieved.
GoMode:
if dat <> 20 then ' If Dat id NOT = 20 then find the mode.
select case dat
case 1 ' Should Dat = 1,
goto linefollow ' Go to LineFollow Routine.
end select
endif
goto boot ' Otherwise, if nothing chnaged, start the process again.
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 ' Here we check is the F84A has been connected (used in Mode routines).
return
include "LineFollow.bas" ' Include the Line Follow mode routine.
end