Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Connecting Two PIC 16F877

Status
Not open for further replies.

AliRazoR

Advanced Member level 4
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
Hi,

I want to connect two pic 16f877 to each other.how can i do that?
i would like to send data to other pic and then receive it.what command do i have to use?

does any one have the sample code in C language?

thanks in advance.
 

Either you can use SPI or I2C to communicate between the two PIC
or Simply Serial Communication!
 
Hi Friend
Check datasheet of PIC16F877

There is one feature
==> Synchronous Serial Port (SSP) with SPI (Master mode) and I2C (Master/Slave)

You can use SPI or I2C for communication between two PIC 16F877.

Shyam
INDIA
 
Hi,
You can use any of the serial communication protocols like SPI, UART, One-Wire Interface or I2C(TWI) or parallel communication, ie just connecting one port of master to one port of slave. I suggest using UART, as it can be used over a reasonable distance, requires only two transmission/reception lines and is fairly simple to code.

Hope this helps.
Tahmid.
 
its simple to use serial interfce (USART). it need only two pins.
if you don't know ablout the interfaces like serial, SPI or I2C. connect any IO port of the two ICS and transfer data as your using it for a switch or LED.
which c compiler you are using.
 
thanks every body for answer.could you tell me what command do i have to use in C language.

---------- Post added at 06:51 ---------- Previous post was at 06:50 ----------

I am using MikroC

---------- Post added at 06:52 ---------- Previous post was at 06:51 ----------

my thanks button is banned.::)))

---------- Post added at 06:56 ---------- Previous post was at 06:52 ----------

Is this right?

unsigned short i;

void main() {
USART_init(19200); // initialize USART module
// (8 bit, 19200 baud rate, no parity bit...)
while (1) {
if (USART_Data_Ready()) { // if data is received
i = USART_Read(); // read the received data
USART_Write(i); // send data via USART
}
}
}
 

Here's a sample code:
Receiver:
Code:
unsigned char PortVal;

void main(void){
     //RX micro
     TRISC.F6 = 0; //Tx output pin
     TRISC.F7 = 1; //Rx input pin
     PORTB = 0;
     TRISB = 0;
     
     USART_Init(9600); //Initialize USART module at 9600bps
     do{
        if (USART_Data_Ready()){
           PortVal = USART_Read(); //Read value through UART
           PORTB = PortVal;
        }
     }while(1);
}

Transmitter:
Code:
unsigned char PortVal;

void main(void){
     //TX micro
     ADCON1 = 7; //Disable comparators
     TRISA = 0x3F; //inputs
     TRISC.F6 = 0; //TX output pin
     TRISC.F7 = 1; //RX input pin

     USART_Init(9600); //Initialize USART module at 9600bps
     do{
        PortVal = PORTA; //Read value off PORTA
        USART_Write(PortVal); //Send PORTA value through USART to Rx micro
        delay_ms(200); //Wait 200ms before sending next value
     }while(1);
     
}

It's in mikroC.

Hope this helps.
Tahmid.

---------- Post added at 06:59 ---------- Previous post was at 06:58 ----------

Hi AliRazor,
When I was posting, your post wasn't there, so I posted the code I just wrote. I tested it and it's fine on simulation.

Hope this helps.
Tahmid.
 
you are the best.thanks,thanks

---------- Post added at 07:03 ---------- Previous post was at 07:00 ----------

which one is better,your first code or second one?

---------- Post added at 07:07 ---------- Previous post was at 07:03 ----------

thanks so much.
 

Hi,
The first code is for the slave, ie receiver. The second is for the master, ie transmitter. Data is sent from the master to slave (Tx to Rx/Transmitter to Receiver) via 2 data lines (it can be sent from Rx to Tx if required as well).

You need the 1st code for one 16F877, the 2nd for another.
Be careful, you specified 16F877, there's another 16F877A which has a comparator that needs to be disabled for use.

Hope this helps.
Tahmid.
 
so, i write the second code in master pic16f877 and first code in slave pic16f877?

---------- Post added at 07:16 ---------- Previous post was at 07:15 ----------

C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
 

Yes,
You could write
Code:
CMCON = 7;
to disable comparators. 16F877A has no C1on or C2on, that's in 16F887. For 16F877A, you have to write 7 to CMCON, ie to the bits CM[2:0]

Hope this helps.
Tahmid.
 
I have one problem with pic16f877,how can i make analog pin digital?
I write ansel=0; anselh=0; adcon1=7; but when i use RA4 as digital input, it does not work.
BTW thanks for informative answers.
 

Hi,
16F877 doesn't have ansel or anselh. That's because RA4 is open collector. Connect a 10k resistor from RA4 to +5v and switch/input from RA4 to gnd.

Hope this helps.
Tahmid.
 
Continue eng
 
Last edited:

thanks again.

I tried the code for receiver and transmitter in MikroC ver 4.1 but it gives error so i did some modification:

Receiver:


unsigned char PortVal;





void main(void){
//RX micro
TRISC.F6 = 0; //Tx output pin
TRISC.F7 = 1; //Rx input pin
PORTB = 0;
TRISB = 0;

UART1_Init(9600); //Initialize USART module at 9600bps
do{
if (UART1_Data_Ready()){
PortVal = UART1_Read(); //Read value through UART
PORTB = PortVal;

}
}while(1);
}


Transmitter:


unsigned char PortVal;

void main(void){
//TX micro
ADCON1 = 7; //Disable comparators
//TRISA = 0x3F; //inputs
trisa=1;
TRISC.F6 = 0; //TX output pin
TRISC.F7 = 1; //RX input pin

UART1_Init(9600); //Initialize USART module at 9600bps
do{
PortVal = PORTA; //Read value off PORTA

UART1_Write (PortVal); //Send PORTA value through USART to Rx micro
delay_ms(200); //Wait 200ms before sending next value
}while(1);

}

---------- Post added at 16:00 ---------- Previous post was at 15:55 ----------

you mean something like this:

98_1290265204.jpg
 

Yes, I mean like that. The error may be due to the fact that you're using v4.1 and I used v8.2. Can you please point out the errors? It is sure to benefit everyone.

Hope this helps.
Tahmid.
 

instead of USART ==> UART1

---------- Post added at 16:25 ---------- Previous post was at 16:22 ----------

sorry i use mikroC pro for pic v4.1

mikroC PRO for PIC - C compiler for PIC microcontroller - mikroElektronika

---------- Post added at 16:26 ---------- Previous post was at 16:25 ----------

i tried exactly that method for RA4 and it was giving Error.but for other ports no problem.
 

for example i connect a led as O/P to port B and I/P to portA.f4, when I/P is HIGH the O/P should be on and for LOW,OFF
the O/P is either ON always Or OFF always when i do simulation.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top