about i2c again

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Posts: 6
Joined: Wed Nov 30, 2005 2:05 pm

about i2c again

Postby manio1 » Sun Mar 19, 2006 8:18 pm

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

Posts: 110
Joined: Thu Sep 29, 2005 10:53 am
Location: Matrix Multimedia
Been thanked: 1 time

Postby Ian » Mon Mar 20, 2006 10:19 am

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

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.

User avatar
Matrix Staff
Posts: 3146
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 103 times
Been thanked: 412 times

Postby Steve » Mon Mar 20, 2006 12:14 pm

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.

Posts: 6
Joined: Wed Nov 30, 2005 2:05 pm

Postby manio1 » Mon Mar 20, 2006 12:38 pm

thanks a lot people. i think i got it.