PDA

View Full Version : 2 PIC, serial communication



lightgreen
- 6th October 2009, 07:31
hi,good day to everyone...
I have a hard time throughout the process of learning serial communication, but gained nothing at last.
I tried with 2 PIC where each of the PIC will control the LED that connected with it.
There are 2 PIC in the drawing. Let the left PIC = A, right PIC = B.
What i want to have is....A control the LED that connected with B, and B control the LED that connected with A.
I started a bit of the coding but stuck somewhere in the middle. I don't know how to use HSERIN and HSEROUT even though I went through the manual.

I know there should be 2 sets of similar coding which i can download it into PIC while doing the simulation in Proteus Isis. But at this moment, I can't even write my coding with HSERIN and HSEROUT.

Attachment below shows the connection in Proteus. I only had problem with my coding.

So any help.....I will be so grateful....Thanks in advance...

Coding as below:

Define osc 8
define HSER_RCSTA 90H 'Enable serial port and continous receive
DEFINE HSER_TXTSTA 24H 'Enable transmit
DEFINE HSER_BAUD 9615
DEFINE HSER_CLROERR 1 'Clear overflow automatically
ADCON1 = 7


TRISB.0 = 1 'switch
TRISB.2 = 0 'LED
TRISC.6 = 0 'set TX(portC.6) to out
TRISC.7 = 1 'set RX(portC.7) to in

led1 var portb.2

Hserout [

mackrackit
- 6th October 2009, 23:19
Welcome to the forum.

I will suggest to get started use SERIN/SEROUT. These are bit banging routines that can be used on most any pin. When you ar comfortable with that look at SERIN2/SEROUT2, another bit of bit banging. Then try the hardware serial ports.

So...
Near the beginning of your code for both PICs add this line:


include "modedefs.bas"

Then for the sending PIC do something like this:


SEROUT PORTC.4,T2400,[9,3]

On the receiving PIC:


SERIN PORTC.4,T2400,[9],net

IF net = 3 THEN LED_ON 'GOTOs label to turn a LED on

The receiving PIC will wait for "9" and put the next character in the variable net.

flithecut
- 9th October 2009, 06:18
I accept with linghtgreen about code:
Coding as below:

Define osc 8
define HSER_RCSTA 90H 'Enable serial port and continous receive
DEFINE HSER_TXTSTA 24H 'Enable transmit
DEFINE HSER_BAUD 9615
DEFINE HSER_CLROERR 1 'Clear overflow automatically
ADCON1 = 7


TRISB.0 = 1 'switch
TRISB.2 = 0 'LED
TRISC.6 = 0 'set TX(portC.6) to out
TRISC.7 = 1 'set RX(portC.7) to in

led1 var portb.2

lightgreen
- 10th October 2009, 16:00
May I know what does T2400 represent?
My guess is baud rate...If T2400 represent baud rate, then how about the baud rate that i defined on the top?

mackrackit
- 10th October 2009, 18:32
May I know what does T2400 represent?
My guess is baud rate...If T2400 represent baud rate, then how about the baud rate that i defined on the top?
Like I said, the example I gave is an alternative to using the hardware ports. Most start off using the alternative... less setup and things to understand at the beginning.

The "T" makes the signal TRUE, the signal that is produced from the hardware ports is TRUE. When talking between to PICs the signal type does not matter as long as both PICs are using the same signal type. When connecting to a PC an INVERTED signal is needed, if using a bit banging routine it would be N2400 or whatever baud rate the PC is set for. When using the hardware ports an inverter chip such as a RS232 will be needed if you want to communicate with a PC.

In response to your PM. When using SERIN/SEROUT you can use most any of the pins on a PIC. That is why I have PORTC.4 in my example to show that.

Dennis
- 21st November 2009, 14:48
Hi all

To communicate between two pics do I need to add a resistor on the TX and RX lines??

Kind regards

Dennis

mackrackit
- 21st November 2009, 15:20
I do not from PIC to PIC.