void pit_init(uint8 pitno,uint32 timeout) { SIM_SCGC6|=SIM_SCGC6_PIT_MASK; //使能PIT时钟 PIT_MCR&=~(PIT_MCR_MDIS_MASK); //调试模式下禁止 PIT_MCR|=PIT_MCR_FRZ_MASK; //使能PIT模块时钟 PIT_LDVAL(pitno)=timeout; //设置周期 PIT_TCTRL(pitno)|=PIT_TCTRL_TEN_MASK; //使能pit模块运行 PIT_TCTRL(pitno)&=~(PIT_TCTRL_TIE_MASK); //关pit中断 }
/************************************************************************* * 野火嵌入式開發工作室 * * 函數名稱:pit_init * 功能說明:PITn定時中斷 * 參數說明:PITn 模塊號(PIT0~PIT3) cnt 延時時間(單位為bus時鐘周期) * 函數返回:無 * 修改時間:2012-1-24 * 備 注: *************************************************************************/ void pit_init(PITn pitn,u32 cnt) { //PIT 用的是 Bus Clock 總線頻率 //溢出計數 = 總線頻率 * 時間 /* 開啟時鐘*/ SIM_SCGC6 |=SIM_SCGC6_PIT_MASK; //使能PIT時鐘 /* PIT模塊控制 PIT Module Control Register (PIT_MCR) */ PIT_MCR &=~(PIT_MCR_MDIS_MASK | PIT_MCR_FRZ_MASK ); //使能PIT定時器時鐘 ,調試模式下繼續運行 /* 定時器加載值設置 Timer Load Value Register (PIT_LDVALn) */ PIT_LDVAL(pitn) =cnt; //設置溢出中斷時間 //定時時間到了後,TIF 置 1 。寫1的時候就會清0 PIT_Flag_Clear(pitn); //清中斷標志位 /* 定時器控制寄存器 Timer Control Register (PIT_TCTRL0) */ PIT_TCTRL(pitn) |=( PIT_TCTRL_TEN_MASK | PIT_TCTRL_TIE_MASK ); //使能 PITn定時器,並開PITn中斷 enable_irq(pitn+68); //開接收引腳的IRQ中斷 }
/******************************************************************************* * void pit_init(uint32_t pit, uint32_t frq, uint32_t interrupt) * * Description: * Initialize specified periodic interrupt timer (PIT) with specified * repetition frequency. * * Parameters: * pit PIT channel to be initialized (PIT0, PIT1, PIT2, PIT3) * frq PIT repetition frequency in Hz * interrupt PIT interrupt generation (ENABLE_INT, DISABLE_INT) * * Return: * none * * Example: * pit_init(PIT0, 1000000, ENABLE_INT); * Initialize PIT0 with a repetition frequency of 1000000 Hz. After the * expiration of a period, PIT0 interrupt service routine calls PIT0 callback * function. * * Notes: * PIT module is clocked by bus clock. * * History: * pka, 27/AUG/2014, removed automatic start of PIT module * pka, 25/MAR/2014, initial code *******************************************************************************/ void pit_init(uint32_t pit, uint32_t frq, uint32_t interrupt) { /* Enable PIT module clock. */ SIM_SCGC6 |= SIM_SCGC6_PIT_MASK; /* Enable PIT module. PIT_MCR[MDIS] bit MUST be enabled before any other * bit manipulation is done. */ PIT_MCR &= ~PIT_MCR_MDIS_MASK; /* Set timer start value. The timer counts down until it reaches 0. When 0 * is reached an interrupt will be generated. */ PIT_LDVAL(pit) = PIT_CLK / frq - 1; /* Check if interrupts are enabled. */ if (interrupt) { /* Enable timer interrupt. */ PIT_TCTRL(pit) |= PIT_TCTRL_TIE_MASK; /* Enable PIT interrupt. */ enable_int(INT_PIT); } }
/******************************************************************************* * void pit_set(uint32_t pit, uint32_t frq) * * Description: * Set repetition frequency of specified periodic interrupt timer (PIT). * * Parameters: * pit PIT channel to be set * frq PIT repetition frequency in Hz * * Return: * actual PIT repetition frequency * * Example: * pit0_frq = pit_set(PIT0, 1000000); * Set PIT0 repetition frequency and get actual frequency value. * * Notes: * According to the specified PIT frequency the timer start value is * calculated. The calculation of this start value contains a division. Since * the start value is loaded to a 32-bit register this may cause rounding * errors and subsequently a wrong PIT repetition frequency. * * History: * pka, 11/SEP/2014, initial code *******************************************************************************/ void pit_set(uint32_t pit, uint32_t frq) { /* Set timer start value. */ PIT_LDVAL(pit) = PIT_CLK / frq - 1; }