📄 timer.c
字号:
#include <hidef.h>
#include <MC9s12H256.h>
#include "Motor.h"
#include "Motor_includes.h"
#include "\Ucos\includes.h"
/*角速度经验值*/
const unsigned int StepProfile[17]={3000,2000,1500,1200,1000,1000,800,800,750,750,750,780,700,600,500,500,500};
extern unsigned int MotorFinalStep[4],MotorCurStep[4];
extern unsigned int MotorMaxStep[4],MotorMinStep[4];
extern int MotorSpeedIndex[4],MaxSpeedIndex[4];
extern char MotorMoveStatus[4];
extern char MotorStatus[4];
//回零加速插值系数
unsigned char t0=0;
unsigned char t1=0;
unsigned char t2=0;
unsigned char t3=0;
extern byte resetback0; //回零标志
unsigned int mt2=0;
unsigned int mt3=0;
/****************************************************************************************
@函数名称 : void init_timer(void)
@参数 : void
@返回值 : 无
@描述 : 定时器初始化
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
void init_timer(void)
{
INTCR = 0x20; // SET interrupt control reg //IRQE IRQEN 0 0 0 0 0 0
HPRIO = 0xe0; // PSEL7 ... PSEL1 0
RTICTL = 0; // CLR RTI 14
TSCR2 = 0x82; //Enable timer overflow interrupt,分频系数4
TIOS = 0x0f; //channel 0,1,2,3 act as o/c;else act as i/c
TSCR1 = 0xa0; //Enable timer freeze mode
TC0 = 0x100;
TC1 = 0x100;
TC2 = 0x100;
TC3 = 0x100;
COPCTL =0; // disable COP
TIE = 0x0; //disable ch0,ch1,ch2,ch3 interrupt
}
/****************************************************************************************
@函数名称 : void Timer0(void)
@参数 : void
@返回值 : 无
@描述 : Timer0中断服务程序
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
#pragma TRAP_PROC
void Timer0(void)
{
OSIntEnter();
OS_SAVE_SP();
if(MotorSpeedIndex[PRES] >= 0)
{
if(resetback0){
if(t0 < 40){
TC0 = TC0 + (10000-(t0*200));
motor_update(PRES,CLOCKWISE);
t0++;
}
else {
TC0 = TC0 + 2000;
motor_update(PRES,CLOCKWISE);
}
}
else{
TC0 = TC0 + (StepProfile[MotorSpeedIndex[PRES]]);
motor_micro_update(PRES,CLOCKWISE);
}
}
if(MotorStatus[PRES]==OFF)
{
MotorMoveStatus[PRES]=IDLE;
MotorSpeedIndex[PRES] = 0;
TIE &= ~1; //finished moving
}
TFLG1 = 0x01;
OSIntExit();
}
/****************************************************************************************
@函数名称 : void Timer1(void)
@参数 : void
@返回值 : 无
@描述 : Timer1中断服务程序
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
#pragma TRAP_PROC
void Timer1(void)
{
OSIntEnter();
OS_SAVE_SP();
if(MotorSpeedIndex[TEMP] >= 0)
{
if(resetback0){
if(t1 < 40){
TC1 = TC1 + (10000-(t1*200));
motor_update(TEMP,CLOCKWISE);
t1++;
}
else {
TC1 = TC1 + 2000;
motor_update(TEMP,CLOCKWISE);
}
}
else{
TC1 = TC1 + (StepProfile[MotorSpeedIndex[TEMP]]);
motor_micro_update(TEMP,CLOCKWISE);
}
}
if(MotorStatus[TEMP]==OFF)
{
MotorMoveStatus[TEMP]=IDLE;
MotorSpeedIndex[TEMP] = 0;
TIE &= ~2; //finished moving
}
TFLG1 = 0x02;
OSIntExit();
}
/****************************************************************************************
@函数名称 : void Timer2(void)
@参数 : void
@返回值 : 无
@描述 : Timer2中断服务程序
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
#pragma TRAP_PROC
void Timer2(void)
{
OSIntEnter();
OS_SAVE_SP();
if(MotorSpeedIndex[SPEEDOMETER] >= 0)
{
if(resetback0){
if(t2 < 40){
TC2 = TC2 + (10000-(t2*200));
motor_update(SPEEDOMETER,CLOCKWISE);
t2++;
}
else {
TC2 = TC2 + 2000;
motor_update(SPEEDOMETER,CLOCKWISE);
}
}
else{
if(mt2 < 220){
TC2 = TC2 + (1000-(mt2)); //1000=220+StepProfile[MotorSpeedIndex[SPEEDOMETER]
motor_micro_update(SPEEDOMETER,CLOCKWISE);
mt2++;
}
else if((mt2 < (SPEEDOMAXSTEP-220))
&&(mt2 >= 220)){
TC2 = TC2 + (StepProfile[MotorSpeedIndex[SPEEDOMETER]]);
motor_micro_update(SPEEDOMETER,CLOCKWISE);
mt2++;
}
else{
TC2 = TC2 + (StepProfile[MotorSpeedIndex[SPEEDOMETER]]) + (mt2-(SPEEDOMAXSTEP-220));
motor_micro_update(SPEEDOMETER,CLOCKWISE);
mt2++;
}
}
}
if(MotorStatus[SPEEDOMETER]==OFF)
{
MotorMoveStatus[SPEEDOMETER]=IDLE;
MotorSpeedIndex[SPEEDOMETER] = 0;
TIE &= ~4; //finished moving
}
TFLG1 = 0x04;
OSIntExit();
}
/****************************************************************************************
@函数名称 : void Timer3(void)
@参数 : void
@返回值 : 无
@描述 : Timer3中断服务程序
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
#pragma TRAP_PROC
void Timer3(void)
{
OSIntEnter();
OS_SAVE_SP();
if(MotorSpeedIndex[TACHOMETER] >= 0)
{
if(resetback0){
if(t3 < 40){
TC3 = TC3 + (10000-(t3*200));
motor_update(TACHOMETER,CLOCKWISE);
t3++;
}
else {
TC3 = TC3 + 2000;
motor_update(TACHOMETER,CLOCKWISE);
}
}
else{
if(mt3 < 96){
TC3 = TC3 + (1480-(mt3*5)); //1480=96*10+StepProfile[MotorSpeedIndex[SPEEDOMETER]
motor_micro_update(TACHOMETER,CLOCKWISE);
mt3++;
}
else if((mt3 < (TACHOMAXSTEP-96))
&&(mt3 >= 96)){
TC3 = TC3 + (StepProfile[MotorSpeedIndex[TACHOMETER]]);
motor_micro_update(TACHOMETER,CLOCKWISE);
mt3++;
}
else{
TC3 = TC3 + (StepProfile[MotorSpeedIndex[TACHOMETER]]) + (mt3-(TACHOMAXSTEP-96));
motor_micro_update(TACHOMETER,CLOCKWISE);
mt3++;
}
}
}
if(MotorStatus[TACHOMETER]==OFF)
{
MotorMoveStatus[TACHOMETER]=IDLE;
MotorSpeedIndex[TACHOMETER] = 0;
TIE &= ~8; //finished moving
}
TFLG1 = 0x08;
OSIntExit();
}
/****************************************************************************************
@函数名称 : void TimerOverflow(void)
@参数 : void
@返回值 : 无
@描述 : Timer中断服务程序
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
#pragma TRAP_PROC
void TimerOverflow(void)
{
OSIntEnter();
OS_SAVE_SP();
TFLG2|=0x80; //clear interrupt flag
OSIntExit();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -