/* * I2C.c * * Created: 11.04.2019 08:39:07 * Author: KTM */ #define F_CPU 16000000L #include #include #define EEDEVADR 0b10100000 #define ERROR 1 #define SUCCESS (!ERROR) #define D7 7 #define D6 6 #define D5 5 #define D4 4 #define E 3 #define RS 2 #define RW 1 #define SS 2 #define MOSI 3 #define MISO 4 #define SCK 5 void lcd_4bity(unsigned char fourbits) { PORTD |= (1<>4); PORTD &= ~(1<>7)); if (TWIGetStatus() != 24) return ERROR; //send the rest of address TWIWrite((uint8_t)(u16addr)); if (TWIGetStatus() != 40) return ERROR; //write byte to eeprom TWIWrite(u8data); if (TWIGetStatus() != 40) return ERROR; TWIStop(); return SUCCESS; } uint8_t EEReadByte(uint16_t u16addr) { //uint8_t databyte; uint8_t sdata; TWIStart(); if (TWIGetStatus() != 8) return ERROR; //select device and send A2 A1 A0 address bits TWIWrite((EEDEVADR)|((uint8_t)((u16addr & 0x0700)>>7))); if (TWIGetStatus() != 36) return ERROR; //send the rest of address TWIWrite((uint8_t)(u16addr)); if (TWIGetStatus() != 40) return ERROR; //send start TWIStart(); if (TWIGetStatus() != 16) return ERROR; //select device and send read bit TWIWrite((EEDEVADR)|((uint8_t)((u16addr & 0x0700)>>7))|1); if (TWIGetStatus() != 64) return ERROR; sdata = TWIReadNACK(); if (TWIGetStatus() != 88) return ERROR; TWIStop(); return sdata; } int main(void) { TWIInit(); uint8_t ddata = 0; uint8_t ew; setup(); ew= EEWriteByte(5, 'b'); if(ew==SUCCESS) { lcd_bajt('S'); } else lcd_bajt('E'); _delay_ms(10); ddata = EEReadByte(5); if(ew==ERROR) { lcd_bajt('E'); } else lcd_bajt('S'); lcd_bajt(ddata); while(1) loop(); return 0; }