?? testhz.asm
字號:
;測量脈沖頻率
;脈沖從PD5(T1)輸入,4位數(shù)碼管動態(tài)掃描顯示脈沖頻率,T/C1定于外計數(shù)方式,每上升沿計數(shù)1次;
;定時器T/C0每隔1s求出T/C1的增加量即為脈沖頻率.
;PCB:AVR.DDB\testHz.sch
;CPU:ATmega8,fosc=8M
.include "m8def.inc"
.def T0cnt = r5
.def temp = r16 ;暫存器
.def T0tmp = r17 ;t0中斷暫存器
;-------------------------------------
.org 0x0000
rjmp reset
.org 0x0009 ;timer0中斷
rjmp time0
;================================================================
.org 0x0020 ;跳過中斷區(qū)
;-------------------------------------------------------------
;timer0溢出中斷子程序
;每隔1s計數(shù)一次,并將二次差值送0x61:0x60,應用寄存器:R10,R11,R12,R13
time0: dec T0cnt
breq t0lp1 ;采樣時長125*8ms=1s
ldi T0tmp,6 ;重置T0初值
out TCNT0,T0tmp
reti
t0lp1: brts t0lp2 ;判斷計數(shù)次數(shù)標志位,1:末次,0:首次
in r10,TCNT1L ;讀TCNT1計數(shù)值到r11:r10
in r11,TCNT1H
set
ldi T0tmp,125 ;計數(shù)125次,125*8ms=1s
mov T0cnt,T0tmp
ldi T0tmp,6 ;重置T0初值
out TCNT0,T0tmp
reti
t0lp2: in r12,TCNT1L ;讀TCNT1計數(shù)值到r13:r12
in r13,TCNT1H
clt
clc
sub r12,r10 ;求2次T/C1計數(shù)差值
sbc r13,r11
sts 0x60,r12 ;將計數(shù)差值r13:r12轉存到0x61:0x60
sts 0x61,r13
rcall sysint
reti
;******************************************************
reset: ldi temp,low(ramend) ;設置堆棧指針
out SPL,temp
ldi temp,high(ramend)
out SPH,temp
ldi temp,0xFF ;B,C口為輸出
out DDRB,temp
out DDRC,temp
ldi temp,0xDF ;PD5為輸入,其它為輸出
out DDRD,temp
ldi temp,0x00
out portb,temp ;B,C,D口初值為0
out portc,temp
out portd,temp
rcall clrRAM
rcall sysint
sei
loop: rcall getNum
rcall scanLED
rjmp loop
;---------------------------------------------------------------------------
sysint: clt ;初始化
ldi temp,0x01 ;允許T0中斷
out TIMSK,temp
ldi temp,125 ;計數(shù)125次,125*8ms=1s
mov T0cnt,temp
ldi temp,6 ;設T0定時初值,8ms,8M,256分頻
out TCNT0,temp
ldi temp,0x04 ;T/C0 256分頻
out TCCR0,temp
ldi temp,0x00
out TCNT1H,temp
out TCNT1L,temp
ldi temp,0x06 ;啟動T1計數(shù)器,外部T1輸入,下降沿計數(shù)
out TCCR1B,temp
ret
;-------------------------------------------------------
;RAM存儲區(qū)0x60—0x7F共32個字節(jié)清0
clrRAM: ldi temp,0x60
mov r26,temp
ldi temp,32
mov r1,temp
clrlp: clr r0
st x+,r0
dec r1
brne clrlp
ret
;----------------------------------------------------------
;轉換數(shù)據(jù),將16進制數(shù)(60H—61H)分解成一位,存儲于70H—73H
getNum:
lds temp,0x60
andi temp,0x0F ;取低8位
sts 0x70,temp ;個位,數(shù)據(jù)送70H-73H
lds temp,0x60
swap temp ;取高8位
andi temp,0x0F
sts 0x71,temp ;十位
lds temp,0x61
andi temp,0x0F ;取低8位
sts 0x72,temp ;百位
lds temp,0x61
swap temp ;取高8位
andi temp,0x0F
sts 0x73,temp ;千位
ret
;---------------------------------------------------
;動態(tài)掃描4位共陽數(shù)碼管顯示(74LS138控制位碼)
;共陽數(shù)碼管,PB1—PB5,PC3—PC5口作段控,PC0—PC2作位控,每隔1ms顯示1位,
;待顯示的16進制數(shù)存儲在0x70-0x73中,
scanLED:
lds temp,0x70 ;取個位數(shù)據(jù)
rcall findLED ;查段碼
cbi portc,0 ;送個位位碼,低電平有效
cbi portc,1
cbi portc,2
rcall t1ms ;延時1ms
lds temp,0x71
rcall findLED
sbi portc,0 ;送十位位線
cbi portc,1
cbi portc,2
rcall t1ms
lds temp,0x72
rcall findLED
cbi portc,0 ;送百位位線
sbi portc,1
cbi portc,2
rcall t1ms
lds temp,0x73
rcall findLED
sbi portc,0 ;送千位位線
sbi portc,1
cbi portc,2
rcall t1ms
ret
;----------------------------
t1ms: ;8M,延時1ms
ldi r24,11
d21: ldi r25,242
d20: dec r25
brne d20
dec r24
brne d21
ret
;---------------------------------------------------
LED0: rjmp code0
LED1: rjmp code1
LED2: rjmp code2
LED3: rjmp code3
LED4: rjmp code4
LED5: rjmp code5
LED6: rjmp code6
LED7: rjmp code7
LED8: rjmp code8
LED9: rjmp code9
LEDA: rjmp codeA
LEDB: rjmp codeB
LEDC: rjmp codeC
LEDD: rjmp codeD
LEDE: rjmp codeE
LEDF: rjmp codeF
findLED:
cpi temp,0 ;多項選擇
breq LED0
cpi temp,1
breq LED1
cpi temp,2
breq LED2
cpi temp,3
breq LED3
cpi temp,4
breq LED4
cpi temp,5
breq LED5
cpi temp,6
breq LED6
cpi temp,7
breq LED7
cpi temp,8
breq LED8
cpi temp,9
breq LED9
cpi temp,10
breq LEDA
cpi temp,11
breq LEDB
cpi temp,12
breq LEDC
cpi temp,13
breq LEDD
cpi temp,14
breq LEDE
cpi temp,15
breq LEDF
;----------------------------------------
code0: cbi portb,1
cbi portb,2
cbi portb,3
cbi portb,4
sbi portb,5
sbi portc,3
cbi portc,4
cbi portc,5
ret
code1: cbi portb,1
sbi portb,2
sbi portb,3
sbi portb,4
sbi portb,5
sbi portc,3
cbi portc,4
sbi portc,5
ret
code2: sbi portb,1
cbi portb,2
cbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
sbi portc,5
ret
code3: cbi portb,1
cbi portb,2
cbi portb,3
sbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
sbi portc,5
ret
code4: cbi portb,1
sbi portb,2
sbi portb,3
sbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
cbi portc,5
ret
code5: cbi portb,1
cbi portb,2
cbi portb,3
sbi portb,4
cbi portb,5
sbi portc,3
sbi portc,4
cbi portc,5
ret
code6: cbi portb,1
cbi portb,2
cbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
sbi portc,4
cbi portc,5
ret
code7: cbi portb,1
sbi portb,2
cbi portb,3
sbi portb,4
sbi portb,5
sbi portc,3
cbi portc,4
sbi portc,5
ret
code8: cbi portb,1
cbi portb,2
cbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
cbi portc,5
ret
code9: cbi portb,1
cbi portb,2
cbi portb,3
sbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
cbi portc,5
ret
codeA: cbi portb,1
sbi portb,2
cbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
cbi portc,5
ret
codeB: cbi portb,1
cbi portb,2
sbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
sbi portc,4
cbi portc,5
ret
codeC: sbi portb,1
cbi portb,2
cbi portb,3
cbi portb,4
sbi portb,5
sbi portc,3
sbi portc,4
cbi portc,5
ret
codeD: cbi portb,1
cbi portb,2
sbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
cbi portc,4
sbi portc,5
ret
codeE: sbi portb,1
cbi portb,2
cbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
sbi portc,4
cbi portc,5
ret
codeF: sbi portb,1
sbi portb,2
cbi portb,3
cbi portb,4
cbi portb,5
sbi portc,3
sbi portc,4
cbi portc,5
ret
;----------------------------------------
dly10d: ldi r20,8
d54: ldi r21,31
d53: ldi r22,100
d52: ldi r23,255
d51: ldi r24,252
d50: dec r24
brne d50
dec r23
brne d51
dec r22
brne d52
dec r21
brne d53
dec r20
brne d54
ret
dly1s: ldi r20,41
d32: ldi r21,255
d31: ldi r22,255
d30: dec r22
brne d30
dec r21
brne d31
dec r20
brne d32
ret
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -