『壹』 步進電機驅動程序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;
}
『貳』 51單片機步進電機正反轉程序
單片機控制步進電機,我想你說的是兩相步進電機,一般是控制其相序分配的專順逆從屬而控制正反轉,一般而言,步進電機相序分配你可以做成一個數組比如step[]={0x03,0x06,0x0c,0x09},這樣來說可以假設P0口是步進電機控制口,那麼可以按如下方式來控制: while(1) { for(i=0;i<4;i++) { if(fx==1)P0=step[i]; //正向 else P0=step[3-i]; //反向 delay(x); //x大小決定電機速度。
根據電機相數買個驅動器。然後用單片機產生脈沖來控制電機的轉動以及正反轉。單片機產生脈沖的方法和單片機控制流水燈是一樣的。ULN2003D 是驅動步進電機的驅動晶元,主要是匹配電機所需的電流。 由於是四相電機,步進電機之所以可以轉動就需要給相繞組提供連續的脈沖,所以需要4個埠來控制四相繞組的工作狀態(P15應該是不需要的),具體的編碼要看電機的拍數; 一旦明白這些,你就可以很容易編寫代碼來控制電機的轉動了,還有在脈沖間你可以設置不同的延時時間來調節電機的轉速。
『叄』 51單片機驅動步進電機電路及程序
1單片機驅動步進電機(含電路圖和源程序代碼)
源程序:
stepper.c
stepper.hex
/*
* STEPPER.C
* sweeping stepper's rotor cw and cww 400 steps
* Copyright (c) 1999 by W.Sirichote
*/
#i nclude c:mc5151io.h /* include i/o header file */
#i nclude c:mc5151reg.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()
*/
『肆』 求利用51單片機實現控制步進電機(PWM電機)前進、後退、加減速 的程序(C語言),以及電路圖 不勝感激
#include"reg52.h"
voiddelay(unsignedintt);
//Motor
sbitF1=P1^0;
sbitF2=P1^1;
sbitF3=P1^2;
sbitF4=P1^3;
unsignedcharcodeFFW[8]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6};//反轉
unsignedcharcodeFFZ[8]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe};//正轉
unsignedintK;
/**********************************************************************
**
*步進電機驅動*
**
***********************************************************************/
voidmotor_ffw()
{
unsignedchari;
unsignedintj;
for(j=0;j<12;j++)//轉1*n圈
{
for(i=0;i<8;i++)//一個周期轉30度
{
if(K==1)P1=FFW[i]&0x1f;//取數據
if(K==2)P1=FFZ[i]&0x1f;
delay(15);//調節轉速
}
}
}
/******************************************************
*
*延時程序
*
********************************************************/
voiddelay(unsignedintt)
{
unsignedintk;
while(t--)
{
for(k=0;k<100;k++)
{}
}
}
main()
{
while(1)
{
K=1;
motor_ffw();
K=2;
motor_ffw();
}
}
給你個例子,這是正反轉的可以用這個修改出加減速程序