Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

How avoid bounce on INT0

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Wed Dec 10, 2014 10:52 pm    Post subject: How avoid bounce on INT0 Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 706

poland.gif
PostPosted: Thu Dec 11, 2014 12:45 am    Post subject: Reply with quote

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 Very Happy
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
View user's profile Visit poster's website
ivangel

Bascom Member



Joined: 20 Sep 2010
Posts: 41
Location: Poland

poland.gif
PostPosted: Thu Dec 11, 2014 9:05 am    Post subject: Reply with quote

Certainly the most interesting are the comments in Polish ... Regards

Pozdrawiam Very Happy

_________________
www.BlasterON.pl
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1864

blank.gif
PostPosted: Thu Dec 11, 2014 2:02 pm    Post subject: Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 706

poland.gif
PostPosted: Thu Dec 11, 2014 2:24 pm    Post subject: Reply with quote

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 Razz
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1864

blank.gif
PostPosted: Thu Dec 11, 2014 2:42 pm    Post subject: Reply with quote

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. Very Happy
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 706

poland.gif
PostPosted: Fri Dec 12, 2014 9:49 pm    Post subject: Reply with quote

In free time I played this today. It will then look something better in the search engine for the future Laughing
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
View user's profile Visit poster's website
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Sat Dec 13, 2014 9:41 am    Post subject: Reply with quote

Instead of label INT0_ISR: for distraction I have used Sub INT0_ISR and it work.

How is possible?
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 495
Location: Cleveland, OH

usa.gif
PostPosted: Sun Dec 14, 2014 6:49 am    Post subject: Reply with quote

Deleted post.

JC
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum