找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5435|回复: 0
打印 上一主题 下一主题
收起左侧

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

[复制链接]
跳转到指定楼层
楼主
ID:73735 发表于 2015-2-18 23:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Delay.c文件:
#include "LPC11XX.h"
#include "gpio.h"
#include "timer16.h"
#include "Delay.h"

/*系统时钟配置为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 根据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 --关闭定时器 */
}

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);            /*微秒延时函数*/

void Delay_Ms(unsigned int DelayTimes);            /*毫秒延时函数*/

void Delay_Sec(unsigned int DelayTimes);          /*秒延时函数*/



#endif





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表