;Eric Melin and Brian Glassmeyer ;;******************************************** ;;*** Final Project: Counter Test ;;*** With pushbuttons and LED display. ;;******************************************** ;Port A Counter Bits (1-8) ;Port B Controls the LEDs ;Port C Out to Counter/Counter Bits (9-12) ;Port D Button Inputs .include "8535def.inc" .device AT90S8535 .def savSREG =r1 ;save the status register .def cntlow =r16 ;the lower byte of captured counter .def cnthigh =r17 ;the upper 4 bits of captured counter .def cntinc =r18 ;clock to increment counter .def temp =r19 ;temporary register .def running =r20 ;0(running)/1(stopped) .def display =r21 ;0(low byte)/1(high bits) .equ pb_start_stop =1 ;button to toggle start/stop .equ pb_clear =2 ;button for clearing the counter .equ pb_display =4 ;button to toggle low/high bits displayed .equ Prescale0 =0x05 ;clk/1024 .equ Prescale1 =0x0C ;clk/256 .equ cntclear =0x80 ;mask for clr signal to counter .equ cntincbit =0x40 ;mask for clk_ signal to counter .equ cnthighmask =0x07 ;mask for incoming high bits from counter ;************************************** .dseg ;************************************** .cseg .org $0000 rjmp RESET ;reset entry vector reti reti reti reti reti rjmp T1compA reti reti ;rjmp T1ovfl ;Increment counter & update LEDs rjmp T0ovfl ;Check button press reti reti reti reti reti reti reti RESET: ldi temp, LOW(RAMEND) ;setup stack pointer out SPL, temp ldi temp, HIGH(RAMEND) out SPH, temp ;start clock ldi display, 0 ldi running, 1 ldi temp, Prescale0 out TCCR0, temp ;do a compare match ldi temp, 0x00 out OCR1AH, temp ldi temp,0xf0 out OCR1AL, temp ldi temp, Prescale1 out TCCR1B, temp ldi temp,0b00010001 ;Enable Timer0 and Timer1 interrupts out TIMSK,temp ;init port directions ldi temp, 0x00 out DDRD, temp out PORTD, temp out DDRA, temp out PORTA, temp ldi temp, 0xFF out DDRB, temp ;Set PORTB as LED outputs ;out PORTB, temp ;Initially turn off LEDs ldi temp, 0xE0 out DDRC, temp ;Set PORTC(5-7) as counter outputs sei SPIN: rjmp SPIN ;interrupt driven test ;****************************************************** ;********Interrupt Routines ;Checks for button press T0ovfl: in savSREG, SREG ;Determine which button sbic PORTD, pb_start_stop rjmp start_stop sbic PORTD, pb_clear rjmp clear sbic PORTD, pb_display rjmp high_low out SREG, savSREG ;No button was pressed reti start_stop: tst running ;Check current running status brne stop ser running ;Was stopped--now run counter out SREG, savSREG reti stop: clr running ;Was running--now stop counter out SREG, savSREG reti clear: ldi temp, cntclear ;set clear signal to counter out PORTC, temp clr temp ;remove clear signal or temp, cntinc ; but don't mess the clk_ signal out PORTC, temp out SREG, savSREG reti high_low: tst display ;Check current display status brne low ser display ;Change to high bits out SREG, savSREG reti low: clr display ;Change to low bits out SREG, savSREG reti ;Increment counter & update LED display T1compA: in savSREG, SREG tst running breq done com cntinc ;Toggle for clk_ signal andi cntinc, cntincbit ;mask for the clk_ signal out PORTC, cntinc ldi cntlow, 0xf0 in cntlow, PINA ;Capture low byte of counter com cntlow ; convert for active low LEDs ;ldi cntlow, 0x04 ;out PORTB, cntlow ;Display low byte in cnthigh, PINC ;Capture high 5 bits of counter andi cnthigh, cnthighmask ; mask just the 4 bits com cnthigh ; convert for active low LEDs tst display brne disphigh out PORTB, cntlow ;Display low byte rjmp done disphigh: ;out PORTB, cnthigh ;Display high bits done: out SREG, savSREG reti