dhmhl 发表于 2006-11-29 17:02:10

麻烦各位帮小弟解答一下:步进电机正反转的问题.(附程序)

下面是我的程序(就是控制步进电机正转一圈再反转一圈,步进角1.8)





;*************************************

;File name         :motor_step

;chip type         :atmel mega8

;programe type       :application

;clock frequency   :8MHZ

;memory model      :small

;External SRAM size:0

;*************************************

;PB0/1/2 AS CONTROL KDY INPUT

;PC0/1/2/3 AS DRIVER SINGAL OUTPUT



.include "m8def.inc"



.def          temp          =r16

.def          temp1         =r17

.def          temp2         =r18

.def          count         =r19



.equ          table_cycle   =0x1ff



.org 0x0000

         rjmp main

.org 0x0020



main:

         ldi         temp,0x02

         out         spl,temp

         ldi         temp,0x5f

         out         sph,temp



         ldi         temp,0b00011011            ;pb.0.1.3.4作为输出

             out         ddrb,temp

             ser         temp

             out         portb,temp

         

         clr         count



loop:





;****************

;*

for:

         clr         count

for_start:

         ldi         zl,low(table*2)

         ldi         zh,high(table*2)

for1:

         ld            temp1,z+

         cpi         temp1,0x00

         breq       for_start

         out         portb,temp1

         rcall      delay

         inc         count

         cpi         count,200

         breq       rev

         rjmp       for1

rev:

         clr         count

rev_start:

         ldi         zl,low(table2*2)

         ldi         zh,high(table2*2)

rev1:

         ld            temp1,z+

         cpi         temp1,0x00

         breq       rev_start

         out         portb,temp1

         rcall      delay

         inc         count

         cpi         count,200

         breq       for

         rjmp       rev1







;***********************

;*

delay:

          ldi          temp2,0x1E       ;延时20ms程序,3层嵌套延时

          push         temp2

L0:       push         temp2

L1:       push         temp2

L2:       push         temp2

L3:       dec          temp2

          brne   L3

          pop          temp2

          dec          temp2

          brne   L2

          pop          temp2

          dec          temp2

          brne   L1

          pop          temp2

          dec          temp2

          brne   L0

          pop          temp2

          ret   





.org table_cycle

table:

.db0x18,0x11,0x03,0x0a

.db0x00,0x00

table2:

.db0x18,0x0a,0x03,0x11

.db0x00,0x00





不知道哪里出错了,PB的四个输出口电平一直为高



         cpi         temp1,0x00

把其中的0X00改成0XFF就会发现PB输出口,每隔一秒钟有20MS的高电平,其余时间为低电平





好像根本就没用到正反转表一样

tyut 发表于 2006-11-29 17:23:07

以前没见过你这种定义数据表的方式.

呵呵

是从表里读数的话应该是用LPM指令吧

rev_start:

         ldi         zl,low(table2*2)

         ldi         zh,high(table2*2)

rev1:

         ld            temp1,z+            ;LD是读SRAM的吧

dhmhl 发表于 2006-11-29 17:36:51

非常感谢:tyut 初学er



请问这样定义数据表行吗?



应该怎么样定义好呢?

dhmhl 发表于 2006-11-29 17:55:40

改成这样了,输出口电平有变化,但变化都不一样,与预期的不一样,请帮忙看看还有哪里错了...

谢谢各位进来的朋友!!!







;*************************************

;File name         :motor_step

;chip type         :atmel mega8

;programe type       :application

;clock frequency   :8MHZ

;memory model      :small

;External SRAM size:0

;*************************************

;PB0/1/2 AS CONTROL KDY INPUT

;PC0/1/2/3 AS DRIVER SINGAL OUTPUT



.include "m8def.inc"



.def          temp          =r16

.def          temp1         =r17

.def          temp2         =r18

.def          count         =r19



.equ          table_cycle   =0x1ff



.org 0x0000

         rjmp main

.org 0x0020



main:

         ldi         temp,0x02

         out         spl,temp

         ldi         temp,0x5f

         out         sph,temp



         ldi         temp,0b00011011            ;pb.0.1.3.4作为输出

             out         ddrb,temp

             ser         temp

             out         portb,temp

         

         clr         count



loop:





;****************

;*

for:

         clr         count

for_start:

         clr         temp

         ldi         zl,low(table*2)

         ldi         zh,high(table*2)

for1:

         add         zl,temp

         lpm            

               mov         temp1,r0

         cpi         temp1,0xff

         breq       for_start

         out         portb,temp1

               inc         temp

         rcall      delay

         inc         count

         cpi         count,200

         breq       rev

         rjmp       for1

rev:

         clr         count

rev_start:

         clr         temp

         ldi         zl,low(table2*2)

         ldi         zh,high(table2*2)



rev1:

         add         zl,temp

         lpm         

               mov         temp1,r0

         cpi         temp1,0xff

         breq       rev_start

         out         portb,temp1

               inc         temp

         rcall      delay

         inc         count

         cpi         count,200

         breq       for

         rjmp       rev1







;***********************

;*

delay:

          ldi          temp2,0x1E       ;延时20ms程序,3层嵌套延时

          push         temp2

L0:       push         temp2

L1:       push         temp2

L2:       push         temp2

L3:       dec          temp2

          brne   L3

          pop          temp2

          dec          temp2

          brne   L2

          pop          temp2

          dec          temp2

          brne   L1

          pop          temp2

          dec          temp2

          brne   L0

          pop          temp2

          ret   





.org table_cycle

table:

.db0x18,0x11,0x03,0x0a

.db0xff,0xff

table2:

.db0x18,0x0a,0x03,0x11

.db0xff,0xff

zhouyh 发表于 2006-11-29 18:02:12

查表不对呀,为什么没有LPM呢?

zhouyh 发表于 2006-11-29 18:03:32

哦,你知道了

dhmhl 发表于 2006-11-29 21:26:19

谢谢各位,东改西改后,可以用了,放出改后的正确代码如下,这段程序是用来做光耦传器控制电机的一小段,整个做好后:





;*************************************

;File name         :motor_step

;chip type         :atmel mega8

;programe type       :application

;clock frequency   :8MHZ

;memory model      :small

;External SRAM size:0

;*************************************

;PB0/1/2 AS CONTROL KDY INPUT

;PC0/1/2/3 AS DRIVER SINGAL OUTPUT



.include "m8def.inc"



.def          temp          =r16

.def          temp1         =r17

.def          temp2         =r18

.def          count         =r19

.def          np            =r20



.equ          table_cycle   =0x1f0



.org 0x0000

         rjmp main

.org 0x0020



main:

         ldi         temp,0x02

         out         spl,temp

         ldi         temp,0x5f

         out         sph,temp



         ldi         temp,0b00011011            ;pb.0.1.3.4作为输出

        out         ddrb,temp

        ser         temp

        out         portb,temp

         

         clr         count



loop:





;****************

;*

for:

         ser         temp

         out         portb,temp

         clr         count

for_start:

         clr         np

         clr         temp

         ldi         zl,low(table*2)

         ldi         zh,high(table*2)

for1:

         inc         np

         cpi         np,5

         breq       for_start

         push          zl

         add         zl,temp

         lpm            

         mov         temp1,r0

         out         portb,temp1

         inc         temp

        pop         zl

         rcall      delay

         inc         count

         cpi         count,200

         breq       rev

         rjmp       for1

rev:

         ser         temp

         out         portb,temp

         clr         count

rev_start:

         clr         np

         clr         temp

         ldi         zl,low(table2*2)

         ldi         zh,high(table2*2)



rev1:

        inc         np

         cpi         np,5

         breq       rev_start

         push          zl

         add         zl,temp

         lpm         

        mov         temp1,r0

         out         portb,temp1

        inc         temp

        pop         zl

         rcall      delay

         inc         count

         cpi         count,200

         breq       for

         rjmp       rev1







;***********************

;*

delay:

          ldi          temp2,0x1E       ;延时20ms程序,3层嵌套延时

          push         temp2

L0:       push         temp2

L1:       push         temp2

L2:       push         temp2

L3:       dec          temp2

          brne   L3

          pop          temp2

          dec          temp2

          brne   L2

          pop          temp2

          dec          temp2

          brne   L1

          pop          temp2

          dec          temp2

          brne   L0

          pop          temp2

          ret   





.org table_cycle

table:

.db0x18,0x11,0x03,0x0a



table2:

.db0x18,0x0a,0x03,0x11

dhmhl 发表于 2006-11-30 08:53:58

请用汇编的朋友加群:2753325
页: [1]
查看完整版本: 麻烦各位帮小弟解答一下:步进电机正反转的问题.(附程序)