GithubHelp home page GithubHelp logo

eecounter's Introduction

EECounter

EECounter is persistent 32-bit counter in EEPROM memory, optimized for increment-by-one on AVR Arduino boards (Arduino Uno/Nano/Mega/Leonardo).

EECounter spreads the write cycles evenly over your selected range of 16, 32, or more bytes of EEPROM. Write/erase cycles are minimized via direct register access to individual EEPROM bits. A counter using just 32 dedicated EEPROM bytes can be incremented over 24 million times while staying under 100,000 erase/write cycles.

Usage

#include "EECounter.h"

The constructor takes two parameters: start_address and length.

EECounter counter1(100, 32); // defines a counter occupying 32 bytes, EEPROM locations 100 thru 131
EECounter counter2(200, 456); // defines a counter occupying 456 bytes, EEPROM locations 200 thru 655

In this example, the only difference between these two counters is that counter2 spreads its writes over a much larger EEPROM area. It could sustain 400 million increments while keeping under 100,000 write cycles.

Calling begin() is optional, and resets corrupted or never-initialized counters to 0. Initialization is optional. A never-initialized counter might start its count at an arbitrary small non-zero integer.

Calling read() reads the counter directly from EEPROM. The count is 32 bits, unsigned, and rolls to zero when it overflows.

Calling increment() increments the counter in EEPROM and returns the new count.

Calling resetcount() re-initializes the counter with a new start count. This consumes a write/erase cycle on the EEPROM on part or all of the range.

Background

Each call to increment() typically erases or writes (not both) exactly one byte of EEPROM. It's probably atomic. The algorithm incorporates the erase step as a countable event so that an immediate write-after-erase is never necessary.

This class implements a circular journal over the assigned portion of the EEPROM. Bytes take turns being part of the "tallies" (which are flipped bit-by-bit or erased to count increments) or the "checkpoint" (the starting point from which the tallies are counted). Direct register access is used to gain access to writing partial bytes (individual bits) at a time, and to erase and write bytes in separate steps.

Tips

EEPROM integrity is vulnerable to overvoltage and undervoltage situations. Sometimes these are unanticipated. If your Arduino project drives relays, motors, or electromagnetic loads, these produce strong voltage surges when powered off (google "back EMF" for more information on this). Those surges will corrupt EEPROM if they reach the Arduino's processor.

Avoid using EEPROM location 0. On Arduino, it is most likely to get corrupted during power offs. Consider it a burner location.

eecounter's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.