PDA

View Full Version : Counter to Save to eeprom, then go back in 24 hour's and read eeprom.



Jeff2
05-20-2010, 09:24 AM
What I am looking to do is. In the program I sent in this post.
Is there A way to count the # of times pin[23] is pushed, save the count each time to the eeprom, and then go back into the eeprom and see how many
times is was pushed. Like in 24 hour's?



















con

_clkmode = xtal1 + pll16x
_xinfreq = 0_500_000


pub Kid




repeat


if ina [23] == 1


right



pub right
dira[0..19] := %11111111111111111111
outa[0..19] := %00000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %00000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %10000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11100000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11110000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111100000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111110000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111100000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111110000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111100000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111110000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111100000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111110000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111100
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111110
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111111
REPEAT WHILE ina[23] == 1
dira[0..19] := %11111111111111111111
outa[0..19] := %11111111111111111111
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %00000000000000000000

Ravenkallen
05-20-2010, 09:38 AM
If you have a 64KB EEPROM, that also serves as the boot eeprom, you can just write to the upper 32KB's of it. You could just make a variable and increment it every time the button was switched and after a while write that to the eeprom. Or were you talking about writing every single increment to different address's in the eeprom?

Jeff2
05-23-2010, 04:23 AM
I just am using the 32 kB EEPROM, DIP-8. What is the best way to write the program so it will increment it every time the button is pushed?

Ravenkallen
05-23-2010, 05:02 AM
So you could hook it up to the same pins that the Program eeprom is on, but you would have to change the i2c address of the other one. Do you want to save every increment to the eeprom in a sequential manner or do you want to keep counting and then after a preset time, dump the contents to a single loaction in memory? I need a little more info.

Jeff2
05-23-2010, 08:17 AM
I want to keep counting and then after a preset time, dump the contents to a single loaction in memory. I don't know much about how the propeller chip saves to the eeprom, I am just looking for help because I didn't find much about it in the Propeller Education Kit Labs. So I thought if I would ask, someone out there will know. So what I want to know is how to save the count and to go into the chip and read the count to see
how many times it was pushed.

Dave Hein
05-23-2010, 09:04 AM
Use the Basic_I2C_Driver object in the OBEX. You can use a location at the end of the boot EEPROM as long as you don't reprogram the EEPROM using the F11 key. I would suggest using the last location in the EEPROM.

You read the last long using i2c.ReadLong(28, $a0, $7ffc) and you can write it using i2c.WriteLong(28, $a0, $7ffc, count). You should follow the write by a call to WriteWait until it returns a value of zero. You could use the loop repeat while i2c.WriteWait(28, $a0, $7ffc).

Dave


Post Edited (Dave Hein) : 5/23/2010 3:02:05 AM GMT

JonnyMac
05-23-2010, 10:46 AM
As Dave points out, I2C is very easy when you know what you're doing (that's easy, too) -- just make sure you use the upper end of the EEPROM for storage so you don't corrupt your program. I tend to use $7F00 as my storage area; it's up very high in the map and not likely to bang into anything (you can confirm with F8).

On another point you're going to need an accurate way to keep track of 24 hours -- not easy to do when your program is busy looking for button pushes. That said, you can launch a software RTC into its own coq quite easily; the attached demo shows an easy way to incorporate a software RTC in your programs.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA

Jeff2
05-23-2010, 11:13 AM
So program would like something like this=









con

_clkmode = xtal1 + pll16x
_xinfreq = 0_500_000


pub Kid




repeat


if ina [23] == 1


right



pub right
dira[0..19] := %11111111111111111111
outa[0..19] := %00000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %00000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %10000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11000000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11100000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11110000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111000000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111100000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111110000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111000000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111100000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111110000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111000000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111100000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111110000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111000000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111100000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111110000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111000
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111100
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111110
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %11111111111111111111
REPEAT WHILE ina[23] == 1
dira[0..19] := %11111111111111111111
outa[0..19] := %11111111111111111111
waitcnt(clkfreq/4 + cnt)
outa[0..19] := %00000000000000000000














con

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
' _xinfreq = 6_250_000

CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
MS_001 = CLK_FREQ / 1_000


con

#0, REG_MS, REG_SC, REG_MN, REG_HR, REG_DY, REG_RST ' clock registers


con

#1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR, #16, CLS ' PST formmatting control


obj

term : "fullduplexserial"


var

long rtc[6] ' rtc registers
long rtcstack[16] ' stack for rtc interpreter


pub main | oldsecs

cognew(softrtc, @rtcstack) ' start "background" rtc

term.start(31, 30, %0000, 115_200) ' start terminal
pause(1)
term.tx(CLS)

setclock(12, 0, 0) ' just like your vcr

repeat
term.tx(HOME)
term.str(string("The time is... "))
showclock
oldsecs := rtc[REG_SC]
repeat until (rtc[REG_SC] <> oldsecs) ' update on new second


pub showclock

dec2(rtc[REG_HR])
term.tx(":")
dec2(rtc[REG_MN])
term.tx(":")
dec2(rtc[REG_SC])


pub dec2(value)

if (value < 10)
term.tx("0")
term.dec(value)


pub pause(ms) | t

t := cnt - 1088 ' sync with system counter
repeat ms
waitcnt(t += MS_001)


pri reset

'' Resets all rtc/timer registers to 0

rtc[REG_RST]~~ ' -1 as flag to reset registers


pri setclock(h, m, s)

'' Sets clock registers

reset ' prevent odd rollovers
pause(2) ' ensure reset done
rtc[REG_HR] := h ' update time registers
rtc[REG_MN] := m
rtc[REG_SC] := s


pri softrtc | t0

'' Software RTC
'' -- runs in standard "clock" mode
'' -- launch with cognew

t0 := cnt ' sync to system counter

repeat
if (rtc[REG_RST] == -1) ' reset flag?
longfill(@rtc, 0, 6) ' yes, clear everything
waitcnt(t0 += MS_001) ' wait 1ms
if (++rtc[REG_MS] == 1_000) ' one second?
rtc[REG_MS]~ ' clear ms
if (++rtc[REG_SC] == 60) ' inc secs, check for 60
rtc[REG_SC]~ ' clear secs
if (++rtc[REG_MN] == 60) ' inc mins, check for 60
rtc[REG_MN]~ ' clear mins
if (rtc[REG_HR] == 24) ' in hrs, check for 24
rtc[REG_HR]~~ ' clear hours
if (rtc[REG_DY] < posx) ' days below max?
++rtc[REG_DY] ' increment
else ' else
rtc[REG_DY]~ ' clear


dat

JonnyMac
05-23-2010, 10:34 PM
No, not even close. You cannot simply bash listings together and expect them to work. In your original post you wanted to count the number of presses in 24 hours; my listing showed you how to create a clock. What you could have done is replaced my main loop that

a) counted presses (these should be debounced/qualified
b) when a new press is detected, save it to EE (I2C not shown, but there's a lot of code by me and others in ObEx)
c) watched the clock -- when the time rolled over to 00:00:00 you hit 24 hours and you can display our total and do what you will

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA