lpc的lpc1768定时器器分频值可以使随意的数值吗

TIMER软件设计与分析
本文所属图书&>&
本书从物联网理论与实践两个方面介绍了物联网技术。主要内容包含物联网综述、国内外物联网应用现状与愿景、传感器技术、RFID技术、有线传输与组网技术、无线传输与组网技术、低功耗ARM Cortex-M0微控制器、低功...&&
在使用定时器做相关工作的时候,主要需要进行以下的基本操作。
(1)定时器的初始化:选择具体使用的定时器及其时钟来源的设置、寄存器值的设置、I/O的配置、标志位的设置。
(2)中断处理函数的实现及其标志位的改变。
(3)应用程序(延时程序)的实现。
下面来看一下关于定时器的几个操作函数。
(1)定时器初始化函数init_timer16(uint8_t timer_num,uint32_t TimerInterval)。该函数实现了对I/O引脚的初始化、相关数值的设置以及中断的使能。其程序代码如下:
void init_timer32(uint8_t timer_num, uint32_t TimerInterval)
& if ( timer_num == 0 )
&&& /*根据选择的工作模式进行相应的管脚设置*/
&&& LPC_SYSCON-&SYSAHBCLKCTRL |= (1&&9);
&&& LPC_IOCON-&PIO1_5 &= ~0x07;&/*Timer0_32 I/O 功能配置*/
&&& LPC_IOCON-&PIO1_5 |= 0x02;&/*Timer0_32 CAP0 功能配置*/
&&& LPC_IOCON-&PIO1_6 &= ~0x07;
&&& LPC_IOCON-&PIO1_6 |= 0x02;&/*Timer0_32 MAT0 功能配置*/
&&& LPC_IOCON-&PIO1_7 &= ~0x07;
&&& LPC_IOCON-&PIO1_7 |= 0x02;&/*Timer0_32 MAT1 功能配置*/
&&& LPC_IOCON-&PIO0_1 &= ~0x07;&
&&& LPC_IOCON-&PIO0_1 |= 0x02;&/*Timer0_32 MAT2 功能配置*/
#ifdef __JTAG_DISABLED
&&& LPC_IOCON-&R_PIO0_11 &= ~0x07;&
&&& LPC_IOCON-&R_PIO0_11 |= 0x03;&/*Timer0_32 MAT3 功能配置*/
&&& timer32_0_counter = 0;
&&& timer32_0_capture = 0;
&&& LPC_TMR32B0-&MR0 = TimerI
#if TIMER_MATCH
&LPC_TMR32B0-&EMR &= ~(0xFF&&4);
&LPC_TMR32B0-&EMR |= ((0x3&&4)|(0x3&&6)|(0x3&&8)|(0x3&&10));&/*MR0/1/2/3 Toggle*/
&/*Capture 0 上升沿触发,并且使能中断*/
&LPC_TMR32B0-&CCR = (0x1&&0)|(0x1&&2);
&&& LPC_TMR32B0-&MCR = 3;&&&/*复位MR0*/
&&& /*使能Timer0中断*/
&&& NVIC_EnableIRQ(TIMER_32_0_IRQn);
& else if ( timer_num == 1 )
(2)中断处理函数的实现。该函数主要是进行中断相关位的改变。代码如下:
void TIMER32_0_IRQHandler(void)
& if ( LPC_TMR32B0-&IR & 0x01 )
&LPC_TMR32B0-&IR = 1;&&&&/*清除中断标志*/
&timer32_0_counter++;
& if ( LPC_TMR32B0-&IR & (0x1&&4) )
&LPC_TMR32B0-&IR = 0x1&&4;&&&/*清除中断标志*/
&timer32_0_capture++;&&&&/*计数值加1*/
(3)定时器的使能与禁止。具体实现如下:
① 使能定时器部分。实现代码如下:
void enable_timer32(uint8_t timer_num)
& if ( timer_num == 0 )
&&& LPC_TMR32B0-&TCR = 1;&&&//使能Timer0的计数器
&&& LPC_TMR32B1-&TCR = 1;
② 禁止定时器部分。实现代码如下:
void disable_timer32(uint8_t timer_num)
& if ( timer_num == 0 )
&&& LPC_TMR32B0-&TCR = 0;&&&&//禁能计数器
&&& LPC_TMR32B1-&TCR = 0;
(4)应用程序的编写&&延时程序。
void delay32Ms(uint8_t timer_num, uint32_t delayInMs)
& if (timer_num == 0)
&&& /*重启Timer0*/
&&& LPC_TMR32B0-&TCR = 0x02;&&/*复位Timer0*/
&&& LPC_TMR32B0-&PR& = 0x00;&&/*将分频值设置为0*/
&&& LPC_TMR32B0-&MR0 = delayInMs * (SystemAHBFrequency / 1000);
&&& LPC_TMR32B0-&IR& = 0&&/*复位所有的中断*/
&&& LPC_TMR32B0-&MCR = 0x04;&&/*停止MR0*/
&&& LPC_TMR32B0-&TCR = 0x01;&&/*启动定时器*/
&&& /*等待直到延时退去*/
&&& while (LPC_TMR32B0-&TCR & 0x01);
& else if (timer_num == 1)
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。
文章下载读书查看: 1788|回复: 0
LPC11XX使用定时器做延时程序_12MHz
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 = DelayT& && && && & /* 所需的延时时间 */
&&LPC_TMR16B1-&IR&&= 0& & & & & & & && && && && && & /* 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 = DelayT
&&LPC_TMR16B1-&IR&&= 0& & & & & & & && && && && && & /* 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*DelayT
&&LPC_TMR16B1-&IR&&= 0& & & & & & & && && && && && & /* 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);& && && & /*秒延时函数*/
Powered by他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)后使用快捷导航没有帐号?
查看: 3058|回复: 2
lpc1114的定时器问题
在线时间17 小时
TA的帖子TA的资源
一粒金砂(中级), 积分 60, 距离下一级还需 140 积分
一粒金砂(中级), 积分 60, 距离下一级还需 140 积分
我用lpc1114 定时器32 计数,想随时读取当前计数值,各位大大,是读取哪个寄存器看吧!我试了都不行啊,不是读TC么?
各位大大,路过好心告诉一声,我不用中断的,就想在随便一个IO口边沿有动作时,读取当中的间隔时间
在线时间17 小时
TA的帖子TA的资源
一粒金砂(中级), 积分 60, 距离下一级还需 140 积分
一粒金砂(中级), 积分 60, 距离下一级还需 140 积分
已解决了,在io中断里开定时器,当另一个来的时候,停止定时器,记取TC 再复位定时器,再打开,记得一定要复位,我就卡这复位上
在线时间6 小时
TA的帖子TA的资源
一粒金砂(中级), 积分 10, 距离下一级还需 190 积分
一粒金砂(中级), 积分 10, 距离下一级还需 190 积分
大哥,能不能把你初始化定时器的那部分程序分享一下,谢谢!
EEWORLD 官方微信
Powered by

我要回帖

更多关于 stm32定时器预分频 的文章

 

随机推荐