Serial comm between two PICs
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.
Re: Serial comm between two PICs
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?
Re: Serial comm between two PICs
Hi,
Quote:
But something is wrong but what?
Well, for starters, the line DEFINE BEBUG_REG PORTC is wrong.
/Henrik.
Re: Serial comm between two PICs
Sorry, but it was wrong tiping the letter, but it is steel not working.
Re: Serial comm between two PICs
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.
2 Attachment(s)
Re: Serial comm between two PICs
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?
Re: Serial comm between two PICs
Hi,
Quote:
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.
Quote:
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.
Re: Serial comm between two PICs
Ahaa I will change PIC1 code to DEFINE OSC 8 and then I will let you know what will happen.Thanks.
Re: Serial comm between two PICs
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
Re: Serial comm between two PICs
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.
Re: Serial comm between two PICs
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.
Re: Serial comm between two PICs
Quote:
Originally Posted by
HenrikOlsson
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.
Re: Serial comm between two PICs
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?
Re: Serial comm between two PICs
Quote:
Originally Posted by
EarlyBird2
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.
Re: Serial comm between two PICs
SERIN PORTB.1,T2400,[BR] should be SERIN PORTB.1, T2400, BR
Re: Serial comm between two PICs
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 :)
Re: Serial comm between two PICs
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.
Re: Serial comm between two PICs
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.