0

I get some stupid error's if I want to try initialise the connection from the TWI master to the bus. The start condition will be send but the processor waits in the infinity loop bevor starting to send the slave address to the bus.

I also have analysed the signals on the bus and one result is that the clock is running but there will be no data send on the bus.

The processor wait's in the line with the marked arrow.

We use the following code to start the and initialise the bus ...

    void i2c_master_init() {
        TWBR = (uint8_t)TWBR_val;
    }

    void i2c_master_stop() {
        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
    }

    uint8_t i2c_master_start(uint8_t address) {

        TWCR = 0;
        TWCR |= (1<<TWSTA);
        TWCR |= (1<<TWEN);
        TWCR |= (1<<TWINT);
        while( !(TWCR & (1<<TWINT)) );   <--

        [...]

    }

Currently I don't know, what's going wrong with the code. Or am I doing something else wrong. Can anyone help me?

Thank you in anticipation.

FoxPixel
  • 101
  • 5

1 Answers1

1

My best guess without hardware on my bench is that you should set all flags to TWI control registry at once by TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN). Meanwhile you set them one by one in 3 separate operations (multiple clock cycles), while datasheet implicitly says flags must be set together, see also datasheet examples.

andy
  • 757
  • 5
  • 13
  • Thanks for helping - but we currently found another problem. The client (slave) runs with 16 MHz and the Host (master) runs with 8 MHz. This is also a problem. So the two systems cannot communicate. – FoxPixel Dec 01 '17 at 13:15
  • However, we have only found the problem now. – FoxPixel Dec 01 '17 at 13:15
  • You are talking about CPU clocks while TWI can setup any bus speed (up to 400kHz) configuring `TWBR` and `TWPS` registers as shown in datasheet chapter 24.5.2. Take a look on [library implementation](https://github.com/maximecb/AVR-Alarm-Clock/blob/master/i2cmaster.c) how it is calculated. – andy Dec 01 '17 at 16:24