; Kevin Chipalowsky 375281 ; Jennifer Librach 374980 ; ee476 ; Friday section ; FINAL PROJECT .include "8535def.inc" .device AT90S8535 .def savREG0 =r0 .def timer1 =r1 .def savREG1 =r2 .def messL =r3 .def messH =r4 .def saveYL =r5 .def saveYH =r6 .def t0overflow=r7 .def temp =r16 ;temporary register .def keysC =r17 .def keysD =r18 .def pulselength=r19 .def tempI0 =r20 ;temporary register for ISR 0 .def output =r21 .def progcount=r22 ;stores 16-current length in memory when programming .def progtemp=r23 ;temporary register for programming .def state =r24 .def maxlen =r25 ;maximum length of a compared memory region .def temp2 =r26 .equ prescal0=3 ;Timer0 prescal =clk/64 .equ prescal1=1 ;Timer1 prescal =clk/1 .equ T1=exp2(OCIE1A) ;timer1 compare match .equ Timer0=exp2(TOIE0);timer0 .equ nootherkey=0xff .equ keys=0 ;state0 .equ prog=1 ;state1 .equ playmess=2 ;state2 .equ erprog=0xee ;displayed error code .equ ert0 ;displayed error code ;********************************************** ;Buttons Decoded .equ button0=0x77 .equ button1=0x7b .equ button2=0x7d .equ button3=0x7e .equ button4=0xb7 .equ button5=0xbb .equ button6=0xbd .equ button7=0xbe .equ button8=0xd7 .equ button9=0xdb .equ button10=0xdd .equ button11=0xde .equ button12=0xe7 .equ button13=0xeb .equ button14=0xed .equ button15=0xee .equ button16=0x77 .equ button17=0x7b .equ button18=0x7d .equ button19=0x7e .equ button20=0xb7 .equ button21=0xbb .equ button22=0xbd .equ button23=0xbe .equ button24=0xd7 .equ button25=0xdb .equ button26=0xdd .equ button27=0xde .equ button28=0xe7 .equ button29=0xeb .equ button30=0xed .equ progkey=0xee ;************************************** .dseg mess0: .byte 1 ;a sixteen pulse count including a zero terminate mess1: .byte 1 ;a sixteen pulse count including a zero terminate mess2: .byte 1 ;a sixteen pulse count including a zero terminate mess3: .byte 1 ;a sixteen pulse count including a zero terminate mess4: .byte 1 ;a sixteen pulse count including a zero terminate mess5: .byte 1 ;a sixteen pulse count including a zero terminate mess6: .byte 1 ;a sixteen pulse count including a zero terminate mess7: .byte 1 ;a sixteen pulse count including a zero terminate mess8: .byte 1 ;a sixteen pulse count including a zero terminate mess9: .byte 1 ;a sixteen pulse count including a zero terminate mess10: .byte 1 ;a sixteen pulse count including a zero terminate mess11: .byte 1 ;a sixteen pulse count including a zero terminate mess12: .byte 1 ;a sixteen pulse count including a zero terminate mess13: .byte 1 ;a sixteen pulse count including a zero terminate mess14: .byte 1 ;a sixteen pulse count including a zero terminate mess15: .byte 1 ;a sixteen pulse count including a zero terminate mess16: .byte 1 ;a sixteen pulse count including a zero terminate mess17: .byte 1 ;a sixteen pulse count including a zero terminate mess18: .byte 1 ;a sixteen pulse count including a zero terminate mess19: .byte 1 ;a sixteen pulse count including a zero terminate mess20: .byte 1 ;a sixteen pulse count including a zero terminate mess21: .byte 1 ;a sixteen pulse count including a zero terminate mess22: .byte 1 ;a sixteen pulse count including a zero terminate mess23: .byte 1 ;a sixteen pulse count including a zero terminate mess24: .byte 1 ;a sixteen pulse count including a zero terminate mess25: .byte 1 ;a sixteen pulse count including a zero terminate mess26: .byte 1 ;a sixteen pulse count including a zero terminate mess27: .byte 1 ;a sixteen pulse count including a zero terminate mess28: .byte 1 ;a sixteen pulse count including a zero terminate mess29: .byte 1 ;a sixteen pulse count including a zero terminate mess30: .byte 400 ;a sixteen pulse count including a zero terminate ;************************************** .cseg .org $0000 rjmp RESET ;reset entry vector reti ;ext int 0 reti ;ext int 1 reti ;timer 2 compare match reti ;timer 2 overflow reti ;timer 1 capture event rjmp T1cmp ;timer 1 compare match A reti ;timer 1 compare match B reti ;timer 1 overflow rjmp T0ovfl ;timer 0 overflow reti ;serial transfer complete reti ;uart rx complete reti ;uart data empty reti ;uart tx complete reti ;adc convertion complete reti ;eeprom ready reti ;analog comparator RESET: ldi temp, LOW(RAMEND) ;setup stack pointer out SPL, temp ldi temp, HIGH(RAMEND) out SPH, temp ;setup timers ; setup Timer 0 ldi temp, Prescal0 out TCCR0, temp ;set T0 prescaler ldi Temp, timer0 ;enable timer0 interrupt out TIMSK, Temp ldi Temp,0x00 out OCR1AH, Temp ;load timer1 compareH ldi Temp, 100 out OCR1AL, Temp ;load timer1 compareL ldi temp, exp2(CTC1) ;enable clear on compare match ori temp, exp2(CS10) ;load T1 prescaler out TCCR1B, temp ;enable keyboard portsD&C as inputs clr temp out DDRD, temp out DDRC, temp ;enable portB as an output, pinB4 as input and out initially 0 ldi temp, 0xef out DDRB, temp clr temp out PORTB, temp ldi temp, T1 mov timer1, temp ;in a register for use in xor funct ldi state, keys ; state=0 ; is anything pressed state = 0 spin: rcall keypadpoll cpi keysD, progkey breq M_prog cpi keysD, nootherkey ;is a key pressed on padD? brne j2play cpi keysC, nootherkey ;is a key pressed on padC? brne j2play rjmp spin j2play: rjmp M_play M_prog: ldi state, prog in temp, PORTB andi temp, 0x3f ; turn on bits 6&7 when in prog state out PORTB, temp ;wait until prog released, then get another button, then record rcall keypadpoll cpi keysD, 0xff brne M_prog waitp: ;timeout? show LED in prog mode? rcall keypadpoll ldi temp, 0x55 out PORTB, temp cpi keysD, progkey breq spin ; if prog was pressed return to main cpi keysC, button0 brne nexttest0 rjmp prog_B0 nexttest0: cpi keysC, button1 brne nexttest1 rjmp prog_B1 nexttest1: cpi keysC, button2 brne nexttest2 rjmp prog_B2 nexttest2: cpi keysC, button3 brne nexttest3 rjmp prog_B3 nexttest3: cpi keysC, button4 brne nexttest4 rjmp prog_B4 nexttest4: cpi keysC, button5 brne nexttest5 rjmp prog_B5 nexttest5: cpi keysC, button6 brne nexttest6 rjmp prog_B6 nexttest6: cpi keysC, button7 brne nexttest7 rjmp prog_B7 nexttest7: cpi keysC, button8 brne nexttest8 rjmp prog_B8 nexttest8: cpi keysC, button9 brne nexttest9 rjmp prog_B9 nexttest9: cpi keysC, button10 brne nexttest10 rjmp prog_B10 nexttest10: cpi keysC, button11 brne nexttest11 rjmp prog_B11 nexttest11: cpi keysC, button12 brne nexttest12 rjmp prog_B12 nexttest12: cpi keysC, button13 brne nexttest13 rjmp prog_B13 nexttest13: cpi keysC, button14 brne nexttest14 rjmp prog_B14 nexttest14: cpi keysC, button15 brne nexttest15 rjmp prog_B15 nexttest15: cpi keysD, button16 brne nexttest16 rjmp prog_B16 nexttest16: cpi keysD, button17 brne nexttest17 rjmp prog_B17 nexttest17: cpi keysD, button18 brne nexttest18 rjmp prog_B18 nexttest18: cpi keysD, button19 brne nexttest19 rjmp prog_B19 nexttest19: cpi keysD, button20 brne nexttest20 rjmp prog_B20 nexttest20: cpi keysD, button21 brne nexttest21 rjmp prog_B21 nexttest21: cpi keysD, button22 brne nexttest22 rjmp prog_B2 nexttest22: cpi keysD, button23 brne nexttest23 rjmp prog_B23 nexttest23: cpi keysD, button24 brne nexttest24 rjmp prog_B24 nexttest24: cpi keysD, button25 brne nexttest25 rjmp prog_B25 nexttest25: cpi keysD, button26 brne nexttest26 rjmp prog_B26 nexttest26: cpi keysD, button27 brne nexttest27 rjmp prog_B27 nexttest27: cpi keysD, button28 brne nexttest28 rjmp prog_B28 nexttest28: cpi keysD, button29 brne nexttest29 rjmp prog_B29 nexttest29: cpi keysD, button30 brne nexttest30 rjmp prog_B30 nexttest30: rjmp waitp prog_B0:ldi ZL, LOW(mess0) ;shifted because pgm memory is words ldi ZH, HIGH(mess0) ; rjmp record prog_B1:ldi ZL, LOW(mess1) ;shifted because pgm memory is words ldi ZH, HIGH(mess1) ; rjmp record prog_B2:ldi ZL, LOW(mess2) ;shifted because pgm memory is words ldi ZH, HIGH(mess2) ; rjmp record prog_B3:ldi ZL, LOW(mess3) ;shifted because pgm memory is words ldi ZH, HIGH(mess3) ; rjmp record prog_B4:ldi ZL, LOW(mess4) ;shifted because pgm memory is words ldi ZH, HIGH(mess4) ; rjmp record prog_B5:ldi ZL, LOW(mess5) ;shifted because pgm memory is words ldi ZH, HIGH(mess5) ; rjmp record prog_B6:ldi ZL, LOW(mess6) ;shifted because pgm memory is words ldi ZH, HIGH(mess6) ; rjmp record prog_B7:ldi ZL, LOW(mess7) ;shifted because pgm memory is words ldi ZH, HIGH(mess7) ; rjmp record prog_B8:ldi ZL, LOW(mess8) ;shifted because pgm memory is words ldi ZH, HIGH(mess8) ; rjmp record prog_B9:ldi ZL, LOW(mess9) ;shifted because pgm memory is words ldi ZH, HIGH(mess9) ; rjmp record prog_B10:ldi ZL, LOW(mess10) ;shifted because pgm memory is words ldi ZH, HIGH(mess10) ; rjmp record prog_B11:ldi ZL, LOW(mess11) ;shifted because pgm memory is words ldi ZH, HIGH(mess11) ; rjmp record prog_B12:ldi ZL, LOW(mess12) ;shifted because pgm memory is words ldi ZH, HIGH(mess12) ; rjmp record prog_B13:ldi ZL, LOW(mess13) ;shifted because pgm memory is words ldi ZH, HIGH(mess13) ; rjmp record prog_B14:ldi ZL, LOW(mess14) ;shifted because pgm memory is words ldi ZH, HIGH(mess14) ; rjmp record prog_B15:ldi ZL, LOW(mess15) ;shifted because pgm memory is words ldi ZH, HIGH(mess15) ; rjmp record prog_B16:ldi ZL, LOW(mess16) ;shifted because pgm memory is words ldi ZH, HIGH(mess16) ; rjmp record prog_B17:ldi ZL, LOW(mess17) ;shifted because pgm memory is words ldi ZH, HIGH(mess17) ; rjmp record prog_B18:ldi ZL, LOW(mess18) ;shifted because pgm memory is words ldi ZH, HIGH(mess18) ; rjmp record prog_B19:ldi ZL, LOW(mess19) ;shifted because pgm memory is words ldi ZH, HIGH(mess19) ; rjmp record prog_B20:ldi ZL, LOW(mess20) ;shifted because pgm memory is words ldi ZH, HIGH(mess20) ; rjmp record prog_B21:ldi ZL, LOW(mess21) ;shifted because pgm memory is words ldi ZH, HIGH(mess21) ; rjmp record prog_B22:ldi ZL, LOW(mess22) ;shifted because pgm memory is words ldi ZH, HIGH(mess22) ; rjmp record prog_B23:ldi ZL, LOW(mess23) ;shifted because pgm memory is words ldi ZH, HIGH(mess23) ; rjmp record prog_B24:ldi ZL, LOW(mess24) ;shifted because pgm memory is words ldi ZH, HIGH(mess24) ; rjmp record prog_B25:ldi ZL, LOW(mess25) ;shifted because pgm memory is words ldi ZH, HIGH(mess25) ; rjmp record prog_B26:ldi ZL, LOW(mess26) ;shifted because pgm memory is words ldi ZH, HIGH(mess26) ; rjmp record prog_B27:ldi ZL, LOW(mess27) ;shifted because pgm memory is words ldi ZH, HIGH(mess27) ; rjmp record prog_B28:ldi ZL, LOW(mess28) ;shifted because pgm memory is words ldi ZH, HIGH(mess28) ; rjmp record prog_B29:ldi ZL, LOW(mess29) ;shifted because pgm memory is words ldi ZH, HIGH(mess29) ; rjmp record prog_B30:ldi ZL, LOW(mess30) ;shifted because pgm memory is words ldi ZH, HIGH(mess30) ; record: ldi temp, 0xf0 out PORTB, temp RXhigh: sbis PINB,4 ; wait until input goes high rjmp RXhigh RXlow: sbic PINB,4 ; now wait until input goes low again rjmp RXlow in progtemp, PINB andi progtemp, exp2(4); only interested in input from RX ; save beginning of message mov messL, ZL mov messH, ZH ; begin recording ldi temp, timer0 ;enable timer0 out TIMSK, temp ldi temp, 1 out TCNT0, temp ldi progcount, 255 ; initialize length of recording clr t0overflow com t0overflow progloop: out PORTB, progcount in temp, PINB andi temp, exp2(4) ; only interested in input from RX cp temp, progtemp brne change ; change in input tst t0overflow brne progloop ldi temp, 0x01 rjmp storenow change: mov progtemp, temp ; update last value in progtemp in temp, TCNT0 neg temp ; 256-time so that reload will overflow correctly storenow: st Z+, temp ; store width of period clr temp inc temp out TCNT0, temp ; reset timer0 clr t0overflow com t0overflow subi progcount, 1 cpi progcount, 0 brne progloop ; write null terminator clr temp st Z+, temp ; turn off all timers clr temp out TIMSK, temp rjmp spin ; state = 1 play message; not program M_play: ldi state, playmess cpi keysC, button0 brne playtest1 ldi ZL, LOW(mess0) ;shifted because pgm memory is words ldi ZH, HIGH(mess0) ; rjmp playnow playtest1: cpi keysC, button1 brne playtest2 ldi ZL, LOW(mess1) ;shifted because pgm memory is words ldi ZH, HIGH(mess1) ; rjmp playnow playtest2: cpi keysC, button2 brne playtest3 ldi ZL, LOW(mess2) ;shifted because pgm memory is words ldi ZH, HIGH(mess2) ; rjmp playnow playtest3: cpi keysC, button3 brne playtest4 ldi ZL, LOW(mess3) ;shifted because pgm memory is words ldi ZH, HIGH(mess3) ; rjmp playnow playtest4: cpi keysC, button4 brne playtest5 ldi ZL, LOW(mess4) ;shifted because pgm memory is words ldi ZH, HIGH(mess4) ; rjmp playnow playtest5: cpi keysC, button5 brne playtest6 ldi ZL, LOW(mess5) ;shifted because pgm memory is words ldi ZH, HIGH(mess5) ; rjmp playnow playtest6: cpi keysC, button6 brne playtest7 ldi ZL, LOW(mess6) ;shifted because pgm memory is words ldi ZH, HIGH(mess6) ; rjmp playnow playtest7: cpi keysC, button7 brne playtest8 ldi ZL, LOW(mess7) ;shifted because pgm memory is words ldi ZH, HIGH(mess7) ; rjmp playnow playtest8: cpi keysC, button8 brne playtest9 ldi ZL, LOW(mess8) ;shifted because pgm memory is words ldi ZH, HIGH(mess8) ; rjmp playnow playtest9: cpi keysC, button9 brne playtest10 ldi ZL, LOW(mess9) ;shifted because pgm memory is words ldi ZH, HIGH(mess9) ; rjmp playnow playtest10: cpi keysC, button10 brne playtest11 ldi ZL, LOW(mess10) ;shifted because pgm memory is words ldi ZH, HIGH(mess10) ; rjmp playnow playtest11: cpi keysC, button11 brne playtest12 ldi ZL, LOW(mess11) ;shifted because pgm memory is words ldi ZH, HIGH(mess11) ; rjmp playnow playtest12: cpi keysC, button12 brne playtest13 ldi ZL, LOW(mess12) ;shifted because pgm memory is words ldi ZH, HIGH(mess12) ; rjmp playnow playtest13: cpi keysC, button13 brne playtest14 ldi ZL, LOW(mess13) ;shifted because pgm memory is words ldi ZH, HIGH(mess13) ; rjmp playnow playtest14: cpi keysC, button14 brne playtest15 ldi ZL, LOW(mess14) ;shifted because pgm memory is words ldi ZH, HIGH(mess14) ; rjmp playnow playtest15: cpi keysC, button15 brne playtest16 ldi ZL, LOW(mess15) ;shifted because pgm memory is words ldi ZH, HIGH(mess15) ; rjmp playnow playtest16: cpi keysD, button16 brne playtest17 ldi ZL, LOW(mess16) ;shifted because pgm memory is words ldi ZH, HIGH(mess16) ; rjmp playnow playtest17: cpi keysD, button17 brne playtest18 ldi ZL, LOW(mess17) ;shifted because pgm memory is words ldi ZH, HIGH(mess17) ; rjmp playnow playtest18: cpi keysD, button18 brne playtest19 ldi ZL, LOW(mess18) ;shifted because pgm memory is words ldi ZH, HIGH(mess18) ; rjmp playnow playtest19: cpi keysD, button19 brne playtest20 ldi ZL, LOW(mess19) ;shifted because pgm memory is words ldi ZH, HIGH(mess19) ; rjmp playnow playtest20: cpi keysD, button20 brne playtest21 ldi ZL, LOW(mess20) ;shifted because pgm memory is words ldi ZH, HIGH(mess20) ; rjmp playnow playtest21: cpi keysD, button21 brne playtest22 ldi ZL, LOW(mess21) ;shifted because pgm memory is words ldi ZH, HIGH(mess21) ; rjmp playnow playtest22: cpi keysD, button22 brne playtest23 ldi ZL, LOW(mess22) ;shifted because pgm memory is words ldi ZH, HIGH(mess22) ; rjmp playnow playtest23: cpi keysD, button23 brne playtest24 ldi ZL, LOW(mess23) ;shifted because pgm memory is words ldi ZH, HIGH(mess23) ; rjmp playnow playtest24: cpi keysD, button24 brne playtest25 ldi ZL, LOW(mess24) ;shifted because pgm memory is words ldi ZH, HIGH(mess24) ; rjmp playnow playtest25: cpi keysD, button25 brne playtest26 ldi ZL, LOW(mess25) ;shifted because pgm memory is words ldi ZH, HIGH(mess25) ; rjmp playnow playtest26: cpi keysD, button26 brne playtest27 ldi ZL, LOW(mess26) ;shifted because pgm memory is words ldi ZH, HIGH(mess26) ; rjmp playnow playtest27: cpi keysD, button27 brne playtest28 ldi ZL, LOW(mess27) ;shifted because pgm memory is words ldi ZH, HIGH(mess27) ; rjmp playnow playtest28: cpi keysD, button28 brne playtest29 ldi ZL, LOW(mess28) ;shifted because pgm memory is words ldi ZH, HIGH(mess28) ; rjmp playnow playtest29: cpi keysD, button29 brne playtest30 ldi ZL, LOW(mess29) ;shifted because pgm memory is words ldi ZH, HIGH(mess29) ; rjmp playnow playtest30: cpi keysD, button30 brne playtest31 ldi ZL, LOW(mess30) ;shifted because pgm memory is words ldi ZH, HIGH(mess30) ; rjmp playnow playtest31: rjmp spin playnow: ; remember message address so that we can repeat it if requested mov messL, ZL mov messH, ZH messageplay: clr temp out PORTB, temp ld pulselength, Z+ out PORTB, pulselength out TCNT0, pulselength mov temp, timer1 ;enable timer1 ori temp, timer0 ;enable timer0 out TIMSK, temp sei ; keypadpoll while playing rcall keypadpoll clr t0overflow playloop: tst pulselength brne playloop ; branch to main if zero terminate reached ; do we play it again? cpi keysD, 0xff brne playagain cpi keysC, 0xff brne playagain ; stop playing clr temp out TIMSK, temp rjmp spin playagain: ; reset message mov ZL, messL mov ZH, messH ; pulse for external trigger on scope in temp, PORTB ori temp, 0x01 out PORTB, temp rjmp messageplay ;************************************************************************* ;ISRs T0ovfl: in savREG0, sREG cpi state, prog breq progoverflow ld pulselength, Z+ out TCNT0, pulselength ; if the previous pulse length was an overflow, then don't invert tst t0overflow brne afterinvert in tempI0, TIMSK eor tempI0, timer1 ;Toggle timer1 enable out TIMSK, tempI0 afterinvert: ; if the recorded pulse length was an overflow, then don't ; toggle timer1 after this pulse clr t0overflow cpi pulselength, 0x01 brne nooverflow com t0overflow nooverflow: ; generate "cleaned up" waveform on pin 2 0 and tempI0, timer1 cp tempI0, timer1 breq done ldi tempI0, 0x02 out PORTB, tempI0 done: out sREG, savREG0 reti progoverflow: ; overflowed during programming, so stop the wait loop clr t0overflow rjmp done T1cmp: in savREG1, sREG com output andi output, exp2(5) out PORTB, output out sREG, savREG1 reti ============================================= ; compare memory regions pointed to by Z and Y and of maximum length maxlen ; bytes are the same if they are within 1. ; If maxlen is returned as 0, the two memory regions are the same. comparemem: ; save old Z and Y mov messL, ZL mov messH, ZH mov saveYL, YL mov saveYH, YH compareagain: ld temp, Z+ ld temp2, Y+ sub temp, temp2 brsh alreadypositive neg temp alreadypositive: cpi temp, 2 ; todo: if temp is less than 2, they are the same, compare next byte brsh return ; count off another byte compared. dec maxlen brne compareagain ; restore pointers and return return: mov ZL, messL mov ZH, messH mov YL, saveYL mov YH, saveYH ret |