lpcware

how to chain timers

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by kevin_liu on Mon Jul 28 16:55:10 MST 2014
Hi,

I am trying to chain 2 timers together in my code: let timer2 to take timer1's match output. However, I cannot get it work.  The following is my code.

I can get each timer work independently,

by toggling the lines controlling timer1 interrupt:
Chip_TIMER_MatchDisableInt(LPC_TIMER1, 3);
//Chip_TIMER_MatchEnableInt(LPC_TIMER1, 3);

and commenting out the line
  Chip_TIMER_TIMER_SetCountClockSrc(LPC_TIMER2, TIMER_CAPSRC_RISING_CAPN, 3);
  
However, I cannot chain them together.

/* Setup a chained timer. Timer2 will take input from Timer1's match 3.*/
static void setupChainTimer(void) {
Chip_TIMER_Init(LPC_TIMER1);
Chip_RGU_TriggerReset(RGU_TIMER1_RST);
while (Chip_RGU_InReset(RGU_TIMER1_RST)) {
}

Chip_TIMER_PrescaleSet(LPC_TIMER1, 0);

Chip_TIMER_Reset(LPC_TIMER1);
Chip_TIMER_ClearMatch(LPC_TIMER1, 3);
Chip_TIMER_SetMatch(TIMER_NUMBER, 3, 4);
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 3);
Chip_TIMER_MatchDisableInt(LPC_TIMER1, 3);
//Chip_TIMER_MatchEnableInt(LPC_TIMER1, 3);

Chip_TIMER_Enable(LPC_TIMER1);
NVIC_EnableIRQ(TIMER1_IRQn);
NVIC_ClearPendingIRQ(TIMER1_IRQn);

Chip_TIMER_Init(LPC_TIMER2);
Chip_RGU_TriggerReset(RGU_TIMER2_RST);
while (Chip_RGU_InReset(RGU_TIMER2_RST)) {
}

LPC_GIMA->CAP0_IN[2][3] = 0x1<<1|0x1<<2|0x2<<4;

Chip_TIMER_Reset(LPC_TIMER2);
Chip_TIMER_TIMER_SetCountClockSrc(LPC_TIMER2, TIMER_CAPSRC_RISING_CAPN, 3);
Chip_TIMER_ClearMatch(LPC_TIMER2, 1);
Chip_TIMER_SetMatch(LPC_TIMER2, 1, 1);
Chip_TIMER_MatchEnableInt(LPC_TIMER2, 1);
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER2, 1);
Chip_TIMER_Enable(LPC_TIMER2);

NVIC_EnableIRQ(TIMER2_IRQn);
NVIC_ClearPendingIRQ(TIMER2_IRQn);
}

//*****

void TIMER1_IRQHandler(void) {
static bool On = false;
static int count = 0;
count++;
if (Chip_TIMER_MatchPending(LPC_TIMER1, 3)) {
Chip_TIMER_ClearMatch(LPC_TIMER1, 3);
On = (bool) !On;
Board_LED_Set(6, On);
}
}

void TIMER2_IRQHandler(void) {
static bool On = false;
static int count = 0;
count++;

if (Chip_TIMER_MatchPending(LPC_TIMER2, 1)) {
Chip_TIMER_ClearMatch(LPC_TIMER2, 1);
On = (bool) !On;
Board_LED_Set(5, On);
}
}

Outcomes