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.

Set timer for MSP430F5438

Hi,

I am trying to set port P8.7 high for a period of time (e.g 10s), and then set the same port P8.7 low.

My objective is to set it high to turn on a motor for 10s and then set it to low to turn the motor off.

Code used to set port high:

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

P8REN |= BIT7; // enable internal pull up, 6

//P8OUT &= ~BIT7; // P1.0 low
P8OUT |= BIT7; // P1.0 high
P8SEL &= ~BIT7; // configure P1.0 for digital I/O
P8DIR |= BIT7; // configure P1.0 as output

//P8OUT &= ~BIT7; // P1.0 low
P8OUT |= BIT7; // P1.0 high


while(1);
}

Any help is appreciated. 

Thank you

  • You'll need some way to detect the passing of time.

    Simplest way is a delay loop.

    If your MCLK is running on 1MHz, the isntruction
    __delay_cycles(10000000);
    will lte the CPU run in circles for 10 million cycles, which takes 10 seconds.

    However, this is the worst way to do it. Better use timers and counters. E.g. a timer that triggers an interrupt every ms. The interupt funciton will *** down a variable and when the variable reaches 0, your main code knows that the time has passed. This even allows for multiple, nested operations at the same time, with different delays.

**Attention** This is a public forum