PDA

View Full Version : I ammm Looosssst (USART Help)



shawn
- 13th May 2006, 22:26
Hi Everyone,
I need help please. What I am trying to figure out is how to use the Usart hardware on the pic 18F452. I need to comunicate between two or more Pics. I've used the serout and serin commands to do this but it is not efficent enough. The two pics will be doing whatever it is they need to do and from time to time they will need to send info back and forth.
If I am not mistaking using the hardware usart they should be able to just send the info and whether the recieving pic is ready or not it will be written in the rx register. Also if I am not mistaking the pics should be able to interupt and retrieve this data once it is received.

I Have no Idea where to start. I read Data sheets for the pic 18f452 and I skimmed threw threads and posts for hours and only got frustrated. Someone please point me in the right direction.

By setting the correct registers one should not have to use the HSerin commands right or am I wrong. It appeared that by writing to the TX register that this would automaticly transmit my info as long as all the usart registers are config. properly.

And while i Am at it can more than 2 pics be connected together using usart? I am sorry for asking questions that have already been asked, but answer are spread out all over the place and I have not figured out how to put the puzzle together.

Thanks Shawn

Charles Linquis
- 14th May 2006, 00:04
Assuming that you don't want to write an interrupt-driven routine -

You can send data using HSEROUT and receive it with HSERIN.

As far as the receive is concerned, probably the easiest way is to watch PIR1.5 If that bit is set, it means that a character was received. You can then use HSERIN to retrieve the byte.

If you have lots of bytes to transfer between PICs, a good way is to use a line between the two as a "Clear To Send" line. You can easily do two-way communication that way. There are as many schemes as the number of users on this forum.

shawn
- 14th May 2006, 00:21
I do want to use simple PBP Interups. Can't PIR1.5 be used as an interupt causing event.

shawn
- 14th May 2006, 03:57
This is the best I can come up with right now for a sample program. I am using 18f452 for the receiver and the transmiter. I want the receiver to use a PBP Interupt handler so that the microcontroller can do its thing and not half to wait idle for data from the transmiter.
Transmiter code is :

DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE HSER_RCSTA 90h ' enable serial port,
' enable continuous receive
'
define HSER_TXSTA 24h ' enable transmit,
' BRGH=1
'
define HSER_SPBRG 103 ' set baudrate to 2400
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

TrisB.0 = 0

Counter Var word
DataIn Var Word
DataOut Var Word

Dataout = 10
Counter = 0
DataIn = 0



Loop:
PortB.0 = 1 'Led on
hserout [DataOut] 'Serial Out PortC.6 Once every 5 seconds
pause 3000
PortB.0 = 0 'Led Off
pause 2000
GoTo Loop






The Reciever code is:

DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE HSER_RCSTA 90h ' enable serial port,
' enable continuous receive
'
define HSER_TXSTA 24h ' enable transmit,
' BRGH=1

define HSER_SPBRG 103 ' set baudrate to 2400
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

TrisB.0 = 0

Counter Var word
DataIn Var Word

Counter = 0
DataIn = 0

on interrupt goto Reader

'For Visual purposes to simulate doing something.
'Flashing an LED on for half sec off for half sec.
Main_Program:
Counter = 0
PortB.0 = 1
While Counter < 500
Counter = Counter + 1
Pause 1
wend
Counter = 0
PortB.0 = 0
While Counter < 500
Counter = Counter + 1
Pause 1
wend
GoTo Main_program


disable ' Disable interrupts
Reader:
rcsta.4 = 0 ' Clears Rx register overrun error,sets SREN off
rcsta.4 = 1 ' Sets SREN on
hserin [DataIn] 'PortC.7 serial In
Serout PortB.1,6,[12,#DataIn] 'Display data on LCD
pause 1000 'This pause should give a noticable
'diference in Led flashing
Serout PortB.1,6,[12] 'Clear LCD Display
resume ' Leave interrupt routine
enable

Everything seems to be working except the reciever module never goes into the interupt routine. Also the Transmiter TX line seems to be always high. I came up with this test code from the PBP manual and many post here.

mister_e
- 15th May 2006, 04:00
http://www.picbasic.co.uk/forum/showpost.php?p=8601&postcount=11

shawn
- 16th May 2006, 05:05
Thanks Steve
That worked, I will probably have more questions but that defenitly gave me something to work with.