Status
Not open for further replies.

#### mrDiode

##### New Member
hello
I have try to test the library of software I2C in mikroC using PIC16F877 by writing some byte to a memory 24c512 then read them and put the output on portD which is connected to leds to make sure that the reading values are correct
I make the program to write once and read many but really what I get is that sequential reading didn't occur and I read them only once and seems as if I get hanged after the first reading
Code:
void main()
{
int i=0;
trisc=0x00;
trisd=0x00;
Soft_I2C_Config(&portc,0,1);

Soft_I2C_Start();
Soft_I2C_Write(0xA0);         //initialization to mem for writing
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x01);           //00000001
Soft_I2C_Write(0x02);           //00000010
Soft_I2C_Write(0x03);
Soft_I2C_Write(0x04);
Soft_I2C_Write(0x05);
Soft_I2C_Write(0x06);
Soft_I2C_Write(0x07);
Soft_I2C_Write(0x08);
Soft_I2C_Write(0x09);
Soft_I2C_Write(0x0a);
Soft_I2C_Stop();

loop:
portd=0x00;

Soft_I2C_Start();
Soft_I2C_Write(0xA0);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);

Soft_I2C_Start();
Soft_I2C_Write(0xA1);          //initialization to mem for reading

while(i!=9)
{
delay_ms(3000);
i++;
}
delay_ms(3000);
Soft_I2C_Stop();

goto loop;

}

#### AtomSoft

##### Well-Known Member
how about trying to start and stop each time. meaning:

Code:
void main()
{
int i=0;
trisc=0x00;
trisd=0x00;
Soft_I2C_Config(&portc,0,1);

Soft_I2C_Start();
Soft_I2C_Write(0xA0);         //initialization to mem for writing
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x01);           //00000001
Soft_I2C_Write(0x02);           //00000010
Soft_I2C_Write(0x03);
Soft_I2C_Write(0x04);
Soft_I2C_Write(0x05);
Soft_I2C_Write(0x06);
Soft_I2C_Write(0x07);
Soft_I2C_Write(0x08);
Soft_I2C_Write(0x09);
Soft_I2C_Write(0x0a);
Soft_I2C_Stop();

loop:
portd=0x00;

Soft_I2C_Start();
Soft_I2C_Write(0xA0);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00); //[COLOR="Red"]
Soft_I2C_Stop();      // [/COLOR]

Soft_I2C_Start();
Soft_I2C_Write(0xA1);          //initialization to mem for reading [COLOR="Red"]
Soft_I2C_Stop();

Soft_I2C_Start();				// [/COLOR]
while(i!=9)
{
delay_ms(3000);
i++;
}
delay_ms(3000);
Soft_I2C_Stop();

goto loop;

}

#### Pommie

##### Well-Known Member
After you write the address you should send a restart before issuing the read command.
After you receive a byte you should send an Ack (acknowledge).
After receiving the last byte you should send a Nack (Not acknowledge).

Mike.

#### AtomSoft

##### Well-Known Member
Mike its MikroC so i assume if its a library it should have included it in the read command

#### mrDiode

##### New Member
I have tried yr advice Atomsoft but not working
but I have noticed a strange thing in my first read process which done correctly
that the last byte to read (value 10) didn't appear on leds as if
the pic try to execute
Code:
portD=Soft_I2C_Read(0);
delay_ms(3000);
Soft_I2C_Stop();
and fail then enter undefined mode and lost communication with I2C
is there any suggestions
thanks

#### AtomSoft

##### Well-Known Member
heh just one question. Why are you reading the same (1) 10 times if you know its not going to change? and why delay 3 seconds?

Code:
     while(i!=9)
{
delay_ms(3000);
i++;
}
This is why i like to create my own C code for stuff like this. Hence the reason i use C18 lol I hate trying to figure out the libraries from another program. If it doesnt get resolved by someone later on today ill download MikroC and see whats up ok

#### mrDiode

##### New Member
hi Atomsoft,
I want to explain something about my code
I have saved 11 values in the memory then I made a sequential read of them from the memory so in the while loop each time I read a new value and the delay just only for noticing the output on the leds
and since this is a sequential read i have to ack each time except in the last one

#### AtomSoft

##### Well-Known Member
This only reads value in field or memory address 0x01 tho i think:

Code:
    while(i!=9)
{
delay_ms(3000);
i++;
}
Wouldnt this be it:

Code:
    while(i!=9)
{
delay_ms(3000);
i++;
}

#### mrDiode

##### New Member
from mikroC help,
Prototype

Returns
Returns one byte from the slave.

Description
Reads one byte from the slave, and sends not acknowledge signal if parameter ack is 0, otherwise it sends acknowledge

Requires
Soft I²C must be configured before using this function. See Soft_I2C_Config.
Also, START signal needs to be issued in order to use this function. See Soft_I2C_Start

Example
Read data and send not acknowledge signal:

short take;
...

#### Pommie

##### Well-Known Member
You must have a stop after a start or the device ignores the commands.

Try,
Code:
     loop:
portd=0x00;

Soft_I2C_Start();
Soft_I2C_Write(0xA0);
Soft_I2C_Write(0x00);
Soft_I2C_Write(0x00);

Soft_I2C_Start();
Soft_I2C_Write(0xA1);          //initialization to mem for reading
while(i!=9)
{
delay_ms(3000);
i++;
}
delay_ms(3000);
Soft_I2C_Stop();

goto loop;
You also weren't initialising i in the loop.

Mike.

#### mrDiode

##### New Member
problem solved

hi,
thanks Atomsoft
thanks Pommie
problem solved

Status
Not open for further replies.