写了两天的步进电机速度调节程序 ,还是没成功,高手来指点一下啊
[复制链接]
启动加速过程,正反转控制已经基本实现,现在就是速度调节有问题,不能调节,请高手指点
#include<reg51.h> #define uchar unsigned char #define uint unsigned int uchar code FFW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//转动数组 uchar code REV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; uchar ffw_down,rev_down,speed_up_down,speed_down_down,stop_down; uchar ffw_flag,rev_flag,speed_up_flag,speed_down_flag,stop_flag;//变量定义 int i,j,pp,ss,ttt,SET;
int TT = 30; int kk = 20;
sbit ffw_moto = P3^4; //正转 sbit rev_moto = P3^5; //反转 sbit speed_up = P3^6; //加速 sbit speed_down = P3^7; //减速 sbit stop_moto = P3^2; //停止
/****************按键延迟函数**********/ void delay(int tt) { uchar a,b; for(a = tt;a > 0;a --) for(b = 0;b < 120;b ++); }
/*************中断初始化***************/ void timer0_init(void) { SET = 2000; TMOD = 0x01; TR0 = 0; TH0=(65535 - SET) / 256; TL0=(65535 - SET) % 256; TR0 = 1; ET0 = 1; EA=1; }
/***************按键扫描****************/ void key_scan(void) { if(ffw_moto == 0) { delay(10); if(ffw_moto == 0) ffw_down = 1; if((ffw_down == 1)&&(ffw_moto == 1)) { ffw_down = 0; ffw_flag = 1; rev_flag =0; stop_flag = 0; } } if(rev_moto == 0) { delay(10); if(rev_moto == 0)rev_down = 1; if((rev_down == 1)&&(rev_moto == 1)) { rev_down = 0; rev_flag = 1; ffw_flag = 0; stop_flag = 0; } } if(speed_up == 0) { delay(10); if(speed_up == 0) speed_up_down = 1; if((speed_up_down == 1)&&(speed_up == 1)) { speed_up_down = 0; speed_up_flag = 1; speed_down_flag = 0; stop_flag = 0; } } if(speed_down == 0) { delay(10); if(speed_down == 0)speed_down_down = 1; if((speed_down_down == 1) && (speed_down == 1)) { speed_down_down = 0; speed_down_flag = 1; speed_up_flag = 0; stop_flag = 0; } } if(stop_moto == 0) { delay(10); if(stop_moto == 0) stop_down = 1; if((stop_down == 1)&&(stop_moto == 1)) { stop_down = 0; stop_flag = 1; ffw_flag = 0; rev_flag = 0; speed_up_flag = 0; speed_down_flag = 0; } }
}
/*****************按键处理函数******************/ void key_chuli(void) { key_scan(); if(speed_down_flag == 1) // 加速按键变量处理 { kk = kk - 1; if(kk < 1) kk = 1; } if(speed_up_flag == 1) //减速按键变量处理 { kk = kk + 1; if(kk > 100)kk = 100; } if(stop_flag == 1) // 停止按键处理 { P1 = 0x00; } }
void timer0_isr(void) interrupt 1 { TR0 = 0; ET0 = 0; TH0=(65535-SET)/256; TL0=(65535-SET)%256; TR0 = 1; ET0 = 1; /*************** 电机转动速度调节*********************/ ss ++; if(ss >= kk) { ss = 0; /******************启动加速阶段处理*********************/ ttt --; if(ttt <= 1) { if(j < TT) { j++; ttt = TT - j; } else j = TT; if(ttt <= 1) ttt = 0;
/*****************电机正向反向转动控制**********************/ if(ffw_flag == 1) { P1 = FFW; //正转数组 i++; if(i >= 8)i = 0; } if(rev_flag == 1) { P1 = REV; // 反转数组 i++; if(i >= 8)i = 0; } }
} }
void main(void) { ttt = TT; timer0_init(); while(1) { key_chuli(); } }
|