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.

MSP430G2553: Timer interrupt every 1 microsecond

Part Number: MSP430G2553

I want to use timer to generate interrupt every 1 microsecond. Setup timer with following code:

    CCTL0 = CCIE;                             // CCR0 interrupt enabled
    CCR0 = F_CPU / 1000000;                 // F_CPU = 1MHz or 8MHz or 16MHz
    TACTL = TASSEL_2 + MC_1;           // SMCLK, Up to CCR0

Inside ISR I increment counter variable and toggle Pin1 when this variable reach 1000, which should give 1 millisecond.

    cnt += 1;
    if(cnt == 1000)
    {
        cnt = 0;
        P1OUT ^= 0x01;
    }

Measuring output pin with oscilloscope and it gives period 1.7 ms. However if I set timer to generate interrupt e.g. every 1000 uS and increment variable up to 1, then period is 1.0 ms. Is there reasonable explanation and sometging I could improve in code?

  • Hi,

    in your code you're setting CCR0 = F_CPU / 1000000 (so I assume F_CPU = F_SMCLK?). Did you check the actual DCO frequency before setting the CCR0 accordingly? And did you calibrate your DCO to make sure it's really set at 1MHz?

    Instead of toggling the LED pin inside the ISR routine you can also directly route the TA output to a pin (check which pins can be used as Timer A CCR0 output) and set the output mode to toggle. Check the MSP430x2xx Family User's Guide for details.

    You can also check some exmaples when going to CCS->TI Resource Explorer->Software->MSP430Ware->Devices->MSP430G2xx->MSP430G2553->Peripheral Examples ->Register Level->msp430g2xx3_ta_01.c through msp430g2xx3_ta_21.c

    Try to figure out if your clock is stable at the frequency you expect and set the CCR0 accordingly instead of using your isr workaournd, this will even save energy when you start using low power modes.

    Let me know after you've looked at the indicated code and document and after reviewing the DCO setting if your issue still persists.

    Best regards,

    Britta

  • Yes, what you overlooked is the fact that the interrupt has 6 cycles latency, as well as executing your increment and if statements when transferred to MSP430 instructions will require multiple other MCU clock cycles adding extra delay within the ISR.
    You might be able to improve it by factoring in the interrupt latency and the extra delay within the ISR, but I would not recommend using this approach.

  • Hi,

    have you been able to troubleshoot and solve your problem?
    Please verify the answer if it solved your problem or otherwise provide further information on the issue.

    Note that I will close this thread soon in case I won't hear back from you. IF your problem persists just reply to the thread again and it will be re-opened.
    Thanks and best regards,
    Britta
  • Hi,

    Sorry for the late reply. The problem was solved by increasing interrupt timeout: from 1 us to 1 ms. Of-course internal oscillator doesn't give the best precision, however it is fine for my project.

  • You are interrupting too frequently so there isn't enough processing power in between the interrupts to process your isr + overhead.

    You either slow it down or pick a faster chip.

**Attention** This is a public forum