📄 timer.c
字号:
//*********************************************************************************
//* ST 7538 DEMOBOARD SOFTWARE timer.c *
//* *
//* timer functions V1.2 *
//*********************************************************************************
#include "global.h"
// OCMP1: Timer 1ms
#pragma DATA_SEG APP_RAM
typedef struct {
unsigned short stop; //absolute stop time (when timer expired)
unsigned short period; //timer period
}timeout_s;
volatile timeout_s Timeout[TIMEOUT_NUM];
volatile unsigned short tim_ms;
unsigned char TSR_L;
unsigned short int TCR;
/**********************************************************************************
* TIMER_Interrupt function updates the 1ms timer and the RS232 timer
**********************************************************************************/
#pragma TRAP_PROC SAVE_REGS
void TIMER_Interrupt(void)
{
TSR_L = TSR;
if(TSR_L&OCF2) {
TCR = CRH;
TCR = CRL; //clear Overflow bit
TCR = OCLR2; //clear Output Compare bit
}
if(TSR_L&OCF1) { // 1ms Timer interrupt
TCR = CRH;
TCR = (TCR<<8);
TCR = TCR + CRL;
TCR = TCR + TIMER_STEP;
OCHR1 = (unsigned char) (TCR>>8);
OCLR1 = (unsigned char) TCR;
tim_ms = tim_ms + 1;
// Button = ADC_ReadButton(); // Buttons Reading
}
// return;
}
/**********************************************************************************
* TIMER_Init initializates the HW timer and enable the interrupt
**********************************************************************************/
void TIMER_Init(void)
{
unsigned short int TCR;
TCR1 = 0x00;
TCR2 = 0x08; // ftimer = fclock/8
TCR = CRH;
TCR = (TCR<<8) + CRL;
TCR = TCR + (unsigned short)TIMER_STEP;
OCHR1 = (unsigned char) (TCR>>8);
OCLR1 = (unsigned char) TCR;
TCR1 |= 0x40; // Enables OCIE
}
/**********************************************************************************
*TIMEOUT_Init initializes (close) all the SW timeouts (which depend on 1ms timer)
**********************************************************************************/
void TIMEOUT_Init()
{
char i;
for(i=0;i<TIMEOUT_NUM;i++) {
Timeout[i].period = 0;
}
tim_ms = 0;
}
/**********************************************************************************
*TIMEOUT_Open opens a timeout with a user selected period (in ms) and returns the tout number
**********************************************************************************/
unsigned char TIMEOUT_Open(unsigned short period)
{
unsigned char i;
for(i=0;i<TIMEOUT_NUM;i++) {
if(Timeout[i].period==0) {
Timeout[i].period = period;
Timeout[i].stop = (unsigned short)(tim_ms + Timeout[i].period);
return (i+1);
}
}
return 0;
}
/**********************************************************************************
*TIMEOUT_Close closes a timeout
**********************************************************************************/
void TIMEOUT_Close(unsigned char timer)
{
if((timer>0)&&(timer<=TIMEOUT_NUM))
Timeout[timer-1].period = 0;
}
/**********************************************************************************
*TIMEOUT_Expired checks if the timeout is expired. return "1" if timeout if tout is expired,
*"0" otherwise
**********************************************************************************/
unsigned char TIMEOUT_Expired(unsigned char timer)
{
return ( ((short int) tim_ms - (short int) Timeout[timer-1].stop >= 0) && ( tim_ms - Timeout[timer-1].stop <= 32767));
}
/**********************************************************************************
*TIMEOUT_Refresh restart the timer updating the stop time
**********************************************************************************/
void __stack_call TIMEOUT_Refresh(unsigned char timer)
{
DI;
if((timer>0)&&(timer<=TIMEOUT_NUM)) {
Timeout[timer-1].stop = (unsigned short) (tim_ms + Timeout[timer-1].period);
}
EI;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -