Push buttons using assembly language

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

Push buttons using assembly language

Jump to solution
5,067 Views
MindMond
Contributor I

Hello,

 

How can I know what push button was pressed by the user in assembly language, i.e. how can I know if  the user pressed the button PTA2, PTA3, PTD2 or PTD3? Because I created a code (an interruption) that detects when any push button was pressed but I need to validate what was the button pressed by the user.

 

I'm using the microcontroller MC9S08QE128.

 

Thank you for your help.

Labels (1)
Tags (1)
0 Kudos
1 Solution
2,900 Views
bigmac
Specialist III

Hello,

 

As quickly as possible, after the interrupt occurs, you should read the port to which the switches are connected. 
This should be before any mechanical contact bounce has an opportunity to occur.  To prevent subsequent bounce from
causing additional interrupts, the KB interrupt should be disabled for a sufficient de-bounce period of say 50ms, or greater.  I would suggest that this timing might be done using the TPM1 overflow interrupt.

 

This will cater for the switch closure.  However, the switch will usually be released after the debounce period has expired.  Here, there is also the possibility of bounce occurring, and causing false interrupts during the turn-off bounce.  I can think of two solutions.

 

While the switch is active,you could alter the next interrupt from negative edge to positive edge.  Since the internal pullup would become a pulldown, this is not appropriate for the switch connection, and the internal pullup/pulldown would need to remain disabled.  This would require that external pullup resistors to be fitted.  This may not be readily feasible if you are using an existing board.

 

Another solution would be to temporarily disable the active switch pin from the KBI module.  The release of the switch would need to be ascertained by polling, to determine when the pin should be re-enabled for the KBI module.

 

There is a further issue for the 'QE128 device, and the pins that you mention.  There are two KBI modules, and each pair of pins is associated with a different module. This means that you will require separate ISR code for each module.

 

The following untested code snippet attempts to demonstrate the first method, where the polarity of the edge is changed.

 

ISR_KBI1: LDA    PTAD  COMA                  ; Invert - switches have common ground          AND    #$0C           ; Mask switch inputs  STA    SWSTAT1        ; Save current switch status  LDA    KBI1ES         ; Edge select register  AND    #$F3           ; Clear bits 2 & 3  ORA    SWSTAT1  STA    KBI1ES         ; Update active edge  MOV    #2,DEBOUNCE1   ; Set debounce delay (1-2 TPM overflows)  MOV    #$04,KBI1SC    ; Clear flag, disable interrupts  RTI  

 

You would need a similar ISR for KBI2 interrupts.  The following code is for the debounce timing for both KBI modules.

 

ISR_TPM1OVF:           TST    DEBOUNCE1   BEQ    ITP1           ; Branch if already zero   DEC    DEBOUNCE1   BNE    ITP1           ; Branch if not timeout     MOV    #$06,KBI1SC    ; Re-enable KB interrupt   ITP1:      TST    DEBOUNCE2   BEQ    ITP2           ; Branch if already zero   DEC    DEBOUNCE2   BNE    ITP2           ; Branch if not timeout           MOV    #$06,KBI2SC    ; Re-enable KB interrupt   ITP2:   BCLR   TPM1SC_TOF,TPM1SC ; Clear timer flag   RTI

 

Regards,

Mac

 

View solution in original post

0 Kudos
1 Reply
2,901 Views
bigmac
Specialist III

Hello,

 

As quickly as possible, after the interrupt occurs, you should read the port to which the switches are connected. 
This should be before any mechanical contact bounce has an opportunity to occur.  To prevent subsequent bounce from
causing additional interrupts, the KB interrupt should be disabled for a sufficient de-bounce period of say 50ms, or greater.  I would suggest that this timing might be done using the TPM1 overflow interrupt.

 

This will cater for the switch closure.  However, the switch will usually be released after the debounce period has expired.  Here, there is also the possibility of bounce occurring, and causing false interrupts during the turn-off bounce.  I can think of two solutions.

 

While the switch is active,you could alter the next interrupt from negative edge to positive edge.  Since the internal pullup would become a pulldown, this is not appropriate for the switch connection, and the internal pullup/pulldown would need to remain disabled.  This would require that external pullup resistors to be fitted.  This may not be readily feasible if you are using an existing board.

 

Another solution would be to temporarily disable the active switch pin from the KBI module.  The release of the switch would need to be ascertained by polling, to determine when the pin should be re-enabled for the KBI module.

 

There is a further issue for the 'QE128 device, and the pins that you mention.  There are two KBI modules, and each pair of pins is associated with a different module. This means that you will require separate ISR code for each module.

 

The following untested code snippet attempts to demonstrate the first method, where the polarity of the edge is changed.

 

ISR_KBI1: LDA    PTAD  COMA                  ; Invert - switches have common ground          AND    #$0C           ; Mask switch inputs  STA    SWSTAT1        ; Save current switch status  LDA    KBI1ES         ; Edge select register  AND    #$F3           ; Clear bits 2 & 3  ORA    SWSTAT1  STA    KBI1ES         ; Update active edge  MOV    #2,DEBOUNCE1   ; Set debounce delay (1-2 TPM overflows)  MOV    #$04,KBI1SC    ; Clear flag, disable interrupts  RTI  

 

You would need a similar ISR for KBI2 interrupts.  The following code is for the debounce timing for both KBI modules.

 

ISR_TPM1OVF:           TST    DEBOUNCE1   BEQ    ITP1           ; Branch if already zero   DEC    DEBOUNCE1   BNE    ITP1           ; Branch if not timeout     MOV    #$06,KBI1SC    ; Re-enable KB interrupt   ITP1:      TST    DEBOUNCE2   BEQ    ITP2           ; Branch if already zero   DEC    DEBOUNCE2   BNE    ITP2           ; Branch if not timeout           MOV    #$06,KBI2SC    ; Re-enable KB interrupt   ITP2:   BCLR   TPM1SC_TOF,TPM1SC ; Clear timer flag   RTI

 

Regards,

Mac

 

0 Kudos