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,528
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.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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: 12
  • I2C Master Slave.rar
    196.2 KB · Views: 11

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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.

117890d1432716015-i2c-m-s-comm.png
 

Attachments

  • I2C Master Slave rev1.rar
    176.3 KB · Views: 16
  • i2c m s comm.png
    i2c m s comm.png
    38.9 KB · Views: 59
Last edited:

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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: 12

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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.

117935d1432845541-i2c.png
 

Attachments

  • I2C Master Slave mikroC PRO PIC rev1.rar
    258.4 KB · Views: 15
  • i2c.png
    i2c.png
    65.5 KB · Views: 51
Last edited:

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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.

117941d1432854448-i2c-more-slaves.png


- - - 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();



117944d1432857613-i2c-working-fine.png


- - - Updated - - -

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


117946d1432859941-i2c-correct-wrong.png
 

Attachments

  • I2C Master Slave mikroC PRO PIC rev3.rar
    392.5 KB · Views: 45
  • i2c more slaves.png
    i2c more slaves.png
    74.3 KB · Views: 58
  • i2c working fine.png
    i2c working fine.png
    63.5 KB · Views: 61
  • i2c correct wrong.png
    i2c correct wrong.png
    136.3 KB · Views: 57

babatundeawe

Full Member level 5
Joined
Apr 12, 2007
Messages
244
Helped
16
Reputation
32
Reaction score
12
Trophy points
1,298
Location
Nigeria
Activity points
2,727
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

Top