DIY编程器网

标题: 步进电机控制程序(c语言+51单片机) [打印本页]

作者: liyf    时间: 2011-4-30 13:15
标题: 步进电机控制程序(c语言+51单片机)
步进电机控制程序(c语言+51单片机)
#include
#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 = 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;
}




欢迎光临 DIY编程器网 (http://diybcq.com/) Powered by Discuz! X3.2