PDA

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.

rakickv
- 8th March 2014, 23:08
I have a problem with comunication betwin two PIC-es
PIC1-16F877A
PIC2-16F628A

PIC1 code:
TRISC=%00000010
PORTC=%00000000
DEFINE BEBUG_REG PORTC
DEFINE DEBUG_BIT 0
DEFINE DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
DEFINE OSC 4
DEFINE DEBUG_PACING 10
BROJ VAR BYTE
POCETAK:
WHILE PORTC.1=0
DEBUG BROJ
WEND
GOTO POCETAK
END


PIC2-code

TRISA=%00000000
TRISB=%00000010
DEFINE OSC 4
DEFINE DEBUG_REG PORTB
DEFINE DEBUG_BIT 1
DEFINE DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
DEFINE DEBUG_PACING 10
BR VAR BYTE
POC:
DEBUGIN [BR]
WRITE 00,BR
GOTO POC
END


But something is wrong but what?

HenrikOlsson
- 9th March 2014, 06:48
Hi,

But something is wrong but what?
Well, for starters, the line DEFINE BEBUG_REG PORTC is wrong.

/Henrik.

rakickv
- 9th March 2014, 11:24
Sorry, but it was wrong tiping the letter, but it is steel not working.

HenrikOlsson
- 9th March 2014, 13:20
It's hard to guess since you haven't posted the actual code you're using, missing variable declarations etc but "it doesn't work" is THE worst problem description you can ever give - of course "it doesn't work"... You wouldn't even be posting here in the first place if it did, right?

So, What exactly doesn't work? How do expect it TO work? What have you done, except posting here, to try finding the issue?

Are the sending PIC actually running? Is it running at the correct speed (4MHz)? Is it sending anything at all? Is the receiving PIC actually running? Is IT running at the correct speed (4MHz)?

One thing, which doesn't really prevent it from "working" but may be fooling you, is that on the sending side the variable BROJ never changes, it's always zero (or perhaps there's more code that you didn't show).

/Henrik.

rakickv
- 9th March 2014, 16:38
Here are the codes of pic1 and pic2.Pic1 have the quartc on 8 MHz but I gave it a DEFINE OSC 4
becouse the pic2 works at 4 MHz.I am shure that everything works in PIC1 exept DEBUG becouse I checkedout.I am shure that PIC2 is running becouse it puts the port-out signals in the right order and it change the PIC2 addresses 00,01,02,03 to zero values (00,00,00,00).PIC2 have an internal OSC and it works at 4 MHz and I can't measure it, but PIC1 works in 8 MHz but I guess it devides the 8MHZ becouse I wrote DEFINE OSC 4.I am wondering if the BAUDRATE is to fast or is the code of DEBUG correct?

HenrikOlsson
- 9th March 2014, 17:05
Hi,


but I guess it devides the 8MHZ becouse I wrote DEFINE OSC 4
Not at all. That's not what DEFINE OSC does, look it up in the manual.


I am wondering if the BAUDRATE is to fast or is the code of DEBUG correct?
Yes, the PIC is running at 8MHz but you're telling the compiler it's running at 4MHz. The compiler calculates the baudrate timings based on 4MHz when in reallity it's running twice as fast - the actual baudrate will be double what you tell it.

/Henrik.

rakickv
- 9th March 2014, 17:28
Ahaa I will change PIC1 code to DEFINE OSC 8 and then I will let you know what will happen.Thanks.

rakickv
- 9th March 2014, 22:54
It is not working.I tried with SERIN and SEROUT instruction but it is the same thing.Is it the SERIN and SEROUT code wrong written?
TRISC=%00000010
PORTC=%00000000
INCLUDE "Modedefs.bas"
DEFINE OSC 8
DEFINE DEBUG_PACING 100
BROJ VAR BYTE
BROJ=9
POCETAK:
WHILE PORTC.1=0
SEROUT PORTC.0,T2400,[BROJ]
WEND
GOTO POCETAK
END


PIC2:
PCON=%00001011
TRISA=%00000000
TRISB=%00000010
DEFINE OSC 4
INCLUDE "Modedefs.bas"
BR VAR BYTE
POC:
SERIN PORTB.1,T2400,[BR]
WRITE 00,BR
GOTO POC
END

HenrikOlsson
- 10th March 2014, 11:37
Hi,
Again, saying "it doesn't work" really does not make it easy trouble shooting someone elses code from a remote location. You really need to figure out WHAT isn't working. Where is the problem, is it the sending PIC or the receiving PIC? Perhaps the communication is working just fine and it's the fact that your sending the same value all the time that's making it LOOK like it's not working? How are you determining that it's not working? What does it do?

One thing I did notice, in your previosuly posted code, is that you're using DEFINE DEBUG_XXXX on the receiving side as well. You need to use DEFINE DEBUGIN_XXXX.

Another thing I'd do is implement some sort of delay, or even handshake, so that the sending PIC sends the value ONCE each time instead of sending it over and over again as fast as it possibly can.

/Henrik.

Charlie
- 10th March 2014, 11:52
I've also found it useful to stick a PC serial port in the middle during development. Set up your PC to run a terminal program, then start with making sure the transmitter and receiver work on their own before trying them together. Divide and conquer.

rakickv
- 10th March 2014, 20:56
your sending the same value all the time that's making it LOOK like it's not working?What does it do?
/Henrik.
I am shure that the PIC1 is sending the diferent value every time becouse I have LCD out view on PIC1.I am not shure that PIC2 is resiveing the data becouse the adress of PIC2: 00 is allwayes FF.So I must get a logic analiser and see what happens on resive PORTB.1.

EarlyBird2
- 11th March 2014, 09:18
Having read the manual the serin statement has this format.

SERIN Pin,Mode,{Timeout,Label,}{[Qual...],}{Item...}

As you can see square brackets are around the Qualifier.

The statement in your code

SERIN PORTB.1,T2400,[BR]

Is therefore waiting for BR as a qualifier?

rakickv
- 11th March 2014, 18:23
SERIN Pin,Mode,{Timeout,Label,}{[Qual...],}{Item...}
The statement in your code
SERIN PORTB.1,T2400,[BR]
Is therefore waiting for BR as a qualifier?
I had never use SERIN or SEROUT or DEBUG or DEBUGIN instruction so I can't shurely tell you what' right. I need to send decimal value of GREJAC from PIC1 to BR on PIC2.Then a decimal value of BR I will compare in SELECT CASE rutine which I didn't wrote.

Charlie
- 12th March 2014, 11:19
SERIN PORTB.1,T2400,[BR] should be SERIN PORTB.1, T2400, BR

EarlyBird2
- 12th March 2014, 12:26
Are you certain Charlie? I was not sure simply because all the way through this thread everyone has used the [BR] version of the serin instruction. Just goes to show that even experts can make mistakes :)

Charlie
- 12th March 2014, 14:32
According to my manual, yes. (0609)
I have not tried it. I like SEROUT for transmitting, but generally find using the hardware receiver (HSERIN) easier to get going.

rakickv
- 14th March 2014, 00:22
Now I made a PIC2 as a transmiter and pic1 as a resiver becouse I have a LCD out on pic1 so I can see what is happening all the time.I took osciloscope and found that pic 2 is not transmiting anything at all.This is the code of the pic2 - transmiter:-16f628a

INCLUDE "Modedefs.bas"
DEFINE OSC 4
PCON=%00001011
CMCON=%00000111
TRISB=%00000000
TRISA=%00100000
PORTA=0
PORTB=0
BR VAR BYTE
BR=15
pocetak:
SEROUT PORTB.1,T2400,[BR]
PAUSE 5000
GOTO pocetak
END

Is the code wrong?I have 5V on the PORTB.1 and it is not changeing at the time.