📄 timer.c
字号:
/******************************************************************************
*
* (c) copyright Freescale Semiconductor Hong Kong Ltd. 2004
* ALL RIGHTS RESERVED
*
*******************************************************************************
** THIS CODE IS ONLY INTENDED AS AN EXAMPLE FOR DEMONSTRATING THE FREESCALE **
** MICROCONTROLLERS. IT HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST. IT IS **
** PROVIDED 'AS SEEN' WITH NO GUARANTEES AND NO PROMISE OF SUPPORT. **
*******************************************************************************
*
* FILE: timer.c REVISION 0.3
*
* DESCRIPTION: Application code is placed here.
* This module handles all tasks related to the internal timer and
* Output Compare (O/C).
* The timer module is used.
* Program to initialise the internal timer parameters.
* O/C features to measure all motors input frequency
* for driving motor position.
* All O/C subroutines are drived motors position.
*
*
* COMPILER: Metrowerks VERSION: ADS v1.2 (initial version)
* VERSION: ADS v2.0 (Demo alpha/beta version)
* VERSION: ADS v3.1 (Demo version)
*
* NOTES
* -----
* All modules remain at their reset addresses.
*
*
* UPDATED HISTORY:
*
* REV YYYY.MM.DD AUTHOR DESCRIPTION OF CHANGE
* --- ---------- ------ ---------------------
* 0.0 2002.04.27 Kenny Lam Initial version
* 0.1 2002.09.12 Kenny Lam Demo alpha version
* 0.2 2004.04.12 Kenny Lam Demo beta version
* 0.3 2006.01.22 Kenny Lam Demo version
*
******************************************************************************/
/* Freescale is not obligated to provide any support, upgrades or new */
/* releases of the Software. Freescale may make changes to the Software at */
/* any time, without any obligation to notify or provide updated versions of */
/* the Software to you. Freescale expressly disclaims any warranty for the */
/* Software. The Software is provided as is, without warranty of any kind, */
/* either express or implied, including, without limitation, the implied */
/* warranties of merchantability, fitness for a particular purpose, or */
/* non-infringement. You assume the entire risk arising out of the use or */
/* performance of the Software, or any systems you design using the software */
/* (if any). Nothing may be construed as a warranty or representation by */
/* Freescale that the Software or any derivative work developed with or */
/* incorporating the Software will be free from infringement of the */
/* intellectual property rights of third parties. In no event will Freescale */
/* be liable, whether in contract, tort, or otherwise, for any incidental, */
/* special, indirect, consequential or punitive damages, including, but not */
/* limited to, damages for any loss of use, loss of time, inconvenience, */
/* commercial loss, or lost profits, savings, or revenues to the full extent */
/* such may be disclaimed by law. The Software is not fault tolerant and is */
/* not designed, manufactured or intended by Freescale for incorporation */
/* into products intended for use or resale in on-line control equipment in */
/* hazardous, dangerous to life or potentially life-threatening environments */
/* requiring fail-safe performance, such as in the operation of nuclear */
/* facilities, aircraft navigation or communication systems, air traffic */
/* control, direct life support machines or weapons systems, in which the */
/* failure of products could lead directly to death, personal injury or */
/* severe physical or environmental damage (High Risk Activities). You */
/* specifically represent and warrant that you will not use the Software or */
/* any derivative work of the Software for High Risk Activities. */
/* Freescale and the Freescale logos are registered trademarks of Freescale */
/* Semiconductor Inc. */
/*****************************************************************************/
#include <hidef.h>
#include <stdlib.h>
#include "cluster_includes.h"
#include "H256Port.h"
#include "Const.h"
#include "GlobalVar.h"
#define MILE_FACTOR 3858 // 60000000000L*/60*4320 //in mm/8msec
#define MILE_FACTOR1 6430 // 100000/3600*4320 //in mm/8msec
#define MILE_FACTOR2 5787 // 90000000/3600*4320 //in mm/sec
//#pragma CODE_SEG CodeForcedToPPage33
/****************************************************************************
* init_timer() -
*
* Description : Timer initilaisation subroutine
* :
* Example :
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : void
****************************************************************************/
void init_timer()
{
int i;
INTCR = 0x20; // SET interrupt control reg
HPRIO = 0xe0; //
RTICTL = 0; // CLR RTI 14
TIE = 0x01; // Enable ch0 as o/c interrupt
TSCR2 = 0x80; // allow timer overflow interrupt
TIOS = 0x01; // Channel 0 to act as output compare.
TSCR1 = 0xa0; // Enable timer & freeze mode
TC0W = 0xfa00; // Timer interrupt is now 8ms
COPCTL =0; // disable COP
for (i=0;i<=4;i++)
{
MotorCurStep[i]=0; // initial value for all motors current step
MotorFinalStep[i]=0; // initial value for all motors final step
motor_flag[i]=0; // motor status is disable
OverFlowCnt=0; // initial value for timer overflow cnt
}
}
/****************************************************************************
* Timer0() -
*
* Description : Timer 0 (system timer) interrupt service subroutine
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : void
****************************************************************************/
#pragma TRAP_PROC
void Timer0(void)
{
div_t result;
unsigned int quot,rem,tempd,tempx,tempy;
TC0W = TC0W + MS8;
Time8ms++; // increment cnt by 8msec
if(MotorCurStep[SPEEDOMETER]!=0)
Mileage_lmm += (unsigned long)MotorCurStep[SPEEDOMETER]; // sum MotorCurStep/8ms
if (++Timems >=125) // 1 sec service
{
quot=(unsigned int)(Mileage_lmm>>16);
rem=(unsigned int)(Mileage_lmm);
asm{
std tempd
stx tempx
sty tempy
ldy quot
ldd rem
ldx #125
ediv
sty quot
std rem
ldd tempd
ldx tempx
ldy tempy
}
Mileage_lmm=rem;
Mileage_mm += (unsigned int)((unsigned long)quot*MILE_FACTOR2/1000);
result=div(Mileage_mm,1000);
Mileage+=result.quot;
Mileage_mm=result.rem;
Timems=0; // reset 1 sec counter
if(ClockFlag==ENABLE)
{
if(LCDTimerFlag==ON)
DigitDP(ON);
else
DigitDP(OFF);
LCDTimerFlag^=1;
}
SecData++; //1 sec increment
if(SecData>=60)
{
SecData=0;
MinData++; //1 min increment
LCD_hex_clk_dec(MinData);
}
if(MinData>=60)
{
MinData=0;
HourData++; //1 hr increment
LCD_hex_clk_dec(MinData);
clock_on();
}
}
if(IndTimes++>60){
if(MotorDemoFlag==ENABLE) {
if(DemoIndex[0]<6) {
//PORTP^=LEDRIGHT; // toggle right led
//PORTP|=LEDLEFT; // off left led
}
else if (DemoIndex[0]<16){
//PORTP^=LEDLEFT; // toggle left led
//PORTP|=LEDRIGHT; // off right led
}
else{
//PORTP|=LEDRIGHT+LEDLEFT; // off left and right led
}
}
IndTimes=0;
}
TFLG1=0x1; //clear interrupt flag
}
/****************************************************************************
* TimerOverflow() -
*
* Description : Timer Overflow interrupt service subroutine
* :
* Example : N/A
* Input : N/A
* Output : N/A
* Modify : N/A
* Return : void
****************************************************************************/
#pragma TRAP_PROC
void TimerOverflow(void)
{
OverFlowCnt++;
TFLG2|=0x80; //clear interrupt flag
}
/****************************************************************************
* delay1() -
*
* Description : Eat time subroutine
* :
* Example : 0x32
* Input : 0x32 (cdata)
* Output : 3 2 (ascii)
* Modify : N/A
* Return : void
****************************************************************************/
void delay1()
{
unsigned int data;
data=0xffff; // data = 65535
while((data--)!=0); // while loop
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -