⑴ 求一份51单片机控制步进电机的综合 C程序
/*-----------------------------------------------
名称:步进电机
内容:本程序用于测试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=1;B1=0;C1=0;D1=0;}//A相通电,其他相断电
#define
Coil_B1
{A1=0;B1=1;C1=0;D1=0;}//B相通电,其他相断电
#define
Coil_C1
{A1=0;B1=0;C1=1;D1=0;}//C相通电,其他相断电
#define
Coil_D1
{A1=0;B1=0;C1=0;D1=1;}//D相通电,其他相断电
#define
Coil_AB1
{A1=1;B1=1;C1=0;D1=0;}//AB相通电,其他相断电
#define
Coil_BC1
{A1=0;B1=1;C1=1;D1=0;}//BC相通电,其他相断电
#define
Coil_CD1
{A1=0;B1=0;C1=1;D1=1;}//CD相通电,其他相断电
#define
Coil_DA1
{A1=1;B1=0;C1=0;D1=1;}//D相通电,其他相断电
#define
Coil_OFF
{A1=0;B1=0;C1=0;D1=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=1;B1=0;C1=0;D1=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=1;B1=0;C1=0;D1=0;}代替
DelayMs(Speed);
//改变这个参数可以调整电机转速
,
//数字越小,转速越大,力矩越小
Coil_C1
DelayMs(Speed);
Coil_B1
DelayMs(Speed);
Coil_A1
DelayMs(Speed);
}
}
⑵ 求单片机控制步进电机的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); //改变这个参数可以调坦逗缺整电机转速
}
}
}
⑶ 51单片机驱动带有驱动器的步进电机C语言编程
#include<reg51.h>
#defineucharunsignedchar
sbitkey=P3^2;
sbitdir=P0^3;
sbitpluse=P0^2;
sbiten=P0^4;
voiddelay(inta)
{
while(a--);
}
main()
{
uchari;
en=0;
pluse=0;
while(1)
{
if(key==0)
{
delay(1000);
if(key==0);
{
while(key==0);
for(i=0;i<200;i++)
{
dir=0;
en=1;
pluse=~pluse;
delay(800);
}
}
}
dir=1;
en=0;
pluse=0;
}
}
⑷ 步进电机驱动程序C语言
步进电机控制程序(c语言+51单片机)
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define ms *77
// f = 12 M
#define LEDLen 4
#define Dj_star() {IE=0x81; pri_dj=0; }
#define Dj_stop() {IE=0x00; pri_dj=1; P1=0xff; shache="0"; delay(800ms); delay(800ms);delay(400ms); shache = 1; }
#define Chilun_Num 8
/* 齿轮数 8 个*/
#define set_display_num() { LEDBuf[0] = tmp / 1000; LEDBuf[1] = tmp / 100 % 10; \
LEDBuf[2] = tmp / 10 % 10; LEDBuf[3] = tmp % 10; }
uchar LEDBuf[LEDLen] = {0,0,0,0};
void read_num (); /* 读播码盘 到 set_round_num * 8 */
void display ();
void delay(uint delay_time) { uint i; for (i=0; i < delay_time ; i++) ; }
void run ();
void fx_run();
uint round_num = 0; /* 记录已转的 齿轮数 , 中断1次 加 1*/
uint set_round_num = 0; /* 播码盘设置 圈数 */
uint set_pwm_width = 0; /* 播码盘设置 步进电机 正向速度 */
bit one_round_flg = 0;
sbit led_1000 = P0^7; //use for display
sbit led_100 = P0^6; //use for display
sbit led_10 = P0^5; //use for display
sbit led_1 = P0^4; //use for display
sbit key_start = P3^0;
sbit key_puse = P3^0;
sbit key_clear = P3^1;
/* P3^2 接齿轮传感器 中断 */
sbit bujin_zx_stop = P3^3; /* 接步进电机 ,正向到位传感器 ,为 0 停机 */
sbit bujin_fx_stop = P3^4; /* 接步进电机 ,反向到位传感器 ,为 0 停机 */
sbit shache = P3^5; /* 接刹车控制继电器 0 电位有效 */
sbit pri_dj = P3^6; /* 接主电机控制继电器 0 电位有效 */
void main(){
TCON = 0x01;
display();
while(1) {
IE="0x00";
round_num = 0;
display();
if ( bujin_fx_stop ) fx_run();
while ( key_start );
delay ( 8ms );
if(!key_start){
read_num();
//set_round_num = 8;
while ( !key_start );
run ();
fx_run();
}
}
}
void run () {
#define Delay_time 180
/* 转一圈 50 次循环,每循环 4 步 ,50 * 4 = 200 , 200 * 1。8 = 360 */
uchar i ;
P1 = 0xff;
set_pwm_width = 15 + set_pwm_width / 10;
while ( 1 ) {
while( !shache | !key_start );
Dj_star();
for ( i="0" ; bujin_zx_stop & !pri_dj;i++ ){
P1 = 0xf9;
delay ( Delay_time ); // bujin_zx_stop = P3^3;
P1 = 0xfc; // bujin_fx_stop = P3^4;
delay ( Delay_time); // key_puse = P3^0;
P1 = 0xf6; // key_clear = P3^1;
delay ( Delay_time ); // shache = P3^5;
P1 = 0xf3; // pri_dj = P3^6;
delay ( Delay_time );
if( i == set_pwm_width ) { P1 = 0xff; i = 0; one_round_flg = 0; while ( !one_round_flg & key_puse );}
if(!key_puse) { delay(4ms); if(!key_puse) break; }
}
P1 = 0xff;
if ( pri_dj ) break;
if ( !key_puse ) {
delay ( 8ms );
if ( !key_puse ) {
Dj_stop();
while ( !key_puse );
// next pree key
while( !shache );
while(1){
while ( key_puse & key_clear );
delay ( 8ms );
if ( !key_clear ) { round_num = 0; display(); }
if ( !key_puse ) break;
}
while( !key_puse );
delay(8ms);
while( !key_puse );
}
}
}
}
void ext_int0(void) interrupt 0 { /* 主电机 齿轮 中断 */
uint tmp;
EA = 0;
if( !pri_dj ){
round_num ++;
if (round_num % Chilun_Num == 0 ){
one_round_flg = 1;
tmp = round_num / Chilun_Num ;
set_display_num();
P0 = 0xf0;
P0 = P0 | LEDBuf[0] ;
led_1000 = 0;
P0 |= 0xf0;
P0 = 0xf0;
P0 = P0 | LEDBuf[1] ;
led_100 = 0;
P0 |= 0xf0;
P0 = 0xf0;
P0 = P0 | LEDBuf[2] ;
led_10 = 0;
P0 |= 0xf0;
P0 = 0xf0;
P0 = P0 | LEDBuf[3] ;
led_1 = 0;
P0 |= 0xf0;
P0 = 0xf0;
}
if ( round_num >= set_round_num ) Dj_stop();
}
EA = 0x81;
}
void display(){
uchar i;
uint tmp = 0;
tmp = round_num / Chilun_Num ;
set_display_num();
for(i = 0; i < LEDLen ; i ++){
P0 = 0xf0;
P0 = P0 | LEDBuf[i] ;
if(i==0) led_1000 = 0; //P0^4
if(i==1) led_100 = 0; //P0^5
if(i==2) led_10 = 0; //P0^6
if(i==3) led_1 = 0; //P0^7
P0 |= 0xf0;
}
P0 = 0xf0;
}
void read_num(){
/* 读播码盘 到 set_round_num ,set_pwm_width */
uchar tmp;
P2 = 0xFF;
P2 = 0xEF; // 1110 1111
delay ( 1ms );
tmp = ~(P2 | 0xF0);
P2 = 0xDF; // 1101 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0 )) * 10 + tmp;
set_round_num = tmp;
P2 = 0xBF; // 1011 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0));
P2 = 0x7F; // 0111 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0)) * 10 + tmp;
set_round_num = set_round_num + tmp * 100;
set_round_num = set_round_num * Chilun_Num;
P2 = 0xFF;
P1 = 0xbF; // 0111 1111
delay ( 1ms );
tmp = ~(P2 | 0xF0) ;
P1 = 0xFF;
P2 = 0xFF;
P1 &= 0x7F; // 1011 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0)) * 10 + tmp ;
set_pwm_width = tmp ;
P1 = 0xFF;
P2 = 0xFF;
}
void fx_run(){
#define f_Delay_time 180
while ( bujin_fx_stop ) { /* 反向 回车 直到 传感器 动作*/
P1 = 0xf3; //0011
delay ( f_Delay_time );
P1 = 0xf6; //0110
delay ( f_Delay_time );
P1 = 0xfc; //1100
delay ( f_Delay_time );
P1 = 0xf9; //1001
delay ( f_Delay_time );
}
P1 = 0xff;
}
⑸ 一个关于单片机控制步进电机的C语言程序 想请高人帮我把每一步都注释出来
C语言程序源代码
#include <REGX51.H> // 51寄存器定义
#include "intrins.h"
#define control P1 //P1_0:A相,P1_1:B相,P1_2:C相,P1_3:D相
#define discode P0 //显示代码控制端口
#define uchar unsigned char //定义无符号型变量
#define uint unsigned int
sbit en_dm=P3^0; //显示代码锁存控制
sbit en_wk=P3^1; //位控锁存控制
uchar code corotation[4]= {0x03,0x06,0x0c,0x09};//电机正转
uchar code rollback[4]={0x0c,0x06,0x03,0x09}; //电机反转
uchar code tab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示字段
uint code Levspeed[5]={500,400,300,200,100};//电机速度等级
uchar Hscan,speedcount;//Hscan行扫描,speedcount 速度等级计数
uint uu; //频率计数
uint step,setstep; //step:电机步伐计数,setstep:手动设置电机步伐
uint speed=500; //电机初始速度
uchar count;
uchar flag[5];
uchar butcount; //按键次数
//****************************************
//flag[0] 正转标志
//flag[1] 反转标志
//flag[2] 加速标志
//flag[3] 减速标志
//flag[4] 设置标志
//****************************************
Delay1mS(unsigned int tt) //延时1ms “Delay1mS”延时子程序,用循环语句延时。
{
unsigned char i;
while(tt--)
for(i=113;i>0;i--);
}
keyscan() //键盘扫描 “keyscan”键盘扫描子程序,用于寻找是否有按键按下。
{
P2=0xf0; //把oxfe赋值给P2口 //将按键控制口置于未按键的状此誉态.单片机输出//口假设不是按键按下状态.
if(P2!=0xf0) //如果P2口的值不等于0xfe //检查是否有按键按下.如果有,处理{}内的事
{ //有按键按下.
Delay1mS(150);//调用延时函数//有按键按下,避开无效读码值的时间.,或者是脉冲长度
Hscan=0xfe; //将Hscan赋值,初始拍运遥控值是要置高电平的.
P2=Hscan; //
while((Hscan&0x10)!=0) //检查X10口是否森贺段有键按下。未按下是1,
//在这显示出你的按键口是P2_4.即检查P2_4是高电平,无//键按下,低电平有键按下。
{
P2=Hscan;
if((P2&0xf0)!=0xf0) //检查按键脉冲是否是变化,
return P2;
else Hscan=(Hscan<<1)|0x01; //这里在存码值,应该是遥控输入。将码值存入Hscan
//这里是读取码值的关键,如果来的脉冲不管是高电平//还是低电平,靠左移一位保存脉冲的状态值.
}
}
else return 0;
}
key_val() //按键处理函数 //这里是读取将存好的码值进行处理.看是什么代码值
{
uchar key;
key= keyscan(); //这里是读取码值并存放在key里
switch(key) //这里是对比读取的码值
{
case 0xee: //按键‘7’ //读取的码值=0xEE,则是按键“7”的代码
//while(P2==0xee);
setstep=setstep*10+7; //步伐数 //这里是输入”setstep +7” 步伐数对比读取的码值, //setstep原来可能是有数字的。
step=setstep;
butcount++; //计数,看输入拉几个数字
if(butcount>=5) //输入的数字超过5个就置0,无效
{
butcount=0;
setstep=0;
}
break;
case 0xde: //按键‘8’ //读取的码值=0xdE,则是按键“8”的代码
//其他同”7”
//while(P2==0xde);
setstep=setstep*10+8;
butcount++;
step=setstep;
if(butcount>=5)
{
butcount=0;
setstep=0;
}
break;
case 0xbe: //按键‘9’ //同上”7”
//while(P2==0xbe);
setstep=setstep*10+9;
butcount++;
step=setstep;
if(butcount>=5)
{
butcount=0;
setstep=0;
}
break;
case 0x7e: //按键‘正转 //正转按键识别
while(P2==0x7e); //等待按键松开//一值按住电机是不转的,放开后才转.有检查//P2口的状态值
flag[0]=0xff; //开启正转标志,关闭反转//置正转标志.
flag[1]=0x00; //清除反转标志.
butcount=0;
speedcount=0;
speed=500; //置电机的转速.
if(!flag[4]) step=0;
TR0=1;
break;
case 0xed: //按键‘4’ //同上”7”
while(P2==0xed);
setstep=setstep*10+4;
butcount++;
step=setstep;
if(butcount>=5)
{
butcount=0;
setstep=0;
}
break;
case 0xdd: //按键‘5’ //同上”7”
//while(P2==0xdd);
setstep=setstep*10+5;
step=setstep;
butcount++;
if(butcount>=5)
{
butcount=0;
setstep=0;
}
break;
⑹ 如何用单片机控制5个电动机
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
sbit K4=P3^3;
sbit K5=P3^4;
sbit MA=P1^0;
sbit MB=P1^1;
sbit ML=P1^2;
sbit MR=P1^3;
sbit MU=P1^4;
void main(void)
{
while(1)
{
察瞎 MU=0;
if(K1==0)
{
while(K1==0)
;
MA=0;MB=1;ML=1;MR=1;
}
if(K2==0)
{
while(K2==0)
枣桐 ;
MA=1; MB=0;ML=1;MR=1;
}
if(K3==0)
{
while(K3==0)
;
MA=1; MB=1;ML=0;MR=1;
败岩空}
if(K4==0)
{
while(K4==0)
;
MA=1; MB=1;ML=1;MR=0;
}
if(K5==0)
{
while(K5==0)
;
MA=1; MB=1;ML=1;MR=1; MU=1;
}
}
}
⑺ 求单片机控制步进电机简单的c程序
几相的步进电机啊?用不用驱动芯片?用的话选什么型号的驱动芯片?你要说具体点啊!
先给你三相的,个不用驱动芯片的步进电机程序。(驱动电路自己做)
#include
#define
uchar
unsigned
char
#define
uint
unsigned
int
uint
time
void
timer1(void)
interrupt
1
using
1{
//定时器0中断,定时1ms//
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
time++;
}
void
delay(uint
n){
//一般延时
while(--n);
}
void
delay(uint
dtime){
//延时
TR0=1;
while(time<=dtime);
time=0;
TR0=0;
}
void
moto(bit
cf,uchar
n,uint
dtime){
//电机驱动子程序,
uchar
idata
zhpai[4]={0x78,0xb8,0xd8,0xe8};
uchar
idata
fpai[4]={0xe8,0xd8,0xb8,0x78};
uchar
i,l=0;
if(cf==0)
//正转,A-B-C-A
for(i=0;i<n;i++){
P1=zhpai[l];//P1.7~1.4为输出口,分别为A、B、C、D
l++;
delay(dtime);
if(l==4)l=0;
}
else{
//反转,C-B-A-C
for(i=0;i<n;i++){
P1=fpai[l];
l++;
delay(dtime);
if(l==4)l=0;
}
}
}
void
main(){
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
while(1){
moto(0,4,70);//正转,转四相,每相间隔70ms
delay(10000);
moto(1,4,70);//反转,转四相,每相间隔70ms
delay(10000);
}
}
改为四相了,你看看吧。不懂再问吧。
⑻ 5线4相步进电机如何应用51单片机实现单步运转,求C程序(用ULN2003控制电机)
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//sbit key1=P1^4;
void delay(uint z);
unsigned char code RUN[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
unsigned char code BACK[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
void BACK_motor()/肆滚/反转子程序
{
uchar j;
for(j=0;j<8;j++)
{
P0=BACK[j];
delay(2);
}
}
void RUN_motor()//正转子程序
{
uchar i;
for(i=0;i<8;i++)//旋转带乱一周 内部旋转一裂行余周
{
P0=RUN[i];
delay(2);
}
}
void main()
{
while(1)
{
RUN_motor();
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=950;y>0;y--);
}
我用的12C60S2控制,L298驱动的,你用2003,是一样的,参考下吧