Hex problem with my DS1302
Hermie
Posts: 36
I'm trying to get my BS2 to do things based on the time and need to convert the Hex values to real numbers that the Stamp can compute. IE:
If mins = 23 THEN......
But the mins are in Hex format and I'm pulling my hair out trying to change the mins to a real number.
Can anyone help?
Hermie
If mins = 23 THEN......
But the mins are in Hex format and I'm pulling my hair out trying to change the mins to a real number.
Can anyone help?
Hermie
Comments
test VAR word
test = $0A
DEBUG DEC test
Run that. Notice that the variable test is defined as a hex number, and is converted to a decimal number to display. You can replace your code with "IF DEC mins = 23 THEN" and I think it will work.
I hope this helps,
Vaati
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Quit buying all those fixed voltage regulators, and·get an Adjustable Power Supply·for your projects!· Includes an LED testing terminal!
*-NEW-* SD Card Adapter·Now available!· Add extra memory to your next Propeller project with ease!
This has tripped me up a couple of times too... It's not so much that the numbers are in HEX format, rather than they are in BCD (Binary Coded Decimal) which is slightly different than just HEX format. Also with the DS1302, there are a couple of masked bits used in the seconds and hours that add different meaning to the overall value.
I will try to touch up on the BCD later if someone doesn't beat me to it... right now I have to run out the door.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Hermie
I got all sorts of confuzzled on this back when I first started using teh DS1302. What threw me was the "time stored in hex format" thing. So when I wanted to see a "10" in the minutes slot, for example, I thought I had to enter it as "0A". But that's not the case. 10 is stored as 10 - but the binary is not 1010, it is 0001 0000. Since the digits in any given slot never exceed 9, it can store as hex, but still operate as dec. I'm not sure this makes sense to you, but it works in all my programs, and I have a LOT of them running in the field now.
It is also a LOT easier if you can use 24 hour time rather than the am/pm thing. If you use 24 hour format, you can forget completely about the masked bits thing. It makes your programming much simpler this way (IMO).
So think of each number in a given time section as a separate nibble, and a time value in total (hours, or mins, or secs) as a byte. So 10:03:51 looks like this:
0001 0000 : 0000 0011 : 0101 0001
I had tried everything under the sun to convert from hex to dec, to enter digits as hex... and I was getting time values like 0A:34:8B...
Here is the link to the original post I made.· Pay particular attention to my last post which begins "I R DUMB!" :-)· That was the post where it finally dawned on me what I needed to do...
http://forums.parallax.com/showthread.php?p=815813
Hope this helps!
Dave X
Post Edited (xanatos) : 9/6/2009 10:35:56 PM GMT
What you are describing is BCD and not really HEX. In order for Hermie to do what I think he wants to do, he needs some sort of BCD<-->Decimal conversion.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Dave
To answer your question, I want to trigger different routines based on the time. In other words: If hrs = xx and mins = xx THEN goto/gosub whatever.
I'm not that fimilar with hex and only today heard of BCD, so I guess I have alot of homework to do
Hey Mike Green, thanks for the tip....but I didn't understand any of that routine....I will have my nose in the manual for awhile figuring that one out. But thank you....you've always been a source of knowledge!
Thanks to everyone else as well, this is deeper than I thought, but I will learn this as it is the heart of the project.
Just think....Another couple thousand hours of Pbasic study and I'll be the one answering questions! [noparse]:)[/noparse]· I love this stuff!
Hermie
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
50 72 6F 6A 65 63 74 20 53 69 74 65
·
I let my BS do things based on the time too, hours, not mins but should that be different?
hrs var byte
gosub CheckTime················· 'Ask 1302 what time..
if (hrs = $21) then....··········· ' Things shall happend at 21.00 (1302 set to 24 hrs mode)
'00=00,01=01,02=02,03=03,04=04,05=05,06=06,07=07,08=08,09=09
'10=16,11=17,12=18,13=19,14=20,15=21,16=22,17=23,18=24,19=25
'20=32,21=33,22=34,23=35,24=36,25=37,26=38,27=39,28=40,29=41
'30=48,31=49,32=50,33=51,34=52,35=53,36=54,37=55,38=56,39=57
'40=64,41=65,42=66,43=67,44=68,45=69,46=70,47=71,48=72,49=73
'50=80,51=81,52=82,53=83,54=84,55=85,56=86,57=87,58=88,59=89
So if I want something to happen at lets say 14:23:59 using 24 hour time it would look like this.
IF hrs=20 and mins=35 and secs=89 then gosub get_temp
This may not be the politically correct way to write the routine, but it works and I understand it. So thanks to all for the help
Hermie