****************************************************************************** ** Function name:init_timer ** ** Descriptions:Initialize timer, set timer interval, reset timer, **install timer interrupt handler ** ** parameters:timer number and timer interval ** Returned value:None ** ******************************************************************************/ void init_timer32(uint8_t timer_num, uint32_t TimerInterval) { if ( timer_num == 0 ) { /* Some of the I/O pins need to be carefully planned if you use below module because JTAG and TIMER CAP/MAT pins are muxed. */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); LPC_IOCON->PIO1_28 &= ~0x07;/* Timer0_32 I/O config */ LPC_IOCON->PIO1_28 |= 0x02;/* Timer0_32 CAP0 */ LPC_IOCON->PIO1_24 &= ~0x07; LPC_IOCON->PIO1_24 |= 0x02;/* Timer0_32 MAT0 */ LPC_IOCON->PIO1_25 &= ~0x07; LPC_IOCON->PIO1_25 |= 0x02;/* Timer0_32 MAT1 */ LPC_IOCON->PIO1_26 &= ~0x07; LPC_IOCON->PIO1_26|= 0x02;/* Timer0_32 MAT2 */ #ifdef __JTAG_DISABLED LPC_IOCON->PIO1_27 &= ~0x07; LPC_IOCON->PIO1_27 |= 0x03;/* Timer0_32 MAT3 */ #endif #if CONFIG_TIMER32_DEFAULT_TIMER32_0_IRQHANDLER==1 timer32_0_counter = 0; timer32_0_capture = 0; #endif //TIMER32_0_DEFAULT_HANDLER LPC_CT32B0->MR0 = TimerInterval; #if TIMER_MATCH LPC_CT32B0->EMR &= ~(0xFF<<4); LPC_CT32B0->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8)|(0x3<<10));/* MR0/1/2/3 Toggle */ #else /* Capture 0 on rising edge, interrupt enable. */ LPC_CT32B0->CCR = (0x1<<0)|(0x1<<2); #endif LPC_CT32B0->MCR = 3;/* Interrupt and Reset on MR0 */ #if CONFIG_TIMER32_DEFAULT_TIMER32_0_IRQHANDLER==1 /* Enable the TIMER0 Interrupt */ NVIC_EnableIRQ(TIMER_32_0_IRQn); #endif } else if ( timer_num == 1 ) { /* Some of the I/O pins need to be clearfully planned if you use below module because JTAG and TIMER CAP/MAT pins are muxed. */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10); #ifdef __JTAG_DISABLED LPC_IOCON->TMS_PIO0_12 &= ~0x07;/* Timer1_32 I/O config */ LPC_IOCON->TMS_PIO0_12 |= 0x03;/* Timer1_32 CAP0 */ LPC_IOCON->TDO_PIO0_13 &= ~0x07; LPC_IOCON->TDO_PIO0_13 |= 0x03;/* Timer1_32 MAT0 */ LPC_IOCON->TRST_PIO0_14 &= ~0x07; LPC_IOCON->TRST_PIO0_14 |= 0x03;/* Timer1_32 MAT1 */ LPC_IOCON->SWDIO_PIO0_15 &= ~0x07; LPC_IOCON->SWDIO_PIO0_15 |= 0x03;/* Timer1_32 MAT2 */ #endif LPC_IOCON->PIO1_27 &= ~0x07; LPC_IOCON->PIO1_27 |= 0x02;/* Timer0_32 MAT3 */ #if CONFIG_TIMER32_DEFAULT_TIMER32_1_IRQHANDLER==1 timer32_1_counter = 0; timer32_1_capture = 0; #endif //TIMER32_1_DEFAULT_HANDLER LPC_CT32B1->MR0 = TimerInterval; #if TIMER_MATCH LPC_CT32B1->EMR &= ~(0xFF<<4); LPC_CT32B1->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8)|(0x3<<10));/* MR0/1/2 Toggle */ #else /* Capture 0 on rising edge, interrupt enable. */ LPC_CT32B1->CCR = (0x1<<0)|(0x1<<2); #endif LPC_CT32B1->MCR = 3;/* Interrupt and Reset on MR0 */ #if CONFIG_TIMER32_DEFAULT_TIMER32_1_IRQHANDLER==1 /* Enable the TIMER1 Interrupt */ NVIC_EnableIRQ(TIMER_32_1_IRQn); #endif } return; } /****************************************************************************** ** Function name:init_timer32PWM ** ** Descriptions:Initialize timer as PWM ** ** parameters:timer number, period and match enable: **match_enable[0] = PWM for MAT0 **match_enable[1] = PWM for MAT1 **match_enable[2] = PWM for MAT2 ** ** Returned value:None ** ******************************************************************************/ void init_timer32PWM(uint8_t timer_num, uint32_t period, uint8_t match_enable) { disable_timer32(timer_num); if (timer_num == 1) { /* Some of the I/O pins need to be clearfully planned if you use below module because JTAG and TIMER CAP/MAT pins are muxed. */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10); /* Setup the external match register */ LPC_CT32B1->EMR = (1<<EMC3)|(1<<EMC2)|(2<<EMC1)|(1<<EMC0)|(1<<3)|(match_enable); /* Setup the outputs */ /* If match0 is enabled, set the output */ if (match_enable & 0x01) { LPC_IOCON->TDO_PIO0_13 &= ~0x07; LPC_IOCON->TDO_PIO0_13 |= 0x03;/* Timer1_32 MAT0 */ } /* If match1 is enabled, set the output */ if (match_enable & 0x02) { LPC_IOCON->TRST_PIO0_14 &= ~0x07; LPC_IOCON->TRST_PIO0_14 |= 0x03;/* Timer1_32 MAT1 */ } /* If match2 is enabled, set the output */ if (match_enable & 0x04) { LPC_IOCON->SWDIO_PIO0_15 &= ~0x07; LPC_IOCON->SWDIO_PIO0_15 |= 0x03;/* Timer1_32 MAT2 */ } /* If match3 is enabled, set the output */ if (match_enable & 0x08) { LPC_IOCON->PIO0_16 &= ~0x07; LPC_IOCON->PIO0_16 |= 0x02;/* Timer1_32 MAT3 */ } #ifdef __JTAG_DISABLED PIO1_0_JTAG_TMS &= ~0x07;/* Timer1_32 I/O config */ PIO1_0_JTAG_TMS |= 0x03;/* Timer1_32 CAP0 */ #endif /* Enable the selected PWMs and enable Match3 */ LPC_CT32B1->PWMC = (1<<3)|(match_enable); /* Setup the match registers */ /* set the period value to a global variable */ timer32_1_period = period; LPC_CT32B1->MR3 = timer32_1_period; LPC_CT32B1->MR0= timer32_1_period/2; LPC_CT32B1->MR1= timer32_1_period/2; LPC_CT32B1->MR2= timer32_1_period/2; /* */ LPC_CT32B1->MCR = 1<<10;/* Reset on MR3 */ } else { /* Some of the I/O pins need to be clearfully planned if you use below module because JTAG and TIMER CAP/MAT pins are muxed. */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); /* Setup the external match register */ LPC_CT32B0->EMR = (1<<EMC3)|(2<<EMC2)|(1<<EMC1)|(1<<EMC0)|(1<<3)|(match_enable); /* Setup the outputs */ /* If match0 is enabled, set the output */ if (match_enable & 0x01) { LPC_IOCON->PIO1_24 &= ~0x07; LPC_IOCON->PIO1_24 |= 0x02;/* Timer0_32 MAT0 */ } /* If match1 is enabled, set the output */ if (match_enable & 0x02) { LPC_IOCON-> PIO1_25 &= ~0x07; LPC_IOCON-> PIO1_25 |= 0x02;/* Timer0_32 MAT1 */ } /* If match2 is enabled, set the output */ if (match_enable & 0x04) { LPC_IOCON->PIO1_26 &= ~0x07; LPC_IOCON->PIO1_26 |= 0x02;/* Timer0_32 MAT2 */ } /* If match3 is enabled, set the output */ if (match_enable & 0x08) { LPC_IOCON->PIO1_27 &= ~0x07; LPC_IOCON->PIO1_27 |= 0x03;/* Timer0_32 MAT3 */ } /* Enable the selected PWMs and enable Match3 */ LPC_CT32B0->PWMC = (1<<3)|(match_enable); /* Setup the match registers */ /* set the period value to a global variable */ timer32_0_period = period; LPC_CT32B0->MR3 = timer32_0_period; LPC_CT32B0->MR0= timer32_0_period;///2; LPC_CT32B0->MR1= timer32_0_period/2; LPC_CT32B0->MR2= timer32_0_period/2; LPC_CT32B0->MCR = 1<<10;/* Reset on MR3 */ } } |