Hi,
I am using I2C protocol (AtMega2560) to write certain registers of the camera module OV7670. Need to implement a repeated start condition for that purpose. I have written the code for that but not sure whether it will work practically or not (i.e. there is no compilation errors).
Actually I have learned the theory of the protocol but implementing it for the first time.
Thanks.
#define F_CPU 16000000UL #include <avr/io.h> #include <util/delay.h> /*I2C Functions Start*/ void twi_init() { TWBR = 0xC6; /*SCL would be of 10MHz with Pre-scale of 1*/ TWCR = (1 << TWEN); TWSR = 0x00; /*Pre-scale is set to 1*/ } void twiStart (void) { TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA); /*(TWI interrupt cleared) | (TWI enabled) | (TWI start condition)*/ while (!(TWCR & (1 << TWINT))); /*Wait for the start condition to be transmitted*/ TWCR |= (0 << TWSTA); /*Clear the start bit*/ } void twiStop (void) { TWCR |= (1 << TWSTO); /*Set the Stop bit*/ } void twiWrite (uint8_t deta) { TWDR = deta; /*Data to be sent*/ TWCR = (1 << TWINT) | (1 << TWEN); while (!(TWCR & (1 << TWINT))); } /*I2C Functions End*/ /*Sart of the Main Function*/ int main() { twi_init(); twiStart(); /*Set the start condition*/ twiWrite(0x42); /*Slave address*/ twiWrite(0x00); /*Address of the first memory location*/ twiWrite(0x02); /*Gain setting*/ _delay_us(500); twiStart(); /*Repeated Start*/ twiWrite(0x84); /*Blue setting*/ _delay_us(500); twiStart(); /*Repeated Start*/ twiWrite(0x84); /*Red setting*/ twiStop(); /*Set the Stop Condition*/ while (1); } /*End of Main Function*/