E đang làm bài dùng encoder để đo chiều dài đoạn dây mà e ko biết mình viết có đúng ko. Mọi người xem và cho e ý kiến nha!!!
Đây là đoạn code e viết:
.include"m16def.inc"
.org 0x0000
rjmp __reset
.org 0x0002
rjmp __encoder
;======================================
;============== Khai bao ==============
;======================================
.equ encoder_countl = 0x0060
.equ encoder_counth = 0x0061
.equ encoder_a = 3
.equ encoder_b = 4
;======================================
;============== Reset =================
;======================================
__reset:
ldi r16,low(ramend)
out spl,r16
ldi r17,high(ramend)
out sph,r17
__reset_encoder:
cbi ddrd,encoder_a
cbi ddrd,encoder_b
sbi portd,encoder_a
sbi portd,encoder_b
ldi r16,0x00
out mcucr,r16
ldi r16,0x80
out gicr,r16
sei
;======================================
;============= voidmain ================
;======================================
__voidmain:
rjmp __voidmain
;======================================
;============= Encoder ================
;======================================
__Encoder:
sbic pind,encoder_b
rjmp __encoder1
rjmp __count_up
__encoder1:
sbis pind,encoder_b
rjmp __encoder_exit
rjmp __count_down
__Encoder_exit:
reti
;======================================
;============= Count up ===============
;======================================
__count_up:
lds r20,encoder_countl
inc r20
sts encoder_countl,r20
brne __count_up_exit
lds r21,encoder_counth
inc r21
sts encoder_counth,r21
brne __count_up_exit
__count_up_exit:
ret
;======================================
;============ Count down ==============
;======================================
__count_down:
lds r20,encoder_countl
dec r20
sts encoder_countl,r20
brne __count_down_exit
lds r21,encoder_counth
dec r21
sts encoder_counth,r21
brne __count_down_exit
__count_down_exit:
ret
Đây là đoạn code e viết:
.include"m16def.inc"
.org 0x0000
rjmp __reset
.org 0x0002
rjmp __encoder
;======================================
;============== Khai bao ==============
;======================================
.equ encoder_countl = 0x0060
.equ encoder_counth = 0x0061
.equ encoder_a = 3
.equ encoder_b = 4
;======================================
;============== Reset =================
;======================================
__reset:
ldi r16,low(ramend)
out spl,r16
ldi r17,high(ramend)
out sph,r17
__reset_encoder:
cbi ddrd,encoder_a
cbi ddrd,encoder_b
sbi portd,encoder_a
sbi portd,encoder_b
ldi r16,0x00
out mcucr,r16
ldi r16,0x80
out gicr,r16
sei
;======================================
;============= voidmain ================
;======================================
__voidmain:
rjmp __voidmain
;======================================
;============= Encoder ================
;======================================
__Encoder:
sbic pind,encoder_b
rjmp __encoder1
rjmp __count_up
__encoder1:
sbis pind,encoder_b
rjmp __encoder_exit
rjmp __count_down
__Encoder_exit:
reti
;======================================
;============= Count up ===============
;======================================
__count_up:
lds r20,encoder_countl
inc r20
sts encoder_countl,r20
brne __count_up_exit
lds r21,encoder_counth
inc r21
sts encoder_counth,r21
brne __count_up_exit
__count_up_exit:
ret
;======================================
;============ Count down ==============
;======================================
__count_down:
lds r20,encoder_countl
dec r20
sts encoder_countl,r20
brne __count_down_exit
lds r21,encoder_counth
dec r21
sts encoder_counth,r21
brne __count_down_exit
__count_down_exit:
ret
Comment