📄 timer.c
字号:
/*****************************************************************************
* timer.c: Timer C file for NXP LPC230x Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2006.09.01 ver 1.00 Prelimnary version, first Release
*
******************************************************************************/
#include "LPC23xx.h" /* LPC23xx/24xx Peripheral Registers */
#include "type.h"
#include "irq.h"
#include "timer.h"
volatile DWORD timer0_counter = 0;
volatile DWORD timer1_counter = 0;
/******************************************************************************
** Function name: Timer0_Handler
**
** Descriptions: Timer/Counter 0 interrupt handler
**
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void Timer0_Handler (void) __irq
{
T0IR = 1; /* clear interrupt flag */
timer0_counter++;
VICVectAddr = 0; /* Acknowledge Interrupt */
}
/******************************************************************************
** Function name: enable_timer0
**
** Descriptions: Enable timer 0
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void enable_timer0( void )
{
T0TCR |= 1;
}
/******************************************************************************
** Function name: enable_timer0
**
** Descriptions: Enable timer 0
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void enable_timer1( void )
{
T1TCR |= 1;
}
/******************************************************************************
** Function name: disable_timer0
**
** Descriptions: Disable timer 0
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void disable_timer0( void )
{
T0TCR &= ~0X1;
}
/******************************************************************************
** Function name: disable_timer1
**
** Descriptions: Disable timer 1
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void disable_timer1( void )
{
T1TCR &= ~0X1;
}
/******************************************************************************
** Function name: reset_timer0
**
** Descriptions: Reset timer 0
**
** parameters: none
** Returned value: None
**
******************************************************************************/
void reset_timer0( void )
{
T0TCR |= 0x02; /* Set reset bit */
T0TCR &= ~0x02; /* Clear reset bit */
}
/******************************************************************************
** Function name: reset_timer1
**
** Descriptions: Reset timer 1
**
** parameters: none
** Returned value: None
**
******************************************************************************/
void reset_timer1( void )
{
T1TCR |= 0x02; /* Set reset bit */
T1TCR &= ~0x02; /* Clear reset bit */
}
DWORD init_timer0 ( DWORD timerInterval )
{
PCONP |= (1 << 1); /* enable power to timer0 */ PCLKSEL0 &= ~(0x03 << 2); /* clear */
PCLKSEL0 |= (0x01 << 2); /* set pclk=cclk 01; pclk=cclk/4 00; pclk=cclk/2 02*/
setTimerCounterMode( 0, TIM_MODE_CNT_ON_FE, 0); /* falling edges; */
setTimerMatchControl(0, 0, TIM_MCR_RST | TIM_MCR_INT);
setTimerMatchValue(0, 0, timerInterval); /* set interval match 0 pin; divide by 256 */
setTimerExternalMatch(0, 0, 1, TIM_EMC_TOGGLE); /* toggle external match0 pin; init outvalue =1 */
PINSEL7 &= ~(0x3 << 14 | 0x3 << 18 ); /* clear p3.23 and p3.25 pin selects */
PINSEL7 |= (0x2 << 14 | 0x2 << 18); /* set p3.23 pin to CAP0.0; p3.25 pin to MAT0.0; */
return 0;
}
DWORD init_timer1 ( DWORD timerInterval )
{
PCONP |= (1 << 2); /* enable power to timer1 */ PCLKSEL0 &= ~(0x03 << 4); /* clear */
PCLKSEL0 |= (0x01 << 4); /* set pclk=cclk 01; pclk=cclk/4 00; pclk=cclk/2 02*/
setTimerCounterMode( 1, TIM_MODE_CNT_ON_FE, 1); /* T1, count on falling edges, Capture 1 */
setTimerMatchControl(1, 0, TIM_MCR_RST ); /* reset on Match 0 */
setTimerMatchValue(1, 0, timerInterval); /* set interval for match0 pin; divide by 256 */
setTimerExternalMatch(1, 0, 1, TIM_EMC_TOGGLE); /* toggle external match0 pin */
// PINSEL3 &= ~(0x3 << 6 | 0x3 << 12 ); /* clear p1.19 and p1.22 pin selects */
PINSEL3 |= (0x3 << 6 | 0x3 << 12); /* set p1.19 pin to CAP0.0; p1.22 pin to MAT0.0; */
return 0;
}
DWORD setTimerCounterMode(DWORD timer, DWORD mode, DWORD inputSelect)
{
timer &= 0x03; /* limit timer range to 2 bits */
mode &= 0x03; /* 0 = timer; 1= counter RE; 2= counter FE; 3= counter BE */
inputSelect &= 0x03;
if (timer == 0x00)
{
T0CTCR &= ~0xf; /* clear mode and input select */
T0CTCR |= (inputSelect << 0x2 | mode); /* set mode & capture input */
if (mode != 0) T0CCR &= ~(0x7<<(inputSelect*3)); /* Set Capture control bits to zero on inputSelect */
}
if (timer == 0x01)
{
T1CTCR &= ~0xf; /* clear mode and input select*/
T1CTCR |= (inputSelect<< 0x2 | mode); /* set mode & capture input */
if (mode != 0) T1CCR &= ~(0x7<<(inputSelect*3)); /* Set Capture control bits to zero on inputSelect */
}
return 0;
}
DWORD setTimerPrescaleValue(DWORD timer, DWORD value)
{
timer &= 0x01; /* limit to two timers */
if (timer == 0x00) T0PR = value;
if (timer == 0x01) T1PR = value;
return 0;
}
DWORD setTimerMatchValue(DWORD timer, DWORD matchReg, DWORD matchValue)
{
timer &= 0x01; /* limit to two timers */
matchReg &= 0x01;
if (timer == 0x00)
{
if (matchReg == 0x00) T0MR0 = matchValue;
if (matchReg == 0x01) T0MR1 = matchValue;
}
if (timer == 0x01)
{
if (matchReg == 0x00) T1MR0 = matchValue;
if (matchReg == 0x01) T1MR1 = matchValue;
}
return 0;
}
DWORD setTimerMatchControl(DWORD timer, DWORD match, DWORD control)
{
timer &= 0x01; /* limit to four timers */
control &= 0x07;
match &= 0x01;
if (timer == 0x00)
{
T0MCR &= ~(0x7 << (match * 3)); /* clear match field */
T0MCR |= (control<< (match * 3)); /* set capture input */
}
if (timer == 0x01)
{
T1MCR &= ~(0x7 << (match * 3)); /* clear match field */
T1MCR |= (control<< (match * 3)); /* set capture input */
}
return 0;
}
DWORD setTimerExternalMatch(DWORD timer, DWORD match, DWORD value, DWORD matchControl)
{
timer &= 0x01; /* limit to four timers */
matchControl &= 0x07;
match &= 0x03;
value &= 0x01;
if (timer == 0x00)
{
T0EMR &= ~(( 0x01 << match)|(0x03 << ( 4 + (match * 2)))); /* clear match field */
T0EMR |= ((value << match)|(matchControl << ( 4 + (match * 2)))); /* set capture input */
}
if (timer == 0x01)
{
T1EMR &= ~(( 0x01 << match)|(0x03 << ( 4 + (match * 2)))); /* clear match field */
T1EMR |= ((value << match)|(matchControl << ( 4 + (match * 2)))); /* set capture input */
}
return 0;
}
DWORD clearTimerExternalMatch(DWORD timer, DWORD match)
{
timer &= 0x01; /* limit to four timers */
match &= 0x03;
if (timer == 0x00)
{
T0EMR &= ~((0x01 << match)|(0x03 << ( 4 + (match * 2)))); /* clear match field */
}
if (timer == 0x01)
{
T1EMR &= ~((0x01 << match)|(0x03 << ( 4 + (match * 2)))); /* clear match field */
}
return 0;
}
/******************************************************************************
** End Of File
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -