'----------------------------------------------------------------------------------------- 'name : TB_peda_debouncer_BP_V1.bas 'date : 20.09.2008 'copyright : public domain 'micro : Mega8 'description : Bulletproof Tasten-Entprell-Routine in BASCOM ' Original in ASM von Peter Dannegger ' an PortD.2 bis D.7 ist ein 16*2 LCD angschlossen ' an PortB.0 bis B.5 sind 6 Taster angeschlossen (gegen GND) ' an PortC.0 bis C.5 sind 6 LEDs angeschlossen (gegen GND) ' Key_state ist aktueller Taster-Zustand gedrückt oder nicht ' Key_press zeigt an, welche Taste gedrückt wurde '----------------------------------------------------------------------------------------- $regfile = "m8def.dat" ' specify the used micro $crystal = 3686400 ' used crystal frequency $baud = 9600 ' use baud rate $hwstack = 32 ' default use 32 for the hardware stack $swstack = 20 ' default use 10 for the SW stack $framesize = 40 ' default use 40 for the frame space '$sim 'REMOVE the above command for the real program !! '$sim is used for faster simulation 'LCD-Display 16*2 HD44780 konfigurieren Config Lcd = 16 * 2 '16 Zeichen und 2 Zeilen Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2 Cls '---------------------------------- 'Variablen für die Entprell-Routine Dim Iwr0 As Byte Dim Key_ct0 As Byte Dim Key_ct1 As Byte Dim Key_state As Byte Dim Key_press As Byte Dim Leds As Byte Key_ct0 = 0 Key_ct1 = 0 Decr Key_ct0 Key_ct1 = Key_ct0 Key_state = 0 Key_press = 0 Leds = 255 'alle LEDs an '---------------------------------- 'Ports konfigurieren Ddrb = &B00000000 'PortB = Eingang Portb = &B00111111 'Pullup aktiv von PortB.0 bis 5 Key_port Alias Pinb '"Key_port" = PinB Ddrc = &B00111111 'PortC.0 bis C.5 = Ausgang Led_port Alias Portc '"Led_port" = PortC '---------------------------------- 'Timer0-Interrupt konfigurieren (Timer0 = 8bit) Config Timer0 = Timer , Prescale = 256 Enable Timer0 On Timer0 Timer0_isr 'Interrupt Service Routine anspringen Enable Interrupts Timer0 = 111 '3,6864MHz+Prescale256 = ca.10ms '---------------------------------- 'Start des Hauptprogramms Do Disable Interrupts 'Interrupts deaktivieren Leds = Leds Xor Key_press 'Job: Leds mit Tastendruck toggeln Key_press = 0 'Key_press nach dem Job löschen Enable Interrupts 'Interrupts wieder aktivieren Led_port = Leds 'LEDs ein/ausschalten Upperline 'LCD 1.Zeile Pos 1 Lcd "K_state:" ; Bin(key_state) 'Key_state (Taste gedrückt?) auf LCD Lowerline 'LCD 2.Zeile Pos 1 Lcd "K_press:" ; Bin(key_press) 'Key_press auf LCD (0,da Auge zu träge) Loop End '============================================================================== 'Interrupt Service Routine für Timer0 Timer0_isr: Timer0 = 111 Iwr0 = Key_port Iwr0 = Not Iwr0 Iwr0 = Iwr0 Xor Key_state Key_ct0 = Key_ct0 And Iwr0 Key_ct1 = Key_ct1 And Iwr0 Key_ct0 = Not Key_ct0 Key_ct1 = Key_ct1 Xor Key_ct0 Iwr0 = Iwr0 And Key_ct0 Iwr0 = Iwr0 And Key_ct1 Key_state = Key_state Xor Iwr0 Iwr0 = Iwr0 And Key_state Key_press = Key_press Or Iwr0 ' ' insert other timer functions here ' Return '----------------------------------