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.

Interface two (2) PIC micro controller by using I2c Protocol

Status
Not open for further replies.

desgin

Full Member level 1
Joined
Apr 7, 2017
Messages
96
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
813
I want to make master(PIC18f4550) and one slave(PIC16f477a) communicate via I2C using these PIC micro controller. I am using MPLAB x IDE for writing the codes.
I have no idea how to do this. Can someone please suggest some tutorial site or code from where I could learn how to use PIC for I2C.
It would be a lot of help if anyone could give me the code for master and slave modules for the above mentioned PICs.
Please help me Guys.
Sorry for bad english :-(

Thanks in advance !!
Desgin
 

Might be a typo on the PIC16F477A (no such device), but if both devices have UARTS, it would be WAY simpler to get the 2 parts to talk to each other.
 

Hello,

... if anyone could give me the code for master and slave modules for .. PICs.

maybe**broken link removed** with 2 PIC 16F1847 could help you ..
One PIC send 2 values to the Slave
The slave add the 2 values
and send the result to the master
(MikroC pro7.01)
 

Might be a typo on the PIC16F477A (no such device), but if both devices have UARTS, it would be WAY simpler to get the 2 parts to talk to each other.

Ok fine.
If i use both PIC18F4550 (Maste and Slave) then it will be OK ??
I am first trying with UART but in that also nothing is coming :-(
Then finally i want to use I2C protocol only

Thanks in advance !!
Desgin
 

I am first trying with UART but in that also nothing is coming
Then finally i want to use I2C protocol only

You are choosing the most painful path.
Serial communications are somewhat easiest to work and easiest to debug.
 

If you can't get a UART to work then, as others have said, going to a I2C solution is only going to make things worse.
You are not helping yourself with vague statements like "nothing is coming".
With the PIC18F4550, can you connect the Tx and Rx pins for the UART together and have the UART receive what ever character you send? If you can then try the same thing with whatever the PIC16F device is that you have. Once you can do that with each device separately, then try to connect them together. At least with that approach you can tell where the problem might be.
Also can you show us the code you have written as the answer will most likely be there, or with the way you are setting up the device with the config settings.
Susan
 

If you can't get a UART to work then, as others have said, going to a I2C solution is only going to make things worse.
You are not helping yourself with vague statements like "nothing is coming".
With the PIC18F4550, can you connect the Tx and Rx pins for the UART together and have the UART receive what ever character you send? If you can then try the same thing with whatever the PIC16F device is that you have. Once you can do that with each device separately, then try to connect them together. At least with that approach you can tell where the problem might be.
Also can you show us the code you have written as the answer will most likely be there, or with the way you are setting up the device with the config settings.
Susan

Ya thanks for the reply.
I checked UART separately, its working correct :)
But when i am connection Tx of 1st controller to Rx of 2nd controller & VCC to VCC - GND to GND.
Nothing is working out.
Is there any changes required in hardware ?

- - - Updated - - -

You are choosing the most painful path.
Serial communications are somewhat easiest to work and easiest to debug.

Ya i am trying with UART first now.
Thanks for your advice.
below is my code.

MASTER Program:

Code:
unsigned char data;

void main(void)
{
    ADCON1 = 0x0F;       
    lcd_on();   
    OSCCON = 0x72; 
    uart_On();
    while(1)
    {               
        data='d';    
        transmitted_data(data);                         
    }
}


SLAVE Program:

Code:
unsigned char data;

void main(void)
{
    ADCON1 = 0x0F;       
    lcd_on();   
    OSCCON = 0x72; 
    uart_On();
    while(1)
    {               
        data = received_data();
        lcd_data(data);                        
    }
}
 
Last edited by a moderator:

Hi,

If both devices have separate power supplies, then don´t connect VCC.
Just: RxD-TxD, TxD-RxD, GND-GND.

**
You need to ensure that baudrates match. And you need to ensure that signal voltage levels match.


Klaus
 

Hi,

If both devices have separate power supplies, then don´t connect VCC.
Just: RxD-TxD, TxD-RxD, GND-GND.

**
You need to ensure that baudrates match. And you need to ensure that signal voltage levels match.


Klaus

Ya thanks for the reply.
I am giving Vcc from 1st controller to 2nd controller & GND to GND.
How to ensure that
1. baudrates are matching ?
2. signal voltage levels match ?
In program i am giving 9600 baud rate ( in both UART source file), its same for both.
 

hello,


if you send to quickly data, UART will hang because OVERRUN or FRAME error .. on Receiver side !
Add a delay beetwen each invoiceon Emitter side..

Code:
unsigned char data;


void main(void)
{
    ADCON1 = 0x0F;       
    lcd_on();   
    OSCCON = 0x72; 
    uart_On();
    while(1)
    {               
        data='d';    
        transmitted_data(data); 
      Delay_ms(100);   // <----------- add a delay !        
    }
}

on receiver :
Where is LCD init ?
You must manage also the position XY on LCD , before writtin data
and after receiving 16 times the char , put again the LCD cursor at the begining of LCD position
 
  • Like
Reactions: desgin

    desgin

    Points: 2
    Helpful Answer Positive Rating
hello,


if you send to quickly data, UART will hang because OVERRUN or FRAME error .. on Receiver side !
Add a delay beetwen each invoiceon Emitter side..

Code:
unsigned char data;


void main(void)
{
    ADCON1 = 0x0F;       
    lcd_on();   
    OSCCON = 0x72; 
    uart_On();
    while(1)
    {               
        data='d';    
        transmitted_data(data); 
      Delay_ms(100);   // <----------- add a delay !        
    }
}

on receiver :
Where is LCD init ?
You must manage also the position XY on LCD , before writtin data
and after receiving 16 times the char , put again the LCD cursor at the begining of LCD position

Ya that also done.. but no output at receiever side.
But my question is, how slave project folder understand to work out.
I have to load slave program in that once ?\
Bit confused really.
Please help me out.

thanks in advance !!
 

How to ensure that
1. baudrates are matching ?

You can echoes the received byte from transitter, or temporarily insert into the receiver's side a routine to send a byte back to the transmitter some character to verify that at least if the baudrate is configured correctly or not.

In program i am giving 9600 baud rate ( in both UART source file), its same for both.

You have not yet shown the part of the code that initializes the LCD and the UART. It were expected some function with the prefix Init_xxx; or are you just assuming that it is already being configured, like using some default value provided by the compiler ?
 
  • Like
Reactions: desgin

    desgin

    Points: 2
    Helpful Answer Positive Rating
Thanks for the reply.
How to start with I2C communication between 2 Microcontroller ?

Thanks in advance!!
Desgin

- - - Updated - - -

You can echoes the received byte from transitter, or temporarily insert into the receiver's side a routine to send a byte back to the transmitter some character to verify that at least if the baudrate is configured correctly or not.



You have not yet shown the part of the code that initializes the LCD and the UART. It were expected some function with the prefix Init_xxx; or are you just assuming that it is already being configured, like using some default value provided by the compiler ?

I already post the code, Sir.
I have initialized LCD and UART both.
I have loaded slave program first, is this correct procedure ?
then master but nothing is coming out at receiver side.

Thanks in advance !!
Desgin
 

I already post the code
I have initialized LCD and UART both.

NO, in the above code there isn't any reference to the initialization of the program to neither to the UART nor to the LCD. Moreover, you did not gave the circuit on which you are doing that.
 

hello,

How to start with I2C communication between 2 Microcontroller ?

if you allways swap between UART or I2C link.. difficult to follow

and be more serious :
post all your project ( Config bit + source) + schematic
Then
you maybe will get usable support.
 
  • Like
Reactions: desgin

    desgin

    Points: 2
    Helpful Answer Positive Rating
So you have verified that that the Master is sending data already via serial tool? Just want the make sure.

Can the Slave put anything on the LCD? Try putting something on the screen before entering the while loop. (Comment out the LCD logic in the loop to make sure it does not corrupt the test.) Just to make sure that the LCD is working.

Do you have an led? Try flashing it. Then make it flash when you send a specific character. Just to make sure you can receive data.

The delay on the transmission, lets make the that stupidly large. Just cause if your gonna make a delay go big! (Within reason! 1-2sec) :)

It is worth noting that you appear to be using the internal clock. This could be a problem. If you have an external crystal, I would try adding it to both boards and reconfiguring it to use it. UART uses a baud which embeds a clock of sorts in the signal. This is agreed on ahead of time and both sides must match. If the clock sources are a wrong there can be a drift in sampling. UART is somewhat forgiving, but only up to a certain point. There is also a small amount of syncing.

Note you will not need the external crystal with i2c, since the master sends the clock to sample off of with the data. Just need to make sure that both can talk at the selected CPU, which is not a problem here since they are the same microcontroller.
 
  • Like
Reactions: desgin

    desgin

    Points: 2
    Helpful Answer Positive Rating
hello,



if you allways swap between UART or I2C link.. difficult to follow

and be more serious :
post all your project ( Config bit + source) + schematic
Then
you maybe will get usable support.

Ya, my goal is to work on I2C, not UART.
I was just simply checking and understanding communication between two micro controller by UART.
So, Please help me for the I2c. UART i will manage by myself.

Thanks in advance!!
Desgin
 

So you have verified that that the Master is sending data already via serial tool? Just want the make sure.

Can the Slave put anything on the LCD? Try putting something on the screen before entering the while loop. (Comment out the LCD logic in the loop to make sure it does not corrupt the test.) Just to make sure that the LCD is working.

Do you have an led? Try flashing it. Then make it flash when you send a specific character. Just to make sure you can receive data.

The delay on the transmission, lets make the that stupidly large. Just cause if your gonna make a delay go big! (Within reason! 1-2sec) :)

It is worth noting that you appear to be using the internal clock. This could be a problem. If you have an external crystal, I would try adding it to both boards and reconfiguring it to use it. UART uses a baud which embeds a clock of sorts in the signal. This is agreed on ahead of time and both sides must match. If the clock sources are a wrong there can be a drift in sampling. UART is somewhat forgiving, but only up to a certain point. There is also a small amount of syncing.

Note you will not need the external crystal with i2c, since the master sends the clock to sample off of with the data. Just need to make sure that both can talk at the selected CPU, which is not a problem here since they are the same microcontroller.

For i2c,
Master and slave both projects will be different folders, right ?
And first slave project has to loaded in micro controller. correct ?

Thanks in advance!!
Desgin
 

For i2c,
Master and slave both projects will be different folders, right ?
And first slave project has to loaded in micro controller. correct ?

You are asking steps that are quite trivial in microcontrollers programming field; Before making a project with communication between two systems ( which would require some debugging skill ) I would recommend doing something a little bit more simple, starting from a blinking led, latter sending data to PC and so on. Answering the above specific question, yes, if transmitter and receiver do different stuffs, you have different codes and you should create projects in sepparated files.
 
  • Like
Reactions: desgin

    desgin

    Points: 2
    Helpful Answer Positive Rating
You are asking steps that are quite trivial in microcontrollers programming field; Before making a project with communication between two systems ( which would require some debugging skill ) I would recommend doing something a little bit more simple, starting from a blinking led, latter sending data to PC and so on. Answering the above specific question, yes, if transmitter and receiver do different stuffs, you have different codes and you should create projects in sepparated files.

Ya i am new to EMBEDDED world,
But I tried with all simple program like LED, push button and sending data to pc and so on.
so only now i am trying with i2c protocol after UART communication.
Can anybody help me with code of Master and Slave i2c programming ?
Its bit urgent, i need to submit within a week to college. :-(

Thanks in advance.
Desgin
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top