hello all,
i’m beginner at atmega so i want to know how to use I2C in order to get data from sensor and post it to the uart.
please ! here is my main program:
`
int main(void)
{
unsigned char ret;
DDRG |=(1<<PG2); // use pins 2 on port G for output
// (active low LED's )
DDRD |=(1<<PD0);
DDRD |=(1<<PD1);
PORTD |=(1<<PD0);
PORTD |=(1<<PD1);
i2c_init(); // init I2C interface
USART_Init(MYUBRR);
char c,k='U' ;
int dec;
while(1)
{
for(c=‘a’;c<=‘z’;c++) USART_Transmit©;
// PORTG |=(1<<PG2);
/* write 0x75 to eeprom address 0x05 (Byte Write) */
ret = i2c_start(ShT21+I2C_WRITE); // set device address and write mode
if ( ret ) {
/* failed to issue start condition, possibly no device found */
i2c_stop();
PORTG &=~(1<<PG2); // desactivate all 8 LED to show error */
}
else {
PORTG |=(1<<PG2); // output byte on the LED’s
_delay_ms(1000);
/* issuing start condition ok, device accessible */
i2c_write(0xE6); // write address = E6 temperature
i2c_write(0xE3); // ret=0 -> Ok, ret=1 -> no ACK
i2c_stop(); // set stop conditon = release bus
/* write ok, read value back from eeprom address 0x05, wait until
the device is no longer busy from the previous write operation */
i2c_start_wait(ShT21+I2C_WRITE); // set device address and write mode
i2c_write(0xE6); // write address = E6
i2c_rep_start(ShT21+I2C_READ); // set device address and read mode
ret = i2c_readNak(); // read one byte
PORTG &=~(1<<PG2);
// _delay_ms(1000);
i2c_stop();
USART_Transmit(printf("%d",ret));
// dec=convToDec(ret);
// USART_Transmit(printf("%d",dec));
}
//twi_match_write_slave();
} }
`