Shop OBEX P1 Docs P2 Docs Learn Events
Question about 74HC165 (switch inputs) and SX chip — Parallax Forums

Question about 74HC165 (switch inputs) and SX chip

T&E EngineerT&E Engineer Posts: 1,396
edited 2007-05-21 19:06 in General Discussion
I have been working on my Real Time Clock time setting routine with little luck in getting it right. However, I think I have something that sort of works but not completely. I can get either the SetMins OR the SetHrs subroutines to work independently but when called right after eachother I beleive I am doing something wrong with the 74HC165 switches software coding.

As you can see I am using the StampWorks routines for the 74HC165 and have added 4 pushbuttons to switches.0, switches.1, switches.2 and switches.3 .

The problem is that when it goes through the SetHrs routine, the user presses the 1 pushbutton (now set to a "1" when pushed) to change the hours and it gets displayed and then when finished, the user presses the 0 pushbutton (now set to a "1" when pushed) and it should go to the SetMins routine. However, it goes out of the MENU routine and back into the actual clock scrolling MAIN program. I think this is because it still thinks that the 0 pushbutton is a "1" and perhaps does not get set back to a "0". I don't really know for sure. I have 4 pushbuttons and I also tried useing pushbuttons 0 and 1 for the SetHrs routine and pushbuttons 2 and 3 for the SetMins routine but it worked but when it got to the MAIN scrolling clock program, only the last routine (SetMins) was updated - not SetHrs. I would like to use as few of buttons as possible. It doesn't make sense why I can not use only 2 pushbuttons for each routine (SetHrs and SetMins). Eventually I will also have to set the AM/PM mode, (maybe the SetSecs) as well as setting date properties like day, month, year and date.

Any ideas on what is wrong in my program?

I have also attached the complete program.
'More coding above and below this.  
 
Start:
[b]HIGH Load1[/b] 
 

' Set up TimeDate array (this initializes the time/date to 06:15:30 PM 05/11/07 Thu)
TimeDate(Secs) =  $30
TimeDate(Mins) =  $15
TimeDate(Hrs) =   $06 ' if modeFlag is set to Hr24, TimeDate(Hrs) = $00 to $23 otherwise $00 to $11 when Hr12 is set
TimeDate(Date) =  $11
TimeDate(Month) = $05
TimeDate(Day) =   $05
TimeDate(Year) =  $07
TimeDate(Ctrl) =  $00
ampmFlag = PM  ' if modeFlag is set to Hr12, ampmFlag is AM or PM
modeFlag = Hr12  ' set to either modeFlag = Hr12 or Hr24
 
For Idx = 0 to MESSAGESIZE  ' Clear any previous message
 Message1(Idx) = " "
 Message2(Idx) = " "
NEXT

[b]Menu[/b]
 
SetMode
SetTimeAndDate
 
Main:
MESSAGESIZE = 16
DO
For Color = 0 to 2
If Color = 0 THEN
 HIGH Green
 LOW Red
ELSEIF Color = 1 THEN
 Low Green
 HIGH Red
ELSEIF Color = 2 THEN
 Low Green
 Low Red
Endif

  WriteTimeToLED
  WriteDateToLED
 
  tmpB1(2) = 1   'scroll Time and Date left
  tmpB1(0) = 0   'time scrolls on the top row
  StringWriter
  tmpB1(0) = 1   'date scrolls on the bottom row
  StringWriter
  tmpB1(2) = 1   'scroll 16x16 picture left
   Pic_Scroll
   PAUSE 1000
   Pic_Scroll2
   PAUSE 1000
   
  Clear_Screen
Next
LOOP
END
 
' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------
[b]Menu:
[/b] HIGH Green
 LOW Red

[b]    SetHrs
    SetMins
   
RETURN[/b]
 
[b]Get_165:
  PULSOUT Load1, 10                             ' load switch inputs
  SHIFTIN SerData, Clock, MSBPRE, switches      ' shift them in
  RETURN[/b]
 
[b]SetHrs:
'First set Hours
  TIMESET = TimeDate(Hrs)
DO
  Get_165
  IF switches.1 = 1 THEN
    tmpB4 = TIMESET & $0F
    IF tmpB4 > $8 THEN
      TIMESET = TIMESET + 7
    ELSE
    TIMESET = TIMESET + 1
    ENDIF   
    IF TIMESET > $12 THEN
      TIMESET = 0
    ENDIF
    MESSAGESIZE = 4
    HEXUPDATE3 TIMESET,0
    tmpB1(2) = 1   'scroll Time and Date left
    tmpB1(0) = 0   'time scrolls on the top row
    StringWriter
  ENDIF
LOOP UNTIL switches.0 = 1
  TimeDate(Hrs) =  TIMESET
RETURN[/b]
[b][/b] 
[b]SetMins:
'Now set Minutes
  TIMESET = TimeDate(Mins)
DO
  Get_165
  IF switches.2 = 1 THEN
    tmpB4 = TIMESET & $0F
    IF tmpB4 > $8 THEN
      TIMESET = TIMESET + 7
    ELSE
    TIMESET = TIMESET + 1
    ENDIF   
    IF TIMESET > $59 THEN
      TIMESET = 0
    ENDIF
    MESSAGESIZE = 4
    HEXUPDATE3 TIMESET,0
    tmpB1(2) = 1   'scroll Time and Date left
    tmpB1(0) = 0   'time scrolls on the top row
    StringWriter
  ENDIF
LOOP UNTIL switches.0 = 1
  TimeDate(Mins) =  TIMESET
RETURN[/b]

Post Edited (T&E Engineer) : 5/21/2007 4:25:37 PM GMT

Comments

  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-05-21 19:03
    I took a quick but not thorough look at your code. Two things stand out. The first is that SetHrs and SetMins are nearly identical in function. With a little rewriting you could probably use a single function to update all of your time settings. I think something like TimeDate(Setting) = ChangeSettingFunction, MaxValue should work quite well for your application.

    My second observation deals directly with the question you raised regarding the quick exit from your second subroutine call. What I think is happening is that when you press the exit button the first subroutine exits immediately and the second subroutine is called while the button is still pressed. The second subroutine should make one pass through your DO LOOP before checking the exit condition. This likely occurs much faster than you could possibly release the button so it exists after only a single pass.

    A quick (I have not given much thought to whether or not it is the best) solution would be place a conditional loop to look for the opposite condition between the two subroutine calls (or at the end of the change settings subroutine if you chose to combine them). Basically you need to look for the '0' button to be released before you enter another subroutine that will also be expected to terminate as soon as it sees the '0' button is pressed.

    You should be aware that if your push buttons are not electrically debounced it is entirely possible that your software could see hundreds of electrical transitions for a single press of the button. If that is the case, you will need to "debounce" the buttons in software. A quick fix might take the following form, though I am sure there are other ways to accomplish the same result.
    WaitForZeroButtonRelease    Sub 0,0
    .
    .
    .
        SetHrs
        WaitForZeroButtonRelease
        SetMins
    .
    .
    .
    WaitForZeroButtonRelease:
        PAUSE 500                    ' Pause 500 milliseconds to allow the button to reach a stable state.
    
        DO WHILE switches.0 = 1      ' Loop while the switch is pressed.
        LOOP                         ' keep looping until the switch is released.
    RETURN
    


    I hope this helps.

    - Sparks
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-05-21 19:06
    This is very good. I will look at it tonight.

    Thanks Sparks!

    I will let you know my findings.
Sign In or Register to comment.