⑴ 求一份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,是一樣的,參考下吧