PDA

View Full Version : Approprite baud



dgcarter
- 2nd February 2009, 22:36
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:

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

My F877A code is as follows:

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

mister_e
- 2nd February 2009, 23:15
There's no real such thing as Best Baudrate, but you need to consider the limitation of a software based Serial communication against Hardware.

If it was me, I would never have use Grandpa 16F84, let him alone on the shelf, he's retired anyways ;) why Microchip still produce them anyways ... apart for money income & 100% compatible effortless drop-in replacement???

However, in case you MUST deal with it for some museum (or available parts) reason, I would suggest you to forget SEROUT but use DEBUG instead. Compare both, DEBUG will work on higher speed than SEROUT on slow speed crystal such as 4 MHz.

Using a built-in USART allow you to have at least interrupt... Way to go in most case as your software can do intelligent stuff in meantime.. what's the big deal to sit there and wait for something anyways ;)

If you look at the PIC datasheet (or use my PICmultiCalc), you'll discover that your PIC running @4MHz may provide a still efficient 250K baud... BUT this will not work in your case, using 19,200 (see the comma Darrel?) baud and still be efficient on both part... considering you're using USART interrupt on the receiver end.

dgcarter
- 2nd February 2009, 23:38
Now that you mention it, I think I do have a PIC16F627A floating around, perhaps it would be wise to use that instead of the F84A.

The reason I still use the F84 is because I have lots of them, unfortunatly in South Africa I pay x14 what one would pay for a PIC chip in the UK, so as a hobby project it becomes pricy, lol.

Anyway, I've found that F627A and i'm going to use it in my keypad along with its own hardware serial port, but I doubt it will solve the problem where the values displayed on the LCD don't correspond with the values which are supposed to be sent by the keypad (and which sometimes randomly change too, lol). And ideas on that one? I've commented my rather unorthadox code to make some sence of my mess, hehe.

Thanks in advance.

Archangel
- 3rd February 2009, 00:40
Now that you mention it, I think I do have a PIC16F627A floating around, perhaps it would be wise to use that instead of the F84A.

The reason I still use the F84 is because I have lots of them, unfortunately in South Africa I pay x14 what one would pay for a PIC chip in the UK, so as a hobby project it becomes pricy, lol.
Damn . . what are they, Illegal there? Do you get them from a shadowy figure in a back alley? :D


Anyway, I've found that F627A and i'm going to use it in my keypad along with its own hardware serial port, but I doubt it will solve the problem where the values displayed on the LCD don't correspond with the values which are supposed to be sent by the keypad (and which sometimes randomly change too, lol). And ideas on that one? I've commented my rather unorthadox code to make some sence of my mess, hehe.

Thanks in advance.
Check these 2 posts. the first one may have your solution, if not go to post 16 of the second post where there is a nice example featuring a ring buffer.
http://www.picbasic.co.uk/forum/showthread.php?p=50654
http://www.picbasic.co.uk/forum/showthread.php?t=4972