Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .include "m2560def.inc"
- .def temp = r16
- .def counterP0 = r17
- .def counterP1 = r18
- .def leds = r19
- .def displayCount = r20
- .equ displayLength = 6
- .equ false = 0
- .equ true = 1
- .macro twoByteClear
- ldi YL, low(@0)
- ldi YH, high (@0)
- clr temp
- st Y+, temp
- st Y, temp
- .endmacro
- .dseg
- pattern0: .byte 1
- pattern1: .byte 1
- tempcounter: .byte 2
- Timer1Counter: .byte 1
- Timer3Counter: .byte 1
- PB0Status: .byte 1
- PB1Status: .byte 1
- .cseg
- .org 0x0000
- jmp RESET
- .org INT0addr
- jmp PB0Press
- .org INT1addr
- jmp PB1Press
- jmp DEFAULT
- .org OVF1addr
- jmp TIMER1OVF
- jmp DEFAULT
- .org OVF0addr
- jmp TIMER0OVF
- .org OVF3addr
- jmp TIMER3OVF
- DEFAULT:
- reti
- RESET:
- ldi temp, high(RAMEND)
- out sph, temp
- ldi temp, low(ramend)
- out spl, temp
- clr temp
- out ddrd, temp //first two pins of port d ( RDx3 and RDx4 on the board) are mapped to input buttons; set port d as input
- ser temp
- out ddrc,temp //set port c as output
- clr temp
- sts PB0Status, temp
- sts PB1Status, temp
- clr temp
- clr leds
- clr counterP0
- clr counterP1
- clr displayCount
- sts pattern0, temp
- sts pattern1, temp
- rjmp main
- PB0Press:
- push temp
- in temp, sreg
- push temp //pushing sreg
- push YL
- push YH
- lds temp, PB0Status
- cpi temp, 1
- breq PB0Press_end
- ldi temp,1
- sts PB0Status, temp
- cpi counterP0, 8 //check if pattern0 is full yet
- breq PB0Press_end //if it is, check pattern1
- lds temp, pattern0 //otherwise, load pattern0
- lsl temp //left shift pattern to store most recent entry (0) as LSB
- sts pattern0, temp
- inc counterP0
- PB0Press_end:
- pop YH
- pop YL
- pop temp //popping sreg
- out sreg, temp
- pop temp
- reti
- PB1Press:
- push temp
- in temp, sreg
- push temp //pushing sreg
- push YL
- push YH
- lds temp, PB1Status
- cpi temp, 1
- breq PB1Press_end
- ldi temp,1
- sts PB1Status, temp
- cpi counterP0, 8 //check if pattern0 is full yet
- breq PB1Press_end //if it is, check pattern1
- lds temp, pattern0 //otherwise, load pattern0
- lsl temp //left shift pattern to store most recent entry (0) as LSB
- inc temp
- sts pattern0, temp
- inc counterP0
- PB1Press_end:
- pop YH
- pop YL
- pop temp //popping sreg
- out sreg, temp
- pop temp
- reti
- Timer0OVF:
- in temp, sreg
- push temp
- push yh
- push yl
- push XH
- push XL
- cpi leds, 0x00 //check if the leds are empty
- brne doNothing //if they are not, do nothing
- cpi counterP0, 8 //check if the pattern0 is full
- brne doNothing
- lds leds, pattern0 //if it is full, load it to leds
- clr counterP0 //clear counter P0, so it is ready for new input
- doNothing:
- lds XL,tempcounter
- lds XH,tempcounter+1
- adiw X,1
- cpi XL, low(7812)
- ldi temp, high(7812)
- cpc XH, temp
- brne notASecond
- next:
- cpi leds, 0x00 //check there is a value loaded in the leds
- breq noDisplay //if there isnt, then nothing will be displayed, keep counting
- cpi displayCount, displayLength //check if there have been previous displays
- brge alreadyDisplayed //if there is, clear display and continue to nodisplay
- mov temp, displayCount //copy over to temp
- andi temp, 0b00000001 //mask display to just the LSB
- cpi temp, 0 //check if the LSB is 0. if it is, number is even -> turn on
- breq display //if it is , go to display to turn on leds
- clr temp //otherwise, if the number is odd
- out PORTC,temp //turn off leds
- inc displayCount //incrments counter
- rjmp NoDisplay
- display:
- out PORTC, leds //turn on leds
- inc displayCount //increment displayCOunt
- rjmp noDisplay //proceed to clear tempcounter
- alreadyDisplayed:
- clr displayCount
- clr leds
- rjmp noDisplay
- noDisplay:
- twoByteClear tempcounter
- rjmp epilogue
- notASecond:
- sts tempcounter, XL
- sts tempcounter+1,XH
- epilogue:
- pop XL
- pop XH
- pop YL
- pop YH
- pop temp
- out sreg,temp
- reti
- TIMER1OVF: //this timer will overfloew every ~30ms with a prescalar of 8 for 16-bit timer
- push temp //
- in temp, sreg
- push temp
- push yh
- push yl
- lds temp, PB0Status
- cpi temp, 1
- breq new501
- new501:
- lds YL, Timer1Counter
- ldi YH, 0
- adiw Y,1
- cpi YL, low(25)
- ldi temp, high(25)
- cpc temp, YH
- brne NOt501
- clr temp
- sts PB0Status, temp
- sts Timer1Counter, temp
- clr YL
- clr YH
- rjmp timer1end
- NOt501:
- sts Timer1Counter, YL
- timer1end:
- pop YL
- pop YH
- pop temp
- out sreg, temp
- pop temp
- reti
- TIMER3OVF: //this timer will overfloew every ~30ms with a prescalar of 8 for 16-bit timer
- push temp //
- in temp, sreg
- push temp
- push yh
- push yl
- lds temp, PB1Status
- cpi temp, 1
- breq new503
- new503:
- lds YL, Timer3Counter
- ldi YH, 0
- adiw Y,1
- cpi YL, low(25)
- ldi temp, high(25)
- cpc temp, YH
- brne NOt503
- clr temp
- sts PB1Status, temp
- sts Timer3Counter, temp
- clr YL
- clr YH
- rjmp timer3end
- NOt503:
- sts Timer3Counter, YL
- timer3end:
- pop YL
- pop YH
- pop temp
- out sreg, temp
- pop temp
- reti
- main:
- twoByteClear tempCounter
- ldi temp, 0b00000000
- out tccr0a,temp
- ldi temp,0b00000010
- out tccr0b, temp
- ldi temp, 1<< toie0
- sts timsk0,temp
- ldi temp, 0b00000011
- out eimsk, temp //external interrupt mask register, 2 LSB are for int0 and int1. enabling this and global interrupts enables the interrupt pin
- ldi temp, 0b00001010
- sts eicra,temp //enable falling edge, with ISCn1 = 1, ISCn0 = 0, for falling edge of INTn. eicra is for INT3:0. using sts as its a register
- ldi temp, 0b00000000 //counter in normal mode (UP), rolls over at
- sts TCCR1A, temp //maximum 16 bit value and restarts from bottom
- ldi temp, 0b00000010 //overflow at 2^16 * 8/16 = 32768microseconds, with 8 prescalar
- sts TCCR1B, temp
- ldi temp, 1<<TOIE1 //enable timer1
- sts timsk1, temp
- ldi temp, 0b00000000 //counter in normal mode (UP), rolls over at
- sts TCCR3A, temp //maximum 16 bit value and restarts from bottom
- ldi temp, 0b00000010 //overflow at 2^16 * 8/16 = 32768microseconds, with 8 prescalar
- sts TCCR3B, temp
- ldi temp, 1<<TOIE3 //enable timer1
- sts timsk3, temp
- ldi temp, true
- sts PB0Status, temp
- sei
- end:
- rjmp end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement