首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 电子技术 > 传感与控制
步进电机控制程序(c语言+51单片机)
来源:本站整理  作者:佚名  2011-05-04 21:26:38



#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 );
                 }
            }
    }
}

关键词:

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)
Copyright © 2007-2017 down.gzweix.Com. All Rights Reserved .
页面执行时间:10,800.78000 毫秒