class test

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

class test

#1 Post by bilal.iq.lhr » 21 Jan 2017 11:56

hi
i m having class project on I2c unable to find the result for finding my project problem will any one help me
using mikro C pro
project is this device one is master and device 2 is slave both are PIC16F887
i attached my zip.
Attachments
i2c c zip.rar
(970 Bytes) Downloaded 197 times

bilal.iq.lhr
Posts: 28
Joined: 10 Aug 2016 18:16

Re: class test

#2 Post by bilal.iq.lhr » 22 Jan 2017 16:43

can any one help one this
can any one tell me whats the error in the code
MASTER block

Code: Select all

void main() {
TRISC.RA3 = 1;
TRISC.RA4 = 1;
ANSEL = 0x00;
ANSELH = 0x00;
TRISA.RA4 = 0;
PORTA = 0x00;
SSPCON.SSPEN = 1;
SSPCON.SSPM3 = 1;
SSPCON.SSPM2 = 0;
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 0;

for(;;)
{
I2C1_Init(100000);
PORTA.RA4 = 1;
I2C1_Start();
I2C1_Wr(0xAA);
I2C1_Repeated_Start();
I2C1_Wr(0x01);
I2C1_Wr(0x02);
I2C1_Wr(0x03);
Delay_ms(200);
PORTA.RA4 = 0;
I2C1_Stop();
}
}
Slave block

Code: Select all

const SlaveA = 0xAA;
int data_rec = 0;
int data_chk = 0;
void main() {
ANSEL = 0x00;
ANSELH = 0x00;
TRISC = 0xFF;
TRISA = 0X00;
PORTA = 0x00;
TRISB = 0x00;
PORTB = 0x00;
SSPCON.SSPEN = 1;
SSPCON.SSPM3 = 0;
SSPCON.SSPM2 = 1;
SSPCON.SSPM1 = 1;
SSPCON.SSPM0 = 0;
SSPCON.CKP = 1;
SSPADD = SlaveA;
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.SSPIE = 1;
I2C1_Init(100000);
for(;;)
{
PORTB = data_rec;
Delay_ms(1000);
PORTB = 0;
PORTB = data_chk;
}
}
                  void interrupt()
                  {
                   if(PIR1.SSPIF == 1)
                   {
                    PORTA.RA4 = 1;
                    PIR1.SSPIF = 0;
                    if(SSPSTAT.D_A == 0)
                    {
                    PORTA.RA5 = 1;
                    }
                               if( SSPSTAT.R_W == 1)
                               {
                               data_rec = SSPBUF;
                               }
                               if(SSPSTAT.R_W == 0)
                               {
                               data_chk = SSPBUF;
                               }
                  }
                  }

User avatar
danilo.milovic
mikroElektronika team
Posts: 501
Joined: 05 Dec 2016 14:59

Re: class test

#3 Post by danilo.milovic » 23 Jan 2017 11:38

Hi,

here are couple of examples of I2C master/slave communication from libstock:

https://libstock.mikroe.com/projects/vi ... ster-slave

https://libstock.mikroe.com/projects/vi ... th-timeout

Hope this will help you.

Regards,

Danilo

Post Reply

Return to “User Projects”