diff --git a/i2c.c b/i2c.c index 01dc24a..819e9ad 100644 --- a/i2c.c +++ b/i2c.c @@ -108,9 +108,23 @@ volatile uint8_t sendbuf[BUFSIZE]; \param address Address the system should listen to in slave mode, unused when configured for master mode. In master mode, receiver address is given to the send command. + + This also sets the I2C address. In slave mode it's the address we listen on. + + In master mode it's the communication target address. As master one can talk + to different devices. Call again i2c_init() for changing the target address, + then. Doing so won't interrupt ongoing transmissions and overhead is small. */ void i2c_init(uint8_t address) { + // In case this is a re-inititalisation, + // don't interrupt an ongoing transmission. + while (i2c_state & I2C_MODE_BUSY) { + delay_us(10); + } + + i2c_address = address; + #ifdef I2C_MASTER_MODE #ifdef I2C_ENABLE_PULLUPS SET_INPUT(SCL); @@ -135,7 +149,6 @@ void i2c_init(uint8_t address) { #endif /* I2C_MASTER_MODE */ #ifdef I2C_SLAVE_MODE - i2c_address = address; TWAR = i2c_address; // We listen to broadcasts if lowest bit is set. TWCR = (0<> 4) & 0x0F), 1); + i2c_write(0x00 | (32 & 0x0F), 0); + i2c_write(0x10 | ((32 >> 4) & 0x0F), 1); // Render text to bitmap to display. - i2c_write(DISPLAY_I2C_ADDRESS, 0x40, 0); + i2c_write(0x40, 0); while (*message) { uint8_t index = (uint8_t)*message - 0x20; // Send the character bitmap. for (i = 0; i < pgm_read_byte(&font_8x4[index].columns); i++) { - i2c_write(DISPLAY_I2C_ADDRESS, - pgm_read_byte(&font_8x4[index].data[i]), 0); + i2c_write(pgm_read_byte(&font_8x4[index].data[i]), 0); } // Send space between characters. for (i = 0; i < FONT_SYMBOLS_SPACE; i++) { - i2c_write(DISPLAY_I2C_ADDRESS, 0x00, 0); + i2c_write(0x00, 0); } message++; } // Send another space for transmission end. - i2c_write(DISPLAY_I2C_ADDRESS, 0x00, 1); + i2c_write(0x00, 1); } #endif /* I2C_TEST */ diff --git a/mendel.c b/mendel.c index 8f549af..a6280f8 100644 --- a/mendel.c +++ b/mendel.c @@ -96,7 +96,7 @@ void init(void) { #endif #ifdef I2C - i2c_init(0); + i2c_init(DISPLAY_I2C_ADDRESS); #endif // set up timers