『壹』 51單片機C語言程序按鍵控制步進電機轉速
sbit K1=P1^0;
sbit K2=P1^1;
char y=0;
while(1)
{
pangan();
for(i=0;i<4;i++) //4相
{
/*P1=F_Rotation[i]; //輸出對應的相 可以自行換成反轉表格
Delay(500); //改變這個參數可以調整電機轉速
Delay(5000);*/
P1=B_Rotation[i];
Delay(265+y);
P1=F_Rotation[i];
Delay(265+y);
}
}
void pangan()
{
if(K1==0)
{ y++; //加
while(~k1)
}
if(K2==0)
{ y--;
while(~k2); //減
}
}
}
沒有下上限 要是調的話 需要判斷顯示延時時間
『貳』 求單片機控制步進電機的C語言程序,急啊
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit KEY1 = P3^2; //步進電機逆時讓辯針方向轉
sbit KEY2 = P3^3; //步進電機順時針方向轉
sbit KEY3 = P3^4; //步進電機調速
uchar step = 0;
bit AB_flag = 0;
unsigned char code A_Rotation[8]={0x08,0x18,0x10,0x30,0x20,0x60,0x40,0x48}; //順時針轉表格
unsigned char code B_Rotation[8]={0x48,0x40,0x60,0x20,0x30,0x10,0x18,0x08}; //逆時針轉表格
/********************************************************************
* 名稱 : Delay_1ms()
* 功能 : 延時子程序,延時時間為 1ms * x
* 輸入 : x (延時一毫秒的個數)
* 輸出 : 無
***********************************************************************/指滾
void Delay(uint i)
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void KEY(void)
{
if(KEY1 == 0) //按P3.2,實現步進電機的逆時針轉動
{
Delay(15);
if(KEY1 == 0)
{
while(1)
{
if(KEY1 == 1)
{
Delay(15);
if(KEY1 == 1)
{
AB_flag = 0;
break;
}
}
}
}
}
if(KEY2 == 0) //按P3.3,實現步進電機的順時針轉動
{
Delay(15);
if(KEY2 == 0)
{
while(1)
{
if(KEY2 == 1)
{
Delay(15);
if(KEY2 == 1)
{
AB_flag = 1;
break;
}
}
}
}
}
if(KEY3 == 0) //按P3.4,實現步進電機的調速
{
Delay(15);
if(KEY3 == 0)
{
while(1)
{
if(KEY3 == 1)
{
Delay(15);
if(KEY3 == 1)
{
step++;
if(step == 3)
{
step = 0;
}
break;
}
}
}
}
}
}
main()
{
uchar i;
while(1)
{
KEY(); //按鍵處理函數
for(i=0;i<8;i++) //因為有8路的控制時序
{
if(AB_flag == 0)
{
P1 = A_Rotation[i]; //逆時針轉動
}
else
{
P1 = B_Rotation[i]; //順時針轉動
}
Delay(2+step); //改變這個參數可以調坦逗缺整電機轉速
}
}
}
『叄』 我想用STC12C5A60S單片機控制STI6608驅動步進電機動作,請問C程序怎麼寫
這個STI6608驅動器輸入信號是Pluse+DIR方式控制,可以帶2個步進電機。Reset可以置位步進電機到位置1。剛啟動是置位1次就可以了。以下編一簡單的演示程序:
#include<reg51.h>
#defineucharunsignedchar
#defineuintunsignedchar
sbitpluse1=P1^0;
sbitpluse2=P1^1;
sbitdir1=P1^2;
sbitdir2=P1^3;
sbitreset1=P1^4;
sbitreset2=P1^5;
voiddelay(uinta)
{
uinti;
while(a--)for(i=0;i<1100;i++);
}
main()
{
uinti;
reset1=0;
reset2=0;
delay(1000);
reset1=1;
reset2=1;
while(1)
{
for(i=0;i<4800;i++)
{
pluse1=~pluse1;
dir1=1;
delay(5);
}
for(i=0;i<4800;i++)
{
pluse2=~pluse2;
dir2=1;
delay(5);
}
for(i=0;i<4800;i++)
{
pluse1=~pluse1;
dir1=0;
delay(5);
for(i=0;i<4800;i++)
{
pluse2=~pluse2;
dir2=0;
delay(5);
}
}
}
『肆』 用C語言編寫單片機驅動步進電機的程序,原理圖如下
While(1)
{
P1=8;
delayms(5);
P1=4;
delayms(5);
P1=2;
delayms(5);
P1=1;
delayms(5);
}
『伍』 一個51單片機同時控制2個步進電機的C語言程序
#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=z;x>0;x--)
for(y=50;y>0;y--);
}
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 xian(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)-1;NXY>=0&&P32!=0&&P33!=0;NXY--)
{ {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)-1;NXY>=0;NXY--)
{ 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=0;z<200;z++)
{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)-1;NXY>=0&&P32!=0&&P33!=0;NXY--)
{
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=0;z<200;z++)
{P35 = 0;
delay(10);
P35 = 1;
delay(10);
}
}
}
int shu1 ()
{
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 shu2 ()
{
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=shu1();
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=shu1();
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 xianchabu()
{ int q1=0,q2=0;
delay(300);
NX=shu1();
delay(300);
NY=shu2();
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();
xian(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)
{
xianchabu();
}
else if(getkey()==1)
{
zhengx();
}
else if(getkey()==2)
{
fux();
}
else if(getkey()==3)
{
zhengy();
} else if(getkey()==4)
{
fuy();
}
}
}
if(GetKey()==12)
{ break;}
}
}
『陸』 關於51單片機控制步進電機編程(c語言),求助
#include<reg52.h>
#defineucharunsignedchar
#defineuintunsignedint
ucharkey_l,key,i;
ucharcodetemp1[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
ucharcodetemp2[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
ucharcodeaa[16]=":";
ucharcodesmg[]="";
sbite=P2^6;
sbitrs=P2^4;
sbitrw=P2^5;
voiddelay(inttt)
{
while(tt--);
}
voidw_cmd(uchardd)
{
rs=0;
rw=0;
e=0;
delay(50);
P0=dd;
e=1;
delay(50);
e=0;
delay(50);
}
voidw_dat(uchardd)
{
rs=1;
rw=0;
e=0;
delay(50);
P0=dd;
e=1;
delay(50);
e=0;
}
voidinit(ucharsmg4[],uchartime[])
{
w_cmd(0x38);
delay(2000);
w_cmd(0x38);
delay(500);
w_cmd(0x38);//注意順序,設置工作方式,顯示模式設置
delay(500);
w_cmd(0x08);//注意順序,關閉顯示
w_cmd(0x01);//清屏
w_cmd(0x07);//游標·畫面滾動模式設置
w_cmd(0x0c);//顯示及游標模式設置
w_cmd(0x80);//
for(i=0;i<16;i++)
w_dat(time[i]);
w_cmd(0x80+0X40);
for(i=0;i<16;i++)
w_dat(smg4[i]);
}
voidTb(ucharsmg[],ucharn,uchars,ucharl,uchard)
{
unsignedcharm;
w_cmd(0x82);
for(m=n;m<s;m++)
w_dat(smg[m]);
w_cmd(0x80+0X42);
for(m=l;m<d;m++)
w_dat(smg[m]);
}
voidmain()
{
init(aa,aa);
while(1)
{
P1=0xf0;
key_l=P1;
if(key_l!=0xf0)
{
delay(100);
if(key_l!=0xf0)
{
P1=key_l|0x0f;
key=P1;
}
}
switch(key)
{
case0xee:Tb(smg,0,10,11,21);
for(i=0;i<8;i++)
{
P2=temp1[i];
delay(500);
}
break;
case0xde:Tb(smg,0,10,19,29);
for(i=0;i<8;i++)
{
P2=temp1[i];
delay(300);
}
break;
case0xbe:Tb(smg,0,10,30,40);
for(i=0;i<8;i++)
{
P2=temp1[i];
delay(100);
}
break;
case0x7e:P2=0xff;Tb(smg,0,10,39,49);break;
case0xed:Tb(smg,50,60,11,21);
for(i=0;i<8;i++)
{
P2=temp2[i];
delay(200);
}
break;
case0xdd:Tb(smg,50,60,19,29);
for(i=0;i<8;i++)
{
P2=temp2[i];
delay(120);
}
break;
case0xbd:P2=0xff;Tb(smg,50,60,40,50);break;
}
}
}