20 decimal to 20 H

General FreeBASIC programming questions.
Post Reply
Dinosaur
Posts: 1486
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

20 decimal to 20 H

Post by Dinosaur »

Hi All

May sound silly to some, but I just can't get my head around this.

Implementing RTC in BeagleBone Black and to set the time I have to convert decimal
Year (20) , Month Day Hours Min & secs into Hex. eg. year 20 = 20 Hex

The year is a good example.
I need to put into a uByte buffer 20 Hex to represent 20 decimal.

Also had to convert from 20 Hex to 20 decimal, but that was easy.
All the Date / Time bits are valued as per:
b7--b6--b5--b4--b3--b2--b1--b0
x----40--20--10--8----4----2----1

Code: Select all

Sub i2cGetTime
    With i2c
        .pBuffer = callocate(7)                     '7 bytes for SD2405 time data.
        '-----i2c Tests-------
        .Result = iolib_init()                      'initialize the library iobb.
        Print "iolib_init   = ";.Result
        configure_i2c_pins(19,20)
        .PortNbr = 2: .Addr = &H32                  'P9-19 & P9-20 the pins to use (no feedback)
        .Handle = i2c_open(.PortNbr,.Addr)          'Open the port and get a handle.
        Print "i2c Handle   = ;"; .Handle
        '--------------------------------
        .Result = i2c_read(.Handle, .pBuffer,7)     'attempt a read of 7 bytes using Handle
        If .Result > -1 Then                        'prints error if -1
            Dim as uByte Xq,xChr
            For Xq = 0 to 6                         'for 6 registers
                xChr = *(.pBuffer + Xq)             'get uByte from pBuffer + Xq
                Dim as uByte upper,lower,Masked
                Upper = xChr shr 4
                Lower = xChr And &B00001111
                Print "Upper = ";Xq,Upper
                Print "Lower = ";Xq,Lower
                Select case Xq
                    Case 0
                        .Secs = (Upper * 10) + Lower
                    Case 1
                        .Mins = (Upper * 10) + Lower
                    Case 2
                        Masked = Upper And &B00000011		'masked for 12/24 Hr
                        .Hours = (Masked * 10) + Lower
                    Case 3
                        .wDay  = Lower
                    Case 4
                        Masked = Upper And &B00111111
                        .Days  = (Masked * 10) + Lower
                    Case 5
                        .Mnths = 0
                        Masked = Upper And &B00011111
                        .Mnths = (Masked * 10) + Lower
                    Case 6
                        .Years = ((Upper * 10) + Lower) + 2000
                End Select
            Next
            Print .Years;"-";.Mnths;"-";.Days;"    ";.Hours;":";.Mins;":";.Secs
        EndIf
        deallocate(.pBuffer)                        'finished with Buffer
        '----------------------------
        .Result = i2c_close(.Handle)
        Print "Close Result = ";.Result
        iolib_free()                                'finished with Library
    End With
End Sub
Would love to see what simple conversion I have ignored.

Regards
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: 20 decimal to 20 H

Post by MrSwiss »

@Dinosaur,

seems to me, that you've misunderstood what's written in the Docs, of the device.
The encoding isn't, as you assumed in HEX, but in BCD (binary coded decimal).
SD2405AL - datasheet wrote: 5.2 Real Time Clock Registers [00h to 06h]

These RTC (Realtimeclock) registers are stored as binary-coded decimal BCD format.
Seconds and Minutes: range from 0 to 59;
Hour: can be set 12-hour or 24-hour mode;
Day: from 1 to 31,
Month: from1 to 12,
Year :from 0 to 99,;
Day of the Week: from 0 to 6.
24 HOUR TIME
If 12_/24 bit of the Hour register is “1”, the RTC uses a 24-hour format.
If the 12_/24 bit is “0”, the RTC uses a 12-hour format

Note:
1. You must clear the hour's highest bit 12_/24 after you have gotten the data from the hour register, otherwise it will be incorrect when the time is P.M
2. After power on reset, the realtime clock data registers aren't cleaned or set to be "1".
3. When writing the realtime data into RTC registers (00H~06H), you must write all of the total seven bytes data onetime. For exemple: when the time is “2006-12-20 Wednesday 18:19:20(24-hour format)”, the register 00~07H should be Assigned by 20H, 19H, 98H, 03H, 20H, 12H, 06H. The assignment of hour should be paid more attention, since 12_/24 bit is “1”.
Sorry to say but, what you've written, didn't make any sense at all.
The title is already confusing ... (20 hex = 32 dec).

The only usefull information provided, comes from a comment in your code:
'7 bytes for SD2405 time data. (from your code comments)
By clearly defining the device (RTC), that you are using.

The datasheet is from another, similar device since, SD2405 seems to be "discontinued".
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: 20 decimal to 20 H

Post by MrSwiss »

More on BCD encoding is found in:

Section: Tips & Tricks
Titled: Union as 'own' data-type
URL: viewtopic.php?f=7&t=27450

This just to give an idea of, how to deal with it ...
(it just does one UByte at the time)
You might have to extend it for your specific needs.
Dinosaur
Posts: 1486
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: 20 decimal to 20 H

Post by Dinosaur »

Hi All

Mr Swiss thanks for the reply.
It doesn't matter what they call it, it ends up as a Hex byte in the registers.
For example:
Reading the registers this morning.
In Byte order from 0 to 6
52 06 82 05 03 07 20 = 2020 July the 3rd Friday 2 am 6 min 52 sec's

The code I posted works correctly to GET the time.(may be a long way around)

Now I need to SET the time correctly.
To do that I have to use the decimal numbers for Year , month etc and put them in the register as noted.
So my problem is not converting 20 to hex, but putting 20 hex to represent 20 decimal.
Would be nice to have Nibble manipulation commands.

Regards
Edit:
Long and dirty:

Code: Select all

Print Date
Jaar = Right(Date,2)
Hi = Val(Left(Jaar,1))
Hi = Hi shl 4
Lo = Val((Right(Jaar,1))
Value = Hi + Lo
Print Hex(Value)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: 20 decimal to 20 H

Post by MrSwiss »

Dinosaur wrote:So my problem is not converting 20 to hex, but putting 20 hex to represent 20 decimal.
OK, now I hopefully understand, what you seemingly need.
Try the below code:

Code: Select all

' toBCD_Mac.bas -- (c) 2020-07-02, MrSwiss
'
' compile: -s console
'

#Define toBCD(v)    ( CUByte( (v) \ 10 Shl 4 + (v) Mod 10 ) )


Dim As UByte    ub1 = 20

Print ub1; Tab(9); Bin(ub1, 8)
Print toBCD(ub1); ; Tab(9); Bin(toBCD(ub1), 8)
Print

For i As UInteger = 0 To 99
    Print i; Tab(9); Bin(toBCD(i), 8)
Next

Print : Print "... done ... ";

Sleep
srvaldez
Posts: 3383
Joined: Sep 25, 2005 21:54

Re: 20 decimal to 20 H

Post by srvaldez »

pardon a silly suggestion, if d=20 decimal and you need it to be &h20 then perhaps this might work, I know it's silly
hd=val("&h"+str(d))
Dinosaur
Posts: 1486
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: 20 decimal to 20 H

Post by Dinosaur »

Hi All

That's great MrSwiss, I must have got something lost in the translation.
But that does exactly what I need.

By the way, what made you say that
SD2405 seems to be "discontinued".
https://core-electronics.com.au/search/?q=sd2405
I have searched for days, but I can't even find the original manufacturer.
The datasheet you pointed to is close to the one I have already, and where ever you find reference to the device
it is the same datasheet.

I was going to post the completed project under an easily found title.

Regards
Dinosaur
Posts: 1486
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: 20 decimal to 20 H

Post by Dinosaur »

Hi All

This prints out 86

Code: Select all

Dim d as uByte = 56
Dim hd as uByte
hd=val("&h"+str(d))
Print hd
Regards
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: 20 decimal to 20 H

Post by MrSwiss »

Dinosaur wrote:By the way, what made you say that

SD2405 seems to be "discontinued".
The web-site pointed to by my search engine (DDG).
Maybe, just related to their own product ?
Btw: that's where I've got the datasheet from ...
Post Reply