This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2531: Using Timer 1 to see how long a command takes and using it as a delay

Part Number: CC2531

Hello all,

I have my timer set up and I am trying to use it as a delay and to see how long a it take to change the receiving channel on the device. After I change the channel I would like to stay on that channel for thirty seconds and then switch channels again. I want to do all of this using the timer but none of the register values seem usable to me. If anyone could weigh in on this, it would be greatly appreciated. Also, if anyone has a different way of doing this, please let me know. Thank you very much.

-Brandon

  • Hey Brandon,

    To read the current count of timer 1, you can read registers T1CNTH and T1CNTL. These are two 8 bit registers that store the upper byte and lower byte of the timer count respectively. A count of 1 means that one clock tick has occurred.

    To measure how long something takes you need to store the value of T1CNTL and T1CNTH (T1CNTL must always be read first).

    Here are the steps for measuring how long a piece of code takes:

    1. Store the value of the count registers before what you want to measure starts
    2. Store the value of the count registers after what you want to measure ends
    3. Calculate the difference of those two values to get the amount of clock ticks that have passed

    The duration of a clock tick depends on how you configured the timer. Say a clock tick is 1ms and the difference you calculated is 45. Then 1ms*45 = 45ms.

    Regards,
    JP
  • To do something for a certain amount of time you can use the timer in output compare mode. Basically what it does is you tell to trigger a task after a certain amount of clock ticks that you define have passed. To learn more about timer 1 I recommend you go to chapter 9 in this document. Section 9.8 is the mode I referred to.

  • Okay, in your opinion, do you think there is a better way to do a delay?
  • JP,

    When using the method below

    count1 = T1CNTL;
    FREQCTRL += 5;
    count2 = T1CNTH;
    difference = count2 - count1;
    printf("Low: %u, High: %u, Difference: %u \n", count1, count2, difference);

    the value of difference does not change in the terminal output but you can clearly see a difference in T1CNTL and T1CNTH, shown below.

    Low: 0, High: 65534, Difference: 6082
    Low: 9, High: 65438, Difference: 6082
    Low: 41, High: 65374, Difference: 6082
    Low: 176, High: 15, Difference: 6082
    Low: 41, High: 65532, Difference: 6082
    Low: 208, High: 99, Difference: 6082
    Low: 105, High: 75, Difference: 6082
    Low: 175, High: 159, Difference: 6082
    Low: 157, High: 150, Difference: 6082
    Low: 168, High: 65493, Difference: 6082
    Low: 170, High: 133, Difference: 6082
    Low: 186, High: 146, Difference: 6082

    Any suggestions on how to fix this?

    Thanks,
    Brandon