View Full Version : Serial comm between two PICs
ALFRED
- 19th February 2006, 09:33
Hi, I am trying to interface two PICs together via serial connection. I am using two PIC16F88s running at 4mhz. PortB.0 on the tx chip is connected to PortB.0 on the rx chip. I have a button connected to PortB.1 on the tx that when pushed causes the pin to go high. I have an Led on PortB.1 of the rx. My goal is to transmit the state of the button to the rx chip to turn the LED on or off. This is just an experiment with serial communications and the actual application will be much more complex. here is my code:
Tx:
Include "modedefs.bas"
b0 VAR BYTE
Start:
IF PortB.1 = 1 THEN LET b0 = 1
LET b0 = 0
SEROUT PortB.0, N2400, [b0]
GOTO Start
Rx:
Include "modedefs.bas"
b0 VAR BYTE
Start:
SERIN PortB.0, N2400, [B0]
IF b0 = 1 THEN HIGH PortB.1
LOW PortB.1
GOTO Start
When I push the button nothing happens. I am confident that the circuit works so I am convinced that the problem is in the software. I do not fully understand the SERIN SEROUT commands and am interested in what happens when a serial command is executed. Does the Rx wait for the data or if there isn't any go to the next line of code? Do the SERIN SEROUT commands have to be executed at the same time for the communication to be successful.
I appreciate all the help i can get on this.
Darrel Taylor
- 19th February 2006, 17:29
Hi ALFRED,
When the program gets to the SEROUT statement, the value of b0 is always 0.
Tx:
Include "modedefs.bas"
b0 VAR BYTE
Start:
IF PortB.1 = 1 THEN LET b0 = 1
LET b0 = 0
SEROUT PortB.0, N2400, [b0]
GOTO Start
>> Does the Rx wait for the data or if there isn't any go to the next line of code?
Yes, the way you have it now, it will wait forever to receive a byte. If you wanted it to continue if it doesn't receive anything, you can add a timeout to the SERIN statement.
>> Do the SERIN SEROUT commands have to be executed at the same time for the communication to be successful.
YES!
<br>
ALFRED
- 19th February 2006, 19:58
Hi, i just realized that I typed my code in wrong. I had it the way you suggested but it still doesn't work. any other ideas of what could be wrong?
Thanks for any more help.
Darrel Taylor
- 19th February 2006, 23:53
Well, it wasn't really a suggestion. I was just pointing out part of the problem.
My suggestion would be to place the b0 = 0 line just prior to the IF PortB.1 = 1 line. Like this...Tx:
Include "modedefs.bas"
b0 VAR BYTE
Start:
LET b0 = 0
IF PortB.1 = 1 THEN LET b0 = 1
SEROUT PortB.0, N2400,
[b]PAUSE 10
GOTO StartAnd, add a short pause in the loop. Just to space things out a bit.
Then on the receiving end...Rx:
Include "modedefs.bas"
b0 VAR BYTE
Start:
SERIN PortB.0, N2400,
IF b0 = 1 THEN
HIGH PortB.1
[b]ELSE
LOW PortB.1
ENDIF
GOTO Start
<br>
Ron Marcus
- 20th February 2006, 01:16
Start:
LET b0 = 0
IF PortB.1 = 1 THEN LET b0 = 1
SEROUT PortB.0, N2400, [b0]
PAUSE 10
GOTO Start
Don't forget the "ENDIF"
Start:
LET b0 = 0
IF PortB.1 = 1 THEN LET b0 = 1
* ENDIF*
SEROUT PortB.0, N2400, [b0]
PAUSE 10
GOTO Start
ALFRED
- 20th February 2006, 02:10
Hi, I tried Ron Marcus's "endif" and it did not compile. nothing seems to be working and I am beginning to thing that I should try the HSERIN and HSEROUT commands instead. My actual application will be a PIC 18F4320 PIC controlling 12 servos and monitoring 12 pushbuttons. The idea is to take the load of controlling servo motion routines of the master controller. I want to control the motion routines of the servos with the master controller via serial link. Does anyone have any ideas? I think that the hardware serial port would make the timing requirements easier to deal with but I do not fully understand the HSERIN HSEROUT commands and USAURT could someone please explain all this to me?
Ron Marcus
- 20th February 2006, 04:27
O.K... Try this,
Start:
b0 = 0
IF PortB.1 = 1 THEN
b0 = 1
ENDIF
SEROUT PortB.0, N2400, [b0]
PAUSE 10
GOTO Start
Now, it looks like you are using Stamp type conventions for labels. b0 is not very descriptive.
button1 var portc.0
This will alias the above port pin (can be any pin) so you can use a more descriptive term and it will be easier to follow the program in the future. Also, "Let" is unnecessary with PBP. It will still work,and if it is habit, then do it. Do you have the apropriate pullup(down) resistors for your switches?
ALFRED
- 20th February 2006, 08:45
HI, I tried the code that Ron Marcus posted and it did not work. I think the problem is in the receiver PIC software, (when I hook a speaker up to the serial line I can hear the data flowing). I have now tried several ways to make a serial connection but none worked. These attempts included DEBUG and DEBUGIN commands as well as HSERIN and HSEROUT commands. I would be extremely happy if someone could post some sample code and hardware information on getting a serial connection working between two PICs using any method ( HSERIN, DEBUG ect.). I appreciate any help you can offer on this problem.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.