Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;---------------------------------------------------
- ;These timing routines worked (mostly) . There's interupt driven ones and polling. Finally
- ; they come up with about the same number of ticks for 1sec Arduino sq wave. Need tidying.
- ;----------some timer0 routines---------------------------
- blinkTimer:
- rcall setUp
- ;rcall showCounters
- rcall waitForPinHigh
- ;rcall showCounters
- rcall waitForPinLow
- ;inc r17
- ;rcall showCounters
- rcall startTim0
- rcall chkInp
- rcall stopTim0
- rcall showCounters
- ; rcall waitForever
- rjmp blinkTimer
- ;--------------------------------------------
- setUp:
- CBI DDRB,1 ;clr PORTB1 FOR inPUT
- clr r17
- clr r18
- clr r19 ;counters
- ;clr r16
- out TCNT0,r17 ;always start with clean count
- ret
- ;----------------------------------------------
- startTim0:
- LDI r16,0b0000_0101 ;SET TIMER PRESCALER TO /1024, 03 is /64
- OUT TCCR0B,r16
- ret ;with timer now started
- ;-----------------------------------------------
- stopTim0:
- LDI r16,0b0000_0000 ;Stop TIMER
- OUT TCCR0B,r16
- ret ;with timer now stopped
- ;----------------------------------------------------------
- waitForPinHigh:
- sbis PINB,1
- rjmp waitForPinHigh
- ret ;when pin PB1 goes high
- ;--------------------------------------------------
- waitForPinLow:
- ; ldi zl,0x36
- ; clr zh
- ; ld r16,z
- ;rcall d16
- ; rcall spacecode
- sbic PINB,1
- rjmp waitForPinLow
- ret ;when pin PB1 goes low
- ;-------------------------------------
- chkInp: ;main loop. Come here after pin gone low
- sbic PINB,1 ;loop until pin PB1 goes high
- rjmp outci
- in r16,TIFR ;TOV0 goes high when TCNT0 overflows
- andi r16, 0b0000_0010 ;TOV0
- breq chkInp ;mostly take this branch
- overflow:
- ldi r16,0b0000_0010
- out TIFR,r16 ;push TOV0 flag back down by writng 1 to it.
- inc r17 ;overflow of TCNT0, therefore, click counters
- brne chkInp ;r17 not overflowing so chk pin all over again
- inc r18 ;if r17 becomes ff +1 click r18
- brne chkInp ;no overflow so start again with loop
- inc r19 ;sometimes, might need this for very long delays.
- rjmp chkInp ;if r19 overflows, bad luck, do nothing
- outci:
- ret ;with counters full but need to stop clock soon
- ;-----------------------------------------
- showCounters: ;after clock has stopped need to see their values
- rcall CR
- in r16,TCNT0
- ;show r16,r17
- rcall d1617
- rcall space
- movw r16,r18
- ;show r16,r17
- rcall d1617
- ret ; with TCNT0,r17,18,19 all showing.
- ;--------------------------------------------------
- waitForever:
- nop
- rjmp waitForever
- ret ;never taken. Jump on spot
- ;---------------------------------------------
- wdscode: ;list just a few words for testing purposes
- push r16
- push r17
- push r22
- push r23
- push r24
- push r6
- pushz
- ldi r16,$0c ;r6 is counter for words
- mov r6,r16 ;stop after 12 words. Best for testing.
- rcall doLatest ;get first link into v
- upwrd:
- rcall jmpNextWord ;pnt to link part of next word
- lpm r23,z+
- lpm r22,z+ ;store link into v=r23,24
- lpm r16,z+ ;get len
- andi r16,$0f ;don't want eg $85 to be len when it means immediate len 5.
- clr r17 ;need eg 0006 on stk not 06 later
- mypush2 r16,r17 ;len byte now on mystk
- ;at this stage z points to the start of word name
- mypush2 zl,zh ;flash start adr of string now on mystack
- rcall swapp ; but wrong way round. Want len = TOS
- rcall Sdot ;print the string on the term
- rcall spacecode ;but add space after each word
- dec r6 ;different from 'words'. Stop after 5
- breq outwds
- tst vl
- brne upwrd ;if vl:vh = r23,24 = 0000 finish
- tst vh
- brne upwrd
- outwds:
- popz
- pop r6
- pop r24
- pop r23
- pop r22
- pop r17 ;TODO macro with multiple pops & pushes
- pop r16
- ret ;with all the words in dic printed
- ;-----------------------
- test_strout:
- rcall strout
- .dw $05
- .db "abcde"
- ret
- ;---------------------------------------------
- insertreti: ;semireti has to end new word with reti = $9518 opcode
- pushx ;both xl,xh saved for later
- movw xl,myhere ;myhere points to next available spot in ram dic
- ldi r16,$18
- st x+,r16 ;$18 part goes first
- ldi r16,$95
- st x+,r16 ;ret now in ram. Just tidy pointers
- movw myhere,xl
- popx ;so x back where it was and reti inserted.
- ret
- ;----------------------------------
- interrupt_0: ;experiment for interrupts
- ;global interrupt enable
- lds r16, $005b ;set PCIE, bit 5 of GMSK
- ori r16,0b0010_0000 ; in order to enable pin change ints
- sts $005b,r16 ;pin changes now enable
- sbi PCMSK,01 ;enable PINB1 for pin change int
- ;assume the vector for pin change interrupts is pointing to ISR yhat ..
- ; ends with reti. Then, when this is run we should see that routine invoked when pin changes.
- sei
- ret
- herei0:
- rjmp herei0
- ;----------------------------
- testT0_ISR0: ;take out later
- inc r18
- brne downt0
- inc r19
- brne downt0
- inc r20
- ;takemeout 'I'
- downt0:
- reti
- ;------------------------------------
- startT0_0: ;just experimenting with getting T0 interrupts
- sei ;need global int
- lds r16,$0059 ;0x39=TMSK(io), bit 1 controls timer0 overflow int
- ori r16,0b000_0010 ;bit 1 =1 => t0 over int enabled
- sts $0059, r16
- rcall interrupt_0 ;set up pinchange interrupt
- ldi zl,$60
- ldi zh,0 ;x points to buf1. Going to store values there
- CBI DDRB,1 ;clr PORTB1 FOR inPUT
- clr r17
- clr r18
- clr r19 ;counters
- out TCNT0,r17 ;always start with clean count
- ;startTim0:
- LDI r16,0b0000_0101 ;SET TIMER PRESCALER TO /1024, 03 is /64
- OUT TCCR0B,r16
- ;things have started and ISR will kick in every overflow. Plan: watch r18. It should
- ; .. climb to 0x20 about every second with 8Mhz clock and 1024 prescale.
- ;so if r18 =0x20, do something, like output a char. Reset counters too.
- ;takemeout 'A'
- chkr18:
- tst r6 ;is there a new val
- breq chkr18
- clr r6 ;if so print it (about once per sec)
- ld r16,z
- mov r17,r6
- ; rcall qmark
- ; rcall d1617
- nop
- rjmp chkr18
- ret ; never taken
- ;------------------------------------------------
- pcISR2: ;pin change interrupt comes here for ISR
- ldi r16,$01
- mov r6,r16 ;a flag. There's a new value.
- lds r16,$0052 ;get TCNT0
- mov r17,r18 ;save where we got to do TCNT0 display later
- clr r18
- clr r19
- sts $0052,r18 ;clr TCNT0
- rcall d1617 ;show count
- rcall space
- reti
- ;----------------------------------------------
- TOVO_ISR:
- ; cli
- inc r5
- ; cpi r18,$20
- ; breq showT
- ; ldi r16,0b0000_0010
- ; out TIFR,r16 ;push TOV0 flag back down by writng 1 to it.
- rjmp finT
- ; sei
- showT:
- ;rcall OK
- finT:
- reti
- ;--------------------------------------
- PC_change_ISR:
- ; cli
- rcall stopTim0
- sts $0070,r5 ;save the val of num of TOVOs
- in r16,TCNT0
- sts $0071,r16
- clr r5
- sts $0072,r5 ;flag = 0 then there's a pin change
- ; sts $0052,r18 ;clr counter0
- ; rcall qmark
- ; mov r17,r18
- ; in r16,TCNT0
- ; mov r16,r18
- ; rcall d16
- ; rcall d16
- ; rcall d1617
- ; rcall space
- clr r5
- out TCNT0,r5
- rcall startTim0
- ; clr r19
- ; inc r19 ;flag
- ; sei
- reti
- ;---------------------------------------------
- quickT0: ;trying to get fastest int driven timer
- ;rcall qmark
- ; lds r16,$0071 ;TCNT0
- ; rcall d16
- ldi r16,1
- sts $0072,r16 ;flag
- rcall setupqt
- ; in r16,TCNT0
- ; rcall d16
- lds r16,$0071
- rcall d16
- ldi r16,0
- out TCNT0,r16
- loopqt:
- ; tst r19
- lds r16,$0072 ;flag
- cpi r16,0
- brne loopqt
- ; brne loopqt
- ; cli
- lds r16,$0070
- rcall d16
- ;rcall d16
- ;rcall d16
- ;rcall space
- ; rcall oneBitTime
- ; rcall qmark
- ; clr r19 ;push Pchange int flag back down
- ; sei
- rjmp quickT0
- ret
- ;----------------------------------------
- setupqt:
- ; sei ;need global int
- lds r16,$0059 ;0x39=TMSK(io), bit 1 controls timer0 overflow int
- ori r16,0b000_0010 ;bit 1 =1 => t0 over int enabled
- sts $0059, r16
- rcall interrupt_0 ;set up pinchange interrupt
- ret
Advertisement
Add Comment
Please, Sign In to add comment