This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

some questions about timers in LPC1768

Hi, I have little problems with timers in LPC1768. I need to start timer2 because I use timer0 and timer1. When I want to configure and start timer2 all program doesn't start and I see nothing reaction - white display and no reaction. I have question: can I use more timers than 2? How many timers can I use ? And the second problem - I'd like to turn off one of the timers. How can I do it ? I understand that I must change pins on TCR register, but when I set bit 0 or 1 from documentation I turned off timer but I can't turn on again...I can turn off only once. I don't understand it.

LPC_TIM1->TCR |= 1 << 0; - start timer
LPC_TIM1->TCR |= 0 << 0; - turn off timer ? It doesn't work...

I also tried LPC_TIM1->TCR |= 1 << 1 but then timer didn't start again when I set
LPC_TIM1->TCR |= 1 << 0 again. I think that you understand what I mean. Thanks for help.

  • "LPC_TIM1->TCR |= 0 << 0; - turn off timer ? It doesn't work..."

    Time to read up on the operators in C.

    Do you really think that or:ing a zero will clear that bit?

    You do know the logic table for bitwise or?

    0 or 0 => 0
    0 or 1 => 1
    1 or 0 => 1
    1 or 1 => 1

    So when the bit in the register is 1 and you or with 0, you still end up with 1.

    Maybe, just maybe, bitwise or isn't a good route if you want to clear a bit...

  • Should I use and operator for clear this bit? What about using a few timers ? For example using 3 timers at once?

  • You use whatever operator gives the result that you require!

    And, of course, you need the appropriate operands to go with that operator!

    We're talking basic logic here; not even specific to 'C' - let alone Keil or LPC1768

    "What about using a few timers ?"

    What about it?

    "For example using 3 timers at once?"

    You just do the same thing - three times!

  • If your processor have two timers - do you suppose that you can use two at the same time?
    If your processor have three times - do you suppose that you can use three at the same time?
    If your processor have four timers - do you suppose that you can use four at the same time?

    Exactly what issue do you see with using multiple timers?

    Exactly what reason do you see for a chip manufacturer to add more timers than you can use at the same time?

  • Peter I don't understand :> I use two timers at once

  • What is the issue? If the chip manufacturer designed the chip with four timers, then you can use all four concurrently. If you couldn't then, then it wouldn't have been meaningful to have four timers in the first place.

  • Unfortunately, that logic does not always follow!

    eg, on some STM32s it can be very difficult (maybe even impossible?) to find a combination of pin assignments that allows all of the peripherals to be used at once.

    IIRC, there were also some STM32s which had both USB and CAN - but only one of them could be used at a time.

    The chip's Documentation will show if there are any resources (eg, pins, memory, interrupts) shared between timers, or any other restrictions on using the timers...

    But this is a purely a detail of the chip, so it should be directed to the chip manufacturer - is has nothing to do with 'C' or Keil.

  • It can not be easy to use 4*UART + 4*timer + 2*I2C + 2*SPI + ... at the same time, because of pin assignment constraints. The better chips have so many peripherials that the sum of peripherial-specific I/O pins can be way more than the number of pins for the package.

    But you can use all four UARTS at the same time without them interfering with each other.
    Same with four timers at the same time.
    Or two I2C.
    ...

    And in the case of timers, you can always use timers without involving I/O. It's only when there is a match-compare mapping for driving a pin output, or a pin input is used as clock signal to drive a timer, that restrictions on available I/O pins needs to be considered.

  • I use two timers without pins configuration. There are normal timers work with interrupts. The problem is that, I'm writing Snake game and timer0 has drawing function which draws the snake, timer1 generate random pixels for snake and the main problem: I want to use next timer to play some melody when for example I'll win or go to next level, or lose the game. I did it by timer0 on the other program, I used pin match and generate square wave with variable frequency. But when I want to add next timer to my game nothing work. I see only white display. I've just read about match pins and I understand that can I use for it only timer0 or timer1 ? I still don't understand this...Can I use 3 timers with one timer with pin match or not ?

  • Not sure if you can.

    But it can be done.

    Have you enabled any interrupt routine for that timer? If you have, but isn't servicing the interrupt, you get into troubles.

  • OK, I've just done it. I have three timers - timer0 generate scuqare wave on pin MAT0.0 (P3.25) and play some melody, timer1 and timer2 draw snake and put random pixels. Now I have to make turning off the timers...I understand turning off is depend on bit in TCR and logical operations, but I read in documentation that, if I clear bit 1 or 2 for timer0 I won't do nothing - "Feature disabled." That while I have to change only one bit if I want to stop timer - I must disable interrupts. I think that...But I'm not sure.