Counting key presses concept - 68908GZ8

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Counting key presses concept - 68908GZ8

756 Views
Bloodhound
Contributor I

Hi All,

 

Just working on a little something for home where I want to be able to control two relays to switch on some lights in a mode dependent on the number of key presses from one single button.

Relay 1 controls some front lights, Relay 2 controls some side lights.

 

The CPU I am using is a 68908GZ8 and I prefer assembler if someone wanted to post some example code :smileyhappy:

 

So what I want to do is something like this -

A) - If the lights are off, one key press turns Relay 1 on.

B) - If the lights are off, two key presses turns Relay 1 & 2 on.

C) - If either light is on, one key press turns both Relays off.

 

Couple of questions, would using the Keyboard Module be necessary? Is the only way to do this by using timers and checking for 'x' time between key presses?

Obviously there would also need to be a time out function from A to skiping B and waiting for C.

 

Any advice on which path to head down would be appreciated.

 

Thanks,

Ross

Message Edited by Bloodhound on 2009-03-22 11:21 PM
Added p/n to subject.
Message Edited by NLFSJ on 2009-03-26 04:11 PM
Labels (1)
0 Kudos
1 Reply

155 Views
bigmac
Specialist III

Hello Ross,

 

Since you have only a single switch input, and you require to monitor and time both the on and off states, the code will probably be more straightforward if you don't make use of the KBI module, but simply monitor the switch state by testing the input state of the pin.

 

For each switching event, there will need to be a debounce interval (perhaps 25-50ms), followed by a switch timeout interval of say 500ms.  These timing periods would need to be applied to both states of the switch.  Timeout would occur both for a "held" switch, and following switch release.

 

The easiest way to implement these timing periods is probably as an integral number of TIM overflow cycles.  Enable TIM overflow interrupts, with an overflow period in the vicinity of say, 5 ms.  This timing is not critical, but would allow both intervals to be handled with an 8-bit counter.  Within the ISR code, the counter would be simply decremented, unless its value was already zero.

 

From within the main loop, a timeout period is started by setting the counter to the required value, and timeout will have occurred when the counter becomes zero.

 

It seems that, if one or both lights are already on, it might be a good idea to require that the switch be held on for greater than the timeout period, before the turn-off occurs.

 

The operation of the code within the main loop would effectively become a simple "state machine".  The following is a possible sequence of events. 

  1. Assuming the switch is currently off, wait for an on state, and then delay for the debounce period.  If the switch has reverted to off, start over again to wait for an on state. 
  2. If the debounced on state is present, start the 500ms timeout period, and then within a tight loop, test for either timeout (switch hold), or switch off. If switch hold occurs, turn on #1 light, and then wait for the switch to be released.
  3. If the switch is released within the timeout period, apply a debounce period, followed by a further 500ms timeout period.  If timeout should occur, turn on #1 light.
  4. However, if the switch becomes again active, wait a further debounce period, and then turn on both lights.  Then wait for the switch to be released.
Steps 1 and 2 would be followed for the turn-off sequence.  If the switch is released prior to timeout occurring, there would be no action.

 

Regards,

Mac

 

0 Kudos