Shop OBEX P1 Docs P2 Docs Learn Events
Does anyone have good routines for setting up a DS1302 RTC with a numeric keypad? — Parallax Forums

Does anyone have good routines for setting up a DS1302 RTC with a numeric keypad?

idbruceidbruce Posts: 6,197
edited 2011-01-13 12:10 in Propeller 1
Hello Everyone

Just trying to save myself a little programming, and I was wondering if anyone had a good set of functions for setting the month, day, year, weekday, hours, minutes, and seconds, of the DS1302 RTC, with a 16 key numeric keypad. If you have any handy, I would appreciate being able to look at them and possibly use them. :)

Thanks In Advance

Bruce

Comments

  • BRBR Posts: 92
    edited 2011-01-11 16:22
    Bruce:

    This is not exactly what you asked for, but you might take a look at this to see if it is of any use to you:
    http://obex.parallax.com/objects/696/

    I'm working on a demo example of this approach for the ds1302_full object, but haven't finished debugging it yet.
  • idbruceidbruce Posts: 6,197
    edited 2011-01-11 16:26
    Thanks BR

    I currently have the buttons and the DS1302, it is all wired up and ready to go. All I have to do is program it.

    Bruce
  • JonnyMacJonnyMac Posts: 9,235
    edited 2011-01-13 12:07
    You should always strive to keep your routines generic so that you can re-use them without recoding. With your keyboard working you would probably want a routine that takes in a decimal value -- like this (I posted something like this before):
    pub getdec(digits) | dcount, c
    
      result := 0                                                   ' clear result
      dcount := 0                                                   ' clear digits count
    
      repeat
        c := kbd.waitkey                                            ' wait for key input
        case c
          0..9:
            if (dcount < digits)
              result := result * 10 + c                             ' add digit
              ++dcount                                              ' update count
            else
              ' error beep
    
          10:                                                       ' backspace/clear
            if (dcount > 0)
              result /= 10                                          ' remove last key
              --dcount                                              ' update count
            else
              ' error beep
    
          13:
            quit                                                    ' done
    
          other:
            ' error beep
    

    As you know from another post you need to provide BCD values to the DS1302 (and most other RTCs). That's simple:
    pub dec2bcd(dec)
    
      dec := 0 #> dec <# 99                                         ' for to legal range (0 to 99)
    
      return ((dec / 10) << 4) | (dec // 10)                        ' convert to BCD
    

    Finally, in your practical code you might do something like this:
    minsreg := dec2bcd(getdec(2))
    

    It is my opinion that this style of code not only serves you, but those you hope to share it with because the input routines can be used in many applications as-is.
  • idbruceidbruce Posts: 6,197
    edited 2011-01-13 12:10
    Thanks for the advice Jon
Sign In or Register to comment.