Problem with multiple timers LPC1769

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with multiple timers LPC1769

1,224 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ghh on Sat Apr 11 13:34:54 MST 2015
I have been trying to implement three different timers for my school project. The timers work properly when i removed the other timers. However, when i run them together the timers do not run for the expected time.
The time each timer is supposed to take:
TIMER0: 250ms
TIMER1: 1000ms
TIMER2: 3000ms

My code:

volatile int startVal = 0;
volatile int endVal = 0;

volatile int startVal2 = 0;
volatile int endVal2 = 0;

volatile int startVal3 = 0;
volatile int endVal3 = 0;

volatile uint32_t msTicks; // counter for 1ms SysTicks

//  SysTick_Handler - just increment SysTick counter
void SysTick_Handler(void)
{
msTicks++;
}


void timer_interrupt()
{
init_segment_display_interrupt();
init_sampling_interrupt();
init_restrictedLedISR();

}

void init_segment_display_interrupt() {
LPC_SC->PCONP |= 1 << 1; // Power up
LPC_SC->PCLKSEL0 |= 0x01 << 2;

LPC_TIM0 ->TCR = (0 << 0);
LPC_TIM0 ->CCR |= (0x00 << 0);
LPC_TIM0 ->TC = 0;
LPC_TIM0 ->PR = (100000000/4.0) - 1; // (CLK/ Peripheral divider * Hz) -1
LPC_TIM0 ->PC = 0;
LPC_TIM0 ->MCR |= (1 << 0);
LPC_TIM0 ->MR0 = 1;
LPC_TIM0 ->IR = 0xFF;
LPC_TIM0-> TCR = (1<<0); // start counting
NVIC_EnableIRQ(TIMER0_IRQn);
}

void init_sampling_interrupt(){
LPC_SC->PCONP |= 1<<2;
LPC_SC->PCLKSEL0 |= 0x01 << 4;

LPC_TIM1 ->TCR = (0 << 0);
LPC_TIM1 ->CCR |= (0x00 << 0);
LPC_TIM1 ->TC = 0;
LPC_TIM1 ->PR = (100000000/1.0) - 1;
LPC_TIM1 ->PC = 0;
LPC_TIM1 ->MCR |= (1 << 0);
LPC_TIM1 ->MR0 = 1;
LPC_TIM1 ->IR = 0xFF;
LPC_TIM1-> TCR = (1<<0); // start counting
NVIC_EnableIRQ(TIMER1_IRQn);
}

void init_restrictedLedISR(){
LPC_SC->PCONP |= 1<<22;
LPC_SC->PCLKSEL1 |= 0x01 << 12;

LPC_TIM2 ->TCR = (0 << 0);
LPC_TIM2 ->CCR |= (0x00 << 0);
LPC_TIM2 ->TC = 0;
LPC_TIM2 ->PR = (100000000/1.0) - 1;
LPC_TIM2 ->PC = 0;
LPC_TIM2 ->MCR |= (1 << 0);
LPC_TIM2 ->MR0 = 3;
LPC_TIM2 ->IR = 0xFF;
LPC_TIM2-> TCR = (1<<0); // start counting
NVIC_EnableIRQ(TIMER2_IRQn);

}


void TIMER0_IRQHandler(void)
{
startVal = (int) msTicks;
LPC_TIM0-> TCR = (10<<0); // stop timer
LPC_TIM0->IR = 0xFF;
int temp =  startVal - endVal;
printf("Time in 0 is %d\r\n",temp);
// stuff
LPC_TIM0-> TCR = (1<<0); // start counting
endVal = (int) msTicks;
}

void TIMER1_IRQHandler(void)
{
startVal2 = (int) msTicks;
LPC_TIM1-> TCR = (10<<0); // stop timer
LPC_TIM1->IR = 0xFF;
int temp = startVal2 - endVal2;
printf("Time in 1 is %d\r\n",temp);
// stuff
LPC_TIM1-> TCR = (1<<0); // start counting
endVal2 = (int) msTicks;

}

void TIMER2_IRQHandler(void)
{
startVal3 = (int) msTicks;
LPC_TIM2-> TCR = (10<<0); // stop timer
LPC_TIM2->IR = 0xFF;
int temp =  startVal3 - endVal3;
printf("Time in 2 is %d\r\n",temp);
// stuff
LPC_TIM2-> TCR = (1<<0); // start counting
endVal3 = (int) msTicks;
}

int main(void)
{
      if (SysTick_Config(SystemCoreClock / 1000)) {
while (1);
}

       timer_interrupt();
       while(1)
       {
           // do stuff
        }
}


When I run them together, I get something like this printed out:
Time in 0 is 250

Time in 0 is 250

Time in 1 is 500   // this supposed to be 1000

Time in 0 is 133 // supposed to be 250

Time in 0 is 250

Time in 0 is 250

Time in 1 is 633

Time in 0 is 122

Time in 0 is 250

Time in 2 is 1608 // supposed to be 3000


Why does this happen? Are my ISRs affecting each other indirectly?
I am using the CPU clock for this timer (100 000 000Hz). I did not select the clock, since thats the default for LPC1769. Could that be the problem?
I cant think of anything else. Any help is appreciated. Thank you

0 Kudos
Reply
3 Replies

1,053 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun Apr 12 02:50:36 MST 2015

Quote: ghh
Is there a better way to this?



To generate 250ms / 1000ms / 3000ms ?

Set a simple timer (RIT) to 250ms interrupt and count this interrupts. Set flags there for 250ms, 1000ms (count %4) and 3000ms (count=12) and reset  the counter.

Then read this flags in your main loop and do your LED / sensor stuff there  :) 
0 Kudos
Reply

1,053 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ghh on Sat Apr 11 20:50:50 MST 2015
Hi

Thank you for replying. I am using the timers to read sensors and switch on leds on the EABaseBoard after a specific amount of time.

I am using printf and Systick interrupt to find out how long it takes for the timers to call each ISR.
Is there a better way to this?


0 Kudos
Reply

1,053 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sat Apr 11 13:55:09 MST 2015

Quote: ghh
I cant think of anything else. Any help is appreciated. Thank you



I can  :O

I'm not sure what you are trying to do in detail, but a few things in your snippet are not very useful  ;-)

#1: Don't use printf in ISR. Printf is very slow and so it's not useful to use it in an interrupt handler...

#2: If you want to use a timer, use TC register. Don't use SysTick interrupt with 1000Hz and count...
0 Kudos
Reply