|
用了一款STC的单片机,在定时器中断0里面有个数组的下标要累加,一开我定义为unsigned char Num;能够正常运转,现在由于数组变大,下标超过了255,我就定义为unsigned int Num;但是程序一运算Num++; 就死机了...
void timer0() interrupt 1 { if(m_flag) { TH0 = Timer_speed[1]/256; TL0 = Timer_speed[1]%256; CK = 1; m_flag = 0; } else { TH0 = Timer_speed[1]/256; TL0 = Timer_speed[1]%256; CK = 0; m_flag = 1;
//---------------------------------------------------------------------- if(UP_flag) //加速部分 { if(Speed_Count==0) { Speed_Count = Speed_Step; //每个定时器速度运算Speed_Step次 Timer_speed[1] = T1_time[T1_Num];//赋值定时器值 T1_Num++; if(T1_Num>=500) //到达预定速度 { UP_flag = False; //加速标志去除,进入匀速 UP_Count = 0; T1_Num--; } UP_Count++; if(UP_Count>=UP_Rate) //速度等级判断,当要求较慢速度时的调节 { UP_flag = False; UP_Count = 0; T1_Num--; } } } Speed_Count--; } if(Down_flag&&(UP_flag==0)) //减速部分 { if(Speed_Count==0) { Speed_Count = Speed_Step; Timer_speed[1] = T1_time[T1_Num]; T1_Num--; if((T1_Num<=1)||(Timer_speed[1]<=Timer_speed[0])) { Timer_speed[1] = Timer_speed[0]; //[0]是串口数据发来的速度的最低限制 Down_flag = False; } } Speed_Count--;
} if(Motor_Count { Down_flag = True; UP_flag = False; } Motor_Count--; Motor_Count0--; if(Motor_Count0==0)Motor_Stop();//电机停止初始值复位 } 程序有点长:具体意思就是一个步进电机的加速,匀速,减速,到停止 |
|