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 : Counting Problem

Author Message
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 11:37pm 29 Jan 2019
Copy link to clipboard 
Print this post

I am using a counting input and saving the variable using VAR SAVE in case of a power loss. My problem is the counter starts from zero, how do I get the counter to start from the last known value.

VAR RESTORE
Setpin 15,CIN
Do
x = Pin(15)
Var Save x
Loop


So I RESTORE x but in the line 'x=Pin(15)' x returns to zero because the counter resets after the power cut.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

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

  Quote   VAR RESTORE
backup = x
' retrieve the saved count
SETPIN 15,CIN
DO
x =
PIN(15) + backup
VAR SAVE x
LOOP


Jim
VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 12:28am 30 Jan 2019
Copy link to clipboard 
Print this post

@ Tassie Jim

I tried that but @#%$!#@$ oops I think I did it wrong.Edited by palcal 2019-01-31
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 12:41am 30 Jan 2019
Copy link to clipboard 
Print this post

Thanks Jim it's working OK.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2944
Posted: 01:26am 30 Jan 2019
Copy link to clipboard 
Print this post

Hey - Won't the above code wear out the Flash memory very quickly?
My understanding is that there is a finite number of 'write' cycles, and the above Do/Loop will hit this limit in no time. Once reached, the returned data is not guaranteed.

I may be wrong, so maybe someone else can confirm . . .Edited by WhiteWizzard 2019-01-31
 
TassyJim

Guru

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

I think it only writes if the variable value has changed so it depends on how fast it's counting.

If it's my rain gauge, you wouldn't have a problem at the moment.

Jim

VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 04:04am 30 Jan 2019
Copy link to clipboard 
Print this post

Well it is my rain gauge and WW brings up a good point. We had 150mm yesterday and so far today(5hrs) we have had another 75mm. I may have to find another way of doing it.

edit. My Do/Loop is not so quick at the moment it is set at 10 minutes.Edited by palcal 2019-01-31
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 04:35am 30 Jan 2019
Copy link to clipboard 
Print this post

Looks like I may be able to use RTC SETREG and RTC GETREG. But according to the manual the format is 'RTC SETREG reg,value' and I don't know if I can use a variable for value I will experiment. Watch this space because I'm sure I will need help.

Edit....Well I tried and like I thought had no idea the memory starts at address 20 decimal

Edit again.... Just found something from good old dependable Grogster (well maybe not so old) in my post about HC-12's. Will try his code when I can get back out to my workshop, it's a bit wet, we have had 450mm. of rain since Sunday.Edited by palcal 2019-01-31
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 06:40am 30 Jan 2019
Copy link to clipboard 
Print this post

OK Tried Grogs code
SUB SAVETORTC(DTA$)
Local X 'A local X variable, perhaps totally seperate from a global X
For X=1 to 3 'Step through the three-byte date data, and save it to the RTC RAM
RTC SETREG 50+X,ASC(MID$(DTA$(X,1)) 'Put data into RTC register
Next
END SUB

and after adding a missing closing bracket I get an ARGUMENT COUNT error on line 4 and can't see why.
Edited by palcal 2019-01-31
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

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

  Quote  RTC SETREG 50+X,ASC(MID$(DTA$,X,1))


You had a bracket after DTA$ instead of a comma.

450mm seems a bit greedy.
We have fires everywhere and could really do with some if you can spare it.

Jim
VK7JH
MMedit
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2944
Posted: 07:09am 30 Jan 2019
Copy link to clipboard 
Print this post

@palcal

I recently needed to store three bytes (values between 0 and 255) and was using VAR SAVE and VAR RESTORE along with OPTION AUTORUN ON. This was on v5.05.01 and I ran into issues as mentioned in another recent theead (I.e. the values were not always restored correctly on power-up) Removing AUTORUN and it worked every time.
So I ended up using three RTC registers (those for Alarm2) and everything worked much better, but there was still the occasional load error on startup (as in the variables were left empty as opposed to being loaded with the stored values).
In the mix, I am also using an OLED driver which loads on start-up BUT to date I have not found a repeatable sequence of events to give Geoff and Peter anything to work with.
Anyway, the RTC registers method will work but you still want to minimise write cycles wherever possible.

Will be keen to hear how you get on. IF you have weird issues, do post your OPTION LIST, and mm.ver

WW

 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 08:40am 30 Jan 2019
Copy link to clipboard 
Print this post

  Quote   My problem is the counter starts from zero, how do I get the counter to start from the last known value.


On some of my versions (can't remember which) I've implemented

PIN(COUNTpin)=initvalue

I think this is generally useful and perhaps something Geoff could adopt.
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 09:02am 30 Jan 2019
Copy link to clipboard 
Print this post

@ Matherp... I agree.

Ok The save now works or at least it runs but a problem retrieving the data
Grogs code
FUNCTION READRTC(DTA$)
Local D$,X,BYTE
For X=1 to 3
RTC GETREG 50+X,BYTE
DTA$=DTA$+CHR$(BYTE)
Next
END FUNCTION

When I use READRTC(DTA$) I get an error 'UNKNOWN COMMAND'
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

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

The original code is a SUB, you have made it a FUNCTION.

Jim
VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 11:53pm 30 Jan 2019
Copy link to clipboard 
Print this post

I copy and pasted that, I thought it should maybe a sub.
Grogs just jotted the code out and warned me it was untested. So no fault of his.
When I get it running I will put it in the library.

"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Grogster

Admin Group

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

Hello.

Glad to see you are playing with this idea some more. No, I never tested the code, I just wrote what I thought would work, but as an untested code, it at least gives you a starting point.

Try: VALUE$=READRTC(DTA$)

This SHOULD return the data bytes generated by the read in the function in DTA$, to string VALUE$. IE: VALUE$ should now represent the data you requested from the RTC memory.

AGAIN - Untested, but it might give you something to play with.

EDIT: ....and I might have totally mucked up my functions and subs. I am not very good with functions!
I probably should have written it as a sub! (still can if you want)
Edited by Grogster 2019-02-01
Smoke makes things work. When the smoke gets out, it stops!
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 11:32pm 31 Jan 2019
Copy link to clipboard 
Print this post

So I have played around with Grogsters code and came up with..

SUB SAVETORTC
Local X 'A local X variable, perhaps totally seperate from a global X
For X=1 to 3 'Step through the three-byte date data, and save it to the RTC RAM
RTC SETREG 50+X, MID$(R$,X,1) 'Put data into RTC register R$ is numeric
Next
END SUB


SUB READRTC
R_Save$=""
Local X,BYTE
For X=1 to 3
RTC GETREG 50+X,BYTE
R_Save$=R_Save$+CHR$(BYTE) ' use "Rain_Saved = Val(R_Save$)" to get value
Next
END SUB


it runs without error but does not work, whether it is not writing or not reading I don't know or maybe both.
Edited by palcal 2019-02-02
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025