Sunday, December 18, 2011

ATTiny.asm

.include "tn2313def.inc"


; Variable Allocations
.def temp = R16
.def mask = R19
.def tmpc = R20

.def ones = R22
.def tens = R23


; zeros in [XH:XL]
; ones in [YH;YL]
; Total in [ZH,ZL]
; These are R27, R26, R29,R28, R31, and R30 respectively. don't use these registers for anything
;else


;********* INIT
; WARNING:: This overlaps the IVT
start: ldi mask,0b11110111 ; [UUUUIOOO]
out DDRB,mask ; port B set up
ldi mask,0b11111111 ; [UUOOOOUU]
out DDRD,mask ; port D set up
ldi mask,0b00001000 ; [UUOOOOUU]
out PORTB,mask ; port D set up



;******** STATE0
state0: ldi XH,0x00 ; ones <-- 1*25 ldi XL,0x19 ldi YH,0x00 ; zeros <-- 1 ldi YL,0x01 whl00: in temp,PINB ; block until 1 ldi mask,0b00001000 and temp,mask breq whl00 ;******** STATE1 :: COUNT THE NUMBER OF ONES whl10: adiw XL,0x0019 ; ONES <- ONES + 25 (Ones * 25) ldi temp,1 ;Delay(smaller is better) d10: nop dec temp brne d10 in temp,PINB ; block until 0 ldi mask,0b00001000 and temp,mask brne whl10 ;******** STATE2 :: COUNT THE NUMBER OF ZER0S whl01: adiw YL,0x0001 ; zeros <- zeros + 1 ldi temp,1 ;Delay(smaller is better) d01: nop dec temp brne d01 in temp,PINB ; block until 1 ldi mask,0b00001000 and temp,mask breq whl01 ;********* STATE 3 :: Calculations ; Shift down YL (the number of zeros/16) clc ror YH ror YL clc ror YH ror YL clc ror YH ror YL clc ror YH ror YL ; Now loop until Z equals [XH,XL] clr ZL clr ZH ldi tmpc,235 ldi mask, 0 RES: add ZL,YL ; calculates the temperature from the ratio of 1's to 0's adc ZH, mask dec tmpc cp ZH,XH brlo RES cp ZL,XL brlo RES ; CONVERT TEMP VALUE TO BCD mov ones,tmpc ;copies tmpc into ones(ones will be actively ;decremented) ldi tens, 0b00000000 ;initializes value of tens to 0 ldi R17, 0x0001 ;creates register 17 with value of 1 ldi R18, 0x000A ;creates register 18 with value of 10 BCD: add tens, R17 ;increments tens by 1 sub ones, R18 ;decrement ones by 10 to account for tens ;incremented cp ones, R18 ;compares ones to register 18 (10) for branch brsh BCD ;if ones >= 10, returns to BCD to repeat
;increment/decrement until ones<10


com tens ;stores the ones complement of tens in tens
out PORTB, tens ;output tens to port b

; OUTPUT THE ONES TO PORT D
com ones
lsl ones ;logical shift ones left 1
lsl ones ;and again This adjusts for the number of LED's
;being different than the number of bits
out PORTD, ones

;********* STATE 4 :: WAIT FOR A ZERO to start again
whl11: in temp,PINB ; block until 0
ldi mask,0b00001000
and temp,mask
brne whl11

rjmp state0

No comments:

Post a Comment