標(biāo)題: LPC11XX使用定時(shí)器做延時(shí)程序_12MHz [打印本頁(yè)]

作者: huge    時(shí)間: 2015-2-18 23:07
標(biāo)題: LPC11XX使用定時(shí)器做延時(shí)程序_12MHz
Delay.c文件:
#include "LPC11XX.h"
#include "gpio.h"
#include "timer16.h"
#include "Delay.h"

/*系統(tǒng)時(shí)鐘配置為12MHz,因此提供給定時(shí)器的時(shí)鐘為12Mhz,因此得出以下分頻值和匹配值*/

void Delay_Us(unsigned int DelayTimes)             /* setup timer #1 for delay */
{       
        //LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /* 開(kāi)啟定時(shí)器Timer16_1時(shí)鐘--時(shí)鐘配置中開(kāi)啟該時(shí)鐘 */
  LPC_TMR16B1->TCR = 0x02;                               /* reset timer */
  LPC_TMR16B1->PR  = 12;                                   /* set prescaler to zero 根據(jù)AHB時(shí)鐘確定分頻值 */
  LPC_TMR16B1->MR0 = DelayTimes;             /* 所需的延時(shí)時(shí)間 */
  LPC_TMR16B1->IR  = 0xff;                               /* reset all interrrupts 復(fù)位所有的中斷 */
  LPC_TMR16B1->MCR = 0x04;                               /* stop timer on match --達(dá)到匹配停止 */
  LPC_TMR16B1->TCR = 0x01;                               /* start timer 啟動(dòng)定時(shí)器 */

        while (LPC_TMR16B1->TCR & 0x01);           /* 等待直到時(shí)間到 */
        LPC_TMR16B1->TCR = 0x00;                               /* Close timer --關(guān)閉定時(shí)器 */
}

void Delay_Ms(unsigned int DelayTimes)       /* setup timer #1 for delay */
{
        //LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /* 開(kāi)啟定時(shí)器Timer16_1時(shí)鐘 */
  LPC_TMR16B1->TCR = 0x02;                               /* reset timer */
  LPC_TMR16B1->PR  = 12000;                               /* set prescaler to zero */
  LPC_TMR16B1->MR0 = DelayTimes;
  LPC_TMR16B1->IR  = 0xff;                               /* reset all interrrupts */
  LPC_TMR16B1->MCR = 0x04;                               /* stop timer on match */
  LPC_TMR16B1->TCR = 0x01;                               /* start timer */

        while (LPC_TMR16B1->TCR & 0x01);           /* 等待直到時(shí)間到 */
        LPC_TMR16B1->TCR = 0x00;                               /* Close timer */
}

void Delay_Sec(unsigned int DelayTimes)      /* setup timer #1 for delay */
{
        //LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /* 開(kāi)啟定時(shí)器Timer16_1時(shí)鐘 */
  LPC_TMR16B1->TCR = 0x02;                               /* reset timer */
  LPC_TMR16B1->PR  = 60000;                               /* set prescaler to zero */
  LPC_TMR16B1->MR0 = 200*DelayTimes;
  LPC_TMR16B1->IR  = 0xff;                               /* reset all interrrupts */
  LPC_TMR16B1->MCR = 0x04;                               /* stop timer on match */
  LPC_TMR16B1->TCR = 0x01;                               /* start timer */

        while (LPC_TMR16B1->TCR & 0x01);           /* 等待直到時(shí)間到 */
        LPC_TMR16B1->TCR = 0x00;                               /* Close timer */
}

Delay.h文件:

#ifndef     __DELAY_H__
#define     __DELAY_H__


void Delay_Us(unsigned int DelayTimes);            /*微秒延時(shí)函數(shù)*/

void Delay_Ms(unsigned int DelayTimes);            /*毫秒延時(shí)函數(shù)*/

void Delay_Sec(unsigned int DelayTimes);          /*秒延時(shí)函數(shù)*/



#endif










歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1