16F18325 Slave Code I2C1

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
robertj
Posts: 11
Joined: 15 Oct 2013 01:36

16F18325 Slave Code I2C1

#1 Post by robertj » 11 Apr 2022 18:20

This is a repeat of a question on the 19f18425 when we discovered it was not supported

We have redesigned our circuit with a 16F18325. We needed a PIC with two I2C modules. One to be a slave and report data back to a controller and one to be a master and read a sensor. The master part works OK but the slave won't respond to the address we assign. We put a marker into the program and discovered the chip is seeing the address and going into the interrupt routine but we get no output. The masted just times out.

Is there a slave library available.
Is there some example code geared to the 16F18325 for I2C1 slave?

Here is the initialization and my code as simple as I can make it. I should get clock pulses that clock out SP1BUFF after CKP is set.


void interrupt() // I2C slave interrupt handler
{
if (PIR1.SSP1IF == 1) // I2C Interrupt ?
{
PIR1.SSP1IF = 0; // reset SSP interrupt flag

if(SSP1STAT.b2 == 1) // READ/_WRITE I2C SLAVE READ?
{
SSP1BUF = 200; // Get data to send
SSP1CON1.B4 = 1 ; // CKP Release SCL line and transmit data to master
j = SSP1BUF; // Read buffer to clear flag
porta.b4 = 0;
porta.b4 = 1; //marker pulse
porta.b4 = 0;
return;
}
}
}

void main()
{
OSCCON1 = 0b00000001; //Set clock to internal PLL 16 mhz
TRISA = 0b11101111; // RA 4 becomes output
TRISC = 0b00110011; //I2C1=RC0,RC1 I2C2=RC4,RC5
ANSELA = 0b00000000; //Disable other modules Port A
ANSELC = 0b00000000; //Disable other modules Port C
PMD4 = 0b00100000;
SSP1ADD = Addy;
PIE1.B3 = 1; // SSP1IE Enables the MSSP interrupt
INTCON = 0b11000000;
SSP1STAT = 0b0000000;
SSP1CON1 = 0b00110110;
SSP1CON2 = 0b10110000;
SSP1CON3 = 0b00000010;
Attachments
SHOULD BE.jpg
SHOULD BE.jpg (38.69 KiB) Viewed 939 times
NO REPLY.jpg
NO REPLY.jpg (37.87 KiB) Viewed 939 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: 16F18325 Slave Code I2C1

#2 Post by filip » 14 Apr 2022 12:44

Hi,

I believe I have answered you on the other topic.

Regards,
Filip.

robertj
Posts: 11
Joined: 15 Oct 2013 01:36

Re: 16F18325 Slave Code I2C1

#3 Post by robertj » 27 Apr 2022 19:44

Hi Filip

I have struggled in vain for a month now trying to get the I2C module to function in a PIC 16F18325. I have looked at and worked with numerous examples of code that claim to be valid code.

I have to explain this to my bosses and a project will have to be scrapped or at least redesigned without a PIC or the use of your compiler. So just to make it clear can you verify you do not support the 16F18325. Can you recommend another PIC or is there simply no support for I2C in slave mode in all PICs with MikroC pro. That seems to be the impression based on your last reply.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: 16F18325 Slave Code I2C1

#4 Post by filip » 29 Apr 2022 14:54

Hi,

The I2C slave is something that is left to user to implement for their application, we provide the library for I2C Master only.

Maybe you could browse through the forum to see if some of our users managed to implement it for your MCU.

Regards,
Filip.

robertj
Posts: 11
Joined: 15 Oct 2013 01:36

Re: 16F18325 Slave Code I2C1

#5 Post by robertj » 12 Jan 2023 01:36

January 2023

I now have something that works. The microprog debugger does not work with the 16F18325 but it does program. I ended up programming an output I could monitor with a scope and going step by step until each line of code worked. I2C2 is the master retrieving 2 bytes of data from a sensor. I2C1 is a slave echoing that data along with two additional bytes of calculations. This was derived from a forum contributor's code for a 16F877. There is no receive data to I2C1 but for some reason the code and variables have to be there. That's why you see RXI along with TXI. If you notice there are nops inserted. These delays are what caused the problems especially CKP.

I left out the code reading from the sensor. That's straight forward using the MIKROC library.

I2C1_init(250000);
OSCCON1 = 0b00000001; //Set clock to internal PLL 16 mhz
TRISA = 0b11011111; // RA 4 becomes output
TRISC = 0b00110011; //I2C1=RC0,RC1 I2C2=RC4,RC5 SET TO INPUTS
ANSELA = 0b00000000; //Disable other modules Port A
ANSELC = 0b00000000; //Disable other modules Port C
PMD4 = 0b00100000;
PIE1.B3 = 1; // SSP1IE Enables the MSSP interrupt
INTCON = 0b11000001;
SSP1STAT = 0b00000000;
SSP1CON1 = 0b00110110;
SSP1CON2 = 0b00100001;
SSP1CON3 = 0b00000101;
I2C2_init(400000);
delay_ms(10);

//Code for reading sensor.

void interrupt() // I2C slave interrupt handler
{
if (PIR1.SSP1IF == 1) // I2C Interrupt ?
{
PIR1.SSP1IF = 0; //reset SSP interrupt flag

if((SSP1STAT.b2) & (!SSP1STAT.b4)) //_WRITE I2C SLAVE READ?
{
asm {
nop
nop
nop
nop
}

SSP1BUF = tx_data[txi]; //load tx buffer

asm {
nop
nop
nop
nop
nop
}

SSP1CON1.B4 = 1 ; // CPK Release SCL line

asm {
nop
nop
nop
nop
nop
}

if (txi > 3) txi = 0;
if(SSP1STAT.b0)txi++; //if buffer not read don't increment

asm {
nop
nop
nop
nop
nop
}

j = SSP1BUF; // reset buffer flag
return;
}

if ((!SSP1STAT.b0)&(SSP1STAT.b4)) // BUFFER FULL all done,
{
txi=0; // all data transmitted, reset array
rxi=0;
j = SSP1BUF;
return;
}

//recieve data from master
if (SSP1STAT.b5 == 1) // D/_A Data [not address]
{
rxbuffer[rxi] = SSP1BUF; // Put buffer rx array
rxi++; // Inc array counter
j = SSP1BUF; // read buffer to clear flag [address]
return;
}

}
j = SSP1BUF; // read buffer to clear flag [address]
}

Post Reply

Return to “mikroC PRO for PIC General”