找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6161|回復: 0
打印 上一主題 下一主題
收起左側(cè)

LPC11XX使用定時器做延時程序_12MHz

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:73735 發(fā)表于 2015-2-18 23:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Delay.c文件:
#include "LPC11XX.h"
#include "gpio.h"
#include "timer16.h"
#include "Delay.h"

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

void Delay_Us(unsigned int DelayTimes)             /* setup timer #1 for delay */
{       
        //LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /* 開啟定時器Timer16_1時鐘--時鐘配置中開啟該時鐘 */
  LPC_TMR16B1->TCR = 0x02;                               /* reset timer */
  LPC_TMR16B1->PR  = 12;                                   /* set prescaler to zero 根據(jù)AHB時鐘確定分頻值 */
  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);           /* 等待直到時間到 */
        LPC_TMR16B1->TCR = 0x00;                               /* Close timer --關(guān)閉定時器 */
}

void Delay_Ms(unsigned int DelayTimes)       /* setup timer #1 for delay */
{
        //LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /* 開啟定時器Timer16_1時鐘 */
  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);           /* 等待直到時間到 */
        LPC_TMR16B1->TCR = 0x00;                               /* Close timer */
}

void Delay_Sec(unsigned int DelayTimes)      /* setup timer #1 for delay */
{
        //LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /* 開啟定時器Timer16_1時鐘 */
  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);           /* 等待直到時間到 */
        LPC_TMR16B1->TCR = 0x00;                               /* Close timer */
}

Delay.h文件:

#ifndef     __DELAY_H__
#define     __DELAY_H__


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

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

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



#endif





分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表