; Specify Device.
.include "8515def.inc"


;;; emacs for dos: toggle-buffer-file-coding-system
	;; how to make a beeper.
	;; connect a pizeo transducer directly to
	;; output pin, and then drive the pin high and
	;; low 2000 times each second. 
	; set high
	; wait 500us
	; set low
	; wait 500us 
;;; 
;;; registers used 
;;; 

.DEF SEG7         =R20		; Register for seven segment LEDs
.DEF switch_state =R22		; setting for the switch
.DEF list_inc     =R23		; tracks with the digit display
.DEF rgen1        =R24		; general register 1
.DEF rgen2        =R25		; general register 2

.DEF EEdrd      =r0		;result data byte


;;; 
;;; Constants
;;; 

.EQU IN_PORT    =PINB		; Push buttons
.EQU DIGIT      =PortC		; Controls the seven segment LEDs
.EQU ROTARY     =PIND		; Rotary switch coming in.
.EQU DIG_OUT    =PortA; 

.EQU SW1_PIN    =0; 
.EQU SW2_PIN    =1; 
.EQU EE_addr1    =$10
.EQU EE_addrh1   =$00
.EQU EE_addr2    =$20
.EQU EE_addrh2   =$00
	
.EQU sw1	=0
.EQU sw2	=1
.EQU sw3	=2
.EQU sw4	=3
.EQU sw5	=4
.EQU sw6	=5
.EQU sw7	=6
.EQU sw8	=7
	
.EQU pushbutton1	=0
.EQU pushbutton2	=1
.EQU pushbutton3	=2

;
; Code segment start
;
.CSEG

	rjmp start; 
	rjmp start;
	rjmp start;

;;; 
;;; Subroutines here
;;; 

beep:	
    rcall beep1
    rcall beep1
    cbi DIG_OUT, SW1_PIN     ; turn em on.
    ret

beep1:	
    ldi rgen1, 60
b_out_loop:
    ldi rgen2, 255
b_i_loop:
    cbi DIG_OUT, SW1_PIN     ; turn em on.
    nop
    sbi DIG_OUT, SW1_PIN     ; off
    nop
    dec rgen2
    brne b_i_loop
    dec rgen1
    brne b_out_loop
    ret
	
delay20ms:
    ldi rgen1, 60
outer_loop:
    ldi rgen2, 255
inner_loop:
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    dec rgen2
    brne inner_loop
    dec rgen1
    brne outer_loop
    ret
	
fix_list_len:
    dec list_inc
	
get_rotary_settings:
    ldi ZH,high(rotary_list*2)
    ldi ZL,low(rotary_list*2)
    lpm
    mov rgen1, list_inc		; dont destroy list_inc
	
	;; doesnt load a register with a value for frequency/duty_cycle
loop1: 
    adiw ZL, 4
    lpm
    mov SEG7, r0
    cpi SEG7, 0xff		; test for 0xff to see if at the end
    breq fix_list_len
    dec rgen1
    cpi rgen1, 0
    brne loop1

    mov SEG7, r0		; load on a value shift it four, this is "tens"
    lsl SEG7
    lsl SEG7
    lsl SEG7
    lsl SEG7

    adiw ZL, 1			; load on the ones
    lpm
    add SEG7, r0
	
    adiw ZL, 1
    lpm
    mov SEG7, r0	

    ret
	
	
func_end:
	ret
	
;;; 
;;; Start of program
;;; 

start:	
    ldi rgen1,HIGH(RAMEND)	; Stack setting to highest RAM adress
    out SPH,rgen1
    ldi rgen1,LOW(RAMEND)
    out SPL,rgen1

    ldi rgen1, 0b11111111	; load register to...
    out DDRA, rgen1		; configure PORT A for all outputs
	
    ldi rgen1, 0b00000000	; load register to...
    out DDRB, rgen1		; configure PORT B for all inputs

    ldi rgen1, 0b11111111	; load register to...
    out DDRC, rgen1		; configure PORT C for all outputs
	
    ldi rgen1, 0b00000000	; load register to...
    out DDRD, rgen1		; configure PORT D for all inputs

main_loop:
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw1             ; skip if bit is zero
    rjmp sw1_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw1 state
;;; 
    ldi SEG7, sw1	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw1_loop:
    sbic ROTARY, sw1             ; when in switch loop, check if its still on.
    rjmp sw1_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw1_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw1 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw1_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw1_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw1 state
;;; 
	
    rcall beep
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw1_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw1_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw1 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw1_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw1_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw1 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw1_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw1_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw1 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw1_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw1_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw1 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw1_loop		 ; back up to the top of sw1 loop
sw1_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw2             ; skip if bit is zero
    rjmp sw2_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw2 state
;;; 
    ldi SEG7, sw2	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw2_loop:
    sbic ROTARY, sw2             ; when in switch loop, check if its still on.
    rjmp sw2_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw2_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw2 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw2_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw2_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw2 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw2_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw2_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw2 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw2_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw2_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw2 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw2_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw2_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw2 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw2_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw2_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw2 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw2_loop		 ; back up to the top of sw2 loop
sw2_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw3             ; skip if bit is zero
    rjmp sw3_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw3 state
;;; 
    ldi SEG7, sw3	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw3_loop:
    sbic ROTARY, sw3             ; when in switch loop, check if its still on.
    rjmp sw3_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw3_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw3 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw3_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw3_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw3 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw3_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw3_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw3 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw3_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw3_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw3 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw3_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw3_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw3 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw3_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw3_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw3 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw3_loop		 ; back up to the top of sw3 loop
sw3_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw4             ; skip if bit is zero
    rjmp sw4_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw4 state
;;; 
    ldi SEG7, sw4	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw4_loop:
    sbic ROTARY, sw4             ; when in switch loop, check if its still on.
    rjmp sw4_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw4_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw4 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw4_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw4_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw4 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw4_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw4_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw4 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw4_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw4_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw4 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw4_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw4_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw4 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw4_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw4_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw4 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw4_loop		 ; back up to the top of sw4 loop
sw4_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw5             ; skip if bit is zero
    rjmp sw5_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw5 state
;;; 
    ldi SEG7, sw5	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw5_loop:
    sbic ROTARY, sw5             ; when in switch loop, check if its still on.
    rjmp sw5_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw5_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw5 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw5_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw5_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw5 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw5_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw5_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw5 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw5_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw5_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw5 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw5_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw5_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw5 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw5_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw5_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw5 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw5_loop		 ; back up to the top of sw5 loop
sw5_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw6             ; skip if bit is zero
    rjmp sw6_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw6 state
;;; 
    ldi SEG7, sw6	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw6_loop:
    sbic ROTARY, sw6             ; when in switch loop, check if its still on.
    rjmp sw6_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw6_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw6 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw6_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw6_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw6 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw6_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw6_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw6 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw6_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw6_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw6 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw6_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw6_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw6 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw6_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw6_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw6 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw6_loop		 ; back up to the top of sw6 loop
sw6_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw7
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw7             ; skip if bit is zero
    rjmp sw7_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw7 state
;;; 
    ldi SEG7, sw7	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw7_loop:
    sbic ROTARY, sw7             ; when in switch loop, check if its still on.
    rjmp sw7_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw7_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw7 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw7_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw7_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw7 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw7_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw7_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw7 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw7_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw7_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw7 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw7_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw7_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw7 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw7_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw7_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw7 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw7_loop		 ; back up to the top of sw7 loop
sw7_off:	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           rotary switch position sw8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sbic ROTARY, sw8             ; skip if bit is zero
    rjmp sw8_off		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for rotary sw8 state
;;; 
    ldi SEG7, sw8	
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
sw8_loop:
    sbic ROTARY, sw8             ; when in switch loop, check if its still on.
    rjmp sw8_off		 ; if it wasnt on, go to this label, else...	 
    sbic IN_PORT, pushbutton1    ; skip if bit is zero
    rjmp sw8_not_pb1		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton1 in sw8 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw8_pb1_loop:			 ; wait for release
    sbis IN_PORT, pushbutton1    ; skip if bit is 1
    rjmp sw8_pb1_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton1 in sw8 state
;;; 
	
    ldi SEG7, 4
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section

sw8_not_pb1:
    sbic IN_PORT, pushbutton2    ; skip if bit is zero
    rjmp sw8_not_pb2		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init parameters for pusbutton2 in sw8 state
;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw8_pb2_loop:			 ; wait for release
    sbis IN_PORT, pushbutton2    ; skip if bit is 1
    rjmp sw8_pb2_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw8 state
;;; 
	
    ldi SEG7, 5
    out DIGIT, SEG7

;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
	
sw8_not_pb2:
    sbic IN_PORT, pushbutton3    ; skip if bit is zero
    rjmp sw8_loop		 ; wasnt on, go to this label, else...
    rcall delay20ms		 ; debounce
;; init parameters for pusbutton3 in sw8 state

;;;;;;;;;;;;;;;;;;;;;;;;;;; end init
	
sw8_pb3_loop:			 ; wait for release
    sbis IN_PORT, pushbutton3    ; skip if bit is 1
    rjmp sw8_pb3_loop		 ; spinning wheels...
    rcall delay20ms		 ; released. debounce
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perform something in pushbutton2 in sw8 state
;;; 
	
    ldi SEG7, 6
    out DIGIT, SEG7
	
;;;;;;;;;;;;;;;;;;;;;;;;;;; end pushbutton1 section
    rjmp sw8_loop		 ; back up to the top of sw8 loop
sw8_off:	

rjmp main_loop

rotary_list: 
.db 0,0,0,0x21
.db 0,1,0,0x21
.db 0,2,1,0x21
.db 0,3,2,0x21
.db 0,4,3,0x21
.db 0,5,4,0x21
.db 0,6,5,0x21
.db 0,0,0,0x21
.db 0xff,0xff,0xff,0xff		; test for 0xff to see if at the end
