Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:56 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : an alternative to VAR SAVE

     Page 1 of 2    
Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 06:04am 12 Jan 2019
Copy link to clipboard 
Print this post

This started before the current VAR RESTORE problems mentioned elsewhere.

Lew247 asked me to help him with a small project.
He wants to know what time power failed (or program stopped for any reason).
He told me about some interesting Microchip offerings.

The 47L04/47C04/47L16/47C16 (47XXX) is a 4/16 Kbit SRAM with EEPROM backup. The device is organized
as 512 x 8 bits or 2,048 x 8 bits of memory, and utilizes the I2C serial interface.



The advantage over 'VAR SAVE' is
The SRAM won't wear out and the backup is fully automatic.
Once the control register is set, it saves the SRAM to EEPROM whenever power fails and restores when power returns.
It also retains data over program reload and even firmware updates.
It uses a ~6.8uF capacitor to give it time to act when power fails.
Cost is about $1

The disadvantage is, you need an extra chip and capacitors.
You may also be tying up the I2C buss a bit depending on how often you update the data.
I went with the 47L16 because it was available in DIP packages.

All the program does is read the data when the program first starts and the last saved data is there.
With a normal stop and restart of the program, the data stays in SRAM but with a power failure, it saves to EEPROM and restores again ready for retrieval.

  Quote   CONST EERAMcr=&h18' address of 47x04/47x16 control register with A0=0 and A1=0
CONST SRAMcb=&h50' address of 47x04/47x16 data register forSRAM with A0=0 and A1=0
'
' these are not I2C device addresses but are sent after the device address
CONST COMMANDreg=&h55 ' address of 47x04/47x16 command register
CONST STATUSreg=&h00 ' address of 47x04/47x16 status register
'
CONST S_store=&h33 ' these are the two command register commands
CONST S_recall=&hdd
CONST SRAMsize = 2048 ' size of 47x16, use 512 for 47x04
'
r = setup() ' we only have to do this once for each chip
z$ = readDATA$(10,8) ' 8 bytes starting at address 10 (any address will do)
PRINT "Last shutdown occurred at "+z$
DO
t$ =
TIME$
PRINT t$
r = writeDATA(
10,t$)
PAUSE 1000
LOOP
END

FUNCTION setup()
x = readstatus()
' get the current status register
x = x OR &b10 ' set bit 2 leaving the rest alone
PRINT BIN$(x,8)
r = writeCMD(STATUSreg,x)
' set autostore
r = readstatus()
PRINT BIN$(r,8)
END FUNCTION

FUNCTION writeDATA(addr,D$)
' write string D$ to EERAM starting at addr
LOCAL L
addr = addr
MOD SRAMsize ' keep address within chip capacity
d$ = CHR$(addr\256)+CHR$(addr MOD 256)+D$
L =
LEN(D$)
ON ERROR SKIP 1
I2C OPEN 400,1000
I2C WRITE SRAMcb,0,L,D$
I2C CLOSE
END FUNCTION

FUNCTION readDATA$(addr,L)
' read string from EERAM starting at addr, L bytes
LOCAL D$
addr = addr
MOD SRAMsize ' keep address within chip capacity
ON ERROR SKIP 1
I2C OPEN 400,1000
I2C WRITE SRAMcb,1,2,addr\256,addr MOD 256 'Reset memory pointer to addr
I2C READ SRAMcb,0,L,D$
readDATA$ = D$
END FUNCTION

FUNCTION writeCMD(reg,EERAMcmd)
'write command to the command or status register
ON ERROR SKIP 1
I2C OPEN 400,1000
I2C WRITE EERAMcr,0,2,reg,EERAMcmd
I2C CLOSE
END FUNCTION

FUNCTION readSTATUS()
'read status register
LOCAL status
ON ERROR SKIP 1
I2C OPEN 400,1000
'I2C WRITE EERAMcr,1,2,STATUSreg,0
I2C READ EERAMcr,0,1,status
I2C CLOSE
readSTATUS = status
END FUNCTION


If you force a save or retrieval, you have to allow time for the operation to complete. The time required depends on the chip used.

Output:
Last shutdown occured at 00:00:12
15:53:00
15:53:01
15:53:02
15:53:03
15:53:04
15:53:05
15:53:06
15:53:07
15:53:08
> RUN
Last shutdown occurred at 15:53:08
15:53:28
15:53:29
15:53:30
15:53:31
15:53:32
15:53:33
15:53:34
>
> DATE$="12/01/2019"
> TIME$="15:54:22"
> RUN
Last shutdown occurred at 15:53:34
15:54:26
15:54:27
15:54:28
15:54:29
15:54:30
>

The first save was before the clock was set.
With a battery backed RTC, you can record the downtime etc.

I used an arm-L4 for the test without battery backed RTC but any mite will do.
I chose to open and close the I2C on each call but, in real life, it might be easier to open once and leave it open.

Jim
Edited by TassyJim 2019-01-13
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:24am 12 Jan 2019
Copy link to clipboard 
Print this post

This is a very interesting chip and arrangement. Thanks very much for posting.

EDIT: This link shows the part, but also has a wee video showing how the chip works. Very clever. Edited by Grogster 2019-01-13
Smoke makes things work. When the smoke gets out, it stops!
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:26am 12 Jan 2019
Copy link to clipboard 
Print this post

Jim you're a star
Thanks
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:28am 12 Jan 2019
Copy link to clipboard 
Print this post

Thank you Lewis for bringing the chip to my attention.
It will be a handy one to have.

Jim
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 07:39am 12 Jan 2019
Copy link to clipboard 
Print this post

  TassyJim said  You may also be tying up the I2C buss a bit depending on how often you update the data.


Perhaps, but you could always use the I2C Cfunction to create a new dedicated I2C port just for the memory chip I suppose. That would free up the normal I2C channel(s).

I've just downloaded the datasheet for this device. Very interesting chip indeed.
Smoke makes things work. When the smoke gets out, it stops!
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 10:20am 12 Jan 2019
Copy link to clipboard 
Print this post

I would like to see these devices with much larger capacities, at least 16KBytes. They all seem to be stuck at 2K (like the UNIO chips too).

I use the winbond W25Qxx multi-megabyte devices with 4 & 8 MB capacities - my latest small controller board has provision for one on the copper which you can use or not depending on application.

For one project I emulated the address/data buses of a conventional CPU then used a CY7C1512 64k x 8 static ram. It was a faff with a couple of '574s to latch the address and quite a few pins but it was a joy to have simple byte access just like trad CPUs - I must say this started out PIC16F877 based (with PORTD as an 8-bit port) and got a "brain transplant" later on.

 
HankR
Senior Member

Joined: 02/01/2015
Location: United States
Posts: 209
Posted: 12:17pm 12 Jan 2019
Copy link to clipboard 
Print this post

  CaptainBoing said   I would like to see these devices with much larger capacities, at least 16KBytes. They all seem to be stuck at 2K (like the UNIO chips too).

According to the video, there is an 8K device in I2C, and up to 128K in SPI, but I gave up trying to find the part numbers and data sheets using Microchip's website search and a distributor's website search.
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 01:02pm 12 Jan 2019
Copy link to clipboard 
Print this post

Only the 4K and 16k I2C devices listed
and no SPI interfaced EERAM listed on microchips site at allEdited by djuqa 2019-01-13
VK4MU MicroController Units

 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 04:40pm 12 Jan 2019
Copy link to clipboard 
Print this post

This is a great little device, especially for the L4 to do some low power logging. Thanks for the efforts and info!!!
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 05:03pm 12 Jan 2019
Copy link to clipboard 
Print this post

  CaptainBoing said   I would like to see these devices with much larger capacities, at least 16KBytes. They all seem to be stuck at 2K (like the UNIO chips too).


Try THIS one Edited by lew247 2019-01-14
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 08:22pm 12 Jan 2019
Copy link to clipboard 
Print this post

One other point to remember is the time it takes to store and recall.
The 16k bit chip takes 25mS to store and 5mS to recall.
Storing shouldn't matter as it would happen after power fail but if you had autorun set, you should pause 5mS at the start of your program to give the chip time to get the SRAM back with it's saved data.

The 4k chip has shorter times.

You can also set a portion of the memory as protected so semi permanent settings are safe from overwrites.

Jim

VK7JH
MMedit
 
HankR
Senior Member

Joined: 02/01/2015
Location: United States
Posts: 209
Posted: 10:48pm 12 Jan 2019
Copy link to clipboard 
Print this post

  HankR said  
  CaptainBoing said   I would like to see these devices with much larger capacities, at least 16KBytes. They all seem to be stuck at 2K (like the UNIO chips too).

According to the video, there is an 8K device in I2C, and up to 128K in SPI, but I gave up trying to find the part numbers and data sheets using Microchip's website search and a distributor's website search.


Okay, should have labelled my figures as being in kbytes, as referenced by Andrew . The kbits and the kbytes numbers overlap so it can be a little confusing.

For the I2C listed by Microchip as really available (LARA) devices, 16kbits (2 kbytes) is biggest.

The 8 kbyte I2C device in video looks like vapor(hard)ware or vapor silicon (VS).

In SPI all devices mentioned in video, with the biggest being 128 kbytes, ALL look like they're VS. Very unfortunate.

This looks like a good thing to ask a Microchip rep or FAE, but I always dread the part of the conversation that addresses how many thousands of ICs your company will (or already is) be buying a month. That actually might not even come up these days.

There might be something about this on the MC forum, too, or a new inquiry made there about whether these much bigger devices might be available "real soon now." TM JP

who will be the first in the gang to identify JP?
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 11:09pm 12 Jan 2019
Copy link to clipboard 
Print this post

Jerry Pournelle.
Keep safe. Live long and prosper.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 04:37pm 13 Jan 2019
Copy link to clipboard 
Print this post

  lew247 said  

Try THIS one


Much better - 128KB is a very useful lump. Cheers
 
HankR
Senior Member

Joined: 02/01/2015
Location: United States
Posts: 209
Posted: 07:39pm 13 Jan 2019
Copy link to clipboard 
Print this post

  CaptainBoing said  
Much better - 128KB is a very useful lump. Cheers


You can do even better. 256 kbytes is available now in I2C.

Microchip/Atmel AT24CM02 family. Prices have been falling too.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 12:16am 14 Jan 2019
Copy link to clipboard 
Print this post

23LC1024 and AT24CM02 are just EEPROM's. They do not have the attractive autosave feature. What I like about the first chip as linked to in the OP, is that it will autosave the data in the SRAM for you when the power fails, and restore it again to the SRAM when the power comes back up. Because the device uses a charged cap to supply the juice to be able to write the SRAM to the EEPROM when the power fails, that limits the size of the memory if you need that cap to be small. C'est La Vie...
Smoke makes things work. When the smoke gets out, it stops!
 
HankR
Senior Member

Joined: 02/01/2015
Location: United States
Posts: 209
Posted: 02:53am 14 Jan 2019
Copy link to clipboard 
Print this post

There has been a distinct fork in the thread. One is for the special backup devices, the other is for just roomy EEPROMs.
 
HankR
Senior Member

Joined: 02/01/2015
Location: United States
Posts: 209
Posted: 03:21am 14 Jan 2019
Copy link to clipboard 
Print this post

  Turbo46 said   Jerry Pournelle.

Not even an hour elapsed and bingo, we have a winner.

Your prize is a much desired family sized package of ultra-premium, certified organic croc sausages all ready to cook up on the barbie this summer.

All the crocs are free range farmed and raised on a proprietary, fully vegetarian diet. As a result, the crocs growth is quite stunted, but the taste and tenderness of the resulting meat is truly amazing.

Jerry among many other things was a columnist for Byte magazine (starting in 1980).
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 06:28am 14 Jan 2019
Copy link to clipboard 
Print this post

Vale Jerry Pournelle. His column was the first one I read when I got my copy of Byte. I have read most of his SciFi books as well. He was not a fan of C as I remember.

Bill
Keep safe. Live long and prosper.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 05:27pm 25 Jan 2019
Copy link to clipboard 
Print this post

Jim
Can you or anyone else tell me how to do this

I'm counting from 0 to 43199 and storing the value in the sram
However its storing with trailing zeros
10000
20000
30000
.
.
.
219000
220000
How can I get it to store with leading zeros instead of trailing zeros?
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025