Jump to content
Sign in to follow this  
maximlee

MCU sends extra bytes on I2C causing problems when writing to EEPROM

Recommended Posts

I am trying (for quite long time) to write to EEPROM 64 bytes of data in 4 chunks (as EEPROM's page size is 16 bytes). Unfortunately, the MCU sends few extra bytes every time it transmits.

For example, if I send 16 bytes, it transmits 22, with 6 last bytes being of unknown origin to me. For example, I send 15 text bytes within for(...) cycle

xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx

but see on the scope

xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx BF F6 FD D7 3A C0

I tried another approach: send 15 bytes + 00. My program stops at 00, then I try to replace 00 with whatever has to be there using yet another write (1 byte only). However, I see 3 extra bytes:

xx 2F 9A C9 00

I tried to write this 4 times at consequent addresses:

50 00 xx 00
50 01 xx 00
50 02 xx 00
50 03 xx 00

but every times I have the same extra bytes, like

50 01 xx 2F 9A C9 00

which result in (when reading 4 bytes)

50 00 xx xx xx xx 2F

(5 bytes are read).

Due to size of my project vs ROM size and also the need to learn C, I create all functions by myself, without using standard libraries. Below is I2C_write function, other ones regulating initialization, address selection or reading do not seem to be a problem.

DCO is running at 16 MHz, with no division.

void I2Ctext_write(unsigned int MemoryAddress, char * text, int length)
{
short int i = 0;

UCB0I2CSA = addressSelect(MemoryAddress).three_bits_offset;
while (UCB0STAT & UCBBUSY); //wait for USCI B0 bus to be inactive
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while (!(IFG2&UCB0TXIFG));
UCB0TXBUF = addressSelect(MemoryAddress).seven_bits_address;

if(length)
{
	for (i = 0; i < length; i++)
	{
		while(*text)
		{
			while (!(IFG2&UCB0TXIFG));
			if(!(*text))
			{
				break;
			}
			UCB0TXBUF = *text;
			*text++;
		}
	}
}
else
{
	while(*text)
	{
		while (!(IFG2&UCB0TXIFG));
		if(!(*text))
		{
			break;
		}
		UCB0TXBUF = *text;
		*text++;
	}
}

while (!(IFG2&UCB0TXIFG));
UCB0TXBUF = 0;

while (!(IFG2&UCB0TXIFG)); // data transmission has begun and the UCTXSTP bit may be set.
UCB0CTL1 |= UCTXSTP; // Stop condition enabled;
while (UCB0CTL1 & UCTXSTP);
}

 

 

Why do I see those extra bytes and what to do to get rid of them?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

Sign in to follow this  

×
×
  • Create New...