一个51单片机同时控制2个步进电机的C语言程序

一个51单片机同时控制2个步进电机的C语言程序,第1张

#include<reg52.h>

#include<intrins.h>

#define mode 0x81 // 方式0,A口、B口输出,C口高4位输出,低4位输入

# include "stdio.h"

# include "string.h"

# include "math.h"

xdata unsigned char PA _at_ 0x7f00

xdata unsigned char PB _at_ 0x7f01

xdata unsigned char PC _at_ 0x7f02

xdata unsigned char caas _at_ 0x7f03//控制字

sbit P32=P3^2

sbit P33=P3^3

sbit P35=P3^5

#define uchar unsigned char

#define uint unsigned int

unsigned char h,Pos

unsigned int R,NX,NY

unsigned char key

code unsigned char KeyTable[] = { // 键码定义

0x0f, 0x0b, 0x07, 0x03,

0x0e, 0x0a, 0x06, 0x02,

0x0d, 0x09, 0x05, 0x01,

0x0c, 0x08, 0x04, 0x00

}

code unsigned char LEDMAP[] = { // 八段管显示码

0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,

0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71

}

unsigned char Code_ // 字符代码寄存器

#define PD1 61 // 122/2 分成左右两半屏(122x32)

unsigned char Column

unsigned char Page_ // 页地址寄存器 D1,DO:页地址

unsigned char Code_ // 字符代码寄存器

unsigned char Command// 指令寄存器

unsigned char LCDData// 数据寄存器

xdata unsigned char CWADD1 _at_ 0x1cff// 写指令代码地址(E1)

xdata unsigned char DWADD1 _at_ 0x1eff// 写显示数据地址(E1)

xdata unsigned char CRADD1 _at_ 0x1dff// 读状态字地址(E1)

xdata unsigned char DRADD1 _at_ 0x1fff// 读显示数据地址(E1)

xdata unsigned char CWADD2 _at_ 0x3cff// 写指令代码地址(E2)

xdata unsigned char DWADD2 _at_ 0x3eff// 写显示数进地址(E2)

xdata unsigned char CRADD2 _at_ 0x3dff// 读状态字地址(E2)

xdata unsigned char DRADD2 _at_ 0x3fff// 读显示数据地址(E2)

//----------------------液晶-----------------

// 清屏

// ************************ 中文显示程序 ***********************************/

/*************************直线 插 补***************************8*/

void delay(uint z)

{

uint x,y

for(x=zx>0x--)

for(y=50y>0y--)

}

void zhengx()

{

PA=0x00

delay(10)

PA=0x01

delay(10)

}

void fux()

{

PA=0x02

delay(10)

PA=0x03

delay(10)

}

void zhengy()

{

PB=0x00

delay(10)

PB=0x10

delay(10)

}

void fuy()

{

PB=0x20

delay(10)

PB=0x30

delay(10)

}

void zhixian(int NX,int NY)

{int FM, NXY, XOY,ZF,z

FM=0

{if(NX>0)

if(NY>0)

XOY=1

else

XOY=4

else

if(NY>0)

XOY=2

else

XOY=3}

for(NXY= fabs(NX) + fabs(NY)-1NXY>=0&&P32!=0&&P33!=0NXY--)

{ {if(NX>0)

if(NY>0)

XOY=1

else

XOY=4

else

if(NY>0)

XOY=2

else

XOY=3}

for(NXY= fabs(NX) + fabs(NY)-1NXY>=0NXY--)

{ if(FM>=0)

{if(XOY==1||XOY==4)

{ZF=1

zhengx()

}

else

{ZF=2

fux()

}

FM=FM-fabs(NY)

}

else

{if(XOY==1||XOY==2)

{

ZF=3

zhengy()

}

else

{ZF=4

fuy()

}

FM=FM+fabs(NX)

}

}

for(z=0z<200z++)

{P35 = 0

delay(10)

P35 = 1

delay(10)

}

}

}

/*************************圆 弧 插 补***************************8*/

void yuanhu1( int X0,int Y0, int NX, int NY ,int RNS )

{

int NXY,BS,ZF,XM,YM,z

int FM=0

BS=fabs(NX-X0) + fabs(NY-Y0)

XM=fabs(X0)

YM=fabs(Y0)

for(NXY= fabs(NX-X0) + fabs(NY-Y0)-1NXY>=0&&P32!=0&&P33!=0NXY--)

{

if(RNS==1||RNS==3||RNS==6||RNS==8)

{

if(FM<0)

{

if(RNS==1||RNS==8)

{ZF=1

zhengx()

}

else

{ZF=2

fux()}

FM=FM+2*fabs(XM)+1

XM=XM+1

}

else

{

if(RNS==1||RNS==6)

{

ZF=3

fuy()

}

else

{ZF=4

zhengy()

}

FM=FM-2*fabs(YM)+1

YM=YM-1

}

}

else

if(FM>=0)

{

if(RNS==2||RNS==7)

{ZF=1

zhengx()

}

else

{ZF=2

fux()

}

FM=FM-2*fabs(XM)+1

XM=XM-1

}

else

{

if(RNS==2||RNS==5)

{ZF=3

zhengy()}

else

{ZF=4

fuy()}

FM=FM+2*fabs(YM)+1

YM=YM+1

}

}

if(P32==0||P33==0)

{

for(z=0z<200z++)

{P35 = 0

delay(10)

P35 = 1

delay(10)

}

}

}

int shuzhi1 ()

{

int i=0,j=0,k=3

while (1)

{

if(testkey())

{ delay(300)

delay1()

if(testkey())

{ j=getkey()

if(j!=14)

{i=i*10 + j

k--}

}}

if(k==0)

break

}

return i

}

int shuzhi2 ()

{

int i=0,j=0,k=3

while (1)

{

if(testkey())

{ delay(300)

delay1()

if(testkey())

{ j=getkey()

if(j!=14)

{i=i*10 + j

k--}

}}

if(k==0)

break

}

return i

}

void yuanhuchabu1()

{ int q=0

delay(300)

R=shuzhi1()

yj1()

q=R/100

Page_ = 0x00

Column = 0x35

Code_ = q

WriteCHN8x16()

q=R%100

q=q/10

Page_ = 0x00

Column = 0x40

Code_ = q

WriteCHN8x16()

q=R%10

Page_ = 0x00

Column = 0x48

Code_ = q

WriteCHN8x16()

yuanhu1(R,0,0,R,5)

yuanhu1(0,R,-R,0,6)

yuanhu1(-R,0,0,-R,7)

yuanhu1(0,-R,R,0,8)

}

void yuanhuchabu2()

{ int q=0

delay(300)

R=shuzhi1()

yj1()

q=R/100

Page_ = 0x00

Column = 0x35

Code_ = q

WriteCHN8x16()

q=R%100

q=q/10

Page_ = 0x00

Column = 0x40

Code_ = q

WriteCHN8x16()

q=R%10

Page_ = 0x00

Column = 0x48

Code_ = q

WriteCHN8x16()

yuanhu1(0,R,R,0,1)

yuanhu1(R,0,0,-R,4)

yuanhu1(0,-R,-R,0,3)

yuanhu1(-R,0,0,R,2)

}

void zhixianchabu()

{ int q1=0,q2=0

delay(300)

NX=shuzhi1()

delay(300)

NY=shuzhi2()

yj2()

Page_ = 0x00

Column = 0x25

Code_ = 0x10

WriteCHN8x16()

q1=NX/100

Page_ = 0x00

Column = 0x30

Code_ = q1

WriteCHN8x16()

q1=NX%100

q1=q1/10

Page_ = 0x00

Column = 0x37

Code_ = q1

WriteCHN8x16()

q1=NX%10

Page_ = 0x00

Column = 0x40

Code_ = q1

WriteCHN8x16()

q2=NY/100

Page_ = 0x00

Column = 0x50

Code_ =q1

WriteCHN8x16()

q2=NY%100

q2=q2/10

Page_ = 0x00

Column = 0x58

Code_ = q2

WriteCHN8x16()

q2=NY%10

Page_ = 0x00

Column = 0x60

Code_ = q2

WriteCHN8x16()

Page_ = 0x00

Column = 0x72

Code_ = 0x11

WriteCHN8x16()

zhixian(NX,NY )

}

void main()

{ int q=0,q1=0,q2=0

caas=mode

PA=0X00

PB=0X00

PC=0x00

R=0X00

while(1)

{

if(testkey())

{

delay1()

if(testKey())

{ delay1()

if(getkey()==15)

{

delay(300)

yuanhuchabu1()

}

else if(getkey()==10)

{ delay(300)

yuanhuchabu2()

}

else if(getkey()==13)

{

zhixianchabu()

}

else if(getkey()==1)

{

zhengx()

}

else if(getkey()==2)

{

fux()

}

else if(getkey()==3)

{

zhengy()

} else if(getkey()==4)

{

fuy()

}

}

}

if(GetKey()==12)

{ break}

}

}

/*-----------------------------------------------

名称:步进电机

内容:本程序用于测试4相步进电机常规驱动

含正反转

使用1相励磁

------------------------------------------------*/

#include

<reg52.h>

sbit

A1=P1^0

//定义步进电机连接端口

sbit

B1=P1^1

sbit

C1=P1^2

sbit

D1=P1^3

#define

Coil_A1

{A1=1B1=0C1=0D1=0}//A相通电,其他相断电

#define

Coil_B1

{A1=0B1=1C1=0D1=0}//B相通电,其他相断电

#define

Coil_C1

{A1=0B1=0C1=1D1=0}//C相通电,其他相断电

#define

Coil_D1

{A1=0B1=0C1=0D1=1}//D相通电,其他相断电

#define

Coil_AB1

{A1=1B1=1C1=0D1=0}//AB相通电,其他相断电

#define

Coil_BC1

{A1=0B1=1C1=1D1=0}//BC相通电,其他相断电

#define

Coil_CD1

{A1=0B1=0C1=1D1=1}//CD相通电,其他相断电

#define

Coil_DA1

{A1=1B1=0C1=0D1=1}//D相通电,其他相断电

#define

Coil_OFF

{A1=0B1=0C1=0D1=0}//全部断电

unsigned

char

Speed

/*------------------------------------------------

uS延时函数,含有输入参数

unsigned

char

t,无返回值

unsigned

char

是定义无符号字符变量,其值的范围是

0~255

这里使用晶振12M,精确延时请使用汇编,大致延时

长度如下

T=tx2+5

uS

------------------------------------------------*/

void

DelayUs2x(unsigned

char

t)

{

while(--t)

}

/*------------------------------------------------

mS延时函数,含有输入参数

unsigned

char

t,无返回值

unsigned

char

是定义无符号字符变量,其值的范围是

0~255

这里使用晶振12M,精确延时请使用汇编

------------------------------------------------*/

void

DelayMs(unsigned

char

t)

{

while(t--)

{

//大致延时1mS

DelayUs2x(245)

DelayUs2x(245)

}

}

/*------------------------------------------------

主函数

------------------------------------------------*/

main()

{

unsigned

int

i=512//旋转一周时间

Speed=8

Coil_OFF

while(i--)

//正向

{

Coil_A1

//遇到Coil_A1

用{A1=1B1=0C1=0D1=0}代替

DelayMs(Speed)

//改变这个参数可以调整电机转速

,

//数字越小,转速越大,力矩越小

Coil_B1

DelayMs(Speed)

Coil_C1

DelayMs(Speed)

Coil_D1

DelayMs(Speed)

}

Coil_OFF

i=512

while(i--)//反向

{

Coil_D1

//遇到Coil_A1

用{A1=1B1=0C1=0D1=0}代替

DelayMs(Speed)

//改变这个参数可以调整电机转速

,

//数字越小,转速越大,力矩越小

Coil_C1

DelayMs(Speed)

Coil_B1

DelayMs(Speed)

Coil_A1

DelayMs(Speed)

}

}

#include<reg51.h>

#define uchar unsigned char

sbit key=P3^2

sbit dir=P0^3

sbit pluse=P0^2

sbit en=P0^4

void delay(int a)

{

    while(a--)

}

main()

{

uchar i

en=0

pluse=0

    while(1)

    {

        if(key==0)

        {

            delay(1000)

            if(key==0)

            {

                while(key==0)

                for(i=0i<200i++)

                {

                    dir=0

                    en=1

                    pluse=~pluse

                    delay(800)

                }

            }

        }

    dir=1

    en=0

    pluse=0

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7747024.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存