定時(shí)器應(yīng)用程序
#include 《hidef.h》 /* common defines and macros */
#include “derivative.h” /* derivative-specific definitions */
// 函數(shù)聲明
void OutputCompare_Init(void);;
void Service_WD(void);
void SetBusClock_24MHz(void);
// 全局變量
uint Timer7_Cnt=0;
void main(void) {
/* put your own code here */
SetBusClock_24MHz();
OutputCompare_Init();
EnableInterrupts;
for(;;) {
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
void OutputCompare_Init(void)
{
TSCR1_TEN = 0; /* Disable Timer module before adjusting registers. */
TIOS_IOS7 = 1; /* Set Channel 0 as output compare. */
TCTL1_OM7 = 0; /* Set channel 0 to toggle when a Timer match occurs. */
TCTL1_OL7 = 1; /* Set channel 0 to toggle when a Timer match occurs. */
TC7 = 0x4926; /* Set a value for channel 0 timer compare. */
TIE_C7I = 1; /* Enable channel 0 interrupt, handled by function TIM0ISR. */
TSCR1_TSWAI = 1; /* Disables the timer module while in wait mode. */
TSCR1_TSFRZ = 1; /* Disables the timer counter while in freeze mode. */
TSCR2_PR = 0x7; /* Set prescaler to divide by 128 */
TSCR2_TCRE = 1;
TSCR1_TEN = 1; /* Timer Enable. */
//中斷周期:0x4926*128/24MHz = 100ms
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt VectorNumber_Vtimch7 TIM7_ISR(void)
{
Timer7_Cnt++;
TFLG1 = TFLG1_C7F_MASK; /* Clear channel 0 flag. */
}
#pragma CODE_SEG DEFAULT
// 看門(mén)狗
void Service_WD(void)
{
CPMUARMCOP = 0x55;
CPMUARMCOP = 0xAA;
}
void SetBusClock_24MHz(void)
{
CPMUOSC_OSCE = 1; /* enable ext osc */
/*
Initialise the system clock from a 16 MHz Crystal,
24 MHz Bus CLK (48 MHz VCO, 48 MHz PLL)
*/
CPMUSYNR = 0x00 | 0x05; /* VCOFRQ[7:6], SYNDIV[5:0] */
CPMUREFDIV = 0x20 | 0x03; /* REFFRQ[7:6], REFDIV[3:0] */
CPMUPOSTDIV = 0x00; /* POSTDIV = 0 FPLL = FVCO */
while(!CPMUFLG_LOCK); /* wait for VCO to stabilize*/
Service_WD();
CPMUCLKS_PLLSEL = 1; /* Switch clk to use PLL */
}
評(píng)論
查看更多