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/MSP430F5529: Implementation of a timer

Part Number: MSP430F5529

Tool/software: Code Composer Studio

Hello everyone!
I am new to the world of microprocessors and for the moment i am using the MSP-EXP430F5529LaunchPad and Code Composer Studio 7.4

I am trying to write a program using DriverLib for a safety belt system with the following specifications:

"The controller will light on a red LED if someone is sitting in a car seat and is not fastened after 15 seconds. If the passenger is fastened within 15 seconds, the controller will light on a green LED respectively.
This system has three inputs and two outputs. The inputs are a Push Button Sensor (P1.1) to know when a person is sitting, a Push Button (P2.1) that recognizes whether the belt is tied or not by the passenger and a timer notifying that the required time has elapsed.
The controller is idle when there is no one in the seat. When someone sits, the controller turns on the timer. If time is over before the safety belt is tightened, the red LED lights up. If the seat belt is fastened in time, the green LED lights up and the controller enters a surveillance mode. During the surveillance it is checked whether the passenger continues to have the belt tied. When the passenger leaves the seat, the controller returns to idle state."

I ended up with the following program:

#include <msp430.h> 
#include <driverlib.h>

#define LF_CRYSTAL_FREQUENCY_IN_HZ 32768
#define HF_CRYSTAL_FREQUENCY_IN_HZ 4000000

volatile unsigned short beltButton = 0;
volatile unsigned short seatButton = 0;

void main(void)
{

    GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2, GPIO_PIN1);
    beltButton = GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN1);
    GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1);
    seatButton = GPIO_getInputPinValue(GPIO_PORT_P1, GPIO_PIN1);
    GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
    GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN7);
    GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
    GPIO_setOutputLowOnPin(GPIO_PORT_P4, GPIO_PIN7);


                     UCS_setExternalClockSource(
                           LF_CRYSTAL_FREQUENCY_IN_HZ,
                            HF_CRYSTAL_FREQUENCY_IN_HZ);

                 UCS_initClockSignal(UCS_ACLK,
                                           UCS_REFOCLK_SELECT,
                                          UCS_CLOCK_DIVIDER_8);

while (1){

    unsigned volatile int i;
    if((seatButton == GPIO_INPUT_PIN_LOW)&&(beltButton == GPIO_INPUT_PIN_HIGH))
        {
           for(i=10000;i>0;i--)


           GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0); }

    if(beltButton == GPIO_INPUT_PIN_LOW)
                {
                    GPIO_setOutputHighOnPin(GPIO_PORT_P4, GPIO_PIN7);
                    GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
                    break;
                }
      else{GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
               i=0;}
          break;
            }
        }

If you press P2.1 the red LED lights up. If you keep pressing P2.1 and press P1.1 the red LED turns off and the green LED lights up. So far, so good.

My problem is that i cannot implement a timer so that the red LED lights up after P2.1 is kept pressed for 15 seconds ("The controller will light on a red LED if someone is sitting in a car seat and is not fastened after 15 seconds.).

Any ideas?

  • Hi,

    there should be different ways to actually implement this.
    You could for example start a timer when 2.1 is pressed and enable an interrupt when the timer expires (15 seconds as your applications requires). If the interrupt occurs you'll light the red LED. Meanwhile you'll use a flag indicating if the seat belt is fastened, i.e. if the other button is pressed the flag will be set, which stops the timer. That way you'd need to poll the flag in your loop though.
    Or you'll configure an interrupt for the second button so that, in case an interrupt is triggered because the passenger fastens the seat belt, the timer is stopped before it triggers the interrupt.

    Those are only some first ideas though which you should elaborate further depending on your overall system needs (performance, current consumption, etc.).

    Best regards,
    Britta
  • Hi,

    note that I am closing this thread due to inactivity.
    Please respond back in case your question hasn't been answered yet. Any reply to this thread will reopen it for further discussions.

    Best regards,
    Britta

**Attention** This is a public forum