⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testhz.asm

📁 AVR频率计
💻 ASM
字号:
;测量脉冲频率
;脉冲从PD5(T1)输入,4位数码管动态扫描显示脉冲频率,T/C1定于外计数方式,每上升沿计数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		;跳过中断区
;-------------------------------------------------------------
;timer0溢出中断子程序
;每隔1s计数一次,并将二次差值送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		;判断计数次数标志位,1:末次,0:首次
	in	r10,TCNT1L	;读TCNT1计数值到r11:r10
	in	r11,TCNT1H
	set
	ldi	T0tmp,125	;计数125次,125*8ms=1s
	mov	T0cnt,T0tmp	
	ldi	T0tmp,6		;重置T0初值
	out	TCNT0,T0tmp
	reti

t0lp2:	in	r12,TCNT1L	;读TCNT1计数值到r13:r12
	in	r13,TCNT1H
	clt
	clc
	sub	r12,r10		;求2次T/C1计数差值
	sbc	r13,r11
	sts	0x60,r12	;将计数差值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		;计数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计数器,外部T1输入,下降沿计数
	out	TCCR1B,temp
	ret

;-------------------------------------------------------
;RAM存储区0x60—0x7F共32个字节清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		
;----------------------------------------------------------
;转换数据,将16进制数(60H—61H)分解成一位,存储于70H—73H
getNum:	
	lds	temp,0x60
	andi	temp,0x0F	;取低8位
	sts	0x70,temp	;个位,数据送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
;---------------------------------------------------
;动态扫描4位共阳数码管显示(74LS138控制位码)
;共阳数码管,PB1—PB5,PC3—PC5口作段控,PC0—PC2作位控,每隔1ms显示1位,
;待显示的16进制数存储在0x70-0x73中,
scanLED:
	lds	temp,0x70	;取个位数据
	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 + -