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] I need I2C Master Slave code

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
I need I2C Master Slave code

Please provide me mikroC PRO PIC Code where there will be 1 master PIC16F887 and another slave PIC16F887. The master requests data from slave and slave sends out data to master.
 

I made this project but it is not working. Please tell me what is wrong in the code. I have only written code for master and slave1.

- - - Updated - - -

I have done the project in CCS C Compiler and it works fine. I want to do it in mikroC. Please help me with my mikroC code.
 

Attachments

  • I2C Master Slave.rar
    152.6 KB · Views: 109
  • I2C Master Slave.rar
    196.2 KB · Views: 119

I have modified the slave1 code and I am only testing with master and slave1. It is still not working. Even though ADC is working properly the variable fValue.fVal holds 0xFFFFFFFF. You can see in the i2c debugger data that after read address is sent 0xFF is transmitted. Also the received data at master is not printing on LCD. What am I doing wrong. How to send Nack from slave after all the data is sent ? See i2c debugger data. Nack is not happening at the end.

broken link removed
 

Attachments

  • I2C Master Slave rev1.rar
    176.3 KB · Views: 107
  • i2c m s comm.png
    i2c m s comm.png
    38.9 KB · Views: 214
Last edited by a moderator:

I modified the code a little but still i2C is not working. What is wrong in my code. Please provide me a solution. I made the project using CCS C and obtained the .lst file of the CCS C slave code and then implemented the ISR code of CCS C slave code in mikroC using C. Nack is not happening. Please help me.
 

Attachments

  • I2C Master Slave rev3.rar
    230.2 KB · Views: 84

I made some changes to the code after reading AN734. Now I2C communication is happening but there are two issues. NACK is not happening. I know because master is not sending Read last byte and Nack. If I do read last byte and then Nack from master then after 1st I2C transmission I2C hangs. I don't see any errors in Proteus and also hardware. The second issue is slave is sending only 0x80 for requested data.

I have 3 EasyPIC v7 boards and I am testing in hardware. I have saleae Logic Analyzer. My connections are correct because I tested the CCS C I2C Master Slave project which I posted earlier and it works fine on hardware and also the signals on the Logic Analyzer is good.

Fore mikroC project I am getting the same data on Logic Analyzer as shown in Proteus I2C debugger.

Please help me solve the two issues.

broken link removed
 

Attachments

  • I2C Master Slave mikroC PRO PIC rev1.rar
    258.4 KB · Views: 125
  • i2c.png
    i2c.png
    65.5 KB · Views: 173
Last edited by a moderator:

Thank you people for helping me. I finally managed to complete the project. There is now one small issue but it is not related to i2c but to LCD. If anybody can help me then I would really appreciate it.

Maybe it is a bug of mikroC.

Even though datas of slaves are printed on different lines of masters' LCD in code the datas are printing on the same line.

Regarding I2C comm I used the example from here.

ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2003/158

Eady-158.zip

I used the same ISR code but added a dummy i2c read after write and it solved the problem of code not working with more than 1 slave PICs.

I tested it in both Proteus and hardware and it is working fine in both. The signals on my Saleae Logic Analyzer is good but the LCD problem remains in hardware also.

broken link removed

- - - Updated - - -

I had done a silly mistake while copy pasting a part of the masters' code. I fixed it and LCD works fine. Here is the fix.

This completes my project.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Read from the slave 1
        I2C1_Start();
        I2C1_Wr(0xA0);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA1);
        value1.b[0] = I2C1_Rd(1);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA1);
        value1.b[1] = I2C1_Rd(1);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA1);
        value1.b[2] = I2C1_Rd(1);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA1);
        value1.b[3] = I2C1_Rd(0u);
        I2C1_Stop();
        
        Delay_ms(1000);
        
        // Read from the slave 1
        I2C1_Start();
        I2C1_Wr(0xA6);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA7);
        value2.b[0] = I2C1_Rd(1);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA7);
        value2.b[1] = I2C1_Rd(1);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA7);
        value2.b[2] = I2C1_Rd(1);
        I2C1_Repeated_Start();
        I2C1_Wr(0xA7);
        value2.b[3] = I2C1_Rd(0u);
        I2C1_Stop();



broken link removed

- - - Updated - - -

Please don't bang your head like me. See this image.


broken link removed
 

Attachments

  • I2C Master Slave mikroC PRO PIC rev3.rar
    392.5 KB · Views: 162
  • i2c more slaves.png
    i2c more slaves.png
    74.3 KB · Views: 216
  • i2c working fine.png
    i2c working fine.png
    63.5 KB · Views: 186
  • i2c correct wrong.png
    i2c correct wrong.png
    136.3 KB · Views: 183
Last edited by a moderator:

This is awesome, its great when you kind of have problem and you were able to solve it yourself

Good job man.

thanks for sharing your experience
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top