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