Skip to content

Instantly share code, notes, and snippets.

@dchapman1988
Created October 17, 2014 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchapman1988/cc2e46f950a699ca8e00 to your computer and use it in GitHub Desktop.
Save dchapman1988/cc2e46f950a699ca8e00 to your computer and use it in GitHub Desktop.
; Name: David Chapman
; Program: Lab 02
; Version: 1.0
; Date Started: 10/15/2014
; Last Update: NA
; Copyright © 2014
;
; Description: Implement a counter that increments the count when SWX is
; pressed and clears the count when SWY is pressed. The count
; is to be displayed via the four LEDs that were used in the
; last Lab. You get to choose the two switches that increment
; and clear (these should be chosen from the switches used in
; the previous lab). Be sure to use the debouncing method
; covered so that you do not add to your count when you do not
; need to.
;******************************************************************************
;-----------------------------Include Libraries--------------------------------
INCLUDE 'derivative.inc' ;Include derivative-
;specific definitions
XDEF Entry, _Startup, main ;export symbols
XREF __SEG_END_SSTACK ;symbol defined by the
;linker for the end of
;the stack
;******************************************************************************
;-----------------------------variable/data section----------------------------
MY_EXTENDED_RAM: SECTION
org $1000 ;Start the memory locations at $1000
R1 equ $1001
R2 equ $1002
R3 equ $1003
btn_count ds.w 1
;******************************************************************************
;-----------------------------code section-------------------------------------
MyCode: SECTION
main:
_Startup:
Entry:
lds #__SEG_END_SSTACK ; initialize the stack pointer
cli ; enable interrupts
ldab #$00
stab btn_count ; set count to 0
ldaa #$00 ; clear register A
ldaa DDRH ; PTH bit 0 and 3 are inputs
anda #$F6 ; 11110110
staa DDRH
ldaa DDRB ; PTB Bits 0 through 3 as output on LED
oraa #$FF ; 00001111
staa DDRB
ldaa DDRJ ; Make PTJ output
oraa #$02 ; 00000010
staa DDRJ
ldaa PTJ ; enabled leds ground
anda #$FD ; 11111101
staa PTJ
ldaa DDRP ; Turn off seven-segment display
oraa #$0F
Staa DDRP
ldaa PTP
oraa #$0F
staa PTP
;******************************************************************************
;-----------------------------PollSwitches:------------------------------------
PollSwitches:
PollSW2:
ldaa PTIH ; Check if SW2 (PH3) has been pressed
anda #$08 ; clear all bits except bit 3
cmpa #$00 ; If bit 3 is 0 then SW2 was pressed
bne PollSW5 ; Poll for SW5 if not pressed
bsr DebounceSW2 ; DebountSW2 because it was pressed
PollSW5:
ldaa PTIH ; Check if SW5 (PH0) has been pressed
anda #$01 ; clear all bits excpet bit 0
cmpa #$00 ; if bit 1 is 0 then SW5 was pressed
bne PollSwitches ; reset the poll if not pressed
bsr DebounceSW5 ; DebounceSW5 because it was pressed
bra PollSwitches ; Forever poll SW2 and SW5
;******************************************************************************
;----------------------------DebounceSW2:--------------------------------------
DebounceSW2:
bsr Delay ; call delay for software debounce
ldaa PTIH ; see if SW2 (PH3) has been pressed
anda #$08 ; clear all bits except bit 3
cmpa #$00 ; if bit 3 is 0, Sw2 was pressed
bne SW2hi ; we branch if SW2 send high signal
ldab btn_count ; making it here means SW2 was pressed
incb ; increment btn_count in Reg A
stab btn_count ; store that value back in btn_count
;bsr LEDCount ; now figure out what LEDs to light
stab PORTB
SW2hi:
rts ; return
;******************************************************************************
;----------------------------DebounceSW5:--------------------------------------
DebounceSW5:
bsr Delay ; call the delay for software debounce
ldaa PTIH ; see if SW5 (PH0) was pressed
anda #$01 ; clear bits except bit 0
cmpa #$00 ; if bit 1 is 0 then SW5 was pressed
bne SW5hi ; we branch if SW5 sends a high signal
ldab #$00 ; SW5 was pressed, clear btn_count
stab btn_count
stab PORTB
;bsr LEDs_OFF ; branch to turn off all LEDs, restart
SW5hi:
rts ; return
;******************************************************************************
;----------------------------LEDCount------------------------------------------
LEDCount:
ldaa btn_count ; check if btn_count is 1
cmpa #$01
bne Light2 ; branch for next count if not
ldaa PORTB ; if btn_count is 1, turn on first LED
oraa #$01
staa PORTB
bra Finished ; all done for a 1 count
Light2:
ldaa btn_count ; check if btn_count is 2
cmpa #$02
bne Light3 ; branch for next count if not
ldaa PORTB ; if btn_count is 2, light up 2 LEDs
oraa #$02
staa PORTB
bra Finished ; all done for a 2 count
Light3:
ldaa btn_count ; check if btn_count is 3
cmpa #$03
bne Light4 ; branch for next count if not
ldaa PORTB ; if btn_count is 3, light up 3 LEDs
oraa #$04
staa PORTB
bra Finished ; all done for a 3 count
Light4:
ldaa btn_count ; check if btn_count is 4
cmpa #$04
bne Finished ; ignore if not a 4 count
ldaa PORTB ; if btn_count is 4, light all 4 LEDs
oraa #$08
staa PORTB
bra Finished ; Finished with LEDCount
Finished:
rts ; return
;******************************************************************************
;----------------------------LEDs_OFF------------------------------------------
LEDs_OFF:
ldaa PORTB ; turn off the 4 LEDs (PB0 - PB3)
anda #$F0
staa PORTB
rts ; return
;******************************************************************************
;-------------------------------Delay:-----------------------------------------
Delay:
psha ; save register A on stack
ldaa #150 ; this value determines delay length
staa R3
L3 ldaa #10
staa R2
L2 ldaa #240
staa R1
L1 nop ; 1 Intruction Cycle
nop ; 1
nop ; 1
dec R1 ; 4
bne L1 ; 3
dec R2 ; Total = 10 cycles
bne L2
dec R3
bne L3
pula ; restore register A
rts
;******************************************************************************
;---------------------------Questions------------------------------------------
; 1) Why is debouncing important?
; Debouncing is important because in modern digital systems, the processors
; are so fast that bounce-back due to the mass of the mechanical switch
; causes noise in the system registering multiple button presses when only
; one is desired.
;
; 2) How can I debounce in hardware?
; Analogues to a software delay technique a hardware delay technique could
; involve an R-C circuit that exploits the time constant found in capacitors
; to drive the time delay needed to wait out the bounce-back from a
; mechanical switch.
;
; 3) What other electro-mechanical device might bounce?
; An electro-mechanical relay would be an example of a device that might
; bounce since it has metal conacts and thus mass. Also because its on a
; moveable strip of metal it has springiness.
;
; 4) List the number of cycles for each of the opcodes used in your delay
; function, then sum the total time of the delay in clock cycles.
; (1/24MHz)*10*240*150 = 15ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment