Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:11 24 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM Flip-Flop in MMBasic

Author Message
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 01:05am 11 Dec 2011
Copy link to clipboard 
Print this post

Gents,
How can I make a Flip-Flop in software?
I want to press a momentary contact button, thereby pulling a MM pin low - and set a flag high. Then press it again and set the flag low, i.e. toggle back and forth. It would need to take into account switch bounce.
Regards
Talbit
Talbit
 
crackerjack

Senior Member

Joined: 11/07/2011
Location: Australia
Posts: 164
Posted: 01:21am 11 Dec 2011
Copy link to clipboard 
Print this post

Quick clue:

X = NOT(X)

As for switch bounce, aren't the digital inputs on the Maximite Schmitt Triggered anyway?
 
RawMicro

Newbie

Joined: 29/10/2011
Location: United States
Posts: 4
Posted: 01:31am 11 Dec 2011
Copy link to clipboard 
Print this post

  crackerjack said   Quick clue:

X = NOT(X)



Brilliant!

- Rich
Fuel Efficient Cars HQ
 
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 02:24am 11 Dec 2011
Copy link to clipboard 
Print this post

Thanks gents,
The MM pin might be schmitt triggered but I still need to compensate for contact bounce.
Anyway, I'll try and use what you've given me to do the job. I might need some more clues though!
Thanks
Talbit
Talbit
 
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 09:30pm 11 Dec 2011
Copy link to clipboard 
Print this post

Thanks,
The simplest solution is always the best! That works fine. There doesn't appear to be much contact bounce in my test setup.
Talbit
Talbit
 
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 05:16am 12 Dec 2011
Copy link to clipboard 
Print this post

Nope - too much contact bounce I'm afraid.
I'm using a push button in my GPS clock to change from Time to Position display. I noticed that it didn't change reliably. I’m using a push button arrangement similar to fig.5 on page 69 of the May 2011 Silicon Chip article. I noticed that the changeover was unreliable so I wrote the attached code to test it.
I have the switch on pin 1 and I have pin 2 driving an LED via a transistor. It doesn’t always light up or switch off. So I had a closer look at it via a cro.
Sure enough – I can see that in fact pin 2 does toggle on and off making it appears as though nothing has happened.
It seems that the interrupt is being serviced ok but by the time it returns, the bounce triggers the interrupt again.
So the solution is to have a debounce on the switch.
I’ve used up my Settick timer for another task so I can’t use that.
Any code floating around I can use?
Regards
Talbit.
20 x=0 ' Value of X determines if LED is 0n or Off
30 SETPIN 2, 8 ' Make Pin 2 a digital output (LED)
60 SETPIN 1, 7, 200 ' Interrupt routine to toggle x after button is pressed
100 DO
110 IF x=0 THEN PIN(2)=0
115 IF x=1 THEN PIN(2)=1
120 LOOP
200 ''' Interrupt routine to toggle X after button is pressed
220 x =NOT(x)
230 IRETURN

Talbit
 
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 05:48am 12 Dec 2011
Copy link to clipboard 
Print this post

Well I might have improved it by adding...
210 Pause 2
i.e. a 2 mS pause inside the interrupt routine. It's not the best way to do it I'm sure.
But I have noticed a few hardware solutions online so might look into those.
Talbit.
Talbit
 
crackerjack

Senior Member

Joined: 11/07/2011
Location: Australia
Posts: 164
Posted: 05:50am 12 Dec 2011
Copy link to clipboard 
Print this post

Your interrupt routine could just do this:

[code]
PIN(2)=NOT PIN(2)
[/code]

And do away with lines 110 and 115. In the interrupt routine you could delay for 20 ms if that's ok with the rest of your main loop processing. That should eliminate the multiple triggering.
 
Keith W.
Senior Member

Joined: 09/10/2011
Location: Australia
Posts: 118
Posted: 06:52am 12 Dec 2011
Copy link to clipboard 
Print this post

Hi Talbit and Crackerjack

Do you have two pins available? If so, and using a button with change over contacts you can use one button contact for ON and one contact for OFF inputs instead of simply detecting on or off with the one pin. There must be a fair mechanical travel that both ON and OFF cannot be active together. Then your code can test for an ON contact and if an OFF software flag is set causes the required action, also clearing the OFF flag which prevents a second action until the software detects an OFF contact, which sets the OFF flag again. Again ready to detect another button push. You can still use an interrupt but it may be called often checking the flag while the contact bounces and maybe another interrupt to handle the off contact also to rearm the flag.

In your main software loop you can always check for an OFF condition and arm the OFF flag as this is not usually as time dependent.

Debouncing with a single input pin requires the introduction of time delays that prevent the triggering of an action until the OFF condition has been detected for a period longer than the worst contact bounce time, again using a software flag to prevent multiple actions. This method can slow button action because of the delays in re-arming. Again the time period may be generated by counting a variable up as each main software loop is repeated to a terminating value, the variable cleared after detection of the ON condition. You can probably determine the delay by setting a pin on reaching a value and checking with your CRO for how long is it low after button release. Not a great method if the software just loops waiting for the button though as the numbers will get very large. I remember that values of about 20 milliseconds are required to cover contact bounces which can also occur on both the pushing and release of a push button.

Best of luck,

Keith W.
 
swoodgate

Newbie

Joined: 24/11/2011
Location: Australia
Posts: 14
Posted: 01:39pm 12 Dec 2011
Copy link to clipboard 
Print this post

Hi Talbit

I don't have the hardware to test this code but it does run on my SM1.
I have only limited hardware knowledge so I'd appreciate it if you could try this idea but I'll certainly understand if you don't want to waste time.
If possible could you tell me how you wired your button and led?


10 countstart = 100 ' sets time for valid button press, adjust as required
20 counter = countstart
30 flipflop = 0
40 SETPIN 2,8
50 SETPIN 1,2
60 PIN(2) = flipflop
70 DO
80 IF counter THEN counter = counter - 1 ' count down to 0
90 IF PIN(1) THEN counter = countstart ' if not pressed reset count down
100 IF NOT counter THEN flipflop = NOT flipflop ' if we've waited long enough flip
110 PIN(2) = flipflop
120 LOOP


Best regards

Steve
 
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 10:58pm 12 Dec 2011
Copy link to clipboard 
Print this post

Thanks gents for your various and very valuable responses.
I'll try the various ideas later but for now I'm happy with the small delay in the interrupt routine.
Today I hope to submit my new GPS clock (for ridicule!)
Regards
Talbit
Talbit
 
Talbit
Senior Member

Joined: 07/06/2011
Location: Australia
Posts: 210
Posted: 10:20pm 13 Dec 2011
Copy link to clipboard 
Print this post

Steve,
Your code is all over the place I'm afraid. When I press the button, it appears as though pin 2 oscillates madly until it settles in either a high or a low state. It might be oscillating for 100 cycles – which is what you set your counter to.
I ‘m not sure what your code is doing wrong.
Have you seen my latest clock I posted yesterday? Forget most of the circuit but all you need to do is connect pin 2 to an LED and add the push button.
I have pin 2 connected to a transistor driver which drives the LED. You could just connect pin 2 directly to an LED via a resistor of about 330 ohms to the MM 5 volts. See my clock circuit to see how I’ve connected my push button. It just pulls pin 1 low, which is normally held high via the 10 K resistor.
I’m afraid I’ll have to leave the rest up to you but thanks for the challenge!
Let me know how you go.
If I get time over Xmas I’ll look further into it. But I can test any code you want. Just post it.
Regards,
Talbit

Talbit
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024