keil_examples/timer/timer.c
/******************************************************************************
** Function name:		init_timer
**
** Descriptions:		Initialize timer, set timer interval, reset timer,
**						install timer interrupt handler
**
** parameters:			timer number and timer interval
** Returned value:		true or false, if the interrupt handler can't be
**						installed, return false.
** 
******************************************************************************/
Function name:         init_timer
Descriptions:          Initialize timer, set timer interval, reset timer,
install timer interrupt handler

timer number and timer interval
Returned value:                true or false, if the interrupt handler can't be
installed, return false.
Function name : init_timer Descriptions : Initialize timer set timer interval reset timer install timer interrupt handler timer number and timer interval Returned value : true or false if the interrupt handler can ' t be installed return false .
uint32_t init_timer ( uint8_t timer_num, uint32_t TimerInterval ) 
{
  uint32_t pclkdiv, pclk;
  if ( timer_num == 0 )
  {
	timer0_m0_counter = 0;
	timer0_m1_counter = 0;
	timer0_capture0 = 0;
	timer0_capture1 = 0;
	LPC_SC->PCONP |= (0x01<<1);
#if TIMER_MATCH
	LPC_PINCON->PINSEL3 &= ~((0x3<<24)|(0x3<<26));
	LPC_PINCON->PINSEL3 |= ((0x3<<24)|(0x3<<26));
#else
	LPC_PINCON->PINSEL3 &= ~((0x3<<20)|(0x3<<22));
	LPC_PINCON->PINSEL3 |= ((0x3<<20)|(0x3<<22));
#endif
	LPC_TIM0->IR = 0x0F;           
	pclkdiv = (LPC_SC->PCLKSEL0 >> 2) & 0x03;
	switch ( pclkdiv )
	{
	  case 0x00:
	  default:
		pclk = SystemFrequency/4;
		break;
	  case 0x01:
		pclk = SystemFrequency;
		break; 
	  case 0x02:
		pclk = SystemFrequency/2;
		break; 
	  case 0x03:
		pclk = SystemFrequency/8;
		break;
	}
	LPC_TIM0->PR  = pclk/1000000; 
	LPC_TIM0->MR0 = TIME_INTERVALmS * 10; 
	LPC_TIM0->MR1 = TIME_INTERVALmS * 10; 
#if TIMER_MATCH
	LPC_TIM0->EMR &= ~(0xFF<<4);
	LPC_TIM0->EMR |= ((0x3<<4)|(0x3<<6));
#else
	LPC_TIM0->CCR = (0x1<<0)|(0x1<<2)|(0x1<<3)|(0x1<<5);
#endif
	LPC_TIM0->MCR = (0x3<<0)|(0x3<<3);	
	NVIC_EnableIRQ(TIMER0_IRQn);
	return (TRUE);
  }
  else if ( timer_num == 1 )
  {
	timer1_m0_counter = 0;
	timer1_m1_counter = 0;
	timer1_capture0 = 0;
	timer1_capture1 = 0;
	LPC_SC->PCONP |= (0x1<<2);
#if TIMER_MATCH
	LPC_PINCON->PINSEL3 &= ~((0x3<<12)|(0x3<<18));
	LPC_PINCON->PINSEL3 |= ((0x3<<12)|(0x3<<18));
#else
	LPC_PINCON->PINSEL3 &= ~((0x3<<4)|(0x3<<6));
	LPC_PINCON->PINSEL3 |= ((0x3<<4)|(0x3<<6));
#endif
	LPC_TIM1->IR = 0x0F;          
	pclkdiv = (LPC_SC->PCLKSEL0 >> 4) & 0x03;
	switch ( pclkdiv )
	{
	  case 0x00:
	  default:
		pclk = SystemFrequency/4;
		break;
	  case 0x01:
		pclk = SystemFrequency;
		break; 
	  case 0x02:
		pclk = SystemFrequency/2;
		break; 
	  case 0x03:
		pclk = SystemFrequency/8;
		break;
	} 
	LPC_TIM1->PR  = pclk/1000000; 
	LPC_TIM1->MR0 = TIME_INTERVALmS * 10; 
	LPC_TIM1->MR1 = TIME_INTERVALmS * 10; 
#if TIMER_MATCH
	LPC_TIM1->EMR &= ~(0xFF<<4);
	LPC_TIM1->EMR |= ((0x3<<4)|(0x3<<6));
#else
	LPC_TIM1->CCR = (0x1<<0)|(0x1<<2)|(0x1<<3)|(0x1<<5);
#endif
	LPC_TIM1->MCR = (0x3<<0)|(0x3<<3);	
	NVIC_EnableIRQ(TIMER1_IRQn);
	return (TRUE);
  }
  return (FALSE);
}
uint32_t init_timer ( uint8_t timer_num , uint32_t TimerInterval ) { uint32_t pclkdiv , pclk ; if ( timer_num == 0 ) { timer0_m0_counter = 0 ; timer0_m1_counter = 0 ; timer0_capture0 = 0 ; timer0_capture1 = 0 ; LPC_SC -> PCONP |= ( 0x01 << 1 ) ; #if TIMER_MATCH LPC_PINCON -> PINSEL3 &= ~ ( ( 0x3 << 24 ) | ( 0x3 << 26 ) ) ; LPC_PINCON -> PINSEL3 |= ( ( 0x3 << 24 ) | ( 0x3 << 26 ) ) ; #else LPC_PINCON -> PINSEL3 &= ~ ( ( 0x3 << 20 ) | ( 0x3 << 22 ) ) ; LPC_PINCON -> PINSEL3 |= ( ( 0x3 << 20 ) | ( 0x3 << 22 ) ) ; #endif LPC_TIM0 -> IR = 0x0F ; pclkdiv = ( LPC_SC -> PCLKSEL0 >> 2 ) & 0x03 ; switch ( pclkdiv ) { case 0x00 : default : pclk = SystemFrequency / 4 ; break ; case 0x01 : pclk = SystemFrequency ; break ; case 0x02 : pclk = SystemFrequency / 2 ; break ; case 0x03 : pclk = SystemFrequency / 8 ; break ; } LPC_TIM0 -> PR = pclk / 1000000 ; LPC_TIM0 -> MR0 = TIME_INTERVALmS * 10 ; LPC_TIM0 -> MR1 = TIME_INTERVALmS * 10 ; #if TIMER_MATCH LPC_TIM0 -> EMR &= ~ ( 0xFF << 4 ) ; LPC_TIM0 -> EMR |= ( ( 0x3 << 4 ) | ( 0x3 << 6 ) ) ; #else LPC_TIM0 -> CCR = ( 0x1 << 0 ) | ( 0x1 << 2 ) | ( 0x1 << 3 ) | ( 0x1 << 5 ) ; #endif LPC_TIM0 -> MCR = ( 0x3 << 0 ) | ( 0x3 << 3 ) ; NVIC_EnableIRQ ( TIMER0_IRQn ) ; return ( TRUE ) ; } else if ( timer_num == 1 ) { timer1_m0_counter = 0 ; timer1_m1_counter = 0 ; timer1_capture0 = 0 ; timer1_capture1 = 0 ; LPC_SC -> PCONP |= ( 0x1 << 2 ) ; #if TIMER_MATCH LPC_PINCON -> PINSEL3 &= ~ ( ( 0x3 << 12 ) | ( 0x3 << 18 ) ) ; LPC_PINCON -> PINSEL3 |= ( ( 0x3 << 12 ) | ( 0x3 << 18 ) ) ; #else LPC_PINCON -> PINSEL3 &= ~ ( ( 0x3 << 4 ) | ( 0x3 << 6 ) ) ; LPC_PINCON -> PINSEL3 |= ( ( 0x3 << 4 ) | ( 0x3 << 6 ) ) ; #endif LPC_TIM1 -> IR = 0x0F ; pclkdiv = ( LPC_SC -> PCLKSEL0 >> 4 ) & 0x03 ; switch ( pclkdiv ) { case 0x00 : default : pclk = SystemFrequency / 4 ; break ; case 0x01 : pclk = SystemFrequency ; break ; case 0x02 : pclk = SystemFrequency / 2 ; break ; case 0x03 : pclk = SystemFrequency / 8 ; break ; } LPC_TIM1 -> PR = pclk / 1000000 ; LPC_TIM1 -> MR0 = TIME_INTERVALmS * 10 ; LPC_TIM1 -> MR1 = TIME_INTERVALmS * 10 ; #if TIMER_MATCH LPC_TIM1 -> EMR &= ~ ( 0xFF << 4 ) ; LPC_TIM1 -> EMR |= ( ( 0x3 << 4 ) | ( 0x3 << 6 ) ) ; #else LPC_TIM1 -> CCR = ( 0x1 << 0 ) | ( 0x1 << 2 ) | ( 0x1 << 3 ) | ( 0x1 << 5 ) ; #endif LPC_TIM1 -> MCR = ( 0x3 << 0 ) | ( 0x3 << 3 ) ; NVIC_EnableIRQ ( TIMER1_IRQn ) ; return ( TRUE ) ; } return ( FALSE ) ; }
Function name:         init_timer
Descriptions:          Initialize timer, set timer interval, reset timer,
install timer interrupt handler
Function name : init_timer Descriptions : Initialize timer set timer interval reset timer install timer interrupt handler
/* Clear MATx interrupt include DMA request */ /* By default, the PCLKSELx value is zero, thus, the PCLK for
	all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 2~3 is for TIMER0 */ /* set prescaler to get 1 M counts/sec */ /* Set up 10 mS interval */ /* Set up 10 mS interval */ /* Capture 0 and 1 on rising edge, interrupt enable. */ /* Interrupt and Reset on MR0 and MR1 */ /* Clear MATx interrupt include DMA request */ /* By default, the PCLKSELx value is zero, thus, the PCLK for
	all the peripherals is 1/4 of the SystemFrequency. */ /* Bit 4~5 is for TIMER0 */ /* set prescaler to get 1 M counts/sec */ /* Set up 10 mS interval */ /* Set up 10 mS interval */ /* Capture 0/1 on rising edge, interrupt enable. */ /* Interrupt and Reset on MR0 and MR1 */