?? timer32.c
字號(hào):
/******************** (C) COPYRIGHT 2010 Embest Info&Tech Co.,LTD. ************
* 文件名: timer32.c
* 作者 : Wuhan R&D Center, Embest
* 日期 : 01/18/2010
* 描述 : 32位定時(shí)器相關(guān)的函數(shù)實(shí)現(xiàn)
*******************************************************************************
*******************************************************************************
* 歷史:
* 01/18/2010 : V1.0 初始版本
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "LPC11xx.h"
#include "timer32.h"
volatile uint32_t timer32_0_counter = 0;
volatile uint32_t timer32_1_counter = 0;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @函數(shù)名:delay32Ms
* @描述:以間隔毫秒啟動(dòng)定時(shí)器直到時(shí)間耗完
* @參數(shù):定時(shí)器號(hào),以毫秒為單位的延遲時(shí)間
* @返回值:無(wú)
*/
void delay32Ms(uint8_t timer_num, uint32_t delayInMs)
{
if (timer_num == 0)
{
/* 建立定時(shí)器0的延遲 */
/* 復(fù)位定時(shí)器 */
LPC_TMR32B0->TCR = 0x02;
/* 設(shè)置預(yù)分頻器為0 */
LPC_TMR32B0->PR = 0x00;
LPC_TMR32B0->MR0 = delayInMs * (SystemAHBFrequency / 1000);
/* 復(fù)位所有的中斷 */
LPC_TMR32B0->IR = 0xff;
/* 停止定時(shí)器匹配 */
LPC_TMR32B0->MCR = 0x04;
/* 啟動(dòng)定時(shí)器 */
LPC_TMR32B0->TCR = 0x01;
/* 等待直到延遲時(shí)間消耗完 */
while (LPC_TMR32B0->TCR & 0x01);
}
else if (timer_num == 1)
{
/* 建立定時(shí)器1的延遲 */
/* 復(fù)位定時(shí)器 */
LPC_TMR32B1->TCR = 0x02;
/* 設(shè)置預(yù)分頻器為0 */
LPC_TMR32B1->PR = 0x00;
LPC_TMR32B1->MR0 = delayInMs * (SystemAHBFrequency / 1000);
/* 復(fù)位所有的中斷 */
LPC_TMR32B1->IR = 0xff;
/* 停止定時(shí)器匹配 */
LPC_TMR32B1->MCR = 0x04;
/* 啟動(dòng)定時(shí)器 */
LPC_TMR32B1->TCR = 0x01;
/* 等待直到延遲時(shí)間消耗完 */
while (LPC_TMR32B1->TCR & 0x01);
}
return;
}
/**
* @函數(shù)名:TIMER32_0_IRQHandler
* @描述:定時(shí)器/計(jì)數(shù)器0中斷例程在60 MHz CPU時(shí)鐘下每10ms執(zhí)行一次
* @參數(shù):無(wú)
* @返回值:無(wú)
*/
void TIMER32_0_IRQHandler(void)
{
/* 清中斷標(biāo)志位 */
LPC_TMR32B0->IR = 1;
timer32_0_counter++;
return;
}
/**
* @函數(shù)名:TIMER32_1_IRQHandler
* @描述:定時(shí)器/計(jì)數(shù)器1中斷例程在60 MHz CPU時(shí)鐘下每10ms執(zhí)行一次
* @參數(shù):無(wú)
* @返回值:無(wú)
*/
void TIMER32_1_IRQHandler(void)
{
/* 清中斷標(biāo)志位 */
LPC_TMR32B1->IR = 1;
timer32_1_counter++;
return;
}
/**
* @函數(shù)名:enable_timer
* @描述:使能定時(shí)器
* @參數(shù):定時(shí)器號(hào):0 或 1
* @返回值:無(wú)
*/
void enable_timer32(uint8_t timer_num)
{
if ( timer_num == 0 )
{
LPC_TMR32B0->TCR = 1;
}
else
{
LPC_TMR32B1->TCR = 1;
}
return;
}
/**
* @函數(shù)名:disable_timer
* @描述:禁止定時(shí)器
* @參數(shù):定時(shí)器號(hào):0 或 1
* @返回值:無(wú)
*/
void disable_timer32(uint8_t timer_num)
{
if ( timer_num == 0 )
{
LPC_TMR32B0->TCR = 0;
}
else
{
LPC_TMR32B1->TCR = 0;
}
return;
}
/**
* @函數(shù)名:reset_timer
* @描述:重啟定時(shí)器
* @參數(shù):定時(shí)器號(hào):0 或 1
* @返回值:無(wú)
*/
void reset_timer32(uint8_t timer_num)
{
uint32_t regVal;
if ( timer_num == 0 )
{
regVal = LPC_TMR32B0->TCR;
regVal |= 0x02;
LPC_TMR32B0->TCR = regVal;
}
else
{
regVal = LPC_TMR32B1->TCR;
regVal |= 0x02;
LPC_TMR32B1->TCR = regVal;
}
return;
}
/**
* @函數(shù)名:init_timer
* @描述:初始化定時(shí)器,設(shè)置定時(shí)器間隔,復(fù)位定時(shí)器設(shè)置定時(shí)器中斷例程
* @參數(shù):定時(shí)器號(hào)和定時(shí)器間隔
* @返回值:無(wú)
*/
void init_timer32(uint8_t timer_num, uint32_t TimerInterval)
{
if ( timer_num == 0 )
{
/* 因?yàn)镴TAG和定時(shí)器CAP/MAT管腳是混合使用的如果在使用了該模塊是一些 I/O 管腳語(yǔ)言仔細(xì)的配置。 */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);
/* Timer0_32 I/O 配置 */
/* Timer0_32 CAP0 */
LPC_IOCON->PIO1_5 &= ~0x07;
LPC_IOCON->PIO1_5 |= 0x02;
/* Timer0_32 MAT0 */
LPC_IOCON->PIO1_6 &= ~0x07;
LPC_IOCON->PIO1_6 |= 0x02;
/* Timer0_32 MAT1 */
LPC_IOCON->PIO1_7 &= ~0x07;
LPC_IOCON->PIO1_7 |= 0x02;
/* Timer0_32 MAT2 */
LPC_IOCON->PIO0_1 &= ~0x07;
LPC_IOCON->PIO0_1 |= 0x02;
#ifdef __JTAG_DISABLED
/* Timer0_32 MAT3 */
LPC_IOCON->JTAG_TDI_PIO0_11 &= ~0x07;
LPC_IOCON->JTAG_TDI_PIO0_11 |= 0x03;
#endif
timer32_0_counter = 0;
LPC_TMR32B0->MR0 = TimerInterval;
// LPC_TMR32B0->EMR &= ~(0xFF<<4);
// LPC_TMR32B0->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8)|(0x3<<10)); /* MR0/1/2/3 Toggle */
/* MR0上的中斷和復(fù)位 */
LPC_TMR32B0->MCR = 3;
/* 使能TIMER0中斷 */
NVIC_EnableIRQ(TIMER_32_0_IRQn);
}
else if ( timer_num == 1 )
{
/* 因?yàn)镴TAG和定時(shí)器CAP/MAT管腳是混合使用的如果在使用了該模塊是一些 I/O 管腳語(yǔ)言仔細(xì)的配置。 */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);
#ifdef __JTAG_DISABLED
/* Timer1_32 I/O 配置 */
/* Timer1_32 CAP0 */
LPC_IOCON->JTAG_TMS_PIO1_0 &= ~0x07;
LPC_IOCON->JTAG_TMS_PIO1_0 |= 0x03;
/* Timer1_32 MAT0 */
LPC_IOCON->JTAG_TDO_PIO1_1 &= ~0x07;
LPC_IOCON->JTAG_TDO_PIO1_1 |= 0x03;
/* Timer1_32 MAT1 */
LPC_IOCON->JTAG_nTRST_PIO1_2 &= ~0x07;
LPC_IOCON->JTAG_nTRST_PIO1_2 |= 0x03;
/* Timer1_32 MAT2 */
LPC_IOCON->ARM_SWDIO_PIO1_3 &= ~0x07;
LPC_IOCON->ARM_SWDIO_PIO1_3 |= 0x03;
#endif
/* Timer0_32 MAT3 */
LPC_IOCON->PIO1_4 &= ~0x07;
LPC_IOCON->PIO1_4 |= 0x02;
timer32_1_counter = 0;
LPC_TMR32B1->MR0 = TimerInterval;
// LPC_TMR32B1->EMR &= ~(0xFF<<4);
// LPC_TMR32B1->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8)); /* MR0/1/2 Toggle */
/* MR0上的中斷和復(fù)位 */
LPC_TMR32B1->MCR = 3;
/* 使能TIMER1中斷 */
NVIC_EnableIRQ(TIMER_32_1_IRQn);
}
return;
}
/**
* @}
*/
/**
* @}
*/
/************* (C) COPYRIGHT 2010 Wuhan R&D Center, Embest *****文件結(jié)束*******/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -