This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

i2c read byte requires dummy read

Hello,

Currently I find that I need to read a register twice to get the correct data. If I run a single time, the byte read is the previous byte. For example:

rtcMonth = readMonth();
rtcMonth = readMonth();
rtcYear = readYear();

gives me an rtcMonth equal to 0x01 (january) but it also gives me 0x01 for the year. If I perform a second readYear operation, rtcYear will be 0x16 (2016). My i2c is set to not generate an autostop.

My code follows:

#define i2cDelay 1000

unsigned char i2cReadByte(const unsigned char slvAddr, const unsigned char regAddr){
	char rxByte;
	while (stopBusy());
	UCB0I2CSA = slvAddr;  // set slave addr
	UCB0CTLW0 |= UCTR + UCTXSTT;  // i2c start
	while(txBusy() | startBusy());
	UCB0TXBUF = regAddr;
	while(txBusy());
	UCB0CTLW0 &= ~UCTR ;
	UCB0CTLW0 |= UCTXSTT;
	while(rxBusy() | startBusy());
	UCB0CTLW0 |= UCTXSTP;  // i2c stop
	rxByte = UCB0RXBUF;
	while (stopBusy());
	_delay_cycles(i2cDelay);
	return rxByte;

char txBusy(void){
	return !(UCB0IFG & UCTXIFG0);
}

char startBusy(void){
	return (UCB0CTLW0 & UCTXSTT);
}

char stopBusy(void){
	return (UCB0CTLW0 & UCTXSTP);
}

char rxBusy(void){
	return !(UCB0IFG & UCRXIFG0);
}

**Attention** This is a public forum