请选择 进入手机版 | 继续访问电脑版

DIY编程器网

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1091|回复: 3

[cnc控制] AT89C2051驱动步进电机的电路和源码

[复制链接]
发表于 2011-5-11 07:56:38 | 显示全部楼层 |阅读模式
AT89C2051驱动步进电机的电路和源码




程序

stepper.c
stepper.hex
/*
* STEPPER.C
* sweeping stepper‘s rotor cw and cww 400 steps
* Copyright (c) 1999 by W.Sirichote
*/
#include c:\mc51\8051io.h /* include i/o header file */
#include c:\mc51\8051reg.h
register unsigned char j,flag1,temp;
register unsigned int cw_n,ccw_n;
unsigned char step[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90}
#define n 400
/* flag1 mask byte
0x01 run cw()
0x02 run ccw()
*/
main()
{
flag1=0;
serinit(9600);
disable(); /* no need timer interrupt */
cw_n = n; /* initial step number for cw */
flag1 |=0x01; /* initial enable cw() */
while(1){
{
tick_wait(); /* wait for 10ms elapsed */
energize(); /* round-robin execution the following tasks every 10ms */
cw();
ccw();
}
}
}
cw(){
if((flag1&0x01)!=0)
{
cw_n--; /* decrement cw step number */
if (cw_n !=0)
j++; /* if not zero increment index j */
else
{flag1&="0x01; /* disable cw() execution */
ccw_n = n; /* reload step number to ccw counter */
flag1 |=0x02; /* enable cww() execution */
}
}
}
ccw(){
if((flag1&0x02)!=0)
{
ccw_n--; /* decremnent ccw step number */
if (ccw_n !=0)
j--; /* if not zero decrement index j */
else
{flag1&="0x02; /* disable ccw() execution */
cw_n = n; /* reload step number to cw counter */
flag1 |=0x01; /* enable cw() execution */
}
}
}
tick_wait(){ /* cputick was replaced by simpler ASM code 10ms wait */
asm‘ JNB TCON.5,*‘; /* wait for TF0 set */
asm‘ CLR TCON.5‘; /* clear TF0 for further set */
asm‘ ORL TH0,#$DC‘; /* reload TH0 with $DC, TL0 = 0 */
}
energize(){
P1 = step[(j&0x07)]; /* only step 0-7 needed */
}


发表于 2013-10-8 23:01:01 | 显示全部楼层
好东西,谢谢楼主
发表于 2015-10-20 22:35:31 | 显示全部楼层
楼主,辛苦了。好资料
发表于 2016-12-2 23:23:13 | 显示全部楼层
好东西,谢谢楼主
*滑动验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|文字版|手机版|DIY编程器网 ( 桂ICP备14005565号-1 )

GMT+8, 2019-3-26 11:14 , 耗时 0.201500 秒, 31 个查询请求 , Gzip 开启.

各位嘉宾言论仅代表个人观点,非属DIY编程器网立场。

桂公网安备 45031202000115号

DIY编程器群(超员):41210778 DIY编程器
DIY编程器群1:3044634 DIY编程器1

Email:libyoufer@sina.com

QQ:28000622

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