Shop OBEX P1 Docs P2 Docs Learn Events
DS1302(changing year/day/time...etc. — Parallax Forums

DS1302(changing year/day/time...etc.

hmlittle59hmlittle59 Posts: 404
edited 2008-02-17 19:15 in General Discussion
I'm learning the DS1302 chip and its operation and have some questions. I use the clock in 24hr mode only. My striped down version works, but is there a better way, to do what I've done, so that i can reduce my code size? When i want to change the time I have 2 separate buttons connected to my BS2 pins to ADD/SUBTRACT to the current YEAR. But it seems to(me) that i have to do to much conversion to get from the ( DS1302 format- to LCD format- back to DS1302 format), is there a better way to skin this cat?

1) My Defaults start in HEX and are saved to YEAR(YEAR = $08)...ex
2) work = year
3) work is converted from BCD to DEC
4) then displayed to the LCD with DEC2
5) then the program loops to see if a ADD/Subtract button has been pressed
6) if so then 'work' is up-dated
7) once you reach your value, another button is pressed (select)
8) 'work' is converted from DEC to BCD and save to year and the DS1302 is up-dated
9) end

Is this the best way this should be done is my question or is there a better way?

any help anyone can give, i've read all that i could find from the forum.

hmlittle59

'==================================================
work = year:up= 100: down = 99
GOSUB BCD2DEC
YearLoop:

SEROUT LCDScreen, Baud, [noparse][[/noparse]lcdline1,"Year ->", DEC2 work] ' LCD Home Position
GOSUB subup_down

PAUSE 100
BUTTON switchNext, 0, 255, 20, btnWrk, 0, YearLoop
GOSUB dec2bcd
'===========================================
'
Sub-Routine(s) for counting up/down

SubUP_Down:
PAUSE 100
BUTTON switchUp, 0, 255, 50, btnWrk, 0, No_Press
work = work + $01 //up
IF work = 00 THEN work = $01
No_Press:
PAUSE 100
BUTTON switchDown, 0, 255, 50, btnWrk, 0, No_Press1
work = work + down //up
IF work = 00 THEN work = down
No_Press1:
RETURN
==========================================
'
[noparse][[/noparse] Convert Decimal to BCD ]
'==========================================

Dec2bcd:
work = (work/10 << 4) + (work//10)
RETURN

' =========================================
'
[noparse][[/noparse] Convert BCD to Decimal ]
' =========================================

BCD2DEC:
tmpB3 = work ' get parameter
tmpB4 = tmpB3 >> 4 ' isolate high nibble
tmpB4 = tmpB4 * 10 ' convert to decimal
tmpB3 = tmpB3 & $0F ' isolate ones
work = tmpB4 + tmpB3 ' add them
RETURN

' =================================

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-02-15 05:30
    hmlittle59-

    Why don't you change (ADD/SUB) from the date before you store it in the DS1320 the first time?

    By the way, each BUTTON command should have its own work variable (btnWrk). Make one of them btnWrk1 and the other btnWrk2.

    You also may want to add some white space (blank lines) in your program for readability. When it's all jammed together it's a bit difficult to read, both for you and for others who may be trying to assist you.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Genius is one percent inspiration and ninety-nine percent perspiration."

    Thomas Alva Edison
  • hmlittle59hmlittle59 Posts: 404
    edited 2008-02-17 18:29
    Hello Bruce Bates,

    1) I want to be able to change these values as I go along...
    2) Btnwrk...I'm confused on what you mean...will this cause some kind of error?
    3) My bad on the formating...this is the way it copied over and i did not fix it.

    any help from anyone please...

    hmlittle59
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-02-17 19:15
    hmlittle59 -

    Sorry, it wasn't apparent from your first post that you wanted to make changes on-the-fly.

    As far as the work area for the BUTTON command is concerned, please check the PBASIC Reference Manual or the PBASIC Help File for details. Indeed there may be problems if separate work areas are not assigned and/or the work area is changed outside of the BUTTON command.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Genius is one percent inspiration and ninety-nine percent perspiration."

    Thomas Alva Edison
Sign In or Register to comment.