Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] EEPROM in RTC DS3231 problem

Status
Not open for further replies.

RobertoPasic

Newbie level 6
Joined
Dec 2, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Macedonia
Activity points
1,390
Hi there, I use eeprom in RTC DS3231 with Arduino uno, I need to save number of button press, every HIGH state on button need to increment counter and save number on eeprom,
I need counter value max 9999, in my case after 255 counter start from 0 again, can somebody help with advice?
thanks
my code

Code:
#include <DS3231.h>
DS3231  rtc(SDA, SCL);

#include "Wire.h"
#define EEPROM_I2C_ADDRESS 0x57

const int buttonPin1 = 7;                // pin7 button in

int buttonState1 = 0;                      // start value for button

int Val = 0;                                        // counter for the number of button presses
int readVal = 0;                              // read value from address 0
int sum = 0;                                    // sum readVal + new
int freshVal = 0;                            // print last sum

void setup() {
   Wire.begin();
   Serial.begin(19200);     
   rtc.begin();
   //The following lines can be uncommented to set the date and time
   //rtc.setTime(16, 31, 0);               // Set the time to 15:36:00 (24hr format)
   //rtc.setDate(06, 06, 2020);            // Set the date to 19 Noemvri 2019
   pinMode(buttonPin1, INPUT);             

   readVal = readEEPROM(0, EEPROM_I2C_ADDRESS);              //if you need to reset value on addres 0 comment this line
   //writeEEPROM(0, 0, EEPROM_I2C_ADDRESS);                           //uncomment this line to write 0 on addres 0
}
 
void loop() {
   buttonState1 = digitalRead(buttonPin1);
   delay(100);
  
    if (buttonState1 == HIGH) {                            // if button is High
    dothis();                                             
    } else {
    delay(50);
  }
 
}

void dothis() {
  Val++;
  sum = readVal + Val;
  writeEEPROM(0, sum, EEPROM_I2C_ADDRESS);
  
  freshVal = readEEPROM(0, EEPROM_I2C_ADDRESS);
  delay(100);
 
  Serial.println(freshVal);     //print freshVal
}

void writeEEPROM(int address, byte sum, int i2c_address){
  // Begin transmission to I2C EEPROM
  Wire.beginTransmission(i2c_address);
 
  // Send memory address as two 8-bit bytes
  Wire.write((int)(address >> 8));   // MSB
  Wire.write((int)(address & 0xFF)); // LSB
 
  // Send data to be stored
  Wire.write(sum);
 
  // End the transmission
  Wire.endTransmission();
 
  // Add 5ms delay for EEPROM
  delay(5);
}

byte readEEPROM(int address, int i2c_address){
  // Define byte for received data
  byte rcvData = 0x00;
 
  // Begin transmission to I2C EEPROM
  Wire.beginTransmission(i2c_address);
 
  // Send memory address as two 8-bit bytes
  Wire.write((int)(address >> 8));   // MSB
  Wire.write((int)(address & 0xFF)); // LSB
 
  // End the transmission
  Wire.endTransmission();
 
  // Request one byte of data at current memory address
  Wire.requestFrom(i2c_address, 1);
 
  // Read the data and assign to variable
  rcvData =  Wire.read();
 
  // Return the data as function output
  return rcvData;
}
 

There is no EEPROM in a DS3231 !
If you are using oneof the common modules that has a DS3231 and an AT24C32 on it, the command to access the EEPROM may be different. I use Makuna's library which uses "rtcEEPROM.SetMemory" and "rtcEEPROM.GetMemory" to access it.

Brian.
 
Hi,

I need to save number of button press, every HIGH state on button need to increment counter
Makes no sense to me.

It makes more sense to save the count of rising edges...

Klaus
 
Hi for all and thanks for quick replay,
sorry FvM because I share info that I use eeprom in DS3231,
I use RTC module based on DS3231 with 24C32 on board eeprom, sorry for my inaccuracy,
high-accuracy-ds3231-rtc-eeprom.jpg

so, EEPROM_I2C_ADDRESS is 0x57.
Thanks Betwixt for advice, I will try to use Makuna's library.
Thanks for advice from KlausST, exactly "button" is a signal from TLD600 vehicle loop detector (from inductive coil), I wait High signal from TLD600,
I will try to use highByte() and lowByte() functions to break the integer into two bytes for storage.
Thanks again,
best regards
 

Hi again,
I fix my problem,
working code:

Code:
#include "Wire.h"

const int buttonPin1 = 7;                                 // pin7 button in

int buttonState1 = 0;                                       // start value for button

int Val = 0;                                                         // counter for the number of button presses
int readVal = 0;                                               // read value from address 0
int sum = 0;                                                      // sum = readVal + new
int freshVal = 0;                                              // print last sum
int addr = 0;                                                     //EEPROM Address 0
byte a = 0;
byte b = 0;

void setup() {
   Wire.begin();
   Serial.begin(19200);     

   pinMode(buttonPin1, INPUT);             

   b = i2c_eeprom_read_byte(0x57, addr);                 // access the first address from the memory
   a = i2c_eeprom_read_byte(0x57, addr+1);               // access the first address from the memory
   readVal = ((b << 0) & 0xFF) + ((a << 8) & 0xFF00);
}
 
void loop() {
   buttonState1 = digitalRead(buttonPin1);
   delay(10);
    
    if (buttonState1 == HIGH) {                            // if button is High
    dothis();                                             
    } else {
    delay(50);
  }
 
}

void dothis() {
  Val++;
  sum = readVal + Val;
 
  i2c_eeprom_write_byte(0x57, addr, sum);                 // write to EEPROM
  delay(10); //add a small delay
    
  b = i2c_eeprom_read_byte(0x57, addr);                     // access the first address from the memory
  a = i2c_eeprom_read_byte(0x57, addr+1);                 // access the first address from the memory

  freshVal = ((b << 0) & 0xFF) + ((a << 8) & 0xFF00);

  Serial.println(freshVal);                                                       //print freshVal

  delay(1000);
}

void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, int data ) {
int rdata = data;

int rdata_low = lowByte(rdata);
int rdata_high = highByte(rdata);

Wire.beginTransmission(deviceaddress);

Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB

Wire.write(rdata_low);
Wire.endTransmission();

delay(10);

Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress+1 >> 8)); // MSB
Wire.write((int)(eeaddress+1 & 0xFF)); // LSB

Wire.write(rdata_high);

Wire.endTransmission();

}

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.read();
    return rdata;
}

thanks
regards
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top