View previous topic :: View next topic |
Author |
Message |
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Wed Dec 10, 2014 10:52 pm Post subject: How avoid bounce on INT0 |
|
|
Hi friends
I have a switch on INT0 that create me bounce problem.
I would avoid bounce using debouce function but not is clear how to use it.
Could you help me? INT0 is set on FALLING.
SUB My_Interrupt_Routine
my code 1
my code 2
my code 3...
END SUB
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Dec 11, 2014 12:45 am Post subject: |
|
|
In most cases INT is not good idea for switch.
You can try this.
Code: | $regfile "m8def.dat"
Dim Lock As Word
Config Int0 = Falling
On Int0 Int0_isr
Enable Interrupts
'main loop
Do
If Lock > 0 Then Decr Lock
If Lock = 0 Then
Enable Int0
End If
Loop
End
Int0_isr
Disable Int0
Lock = 10000
'do some stuff
Return |
For POWER DOWN I have already made instruction for On/Off on One switch
All work after button is relased because interrupt can wake micro
Code: | Dim Lock As Word 'ilosc cykli do ponownego wcisniecia
Dim Turn_off As Bit 'flaga ustawiana by zadziałało po puszczeniu
Config PORTC.5 = Output : Set PORTC.5 'dowolny port na przykład podświetlanie LCD
Led Alias PORTC.5 '+ z procesora, dioda do masy
Config PORTD.2 = Input : Set PORTD.2 'przycisk na wejściu INTx
Switch Alias PIND.2
Config INT0 = Low Level 'important "LOW LEVEL"
On INT0 Int0_isr
Enable Interrupts
'*** START ***
Do
If Switch = 0 Then 'jesli wcisnieto przycisk
If Lock = 0 Then 'a zmienna już wyzerowana
Lock = 10000 'to zablokuj poprzez wpis do zmiennej
Turn_off = 1 'ustaw flage by po puszczeniu przycisku zadziałało
End If
Else
If Lock > 0 Then Decr Lock 'jesli blokada to zdejmuj po jednej
If Lock = 0 Then 'jesli blokada równa zero
If Turn_off = 1 Then 'a byl wcisniety przycisk
Turn_off = 0 'to skasuj flage
Reset Led 'zgas diode
Enable INT0 'dopiero zezwalaj na przerwanie(inaczej ten kod by sie nie wykonal)
'Display Off 'ewentualne wyłaczenie/czyszczenie LCD
Config Powermode = Powerdown 'idź spac
End If
End If
End If
Loop
End
'*** KONIEC ***
Int0_isr:
Disable INT0 'wylacz to przerwanie
Set Led 'wlacz diode
'Display On 'wlacz LCD
Lock = 10000 'zablokuj automatyczne przejscie znow
Return |
|
|
Back to top |
|
|
ivangel
Joined: 20 Sep 2010 Posts: 41 Location: Poland
|
Posted: Thu Dec 11, 2014 9:05 am Post subject: |
|
|
Certainly the most interesting are the comments in Polish ... Regards
Pozdrawiam _________________ www.midaz.com.pl |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Dec 11, 2014 2:02 pm Post subject: |
|
|
EDC wrote: | Code: |
'...
Reset Led
Enable INT0
Int0_isr:
Disable INT0
...
Return |
|
Your code is faulty insofar, that you forget to clear pending interrupts, before re-enabling the Int0.
The result is, as entry into the ISR is faster than bouncing duration, that after leaving the Int0-ISR the Int0-flag is set.
Immediately after re-enabling Int0, the ISR is called one more time, instantly disabling Int0, with the effect, that Int0 can't wake up the µC from sleep.
So, this code, as is, won't work. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Dec 11, 2014 2:24 pm Post subject: |
|
|
I know about pending interrupts and how set bit in GIFR but code for Powerdown is tested (not once) So if You test it, then post, not before with only theoretical considerations.
Magic words "LOW LEVEL"
Quote: | Config INT0 = Low Level 'important "LOW LEVEL" |
...with "Important" remarks |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Dec 11, 2014 2:42 pm Post subject: |
|
|
EDC wrote: | Config INT0 = Low Level 'important "LOW LEVEL" |
Right, low level won't remember the flag, then I withdraw my opinion for the second code and tell you, only the first is crap. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Dec 12, 2014 9:49 pm Post subject: |
|
|
In free time I played this today. It will then look something better in the search engine for the future
Code: |
$regfile = "m328pdef.dat"
$crystal = 16000000
Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.1 , Rs = Portd.0 ', Wr = Portc.3
Config Lcd = 20 * 4
Cursor Off
Cls
Dim Lock As Word , Count As Byte , Flag As Byte
Lcd "Counter:"
Config Portd.2 = Input : Set Portd.2
Switch Alias Pind.2
Config Int0 = Falling
On Int0 Int0_isr
Enable Interrupts
Flag = 1
'*** START ***
Do
If Switch = 1 Then
If Lock > 0 Then Decr Lock
If Lock = 0 Then
Eifr.0 = 1
Enable Int0
End If
End If
If Flag = 1 Then
Flag = 0
Locate 1 , 9 : Lcd Count
End If
Loop
End
'*** END ***
Int0_isr:
Disable Int0
Eifr.0 = 1
If Flag = 0 Then
Incr Count
Flag = 1
End If
Lock = 65535
Return
|
|
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Sat Dec 13, 2014 9:41 am Post subject: |
|
|
Instead of label INT0_ISR: for distraction I have used Sub INT0_ISR and it work.
How is possible? |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 584 Location: Cleveland, OH
|
Posted: Sun Dec 14, 2014 6:49 am Post subject: |
|
|
Deleted post.
JC |
|
Back to top |
|
|
|