搜索
bottom↓
回复: 7

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

[复制链接]

出0入0汤圆

发表于 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:

.db  0x18,0x11,0x03,0x0a

.db  0x00,0x00

table2:

.db  0x18,0x0a,0x03,0x11

.db  0x00,0x00





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



         cpi           temp1,0x00

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





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

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 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的吧

出0入0汤圆

 楼主| 发表于 2006-11-29 17:36:51 | 显示全部楼层
非常感谢:tyut 初学er



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



应该怎么样定义好呢?

出0入0汤圆

 楼主| 发表于 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:

.db  0x18,0x11,0x03,0x0a

.db  0xff,0xff

table2:

.db  0x18,0x0a,0x03,0x11

.db  0xff,0xff

出0入0汤圆

发表于 2006-11-29 18:02:12 | 显示全部楼层
查表不对呀,为什么没有LPM呢?

出0入0汤圆

发表于 2006-11-29 18:03:32 | 显示全部楼层
哦,你知道了

出0入0汤圆

 楼主| 发表于 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:

.db  0x18,0x11,0x03,0x0a



table2:

.db  0x18,0x0a,0x03,0x11

出0入0汤圆

 楼主| 发表于 2006-11-30 08:53:58 | 显示全部楼层
请用汇编的朋友加群:2753325
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-29 07:45

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表