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.

P401R and clock cycles



Hello!


I have one problem with MSP432. Not a crucial one, but just curious...

I needed a quick & dirty millisecond delay function, so I have implemented it like this:
void delay(uint16 ms) {
    uint16 count = 0;
    while(count++ < ms) {
        __delay_cycles(CLOCK_KHZ);
    }
}

As you probably have guessed, CLOCK_KHZ = CLOCK/1000. This should give me a delay of
approximately 1 ms. Therefore calling it n times should give me a dealy of n ms. (I didn't
take into account the clocks needed by the loop itself. After all, it's quick & dirty.

The problem: the delay I get is double of what I expected. Just in case, I verified the
clock on my scope (which is reliable by the way), and it's close enough to 48 MHz.

Any idea of what may happen? Of course, the workaround __delay_cycles(CLOCK_KHZ/2);
works fine, but I would like to understand what happens.

Thanks for any hint.

Pascal

  • The delay introduced by  __delay_cycles() assumes zero wait-states.

    When the clock is 48 MHz 2 flash wait-states are required as per section 5.8 Operating Mode Execution Frequency vs Flash Wait-State Requirements of the MSP432P401R datasheet. The flash wait-states will slow down execution, and make the __delay_cycles() take longer than the requested delay.

**Attention** This is a public forum