Jump to content

Recommended Posts

Hi all - I found a bug in my I2C Slave demo code. State 3 & 4 are incorrectly defined. The following lines of code corrects the bug:

 

#define SSPSTAT_BIT_MASK  0b00101101			// Mask for I2C status bits
// bit 0 BF: Buffer Full Status bit
// bit 2 R/W: Read/Write bit Information (I2C mode only)
// bit 3 S: START bit
// bit 5 D/A: Data/Address bit (I2C mode only)

// AN734a DS00734A-page 10 - Definitions for Older Devices
//---------------------------------------------------------------------
// The I2C code below checks for 5 states:
// S   1 = Start condition occuurred last
// R/W 0 = Master writing data to Slave 1 = Master reading from Slave
// D/A 0 = Last Byte was an Address	 1 = Last Byte was Data
// BF  0 = Buffer Empty				 1 = Buffer Full
//---------------------------------------------------------------------
// The following table on AN734a DS00734A-page 10
// State 1: I2C write operation, last byte was an address byte.
// SSPSTAT bits: S = 1, D_A = 0, R_W = 0, BF = 1
// State 2: I2C write operation, last byte was a data byte.
// SSPSTAT bits: S = 1, D_A = 1, R_W = 0, BF = 1
// State 3: I2C read operation, last byte was an address byte.
// SSPSTAT bits: S = 1, D_A = 0, R_W = 1, BF = 0
// State 4: I2C read operation, last byte was a data byte.
// SSPSTAT bits: S = 1, D_A = 1, R_W = 1, BF = 0
// State 5: Slave I2C logic reset by NACK from master.
// SSPSTAT bits: S = 1, D_A = 1, R_W = 0, BF = 0
//----------------------------------------------------------------------
//				76543210
//				XXDXSRXB
#define state_1 0b00001001			// State 1 (0x09) - SSPSTAT bits: D_A = 0, S = 1, R_W = 0, BF = 1
#define state_2 0b00101001			// State 2 (0x29) - SSPSTAT bits: D_A = 1, S = 1, R_W = 0, BF = 1
#define state_3 0b00001100			// State 3 (0x0C) - SSPSTAT bits: D_A = 0, S = 1, R_W = 1, BF = 0
#define state_4 0b00101100			// State 4 (0x2C) - SSPSTAT bits: D_A = 1, S = 1, R_W = 1, BF = 0
#define state_5 0b00101000			// State 5 (0x28) - SSPSTAT bits: D_A = 1, S = 1, R_W = 0, BF = 0

Code_877A_1.c

Link to post
Share on other sites
Hi all - I found a bug in my I2C Slave demo code. State 3 & 4 are incorrectly defined. The following lines of code corrects the bug:

Hi Bernard,

 

I have a prototype PIC18F4520 slave apparently running OK with not too dissimilar code to yours.

 

What exactly were the incorrect definitions and how did the bug manifest itself?

 

I just want to make sure that I haven't made the same mistakes.

 

Have you posted the incorrect I2C Slave demo code previously? Your file Version and date of the latest code are very old!

 

Regards

 

davidb

Link to post
Share on other sites

Yes, the code is old, i have not used/updated it for while. I have swopped bits 1 and 2 in the following definitions.

 

#define state_3 0b00001100 // State 3 (0x0C) - SSPSTAT bits: D_A = 0, S = 1, R_W = 1, BF = 0

#define state_4 0b00101100 // State 4 (0x2C) - SSPSTAT bits: D_A = 1, S = 1, R_W = 1, BF = 0

 

the bug results in a State of 0x0A and 0x2C, which will not allow you to read data from the slave correctly.

 

> My apologies, I did not update the version and date in the source file which I uploaded (after I made the corrections).

Edited by RSABear
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...