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

📄 seg7.asm

📁 ATMEGA16使用的5位数码管动态扫描程序。
💻 ASM
字号:
;FastAVR Basic Compiler, ver.4.0.1, by MicroDesign
;Created 14:13:44, 02-09-2006
;
.include	"C:\FastAVR\inc\m16def.inc"
;
		.DSEG
n:						.byte	 2
a:						.byte	 1
i:						.byte	 1
dxs:					.byte	 5
bcd_1:					.byte	 1
bcd_2:					.byte	 1
bcd_3:					.byte	 1
bcd_4:					.byte	 1
bcd_5:					.byte	 1
		
		.CSEG
.ORG	0
		rjmp	_Reset
.ORG		INT0addr
		reti
.ORG		INT1addr
		reti
.ORG		OC2addr 
		reti
.ORG		OVF2addr
		reti
.ORG		ICP1addr
		reti
.ORG		OC1Aaddr
		reti
.ORG		OC1Baddr
		reti
.ORG		OVF1addr
		reti
.ORG		OVF0addr
		reti
.ORG		SPIaddr 
		reti
.ORG		URXCaddr
		reti
.ORG		UDREaddr
		reti
.ORG		UTXCaddr
		reti
.ORG		ADCCaddr
		reti
.ORG		ERDYaddr
		reti
.ORG		ACIaddr 
		reti
.ORG		TWIaddr 
		reti
.ORG		INT2addr
		reti
.ORG		OC0addr 
		rjmp	IntN19

_Reset:
		ldi		yl,low(RAMEND)
		out		SPL,yl
		ldi		yh,high(RAMEND)
		out		SPL+1,yh
		sbiw	yl,32


		ldi		zl,8
		out		TCCR0,zl

;******	USERS BASIC CODE **********************

;-Line--0031----DDRB=255--
		ldi		zl,low(255)
		out		ddrb,zl

;-Line--0032----DDRA=DDRA Or &b11110000--
		in		r24,DDRA
		ldi		zl,low(240)
		or		zl,r24
		out		ddra,zl

;-Line--0034----Enable Interrupts          '允许全局中断--
		sei

;-Line--0035----ocr0=&h3A                  'timer0比较匹配中断时间=2ms--
		ldi		zl,low(58)
		out		ocr0,zl

;-Line--0036----Enable Oc0                 '允许timer0比较匹配中断--
		in		zl,TIMSK
		sbr		zl,0x02
		out		TIMSK,zl

;-Line--0037----Start Timer0               '启动定时器0--
		in		zl,TCCR0
		sbr		zl,4
		out		TCCR0,zl

;-Line--0038----Do--
L0000:

;-Line--0040----If i>1 Then--
		lds		r24,i
		ldi		zl,low(1)
		inc		zl
		cp		r24,zl
		brsh	PC+0x02
		rjmp	L0003
L0004:

;-Line--0041----word_bcd()         '将变量n转换成BCD码--
		call	word_bcd

;-Line--0042----i=0--
		ldi		zl,low(0)
		sts		i,zl

;-Line--0043----Start Adc, Vref=Int'启动ADC转换,使用内部电压基准--
		ldi		zl,0x86
		out		ADCSR,zl
		ldi		zl,0xC0
		out		ADMUX,zl

;-Line--0044----n=Adc(0)           '获得ADC0的值--
		ldi		zl,low(0)
		in		zh,ADMUX
		andi	zh,0xe0
		or		zl,zh
		out		ADMUX,zl
		call	_Adc
		sts		n,zl
		sts		n+1,zh

;-Line--0045----Stop Adc           '停止ADC--
		cbi		ADCSR,7

;-Line--0046----dxs(1)=tab(bcd_1)  '查表获得个位7段码--
		ldi		zl,low(1)
		ldi		zh,high(1)
		push	zh
		push	zl
		lds		zl,bcd_1
		clr		zh
		ldi		r24,low(tab*2)
		ldi		r25,high(tab*2)
		add		zl,r24
		adc		zh,r25
		lpm
		mov		zl,r0
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r24
		pop		r25
		add		xl, r24
		adc		xh, r25
		st		X,zl

;-Line--0047----dxs(2)=tab(bcd_2)  '查表获得十位7段码--
		ldi		zl,low(2)
		ldi		zh,high(2)
		push	zh
		push	zl
		lds		zl,bcd_2
		clr		zh
		ldi		r24,low(tab*2)
		ldi		r25,high(tab*2)
		add		zl,r24
		adc		zh,r25
		lpm
		mov		zl,r0
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r24
		pop		r25
		add		xl, r24
		adc		xh, r25
		st		X,zl

;-Line--0048----dxs(3)=tab(bcd_3)  '查表获得百位7段码--
		ldi		zl,low(3)
		ldi		zh,high(3)
		push	zh
		push	zl
		lds		zl,bcd_3
		clr		zh
		ldi		r24,low(tab*2)
		ldi		r25,high(tab*2)
		add		zl,r24
		adc		zh,r25
		lpm
		mov		zl,r0
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r24
		pop		r25
		add		xl, r24
		adc		xh, r25
		st		X,zl

;-Line--0049----dxs(4)=tab(bcd_4)  '查表获得千位7段码--
		ldi		zl,low(4)
		ldi		zh,high(4)
		push	zh
		push	zl
		lds		zl,bcd_4
		clr		zh
		ldi		r24,low(tab*2)
		ldi		r25,high(tab*2)
		add		zl,r24
		adc		zh,r25
		lpm
		mov		zl,r0
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r24
		pop		r25
		add		xl, r24
		adc		xh, r25
		st		X,zl

;-Line--0050----dxs(5)=tab(bcd_5)  '查表获得万位7段码--
		ldi		zl,low(5)
		ldi		zh,high(5)
		push	zh
		push	zl
		lds		zl,bcd_5
		clr		zh
		ldi		r24,low(tab*2)
		ldi		r25,high(tab*2)
		add		zl,r24
		adc		zh,r25
		lpm
		mov		zl,r0
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r24
		pop		r25
		add		xl, r24
		adc		xh, r25
		st		X,zl

;-Line--0051----PORTA=PORTA And &b00001111                 '关闭所有的显示位而不影响其它位--
		in		r24,PORTA
		ldi		zl,low(15)
		and		zl,r24
		out		porta,zl

;-Line--0052----Select Case a--
		lds		zl,a
		mov		r24,zl

;-Line--0053----Case 1--
		ldi		zl,low(1)
		cp		r24,zl
		brne	L0006

;-Line--0054----PORTA=PORTA Or &b00010000:PORTB=dxs(1)  '显示个位--
		in		zl,PORTA
		push	zl
		ldi		zl,low(16)
		pop		r24
		or		zl,r24
		out		porta,zl
		ldi		zl,low(1)
		ldi		zh,high(1)
		push	zl
		push	zh
		ldi		zl,1
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r25
		pop		r24
		add		xl, r24
		adc		xh, r25
		ld		zl,X
		out		portb,zl

;-Line--0055----Case 2--
		rjmp	L0005
L0006:
		ldi		zl,low(2)
		cp		r24,zl
		brne	L0007

;-Line--0056----PORTA=PORTA Or &b00100000:PORTB=dxs(2)  '显示十位--
		in		zl,PORTA
		push	zl
		ldi		zl,low(32)
		pop		r24
		or		zl,r24
		out		porta,zl
		ldi		zl,low(2)
		ldi		zh,high(2)
		push	zl
		push	zh
		ldi		zl,1
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r25
		pop		r24
		add		xl, r24
		adc		xh, r25
		ld		zl,X
		out		portb,zl

;-Line--0057----Case 3--
		rjmp	L0005
L0007:
		ldi		zl,low(3)
		cp		r24,zl
		brne	L0008

;-Line--0058----PORTA=PORTA Or &b01000000:PORTB=dxs(3)  '显示百位--
		in		zl,PORTA
		push	zl
		ldi		zl,low(64)
		pop		r24
		or		zl,r24
		out		porta,zl
		ldi		zl,low(3)
		ldi		zh,high(3)
		push	zl
		push	zh
		ldi		zl,1
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r25
		pop		r24
		add		xl, r24
		adc		xh, r25
		ld		zl,X
		out		portb,zl

;-Line--0059----Case 4--
		rjmp	L0005
L0008:
		ldi		zl,low(4)
		cp		r24,zl
		brne	L0009

;-Line--0060----PORTA=PORTA Or &b10000000:PORTB=dxs(4)  '显示千位--
		in		zl,PORTA
		push	zl
		ldi		zl,low(128)
		pop		r24
		or		zl,r24
		out		porta,zl
		ldi		zl,low(4)
		ldi		zh,high(4)
		push	zl
		push	zh
		ldi		zl,1
		ldi		xl,low(dxs)
		ldi		xh,high(dxs)
		pop		r25
		pop		r24
		add		xl, r24
		adc		xh, r25
		ld		zl,X
		out		portb,zl

;-Line--0063----End Select--
L0009:
L0005:

;-Line--0064----Else--
		rjmp	L0002
L0003:

;-Line--0065----End If--
L0002:

;-Line--0066----Loop--
		rjmp	L0000
L0001:

;-Line--0069----Interrupt Oc0(),Save 1       'timer0比较匹配中断服务无需装初值--

;-Line--0070----i=255                        '设置时间标志--
IntN19:
		in		r2,SREG
		push	zl
		push	zh
		ldi		zl,low(255)
		sts		i,zl

;-Line--0071----a=a+1                        '控制数码管各位轮流显示--
		lds		r24,a
		ldi		zl,low(1)
		add		zl,r24
		sts		a,zl

;-Line--0072----If a>5 Then a=1              '显示到第5位回到第一位--
		lds		r24,a
		ldi		zl,low(5)
		inc		zl
		cp		r24,zl
		brsh	PC+0x02
		rjmp	L0012
L0013:
		ldi		zl,low(1)
		sts		a,zl
L0012:

;-Line--0073----Enable Interrupts            'AVR在进入中断之后会关闭全局中断,所以在这里要重新打开全局中断--
		sei

;-Line--0074----End Interrupt                '中断服务程序结束--
		pop		zh
		pop		zl
		out		SREG,r2
		reti

;-Line--0077----Sub word_bcd()--
word_bcd:

;-Line--0078----word_bcd1:--
word_bcd1:

;-Line--0079----bcd_5=255--
		ldi		zl,low(255)
		sts		bcd_5,zl

;-Line--0080----word_bcd2:--
word_bcd2:

;-Line--0081----Incr bcd_5--
		lds		zl,bcd_5
		inc		zl
		sts		bcd_5,zl

;-Line--0082----If n > 9999 Then--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(9999)
		ldi		zh,high(9999)
		adiw	zl,1
		cp		r24,zl
		cpc		r25,zh
		brsh	PC+0x02
		rjmp	L0016
L0017:

;-Line--0083----n=n-10000 :GoTo word_bcd2--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(10000)
		ldi		zh,high(10000)
		sub		r24,zl
		sbc		r25,zh
		mov		zl,r24
		mov		zh,r25
		sts		n,zl
		sts		n+1,zh
		jmp		word_bcd2

;-Line--0084----Else--
		rjmp	L0015
L0016:

;-Line--0085----bcd_4=255--
		ldi		zl,low(255)
		sts		bcd_4,zl

;-Line--0086----End If--
L0015:

;-Line--0087----word_bcd3:--
word_bcd3:

;-Line--0088----Incr bcd_4--
		lds		zl,bcd_4
		inc		zl
		sts		bcd_4,zl

;-Line--0089----If n > 999 Then--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(999)
		ldi		zh,high(999)
		adiw	zl,1
		cp		r24,zl
		cpc		r25,zh
		brsh	PC+0x02
		rjmp	L0019
L0020:

;-Line--0090----n=n-1000 :GoTo word_bcd3--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(1000)
		ldi		zh,high(1000)
		sub		r24,zl
		sbc		r25,zh
		mov		zl,r24
		mov		zh,r25
		sts		n,zl
		sts		n+1,zh
		jmp		word_bcd3

;-Line--0091----Else--
		rjmp	L0018
L0019:

;-Line--0092----bcd_3=255--
		ldi		zl,low(255)
		sts		bcd_3,zl

;-Line--0093----End If--
L0018:

;-Line--0094----word_bcd4:--
word_bcd4:

;-Line--0095----Incr bcd_3--
		lds		zl,bcd_3
		inc		zl
		sts		bcd_3,zl

;-Line--0096----If n > 99 Then--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(99)
		ldi		zh,high(99)
		adiw	zl,1
		cp		r24,zl
		cpc		r25,zh
		brsh	PC+0x02
		rjmp	L0022
L0023:

;-Line--0097----n=n-100 :GoTo word_bcd4--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(100)
		ldi		zh,high(100)
		sub		r24,zl
		sbc		r25,zh
		mov		zl,r24
		mov		zh,r25
		sts		n,zl
		sts		n+1,zh
		jmp		word_bcd4

;-Line--0098----Else--
		rjmp	L0021
L0022:

;-Line--0099----bcd_2=255--
		ldi		zl,low(255)
		sts		bcd_2,zl

;-Line--0100----End If--
L0021:

;-Line--0101----word_bcd5:--
word_bcd5:

;-Line--0102----Incr bcd_2--
		lds		zl,bcd_2
		inc		zl
		sts		bcd_2,zl

;-Line--0103----If n > 9 Then--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(9)
		ldi		zh,high(9)
		adiw	zl,1
		cp		r24,zl
		cpc		r25,zh
		brsh	PC+0x02
		rjmp	L0025
L0026:

;-Line--0104----n=n-10 :GoTo word_bcd5--
		lds		r24,n
		lds		r25,n+1
		ldi		zl,low(10)
		ldi		zh,high(10)
		sub		r24,zl
		sbc		r25,zh
		mov		zl,r24
		mov		zh,r25
		sts		n,zl
		sts		n+1,zh
		jmp		word_bcd5

;-Line--0105----Else--
		rjmp	L0024
L0025:

;-Line--0106----bcd_1=n--
		lds		zl,n
		sts		bcd_1,zl

;-Line--0107----End If--
L0024:

;-Line--0108----End Sub--
L0014:
		ret

;-Line--0110----tab=192,249,164,176,153,146,130,248,128,144  '数码管7段码码表--
tab:
		.db	0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90

;****** END OF USER BASIC CODE ****************


;////// ADC ///////////////////////////////
_Adc:	sbi		ADCSR,0x06
_Adc1:	sbic	ADCSR,0x06
		rjmp	_Adc1
		in		zl,ADCL
		in		zh,ADCH
		ret

;System Global Variables: 0  bytes
;User   Global Variables: 14  bytes

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -