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.

CCS/MSP432P401R: microsecond delay

Part Number: MSP432P401R


Tool/software: Code Composer Studio

How do I create a microsecond delay function MSP432?

I have thought of two ways:

1. 

#define CPU_FREQ 3000000
#define delay_us(t) (__delay_cycles(((t)*CPU_FREQ/1000000)))

//doesn't work effectively

2. I still need to fully understand how I can do this as I am not sure about the numbers I need to insert to divide from j:

void delay_us(uint32_t us) {
    //    Calibrated for 40 us     
    uint32_t j;
    uint16_t us_8 = us/8;
    uint32_t clk_freq = CS->CTL0;
    switch (clk_freq) {
        case(CS_CTL0_DCORSEL_0): for(j=0; j<us_8-5; j++){}
            break;
        case(CS_CTL0_DCORSEL_1): for(j=0; j<us_8-10; j++){}
           break;
        case(CS_CTL0_DCORSEL_2): for(j=0; j<us_8-; j++){}
           break;
        case(CS_CTL0_DCORSEL_3): for(j=0; j<us_8-; j++){}
            break;
        case(CS_CTL0_DCORSEL_4): for(j=0; j<us_8-; j++){}
          break;
   }

 

  • Hallo,

    as a general answer, I would suggest to have a look into these examples.

    or

    The result depends on your clock settings

    Let me know if that helps

    Regards

    Kostas

  • 1 shouldn't compile. since delay_cycles is an intrinsic, it needs a compile time constant.

    2 needs to have the variables declared volatile so the compiler does not optimise them away.

    You can combine the 2:

    switch (delay)

    {

    case 1:

    delay_cycles(1);

    break;

    case 2:

    delay_cycles(2);

    break;

    default:

    break;

    }

    or

    if (delay < 10)

    {

    delay_cycles(10)

    } else if (delay > 10 && delay < 20)

    {

    delay_cycles(20)

    }

    ...

**Attention** This is a public forum