PDA

View Full Version : Counting problems....

Mikael S
01-28-2010, 02:41 AM
In a code for a digital clock i use this instruction: "mins = mins + 1" (mins VAR Word) for·setting the minutes. It works execept when it going to change the 10'th minutes. For example can i see in my debug screen when·it counts: 28 - 29 - 2A - 2B - 2C -2D - 2F -30 -31 -32. Why does it count the letters?? How can i fix this??

Thanks for any help!!

Regards,

Mikael S
·

Wolfbrother
01-28-2010, 02:47 AM
Hi,

I think you are counting in HEX. Do a quick search and you will see how to do base 10 and base 60 to use a clock function.

Mikael S
01-28-2010, 03:19 AM
This is a example of my problem. How can i do to count from 0 - 60 without the (HEX) letters·??

' {\$STAMP BS2}
' {\$PBASIC 2.5}

· mins· VAR Byte

start:
· mins· = mins + 1
· 'PAUSE 50
· IF mins.HIGHNIB = 6 THEN mins = 0
· DEBUG HEX2 mins
· PAUSE 500
· GOTO start

Mikael

Unsoundcode
01-28-2010, 05:41 AM
Hi Mikael , internal to your Stamp the variable "mins" is a binary number consisting of a bunch of 1's and 0's.
To enable you to read the value of your variable on your computer monitor there are several "formatters" that take your 1's and 0's and format them in a way you are able to read them.
In your code you are using the HEX2 formatter that displays your 1's and 0' as a 2 digit hex value.
What it seems like you really need is the DEC2 formatter which will display the variable as a decimal number.

mins=mins+1
IF mins>59 THEN mins=0 : ENDIF
DEBUG DEC2 mins," "

Do a little research on computer numbering systems , it's really worthwhile.

Jeff T.

sam_sam_sam
01-28-2010, 07:03 AM
Here is another way of doing what your trying do

seconds VAR BYTE ' or WORD depending how many second you want to count

secs = \$00
GOSUB Set_Secs

DO
GOSUB Get_secs

IF secs <> oldsecs THEN ' This Routine Compars the oldsecs to secs
oldsecs = secs ' This Routine is so that Counter Update once a Seconds
s = s + 1
ENDIF

DEBUG HOME,HEX secs,CR, DEC3 s ' To Read The Seconds
LOOP

HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, Clock, LSBFIRST, [RdSecs]
SHIFTIN DataIO, Clock, LSBPRE, [secs.BIT0]
LOW CS1302 ' Deselect DS1302
RETURN

or· you need to ,HEX secs , HEX mins, ·HEX hrs

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·http://forums.parallax.com/images/smilies/idea.gif·that you may have and all of your time finding them http://forums.parallax.com/images/smilies/smile.gif

·
·
·
·
Sam

Post Edited (sam_sam_sam) : 1/28/2010 12:12:58 AM GMT

Mikael S
01-28-2010, 01:28 PM
I use the "mins = mins + 1" to set the·minutes to a ds1302. Is there a way to tell the variable "mins" to just count in decimal, i cant figure it out!

Mike2545
01-30-2010, 12:44 AM
Hmmm, do you mean the DS 1302 is loaded and displays HEX numbers just fine but when you try to change the numbers by mins = mins +1 you get weird results?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike2545

Mikael S
01-30-2010, 01:03 AM
Yes the ds1302 seem to work fine,·i use it to show the time·on LED display. The problem is·when i want to set the time, and show·the setting on the led-display. I just want the·display to step up one minute·at a time when i press a button.··I use the instruction "mins = mins +1", so i need the variable mins to be decimal.

Mike2545
01-30-2010, 01:16 AM
I'll bet your time is in 24 hr format too...I have run into the same situation, check out LOOKUP and change the min from hex to dec and then back again, unless someone has a better way to do it...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike2545

allanlane5
01-30-2010, 02:21 AM
The 1302 does what is called "Binary coded decimal", aka BCD. The low nibble (4-bits) counts 0 to 9, then increments the top nibble while the low nibble goes to zero again. The upshot of this is when you DISPLAY it as Hex, it LOOKS like Decimal.

However, the BS2 doesn't do this. When the low nibble counts from 9 to the next number, in binary (and hex) that's \$0A, not \$10. So, to send data (and interpret data) from the 1302, realize it's BCD. To send data (and interpret data) from the BS2, you can use the "DEC" modifier. But the two systems -- BCD and HEX -- ARE different from each other, and do require some conversion to use properly.

Mike2545
01-30-2010, 06:27 AM
Mikael, here is a work-around I came up with

'For displaying time
setclock:
second =0

if ampm = 0 then lookup Hr_hour,[00,01,02,03,04,05,06,07,08,09,16,17,18], hour
if ampm = 1 then lookup Hr_hour,[00,19,20,21,22,23,24,25,32,33,34,35,36], hour
LOOKUPmin_minute[00,01,02,03,04,05,06,07,08,09,16,17,18,19,20,21,22 ,23,24,25,32,33,34,35,36,37,38,39,40,41,48,49,50,5 1,52,53, 54,55,56,57,64,65,66,67,68,69,70,71,72,73,80,81,82 ,83,84,85,86,87,88,89,96],minute
I2COUT 6,7, I2C_WR, [0,Second,Minute,Hour,Day,Date,Month,Year]

I2COUT 7, I2C_WR, [0]
I2CIN 6 ,7, I2C_RD, [Second,Minute,Hour,Day,Date,Month,Year]
gosub gettime
serout 1,baud,["?y0?x11",dec2 Hr_hour,":",dec2 min_MINUTE ] 'lcd display
if hour =>18 then
serout 1 ,baud, ["?y0?x17PM"] 'display PM
ampm = 1
endif
if hour =<17 then
serout 1, baud, ["?y0?x17AM"]"display AM
ampm = 0
goto main
'-----------------------------------------------------
lookup hour,[12,1,2,3,4,5,6,7,8,9,2,2,2,2,2,2,10,11,12,1,2,3,4, 5,6,7,2,2,2,2,2,2,8,9,10,11],Hr_hour
lookup minute,[0,1,2,3,4,5,6,7,8,9,2,2,2,2,2,2,10,11,12,13,14,15, 16,17,18,19,2,2,2,2,2,2,20,21,22,23,24,25,26,27,28 ,29,2,2,2,2,2,2,30,31,32,33,34,35,36,37,38,39,2,2, 2,2,2,2,40,41,42,43,44,45,46,47,48,49,2,2,2,2,2,2, 50,51,52,53,54,55,56,57,58,59,2,2,2,2,2,2,60],miN_MINUTE
return

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike2545

Post Edited (Mike2545) : 1/30/2010 12:48:03 AM GMT

Mikael S
02-02-2010, 01:57 PM
Thanks! Will try this.

/Mikael