Page 1 of 1

about i2c again

Posted: Sun Mar 19, 2006 8:18 pm
by manio1
char I2C_ReadByte(char lastbyte)
{
char retVal = 0;
char mask;

//wait until the bus is idle...
mask = I2C_MaskFromBit(R_W);
while (sspstat & mask); // tx in progress?

mask = I2C_MaskFromBit(ACKEN);
mask = mask | I2C_MaskFromBit(RCEN);
mask = mask | I2C_MaskFromBit(PEN);
mask = mask | I2C_MaskFromBit(RSEN);
mask = mask | I2C_MaskFromBit(SEN);
while (sspcon2 & mask); // bus busy?

//set RCEN and wait for it to clear
mask = I2C_MaskFromBit(RCEN);
sspcon2 |= mask;
while ((sspcon2 & mask) == mask);

if (lastbyte == 0)
{
clear_bit(sspcon2, ACKDT);
} else {
set_bit(sspcon2, ACKDT);
}

//set ACKEN and wait for it to clear
mask = I2C_MaskFromBit(ACKEN);
sspcon2 |= mask;
while ((sspcon2 & mask) == mask);

retVal = sspbuf;

return (retVal);
}

Can anybody explain me how the lastbyte parameter works in the code above?

I was looking at the c code generated by the web server component, and the function there is used one time with parameter 1, and then with parameter 0

Posted: Mon Mar 20, 2006 10:19 am
by Ian
The following was taken from the 16F877A datasheet p85
(PAge deals with SSPCON2 register so is worth looking at)

ACKDT: Acknowledge Data bit (Master Receive mode only)
1 = Not Acknowledge
0 = Acknowledge
Note: Value that will be transmitted when the user initiates an Acknowledge sequence at
the end of a receive.

The following was taken from the 16F877A datasheet p104

ACKNOWLEDGE SEQUENCE
TIMING
An Acknowledge sequence is enabled by setting the
Acknowledge Sequence Enable bit, ACKEN
(SSPCON2<4>). When this bit is set, the SCL pin is
pulled low and the contents of the Acknowledge data bit
are presented on the SDA pin. If the user wishes to generate
an Acknowledge, then the ACKDT bit should be
cleared. If not, the user should set the ACKDT bit before
starting an Acknowledge sequence. The Baud Rate
Generator then counts for one rollover period (TBRG)
and the SCL pin is deasserted (pulled high). When the
SCL pin is sampled high (clock arbitration), the Baud
Rate Generator counts for TBRG. The SCL pin is then
pulled low. Following this, the ACKEN bit is automatically
cleared, the baud rate generator is turned off and the
MSSP module then goes into Idle mode



The above was from the 16F877A datasheet, but other PICs will have a similar MSSP section.Which you would be advised to read through.

So basically it is used to control acknowledment sequencing.

Posted: Mon Mar 20, 2006 12:14 pm
by Steve
I seem to remember that when reading a continuous block of data from the Internet module, you need to maintain this ASK bit high except when reading the final byte.

Note that this may be specific to the behaviour of the Internet module rather than general to I2C.

Posted: Mon Mar 20, 2006 12:38 pm
by manio1
thanks a lot people. i think i got it.