/*
* Mega328_I2C_EEPROM.c
*
* Created: 10/30/2016 10:44:51 PM
* Author: jgmDESIGNS-Laptop
*/
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "i2cmaster.h"
#define Dev_24LC08B 0xA0 // device address of EEPROM 24LC88, see datasheet <---jgmdesigns changed device name and address for device - See datasheet
int main(void){
unsigned char ret;
DDRB = 0x0f; // use LOWER pins on port B for output <---jgmdesigns changed PINS USED
PORTB = 0x0f; // (active HIGH LED's ) <---jgmdesigns changed to ACTIVE HIGH
i2c_init(); // init I2C interface
/* write 0x75 to eeprom address 0x05 (Byte Write) */
ret = i2c_start(Dev_24LC08B+I2C_WRITE); // set device address and write mode
if ( ret ){
/* failed to issue start condition, possibly no device found */
i2c_stop();
PORTB=0x00; // activate all 8 LED to show error */
}else{
/* issuing start condition ok, device accessible */
i2c_write(0x05); // write address = 5
i2c_write(0x75); // 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(Dev_24LC08B+I2C_WRITE); // set device address and write mode
i2c_write(0x05); // write address = 5
i2c_rep_start(Dev_24LC08B+I2C_READ); // set device address and read mode
ret = i2c_readNak(); // read one byte
i2c_stop();
PORTB = ~ret; // output byte on the LED's
/* write 0x70,0x71,072,073 to eeprom address 0x00..0x03 (Page Write),
wait until the device is no longer busy from the previous write operation */
i2c_start_wait(Dev_24LC08B+I2C_WRITE); // set device address and write mode
i2c_write(0x00); // write start address = 0
i2c_write(0x01); // write data to address 0 <---jgmdesigns changed value for demo
i2c_write(0x02); // " " " " 1 <---jgmdesigns changed value for demo
i2c_write(0x04); // " " " " 2 <---jgmdesigns changed value for demo
i2c_write(0x08); // " " " " 3 <---jgmdesigns changed value for demo
i2c_stop(); // set stop conditon = release bus
/* write ok, read value back from eeprom address 0..3 (Sequencial Read),
wait until the device is no longer busy from the previous write operation */
i2c_start_wait(Dev_24LC08B+I2C_WRITE);// set device address and write mode
i2c_write(0x00); // write address = 0
i2c_rep_start(Dev_24LC08B+I2C_READ); // set device address and read mode
ret = i2c_readAck(); // read one byte form address 0
PORTB = ret; //<---jgmdesigns added output for display on LED's
_delay_ms(750); //<---jgmdesigns changed - added delay for display on PORTB
ret = i2c_readAck(); // " " " " " 1
PORTB = ret; //<---jgmdesigns added output for display on LED's
_delay_ms(750); //<---jgmdesigns changed - added delay for display on PORTB
ret = i2c_readAck(); // " " " " " 2
PORTB = ret; //<---jgmdesigns added output for display on LED's
_delay_ms(750); //<---jgmdesigns changed - added delay for display on PORTB
ret = i2c_readNak(); // " " " " " 3
PORTB = ret; //<---jgmdesigns added output for display on LED's
_delay_ms(750); //<---jgmdesigns changed - added delay for display on PORTB
i2c_stop(); // set stop condition = release bus
//<---jgmdesigns changed - removed last PORT B write
}
for(;;);
}