Shop OBEX P1 Docs P2 Docs Learn Events
DEC to HEX — Parallax Forums

DEC to HEX

starbotstarbot Posts: 4
edited 2005-04-17 16:01 in BASIC Stamp
I'm using a DS1302 real time clock for a project. The clock is set in hex values I believe, I'm using a DEC variable x to set time, how do i convert this to a HEX to then output it to the 1302. Thanks

Comments

  • JonbJonb Posts: 146
    edited 2005-04-17 06:19
    It doesnt really matter how you represent it in high level code.

    In this·clock code the register addresses are represented in binary(%00010), the control register commands in Hex($80) and the data in decimal (12). It all gets converted to binary. Basically you can send it decimal numbers no problem.




    ' {$STAMP BS2sx} 
    ' {$PBASIC 2.5} 
    '*************************************************************************** 
    '* DS1302 RTC 
    '* 
    '*************************************************************************** 
    
    ' ---------------------------------------- 
    ' I/O Definitions 
    ' ---------------------------------------- 
    Clk PIN 0 
    Dta PIN 1 
    RTCReset PIN 2 
    DIRS = %0000000000111111 
    OUTS = %0000000000000000 
    ' ---------------------------------------- 
    ' Constants 
    ' ---------------------------------------- 
    CoreBank CON 0 'Program Banks 
    ClockBank CON 2 
    TskResetRCT CON 0 'Program Tasks 
    TskReadRTCBurst CON 1 
    YskWriteRTC CON 2 
    TskReadRTCRAM CON 3 
    TskWriteRTCRAM CON 4 
    
    SecReg   CON %00000 'Register Adresses 
    MinReg   CON %00001 
    HrsReg   CON %00010 
    DayReg   CON %00011 
    MonReg   CON %00100 
    YrReg    CON %00110 
    CtrlReg  CON %00111 
    BurstReg CON %11111 
    ' ---------------------------------------- 
    ' Variables 
    ' ---------------------------------------- 
    Task     VAR Nib 'Current Task 
    RTCCmd   VAR Byte 
    Temp     VAR Byte 
    Seconds VAR Byte 
    Minutes VAR Byte 
    Hours   VAR Byte 
    Day     VAR Byte 
    Month   VAR Byte 
    Year    VAR Byte 
    

    TaskManager: 
    GET ClockBank, Task 
    BRANCH Task, [noparse][[/noparse]ResetRTC, ReadRTCBurst, WriteRTC, ReadRTCRAM, WriteRTCRAM] 
    
    [color=red]ResetRTC:                   'Set Time and date to RTC[/color] 
    GET 10,Seconds,Minutes,Hours,Day,Month,Year 
    Temp = [b]$10[/b]               ' Clear Write Protect bit in control register 
    RTCCmd = CtrlReg 
    GOSUB WriteRTC 
    
    Temp = Year 'Set Year 
    RTCCmd = YrReg 
    GOSUB WriteRTC 
    
    Temp = Month 'Set Month 
    RTCCmd = MonReg 
    GOSUB WriteRTC 
    
    Temp = Day 'Set Day 
    RTCCmd = DayReg 
    GOSUB WriteRTC 
    
    Temp = Hours 'Set Hours 
    RTCCmd = HrsReg 
    GOSUB WriteRTC 
    
    Temp = Minutes 'Set Minutes 
    RTCCmd = MinReg 
    GOSUB WriteRTC 
    
    Temp = Seconds'Set Seconds 
    RTCCmd = SecReg 
    GOSUB WriteRTC 
    
    Temp = [b]$80[/b]                 'Set Write Protect bit in control register 
    RTCCmd = CtrlReg 
    GOSUB WriteRTC 
    
    GOSUB Done 
    
    ReadRTCBurst: 
    
                      'Read From RTC 
    HIGH RTCReset 
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BurstReg\5,%10\2] 
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds,Minutes,Hours,Day,Month,Year,Year] 
    LOW RTCReset 
    PUT 10, Seconds, Minutes, Hours, Day, Month, Year 
    GOSUB Done 
    
    WriteRTC: 
                      'Write to DS1202 RTC 
    HIGH RTCReset 
    SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%10\2,Temp] 
    LOW RTCReset 
    RETURN 
    
    ReadRTCRAM: 
                      'Read from RTC RAM 
    HIGH RTCReset 
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,RTCCmd\5,%11\2] 
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Temp] 
    LOW RTCReset 
    GOSUB Done 
    
    WriteRTCRAM: 
                      'Write to RTC RAM 
    HIGH RTCReset 
    SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%11\2,Temp] 
    LOW RTCReset 
    GOSUB Done 
    Done: 
    RUN CoreBank
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (Jonb) : 4/17/2005 7:00:40 AM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-04-17 13:01
    DEC-to-HEX (BCD) is pretty easy with PBASIC:

    · bcdVal = (decVal / 10 << 4) + (decVal // 10)

    You can go the other direction too:

    · decVal = (bcdVal.NIB1 * 10) + bcdVal.NIB0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • starbotstarbot Posts: 4
    edited 2005-04-17 13:39
    Ok, now that I've got the setting of the time working, why does minutes 59 roll over to 68 (instead of increasing the hour by 1 and going to minutes 00).

    Code:


    setting:
    number=-1
    y=-1
    GOSUB inbutton
    SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]"?f"]
    SELECT number
    CASE =1
    x=x+1
    CASE =2
    x=x-1
    CASE =3
    y=0
    ENDSELECT
    SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]DEC x]
    PAUSE 100
    IF y<>0 THEN setting
    RETURN

    set:
    Temp = $10
    RTCCmd = CtrlReg
    GOSUB WriteRTC
    Temp = 98
    RTCCmd = YrReg
    GOSUB WriteRTC
    Temp = $08
    RTCCmd = MonReg
    GOSUB WriteRTC
    Temp = $27
    RTCCmd = DateReg
    GOSUB WriteRTC
    GOSUB setting
    Temp = x
    RTCCmd = HrsReg

    GOSUB WriteRTC
    x=0
    GOSUB setting
    Temp = x
    RTCCmd = MinReg
    GOSUB WriteRTC
    Temp = $00
    RTCCmd = SecReg
    GOSUB WriteRTC
    Temp = $80
    RTCCmd = CtrlReg
    GOSUB WriteRTC

    readtime:
    GOSUB ReadRTCBurst

    SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]DEC Hours,":",DEC Minutes,":",DEC Seconds.HIGHNIB,DEC Seconds.LOWNIB," ",DEC Month.HIGHNIB,DEC Month.LOWNIB,"/",DEC Date.HIGHNIB, DEC Date.LOWNIB,"/",DEC Year.HIGHNIB, DEC Year.LOWNIB]
    PAUSE 80
    SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]"?f"]
    GOTO readtime
    WriteRTCRAM:
    'Write to DS1202 RTC
    HIGH RTCReset
    SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%11\2,Temp]
    LOW RTCReset
    RETURN
    WriteRTC:
    'Write to DS1202 RTC
    HIGH RTCReset
    SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%10\2,Temp]
    LOW RTCReset
    RETURN
    ReadRTCBurst:
    HIGH RTCReset
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds,Minutes,Hours,Date,Month,Year,Year]
    LOW RTCReset
    RETURN
    ReadRTCRAM:
    HIGH RTCReset
    SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,RTCCmd\5,%11\2]
    SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Temp]
    LOW RTCReset
    RETURN
  • starbotstarbot Posts: 4
    edited 2005-04-17 13:47
    Thank you JonB and Jon Williams, I was able to use Williams' dec to bcd to solve my problem, and JonB's information to solve some of my other issues.
    An interesting note, all posters in this thread have the name Jon (I'm Jon Gowa).
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-04-17 16:01
    Starbot,

    ·· In case you need it, I also recently posted some very easy to use code for accessing the DS1302, including the on-board RAM.· The code handles 12 and 24 hour modes very easily as well.· You can find it here:

    http://forums.parallax.com/showthread.php?p=531080



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.