Shop OBEX P1 Docs P2 Docs Learn Events
AM/PM indicator — Parallax Forums

AM/PM indicator

MRMR Posts: 46
edited 2009-03-28 13:29 in BASIC Stamp
I have viewed the stamp works, and other posts on the forum but I am still stuck.

How do I put the DS1302 in 12 hour mode using the attached code?

Help me understand what is inside the brackets in these subroutines? I didn't write this code but I am trying to learn from it.
MR said...


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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
They say if you Play a Microsoft CD backwards you can hear satanic messages, that's nothing because if you play it forward it installs Vista!

I didn't Fail 10,000 times, I simply found 10,000 ways that didn't work. -Thomas Edison

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-27 22:48
    The last paragraph on page 6 of the DS1302 datasheet reads:
    The DS1302 can be run in either 12-hour or 24-hour mode. Bit 7 of the hours register is defined as the 12- or 24-hour mode-select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20–23 hours). The hours data must be re-initialized whenever the 12/24 bit is changed.
    



    The SHIFTOUT statements all transfer 16 bits to the DS1302 (the DS1302 is upward compatible from the DS1202). The first 8 bits is the command byte as specified on the bottom of page 5 of the datasheet. Note that the LSB comes first as described there. The %0\1 or %1\1 is the LSB, then there's 5 bits of address, then %11\2 or %10/2 for the last 2 bits (for RTC vs RAM). The next 8 bits is the actual data to be written to the DS1302 register or RAM location. The same thing happens on a read except the last 8 bits are read using a SHIFTIN.
  • MRMR Posts: 46
    edited 2009-03-28 12:02
    Mike Green said...
    The last paragraph on page 6 of the DS1302 datasheet reads:
    The DS1302 can be run in either 12-hour or 24-hour mode. Bit 7 of the hours register is defined as the 12- or 24-hour mode-select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20–23 hours). The hours data must be re-initialized whenever the 12/24 bit is changed.

    Okay I understand setting Bit 7 will toggle 12hr/24hr modes on the DS 1302, and Bit 5 selects am/pm of the hours register. I have played around with the code all night and I still can't get the
    clock into 12 hour mode. When I set the clock to 12:59:55 it cycles over to 13:00:00 and not 01:00:00 PM. I don't understand how to change bit7 and bit5 of the hours register. Please look at the attached code and tell me
    what I am doing wrong.

    MR said...

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    '
    [noparse][[/noparse]Clock]

    '
    [noparse][[/noparse]Variables]
    AUXIO
    RTCCmd VAR Byte
    Temp VAR Byte
    Seconds VAR Byte
    Minutes VAR Byte
    Hours VAR Byte
    Date VAR Byte
    Month VAR Byte
    year VAR Byte
    Year1 VAR Nib
    Year2 VAR Nib
    Year3 VAR Nib
    Year4 VAR Nib
    counter VAR Byte ' binary counter
    pattern VAR Byte
    Hold VAR Byte
    HoldL VAR Byte 'Latch Variable
    idx VAR Byte
    ampm VAR hours.BIT5 ' AM/PM Flag Bit
    clockMode VAR hours.BIT7 ' 12/24 Hour Mode Bit
    ampmFlag VAR Bit ' 0 = AM, 1 = PM
    modeFlag VAR Bit ' 0 = 24, 1 = 12 (Hours)
    Hr24 CON 0 ' 24 Hour Mode
    Hr12 CON 1 ' 12 Hour Mode

    modeflag=Hr12
    clockmode=modeflag
    ampmflag=0
    ampm=ampmflag
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    They say if you Play a Microsoft CD backwards you can hear satanic messages, that's nothing because if you play it forward it installs Vista!

    I didn't Fail 10,000 times, I simply found 10,000 ways that didn't work. -Thomas Edison
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-03-28 13:29
    MR

    Here is what you are looking for

    ·reg = $84········· ' Point to register 84h
    ioByte = %00000000 ' initializes the clock in 24 hour mode and clear the hours to "00"
    GOSUB RTC_Out····· ' Send Command

    ·reg = $84········· ' Point to register 84h
    ioByte = %00000010 ' initializes the clock in·12 hour mode and clear the hours to "00"
    GOSUB RTC_Out····· ' Send Command

    This from the·( DS1302_Demo.bs2··<<..·click here·)

    ' When reading the DS1302's clock data, BIT7 will be set when the RTC
    · ' is in 12 Hour Mode.· We need to save this information, but we must
    · ' then clear this bit so it doesn't interfere with the display.· Since
    · ' the data is in BCD format, leaving BIT7 set would give us invalid
    · ' value to display.· If the DS1302 is in 12 Hour Mode then we must
    · ' also save the status of BIT5, which is the AM/PM bit, however, once
    · ' again we must clear this Bit so our digits are valid for display.
    · ' If the DS1302 is in 24 Hour Mode we must LEAVE BIT5 alone, since it
    · ' is used when the hours are greater than 19.
    · ' These bits remain at their original value on the DS1302, and will
    · ' be reset on each read.


    · modeFlag = clockMode················· ' Save 12/24 Hour Clock Mode
    · clockMode = 0························ ' Clear BIT7
    · ampmFlag = ampm······················ ' Save AM/PM Flag

    · IF modeFlag = 1 THEN ampm = 0········ ' Clear BIT5 (12 Hour Mode only!)

    · DEBUG " ", HEX2 month, "/", HEX2 date, "/", HEX2 year, " "


    · ' If clockMode is cleared, then the modeFlag will be 0 and the
    · ' time will display in 24 hour format.

    · ' If clockMode is set, then the modeFlag will be 1 and the
    · ' time will display in 12 Hour format with an AM/PM suffix.

    · IF (modeFlag = Hr24) THEN············ ' Check For 24 Hour Display mode
    ··· DEBUG HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
    · ELSE
    ··· DEBUG HEX2 hrs, ":", HEX2 mins····· ' Print Hours/Minutes
    ··· IF ampmFlag = 0 THEN
    ····· DEBUG "AM"
    ··· ELSE
    ····· DEBUG "PM"
    ··· ENDIF

    ··· DEBUG " [noparse][[/noparse]", HEX2 secs, "]"
    · ENDIF
    · RETURN



    I hope this helps


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/28/2009 1:44:02 PM GMT
Sign In or Register to comment.