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
Full Member level 1
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.
 

ratheeshbr

Member level 1
Member level 1
Joined
Jul 26, 2009
Messages
37
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
1,445
Either you can use SPI or I2C to communicate between the two PIC
or Simply Serial Communication!
 

g_shyam1682

Full Member level 4
Full Member level 4
Joined
Jan 11, 2010
Messages
204
Helped
53
Reputation
106
Reaction score
43
Trophy points
1,308
Location
Udaipur-Rajshan-India
Activity points
2,538
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
 

Tahmid

Advanced Member level 6
Advanced Member level 6
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

emailkprajesh

Junior Member level 2
Junior Member level 2
Joined
Nov 24, 2005
Messages
24
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,283
Location
India
Activity points
1,382
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.
 

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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
}
}
}
 

Tahmid

Advanced Member level 6
Advanced Member level 6
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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.
 

Tahmid

Advanced Member level 6
Advanced Member level 6
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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;
 

Tahmid

Advanced Member level 6
Advanced Member level 6
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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.
 

Tahmid

Advanced Member level 6
Advanced Member level 6
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

Eng_TRCT

Newbie level 6
Newbie level 6
Joined
Nov 18, 2010
Messages
11
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,293
Activity points
1,340
Continue eng
 
Last edited:

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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
 

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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.
 

AliRazoR

Advanced Member level 4
Full Member level 1
Joined
Oct 26, 2010
Messages
115
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,001
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

Top