|
.include"m8def.inc"
.dseg
.org 0x0a0
;换相时间为tf,过零时间tg
zkb:.byte 1 ;占空比
tf1h:.byte 1 ;换相时间1
tf1l:.byte 1
tf2h:.byte 1 ;换相时间2
tf2l:.byte 1
;--------
tfch:.byte 1 ;t1的初值6d,6e
tfcl:.byte 1
t0a:.byte 1
t0b:.byte 1
t0bz:.byte 1
;---------------
t0_ch:.byte 1 ;t0初值
t0_fp:.byte 1 ;t0预分频模式
ys_h:.byte 1 ;延时与记数值
ys_l:.byte 1
js_h:.byte 1
;--------
.org 0x0af
js_l:.byte 1
bzq00:.byte 1
tf3h:.byte 1
tf3l:.byte 1
jwch:.byte 1
jwcl:.byte 1
jwc01h:.byte 1
jwc01l:.byte 1
;03版;03版
;-------------
;03版
wch:.byte 1
;--------
wcl:.byte 1
wc01h:.byte 1
wc01l:.byte 1
wc001h:.byte 1
wc001l:.byte 1
;--------
jih:.byte 1
jil:.byte 1
tfc0h:.byte 1
tfc0l:.byte 1
tfcyh:.byte 1
;--------
tfcyl:.byte 1
tfcmh:.byte 1
tfcml:.byte 1
tfcsh:.byte 1
tfcsl:.byte 1
;--------
jsji:.byte 1
jsja:.byte 1
jstz:.byte 1
jsjs:.byte 1
eeah:.byte 1
;----------
eeal:.byte 1
ckz_0h:.byte 1
ckz_0l:.byte 1 ;占空比低8位
.org 0x0d0
;****************
a001:.byte 1
b001:.byte 1
c001:.byte 1
d001:.byte 1
kongz:.byte 1
zkb01:.byte 1 ;过零前,pinc值
.org 0x60
stack:.byte 0x9f
.cseg
rjmp start ;----------跳到START段
;输入捕捉中断入口
.org 0x005
.org 0x06
;比较a
rjmp guanb
.org 0x07
;比较匹配b
rjmp dakai
;t1溢出中断入口
.org 0x008
rjmp qzfx
.org 0x09
;T0溢出中断入口
rjmp tdian;-------
.org 0x0b
rjmp tace01
;dyd为启动段
dyd:
sbr r25,0b01000000 ;启动标志,6
sbr r25,0b10000000 ;屏蔽USART,7
ldi r16,0x20
sts zkb,r16
;锁定
suodin:
;允许T1溢出中断和输入捕捉中断,T0溢出 ,ocie2使能
out ocr2,r16
ldi r16,0b01110001;;设置t2,相位修正模式8分频启动t2,,----0输出,1截止 ;11
out tccr2,r16
sbi portc,2
sbi portb,2 ;ca
rcall dey02
rcall dey03
;*****************
cbi portb,2
sbi portb,5 ;cb
rcall dey03
;*******************
clr r16 ;-t1装入初值
ldi r17,0x02
sts tfch,r17
sts tfcl,r16
rcall gen
rjmp main
gen: ;8 更新t1初值
lds r16,tfcl ;2
lds r17,tfch
out tcnt1h,r17 ;1
out tcnt1l,r16
ldi r16,0b10000001 ;1
out tccr1b,r16 ;T1启动
ldi r16,0b00000001 ;为delay传送工作模式和初值
sts t0_fp,r16
ldi r16,0xc0
sts t0_ch,r16
ret ;4
;通电----------t0中断服务程序
tdian:
;*************
clr r16 ;1
out tccr0,r16 ;1
;================
lds r16,zkb ;更新ZKB ;1
;===================
out ocr2,r16 ;1
ldi r16,0b01110001;;设置t2,相位修正模式8分频启动t2,,----0输出,1截止 ;1
out tccr2,r16 ;1
rcall soucai
rcall tace02
rcall jsd01
jup01:
sei
reti
;主程序-----------------
start:
ldi r16,0b00100101
out timsk,r16
sei
ldi r16,0xdf
out spl, r16 ;堆栈指针
rcall dey03
rcall dey03
;pb定义
ldi r16,0b00111110
out ddrb,r16
sbi ddrc,1
sbi ddrc,2
rcall gdk ;关闭复位后,端口输出高电,功率管关闭
clr r25
;-----------------
;赋初值
dyd01:
ldi r22,0x02 ;步进
ldi r23,0x02
ldi r24,0x02
ldi r30,0x80
ser r16
sts tf3h,r16
;--------------------
ldi r16,0b00111000 ;过零端口上拉
out portc,r16
ldi r16,0b11111110 ;探测端口上拉置位
out portd,r16
out pind,r16
sbi portb,0
clr r16
sts zkb,r16 ;-----占空比
sts zkb01,r16
clr r17
sts kongz,r17
;----看门狗
out wdtcr,r17
;====================
usart: ;usart设置
cli
; 设置波特率;异步,U2X=0,fosc=8m,urr=1,误差=0.0% ,
ldi r16,0x01
clr r17
out ubrrh,r17
out ubrrl,r16
; tx使能,9 个数据位
; 设置帧格式: 9 个数据位, 1 个停止位,偶校验
ldi r16, (1<<URSEL)|(1<<upm1)|(3<<UCSZ0)
out UCSRC,r16
sei
tace:
rcall tace02
ldi r16,0xdf
out spl, r16
rjmp tace
main: ;在此等待
sei
ldi r16,0xdf
out spl, r16 ;重设置堆栈指针,防止溢出
rjmp main
;换相子程序,闭环和开环均需在此进入换相
;断电--关端口(pb124567)--调用子程序换相
fxd:
ldi r16,0xdf
out spl, r16 ;重设置堆栈指针,防止溢出
clr r16
out tccr2,r16 ;关t2
ldi r16,0b00001000 ;pb各端口赋值,关断各相
out portb,r16
cbi portc,1
cbi portc,2
rcall delay
ldi r31,0x0
ijmp
;换相指令段------------
.org 0x0080
zt01:
sbi portb,1
sbi portb,5 ;2
ldi r30,0x86 ;1
rcall gen
ldi r26,0b00001000 ;3c
rjmp main ; ab
zt02:
sbi portb,1 ; ac
sbi portc,1
ldi r30,0x8c
rcall gen
ldi r26,0b00010000 ;4b
rjmp main
zt03:
sbi portb,4 ;bc
sbi portc,1
ldi r30,0x92
rcall gen
ldi r26,0b00100000 ;5a
rjmp main
zt04:
sbi portb,4 ;ba
sbi portb,2
ldi r30,0x98
rcall gen
ldi r26,0b00001000 ;3c
rjmp main
zt05:
sbi portc,2
sbi portb,2
ldi r30,0x9e ;ca
rcall gen
ldi r26,0b00010000 ;4b
rjmp main
zt06:
sbi portc,2
sbi portb,5
ldi r30,0x80
rcall gen ;cb
ldi r26,0b00100000 ;5a
rjmp main
;***********
qzfx:
tst r24
brne ysh00
sbr r25,0b00100000 ;标志,5
wdr
ldi r16,(1<<wdce)|(1<<wde)|(1<<wdp2)|(1<<wdp0)
out wdtcr,r16
rjmp fxd
ysh00:
ldi r16,0x30
sts zkb,r16
dec r22
breq ysh
reti
ysh:
mov r22,r24 ;恢复r22的值(无变化
dec r23
breq ysh01
rjmp fxd
ysh01:
dec r24
mov r23,r24
rjmp fxd
;USART接收**********************
tace01:
in r18,ucsra
in r17,ucsrb
in r16,udr
cbi ucsrb,rxen
andi r18,(1<<fe)|(1<<dor)|(1<<pe)
breq tace00
reti
tace00:
sbrc r17,1
rjmp tace03
sts kongz,r16
reti
tace03:
mov r17,r16 ;判断01为ZKB
cbr r17,0b00000010
sbr r17,0b00000001
cpse r17,r16
reti
cbr r16,0b00000011 ;6位精度
sts zkb01,r16
reti
genf:
lds r16,zkb01 ;待机状态,直接刷新ZKB
cpi r16,0x20
brsh genf02
sts zkb,r16
ret
genf02:
rjmp qidd
genf01:
sbrs r25,6 ;已经启动则跳过
ret
sbrc r25,7 ;在启动阶段,此位=1 USART被屏蔽
ret
;**************************
lds r16,zkb
lds r17,zkb01
lds r18,kongz
clc
cpi r17,0x0a
brlo genf011
cp r16,r17
brsh genf001
rcall jisd ;加占空比
ret
genf011:
rjmp wdog
genf001:
rcall jand ;减占空比
ret
tace02:
cli
sbrs r25,6 ;6位为0待机
rcall genf
rcall genf01
ldi r16,(1<<rxcie)|(1<<rxen)|(1<<ucsz2)
out ucsrb,r16
;对KONGZ解码
lds r16,kongz
clr r17
sts kongz,r17
ldi r17,0b10110101
cpse r16,r17
rjmp kzi
rjmp qidd
kzi:
ldi r17,0b01011101 ;REST
cpse r16,r17
rjmp kzi01
rjmp wdog
kzi01:
sei
ret
qidd:
sbrc r25,6
ret ;屏蔽启动键
sbi pind,7 ;跳赋值和定时器启动段dyd
rjmp dyd
jisd:
;加占空比
lds r16,tf3h ;限速<0xea
cpi r16,0x0a
brsh jisd00
ret
jisd00:
sbi pind,2
lds r28,zkb
cpi r28,0xe0 ;占空比超过00xf0,则跳
brsh retd
lds r16,ckz_0l
ldi r17,0x04
clc
add r16,r17
sts ckz_0l,r16
brcc retd ;无进位,返回
inc r28 ;高位加1
sts zkb,r28
rjmp retd
jand:
sbi pind,1
;减占空比
lds r16,tf3h
cpi r16,0xf0 ;限速 >0x30
brlo jand00
sbrc r25,0
ret
jand00:
lds r28,zkb
cpi r28,0x0b
brsh jand01 ;小于,RSET
rjmp wdog
jand01:
lds r16,ckz_0l
ldi r17,0x04
clc
sub r16,r17
sts ckz_0l,r16
brcc retd ;无进位,返回
dec r28 ;高位减1
sts zkb,r28
retd:
ret
;加速模块,步进加速; 加TFCH(每次加4),tfc为t1初值,控制换相时间
;***************************************
t1_zj:
clc
lds r27,tfch
neg r27 ;取反,速度增加,负载增加,加速度变小
mov r16,r27
lsr r16
lsr r16
lsr r16
lsr r16
lsr r16
lds r17,tfch
add r17,r16
sts tfch,r17
mov r16,r27
cbr r16,0b11100000
;=======================
jus00:
clc
lds r17,tfch
lds r28,tfcl
add r16,r28
sts tfcl,r16
brcc jup
inc r17
sts tfch,r17
jup:
lsr r17
lsr r17
lsr r17
lsr r17
ldi r16,0x30
add r16,r17
sts zkb,r16
sts zkb01,r16
ret
;**********************************************************
;t0延时子程序-------------
delay:
lds r16,t0_ch
out tcnt0,r16
lds r16,t0_fp ;T0预分频8
out tccr0,r16
sei
ret
;================== |
|