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.

coding problem...

Other Parts Discussed in Thread: MSP430G2533

I have 4 red led and 4 green leds .In which 3 red and 1 green led glow at a time and it keep on switching after few seconds..now problem is I want add to it that if input signal  is given to msp430G2533 at one port,it will turn on one green led corresponding to that input if it is red,and turn on 3 red led ,for a few seconds and then return back to the original task.There are 4 inputs to msp430 and for every input ,task perform by msp is same as above.pls..help in this second part of coding..what changes should i make to the code given below..thanx.

My first part of coding is given below and it is working fine:

#include <msp430G2533.h> // Specific device;
void delay()
{
unsigned int i,j;
for(i=0;i<800;i++)
for(j=0;j<1024;j++);
}
void main()
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
while(1)
{
P1DIR = 0x26;
P2DIR = 0x1f;
P1OUT = 0x22;
P2OUT = 0x0a;
delay();
P1OUT = 0x22;
P2OUT = 0x14;
delay();
P1OUT = 0x02;
P2OUT = 0x13;
delay();
P1OUT = 0x24;
P2OUT = 0x12;
delay();
}
}

  • First of all, you rdelay function is fragile at best.
    Depending on compiler, even compiler version, and project settings, the complete delay might vanish, as it doesn't do anything (except wasting time). If you completely clear teh function, the memory after call will be the same as if the loops were in there, so the compiler may at any tiem chose to eliminate it.

    If you want a delay, either use the specific __delay_cycles() intrinsic, or use a timer. That's what timers are made for: do timing.

    This also gives you the required startign point for one of your projects pobjectives: return to an initial state after some time. With a tiemr running that counts, say, milliseconds, you know that when a certain tiem ahs passed, teh delay is over. You can even 'retrigger' by recalculating the 'over' time when anotehr event happens before the display was reset. (with your current delay funciton, you couldn't even react as the CPU were busy to loop)

    For the rest, well, currently you program all 8 bits of a port together by assigning a fixed value to P1OUT. YOu can ratehr set and clear bits individually.

    P2OUT |= BIT2 will set the port pin 2.2 to '1' while leaving all others untouched. P2OUT&=~BIT2 will set the same bit back to 0. Again without touching all the others. So you can indvidually switch LEDs on and off.

    The current input state of all ports (including bits that are switched to output) can be read by PxIN. Here you can mask individual bits (= port pins).

    if (P1IN&BIT1) is true if pin P1.1 is 1 and false if P1.1 is 0. (actually, the term (P1IN&BIT1) will return either 0 or BIT1 as value. and since everything that is not 0 is considered true...

**Attention** This is a public forum