📄 timer.c
字号:
/*********************************************************************
* Description:
* This pgm demostrate the timer setting and input/output handling.
* All processing is based on interrupt mothed.
* History:
* 1) Rev. 1.0: primarily version; by felix; Oct.10/2002
*
* Status:
* No bug reported.
* All rights reserved by Fudan JHB Tech. Co. Ltd.
*
* Any quesiton or comments, please feel free to contact us.
*********************************************************************/
#include "global_function.h"
#include "timer.h"
extern B8 _EscFlag;
B8 CountH, CountL; //count number for two display unit
int Timer_demo(void)
{
CountH = 0;
CountL = 0;
TimerInit();
//* config PortG
rPCONG = 0xff55; // GPIOG: EINT4,5,6,7 + output 0,1,2,3
rEXTINT = 0x0; // EINT4567 low level trigger0
printf("\n\n\r------------------ Timer demo ------------------\n\r");
printf(": Push button to pause or resume timer\n\r");
printf(":\tD4 -- timer1 -- 13.2Hz\n\r");
printf(":\tD5 -- timer2 -- 6.6Hzn\r");
printf(":\tD6 -- timer3 -- 3.3Hz\n\r");
printf(":\tD7 -- timer4 -- 1.6Hz\n\r");
printf(":\tUp counter -- timer0 -- 5Hz\n\r");
printf("\n\r:Esc to return");
//* start timer
rTCON = (B32)0xaaaa0a; // auto-reload enable and load initial count num to counter
rTCON = (B32)0xdddd0d; // start counter
while(!_EscFlag); // int handling loop
Timer_demo_end();
}
void TimerInit(void)
{
// timer setting
rTCFG0 = (B32)0x4a4a4a; //timer0/1/2/3/4 prescaler = 74+1
rTCFG1 = (B32)0x01234; //timer divider:
// timer0: 1/32;
// timer1: 1/16
// timer2: 1/8
// timer3: 1/4
// timer4: 1/2
// MCLK = 60MHZ ~~ divider = 1/32 ==>
// period = 1/60 * 32*75 = 40us
// set count number for each timer
rTCNTB0 = (B32)5000;
rTCNTB1 = (B32)30000;
rTCNTB2 = (B32)30000;
rTCNTB3 = (B32)30000;
rTCNTB4 = (B32)30000;
rINTMSK &= ~(BIT_EINT4567|BIT_TIMER0|BIT_TIMER1|BIT_TIMER2|BIT_TIMER3|BIT_TIMER4);
//* int priority is default
//rINTPSLV
//rINTPMST
//* int serve funciton table
pISR_TIMER0 = (int)Timer0Done;
pISR_TIMER1 = (int)Timer1Done;
pISR_TIMER2 = (int)Timer2Done;
pISR_TIMER3 = (int)Timer3Done;
pISR_TIMER4 = (int)Timer4Done;
pISR_EINT4567 = (int)PushButton;
}
void __irq Timer0Done(void) // control 7set-LED display
{
//* 2-digit decimal increase counter
if(CountL++ >= 9)
{
if(CountH++ >= 9)
{
CountH = 0;
}
CountL = 0;
}
mLEDSET = (getBCD(CountH) << 8) | getBCD(CountL); //display
rI_ISPC = BIT_TIMER0; // clear int_pending bit
}
void __irq Timer1Done(void) //control LED(D4)
{
rPDATG ^= 0x1; // toggle LED light state
rI_ISPC = BIT_TIMER1; // clear int_pending bit
}
void __irq Timer2Done(void) // control LED(D5)
{
rPDATG ^= 0x2; // toggle LED light state
rI_ISPC = BIT_TIMER2; // clear int_pending bit
}
void __irq Timer3Done(void) // control LED(D6)
{
rPDATG ^= 0x4; // toggle LED light state
rI_ISPC = BIT_TIMER3; // clear int_pending bit
}
void __irq Timer4Done(void) // control LED(D7)
{
rPDATG ^= 0x8; // toggle LED light state
rI_ISPC = BIT_TIMER4; // clear int_pending bit
}
//* 4 buttones on the board share the same one int source: extint4567
// so when a extint4567 occures, software should read rEXTINTPND to detect
// whitch button is pushed
void __irq PushButton(void) // ext_int triggled (button push)
{
B32 IntPendStatus;
B8 ExtInt4=0, ExtInt5=0, ExtInt6=0, ExtInt7=0;
IntPendStatus = rEXTINTPND & 0x0000000f;
Delay(200); // delay: button jitter prevent
switch (IntPendStatus)
{
case 0x1: ExtInt4 = 1; break;
case 0x2: ExtInt5 = 1; break;
case 0x4: ExtInt6 = 1; break;
case 0x8: ExtInt7 = 1; break;
}
if(ExtInt4)
{
rINTMSK ^= BIT_TIMER1; // toggle TIMER1 int mask status
}
if(ExtInt5)
{
rINTMSK ^= BIT_TIMER2; // toggle TIMER2 int mask status
}
if(ExtInt6)
{
rINTMSK ^= BIT_TIMER3; // toggle TIMER3 int mask status
}
if(ExtInt7)
{
rINTMSK ^= BIT_TIMER4; // toggle TIMER4 int mask status
}
rEXTINTPND = 0xf; // clear pending bit
rI_ISPC = BIT_EINT4567; // clear pending bit
}
void Timer_demo_end(void)
{
rINTMSK |= (BIT_EINT4567|BIT_TIMER0|BIT_TIMER1|BIT_TIMER2|BIT_TIMER3|BIT_TIMER4);
rPDATG = 0xff;
rTCON = 0x0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -