![]() |
Forum Index : Microcontroller and PC projects : Counting Problem
Author | Message | ||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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: AustraliaPosts: 6283 |
Jim VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
@ Tassie Jim I tried that but @#%$!#@$ oops I think I did it wrong. "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: AustraliaPosts: 1993 |
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 KingdomPosts: 2944 |
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 . . . |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
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: AustraliaPosts: 1993 |
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. "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: AustraliaPosts: 1993 |
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. "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: AustraliaPosts: 1993 |
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. "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: AustraliaPosts: 6283 |
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 KingdomPosts: 2944 |
@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 KingdomPosts: 10315 |
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: AustraliaPosts: 1993 |
@ 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: AustraliaPosts: 6283 |
The original code is a SUB, you have made it a FUNCTION. Jim VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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 ZealandPosts: 9610 |
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) Smoke makes things work. When the smoke gets out, it stops! |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |