📄 motor speedup.c
字号:
#include <pic.h>
#include"main.h"
unsigned int k1_count,k2_count;
unsigned char count1;
bit k1,k2;
bit flag;
unsigned int track;
unsigned char grads;
persistent unsigned int mcount=0; //motor run total step count
unsigned int current_count=0; //motor current move step count
unsigned int time_reload_value; //the value reload to TIMExH and TIMExL
unsigned int invariance_cnt; //invariance speed count
unsigned char stair=4; //stair count at speedup or speeddown
unsigned char wave=10; //wave step count in every stair
//0xf82f 500pps/s
//0xfcdf 1250pps/s
const unsigned int grads_tab[5]={0xf82f,0xfa23,0xfb1d,0xfc17,0xfcdf};
struct {
unsigned run:1; //run flag
unsigned stop:1; //stop
unsigned origin:1; //at origin
unsigned speedup:1; //motor is speedup
unsigned speeddn:1; //speeddown
unsigned invariance:1; //invariance flag
unsigned forward:1;
unsigned reverse:1;
} sta;
void init_timer(void)
{
//----TIME0 init-----------------
OPTION=3;
T0IE=1;
//----TIME1 init-----------------
T1CON=1;//enable TMR1 on , Prescaler Rate is 1:1
TMR1H=0xFE; //10ms interrupt once
TMR1L=0x0B;
time_reload_value=0xf82f;
TMR1IE=1;
PEIE=1; //enable TMR1 interrupt
//----TIME2 init-----------------
TMR2ON=1;
T2CON=0x7f;
TMR2IE=1;
PEIE=1; //enable TMR1 interrupt
PR2=38;//255;
//-------------------------------
GIE=1; //enable all unmasked interrupts
}
void interrupt ISR (void)
{
//==============time0 isr=========================
if(TMR0IF)//alarm
{
TMR0IF=0; TMR0=0; //4.42ms
/* if(K1==0)
{
if(k1_count==30)
{ k1_count=0; k1=1; }
else k1_count++;
}*/
//---------------------------------------------
if(K2==0)
{
if(k2_count==20)
{
k2_count=0;
//--------------
LED1=0;
sta.run=1;
sta.speedup=1;
sta.speeddn=0;
sta.invariance=0;
stair=4;
wave=10;
//--------------------
}
else k2_count++;
}
else LED1=1;
//===========================================
}
//================time1 isr=========================
if(TMR1IF&TMR1IE)//process
{
TMR1IF=0;//前沿、后沿各50step*/
if(sta.run)
{
if(current_count==1000)
{
current_count=0;
EN=0; CLK=0;
sta.run=0;
}
else
{
CLK=!CLK;
EN=1;
current_count++;
if(sta.speedup==1) //speed up
{
wave-=1; /*波形数*/
if(wave==0)
{
stair-=1; /*台阶数*/
grads++;
time_reload_value=grads_tab[grads];
wave=10;
}
if(stair==0)
{
sta.speedup=0;
sta.invariance=1;
invariance_cnt=900;
}
}
if(sta.invariance==1) //invariance speed
{
invariance_cnt-=1;
if( invariance_cnt==0)
{
sta.invariance=0;
sta.speeddn=1;
wave=10;
stair=4;
grads=4;
}
time_reload_value=0xfcdf; // speed is 2400pps/s.
}
if(sta.speeddn==1) // speed down
{
wave-=1;
if(wave==0)
{
stair-=1;
grads--;
time_reload_value=grads_tab[grads];
wave=10;
}
if(stair==0) //motor is stop
{
sta.speeddn=0;
EN=0; CLK=0;
//stop timer
}
}
}
//-----------------------------------------
}
TMR1H=time_reload_value>>8;
TMR1L=(unsigned char)time_reload_value;
}
//==================time2 isr========================
if(TMR2IF&TMR2IE)//buzzer
{
TMR2IF=0;
TMR2=0;
if(count1==100)
{
LED=!LED;
count1=0;
//---------------------
/* if(track==3)
{
track=0;
LED1=0;
sta.run=1;
sta.speedup=1;
sta.speeddn=0;
sta.invariance=0;
stair=4;
wave=10;
}
else track++;*/
//--------------------
}
else count1++;
}
//===================================================
}
void inti_io(void)
{
TRISB1=0;TRISB2=0;
TRISC5=0;
TRISB4=0;
TRISB5=0;
TRISC4=0;
CCW =1;
EN =0;
CLK=0;
LED1=1;
LED=0;
LED1=0;
LED=1;
LED1=1;
LED=0;
}
void key()
{
if(K2==0)
{
if(k2_count==1000)
{
k2_count=0;
LED1=0;
sta.run=1;
sta.speedup=1;
}
else k2_count++;
}
else LED1=1;
}
/* if(k2) {
LED1=0;
EN=1; //motor begin satr
CCW=0;
if(mcount==800)
{ EN=0; mcount=0; k2=0; LED1=1; }
else mcount++;
}
if(sta.run)
{
if(current_count==60) //电机达到运行位置
{
current_count=0;
EN=0; //停止运行
sta.run=0;LED1=1;
}
else
{
CLK=!CLK;
LED1=0;
EN=1;
CCW=1;
if(sta.speedup==1) //加速阶段
{
wave-=1;
if(wave==0)
{
grads++;
time_reload_value=grads_tab[grads];
wave=4;
}
if(grads==4)
{
sta.speedup=0;
sta.invariance=1;
invariance_cnt=18;
}
}
if(sta.invariance==1) //恒速阶段
{
invariance_cnt-=1;
if( invariance_cnt==0)
{
sta.invariance=0;
sta.speeddn=1;
wave=4;
grads=4;
}
time_reload_value=0xfcdf;
}
if(sta.speeddn==1) // 减速阶段
{
wave-=1;
if(wave==0)
{
grads--;
time_reload_value=grads_tab[grads];
wave=4;
}
if(grads==0) //停止运行
{
sta.speeddn=0;
EN=0; LED1=1;sta.run=0;
CLK=0;
}
}
}
}*/
/* if(k1) {
CLK=!CLK;
LED1=0;
EN=1; //motor begin satr
CCW=1;
if(mcount==800)
{ EN=0; mcount=0; k1=0; LED1=1; }
else mcount++;
TMR1H=0xfc;
TMR1L=0x17;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -