I'm having some trouble using the I2C EEPROM 24LC256 chip. I found some tutorials and some test code and it seems like it is wired correctly and responding with those tests. But now when I use my code (which is based off of when I was just using internal EEPROM), it gets jibberish when I go into the 'review' mode to look at previous readings (the final product is more or less a glucose reader that can store readings along with a time stamp and then eventually transfer over Xbee). Also on the I2C line is a Dallas Semiconductor RTC. The RTC has an address of 0x68 and the EEPROM has an address of 0x50 since I have all the analog pins wired to ground. Any ideas of what is going wrong in my code? Am I separating my int's into bytes correctly?
Thanks!
#include <LiquidCrystal.h>
#include <Wire.h>
#include <EEPROM.h>
#include <XBee.h>
#define DS1307_I2C_ADDRESS 0x68
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 10, 9);
//41
//Global Variables
volatile int curDay = 0;
volatile int curMon = 0;
volatile int curYear = 0;
volatile int curHour = 0;
volatile int curMin = 0;
volatile int curSec = 0;
volatile int curWkday = 0;
volatile float curReading = 0;
volatile float readOne = 0;
volatile float readTwo = 0;
volatile float readThree = 0;
volatile float readFour = 0;
volatile float readFive = 0;
volatile float readSix = 0;
volatile float curVoltage = 0;
volatile int curActVal = 0;
volatile int curState = 1;
volatile int okState = 0;
volatile int dispUpdate = 0;
volatile int eeCount = 0;
volatile int toGet = 1;
uint8_t payload[18] = {"12;34;56;78;9012;111"};
//States
#define GETREADING 1
#define REVIEW 2
//#define CLOCK 3
#define TRANSFER 3
#define CLEAR 4
#define TOTALSTATES 4
#define EEPROMADDR 0x50
//Pin assignments
volatile int advButtonPin = 2;
volatile int okButtonPin = 3;
volatile int sensorPin = 3; //this is analog
//volatile int triggerPin = 3; //this is analog
XBee xbee = XBee();
void setup(){
//clear EEPROM
pinMode(advButtonPin, INPUT);
pinMode(okButtonPin, INPUT);
pinMode(A3, INPUT);
analogReference(DEFAULT);
attachInterrupt(1, advanceAction, FALLING);
attachInterrupt(0, okAction, FALLING);
Wire.begin();
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Glucose Reader");
delay(2000);
lcd.clear();
lcd.print("Select a");
lcd.setCursor(0,1);
lcd.print("Function");
eeCount = EEPROMReadInt(0);
}
void loop(){
if(dispUpdate){
lcd.clear();
if(curState == GETREADING){
lcd.print("Take Reading.");
lcd.setCursor(0,1);
lcd.print("Press OK Button");
}else if(curState == REVIEW){
lcd.print("Review Readings.");
lcd.setCursor(0,1);
lcd.print("Press OK Button");
//}else if(curState == CLOCK){
//getDateDs1307();
//displayReadings();
}else if(curState == TRANSFER){
lcd.print("Transfer Readings.");
lcd.setCursor(0,1);
lcd.print("Press OK Button");
}else if(curState == CLEAR){
lcd.print("Clear Memory");
lcd.setCursor(0,1);
lcd.print("Press OK Button");
}
dispUpdate = 0;
}
if(okState == 1 && curState == REVIEW){
reviewReadings();
okState = 0;
}
if(okState == 1 && curState == GETREADING){
doReading();
okState = 0;
}
if(okState == 1 && curState == TRANSFER){
transmitData();
okState = 0;
}
if(okState == 1 && curState == CLEAR){
clearReadings();
okState = 0;
}
}
void advanceAction(){
curState++;
if(curState > TOTALSTATES){
curState = 1;
}
dispUpdate = 1;
}
void okAction(){
okState = 1;
}
void doReading(){
lcd.clear();
lcd.print("Waiting...");
lcd.setCursor(0,1);
//lcd.print(analogRead(A3), DEC);
//delay(1000);
while(analogRead(A3) < 20);
lcd.clear();
lcd.print("Taking Reading...");
delay(4999);
readOne = analogRead(A3);
readTwo = analogRead(A3);
readThree = analogRead(A3);
readFour = analogRead(A3);
readFive = analogRead(A3);
readSix = analogRead(A3);
curReading = (readOne+readTwo+readThree+readFour+readFive+readSix)/6;
curVoltage = (curReading/1025)*5;
curActVal = (int)((curVoltage/0.009)-32.75);
lcd.clear();
//lcd.print("Reading: ");
//lcd.print(curReading, DEC);
//lcd.setCursor(0,1);
lcd.print("Glucose: ");
lcd.print(curActVal, DEC);
getDateDs1307();
writeEeprom();
}
void clearReadings(){
for(int i=0; i<2048; i++){
writeEEPROM(i,0);
}
eeCount = 0;
lcd.clear();
lcd.print("Readings Cleared!");
}
void transmitData(){
lcd.clear();
lcd.print("Transferring");
lcd.setCursor(0,1);
lcd.print("Readings...");
xbee.begin(9600);
for(int i=1; i<=eeCount; i++){
toGet = i;
readEeprom();
payload[0] = highByte(curActVal);
payload[1] = lowByte(curActVal);
payload[2] = ';';
payload[3] = highByte(curMon);
payload[4] = lowByte(curMon);
payload[5] = ';';
payload[6] = highByte(curDay);
payload[7] = lowByte(curDay);
payload[8] = ';';
payload[9] = highByte(curYear);
payload[10] = lowByte(curYear);
payload[11] = ';';
payload[12] = highByte(curHour);
payload[13] = lowByte(curHour);
payload[14] = ';';
payload[15] = highByte(curMin);
payload[16] = lowByte(curMin);
payload[17] = '
;
Tx16Request zbTx = Tx16Request(0x1234, payload, sizeof(payload));
xbee.send(zbTx);
}
delay(2000);
lcd.clear();
lcd.print("Data Transmitted");
}
void writeEeprom(){
eeCount++;
int curSpot = ((eeCount-1)*12)+2;
EEPROMWriteInt(curSpot, curMon);
curSpot = curSpot+2;
EEPROMWriteInt(curSpot, curDay);
curSpot = curSpot+2;
EEPROMWriteInt(curSpot, curYear);
curSpot = curSpot+2;
EEPROMWriteInt(curSpot, curHour);
curSpot = curSpot+2;
EEPROMWriteInt(curSpot, curMin);
curSpot = curSpot+2;
EEPROMWriteInt(curSpot, curActVal);
EEPROMWriteInt(0, eeCount);
}
void reviewReadings(){
if(toGet<=eeCount){
readEeprom();
displayReadings();
toGet++;
}else{
lcd.clear();
lcd.print("No readings!");
toGet = 1;
}
}
void readEeprom(){
int curSpot = ((toGet-1)*12)+2;
curMon = EEPROMReadInt(curSpot);
curSpot = curSpot+2;
curDay = EEPROMReadInt(curSpot);
curSpot = curSpot+2;
curYear = EEPROMReadInt(curSpot);
curSpot = curSpot+2;
curHour = EEPROMReadInt(curSpot);
curSpot = curSpot+2;
curMin = EEPROMReadInt(curSpot);
curSpot = curSpot+2;
curActVal = EEPROMReadInt(curSpot);
}
//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, int p_value)
{
byte theLowByte = ((p_value >> 0) & 0xFF);
byte theHighByte = ((p_value >> 8) & 0xFF);
writeEEPROM(p_address, theLowByte);
writeEEPROM(p_address + 1, theHighByte);
}
//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
int EEPROMReadInt(int p_address)
{
byte theLowByte = readEEPROM(p_address);
byte theHighByte = readEEPROM(p_address + 1);
return ((theLowByte << 0) & 0xFF) + ((theHighByte << 8) & 0xFF00);
}
void writeEEPROM(int eeaddress, byte data ) {
//Wire.begin(EEPROMADDR);
Wire.beginTransmission(EEPROMADDR);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.send(data);
Wire.endTransmission();
}
byte readEEPROM(unsigned int eeaddress ) {
///Wire.begin(EEPROMADDR);
byte rdata = 0xFF;
Wire.beginTransmission(EEPROMADDR);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROMADDR,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
void displayReadings(){
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(curHour, DEC);
lcd.print(":");
if (curMin<10){
lcd.print("0");
}
lcd.print(curMin, DEC);
lcd.print(" R:");
lcd.print(curActVal, DEC);
lcd.setCursor(0,1);
lcd.print(curMon, DEC);
lcd.print("/");
lcd.print(curDay, DEC);
lcd.print("/20");
lcd.print(curYear, DEC);
}
int decToBcd(int val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
int bcdToDec(int val)
{
return ( (val/16*10) + (val%16) );
}
// Gets the date and time from the ds1307
void getDateDs1307(){
// Reset the register pointer
//Wire.begin(DS1307_I2C_ADDRESS);
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
curSec = bcdToDec(Wire.receive() & 0x7f);
curMin = bcdToDec(Wire.receive());
curHour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
curWkday = bcdToDec(Wire.receive());
curDay = bcdToDec(Wire.receive());
curMon = bcdToDec(Wire.receive());
curYear = bcdToDec(Wire.receive());
}