Project with "IF" function and timer

Hello guys,

I am a Belgian maker and I am actually trying to learn how to program MSP430 in C language. I have read of course some tutorials but I am stuck with this problem.

I would like to do this :

- If I push the bouton with a timing lesser than 1 second, the RED light switch on.

- If I push the button with a timing higher than 2 second, the GREEN light switch on.

- If a LED is switch on, when I push the button, the LED are off.

I tried with this in order to switch on the LED at least but it is not working.

#include

void main(void)

{ P1DIR = 0x41; WDTCTL = WDTPW + WDTHOLD; P1OUT = 0x00;

BCSCTL2 = SELS; TA0CTL = MC_1 + TASSEL_2 + TACLR; CCTL0 = CCIE;

while(1){ if(((P1IN & BIT3) == 0) && (CCR0 = 65536)){ P1OUT=0x01)}} }

Someone can help me ?

Thanks by advance.

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim
Loading thread data ...

I'm not thoroughly familiar with that processor, but here are some things to try.

First, whenever you're working with embedded systems, when something doesn't work, _simplify_. You're trying to time things, detect a button press AND turn on a light. Fine. Ditch everything except for turning on the light. If you can run the code and the light comes on, that's not the problem. Then put in just the timing, to try to blink the light. If you can run the code and the light blinks at the expected speed, then the timing is working. Finally, add in the button press.

When your code acts like the button is getting pressed multiple times, you will have discovered switch bounce, and you can come back here and ask about it.

--
Tim Wescott 
Wescott Design Services 
 Click to see the full signature
Reply to
Tim Wescott

Your code probably would not even compile, with the extra ')' and '}'.

Reply to
edward.ming.lee

Homework?

--

-TV
Reply to
Tauno Voipio

To add to this advice, "not working" is not a very good description of what is happening. Do you mean the leds are not going on? Or are they not going off? Or is it working sometimes? Or have you released the magic grey smoke?

So as well as simplifying the code, keep track of what happens when you try it. And keep track of different versions of your test code, so that you can refer back to them as you make progress.

Reply to
David Brown

light

not

things

button

on

If

the

Thanks for all your answers and advice. I will try this and give you a feedback.

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

On Thursday, May 28, 2015 at 12:30:29 PM UTC-4, Iron-Sim wrote: []

Please do.

Reply to
Ed Prochak

//*************************************************************************************** // MSP430 PushButton that toggles LED at P1.0 On and OFF // // Description; PushButton in P1.3 through interrupt turns on and off the LED in P1.0

// By changing the P1.3 interrupt edge, the interrupt is called every time the button

// is pushed and pulled; toggling the LED everytime. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430x2xx // ----------------- // /|| XIN|- // | | | // --|RST XOUT|- // | | // | P1.0|-->LED // // Aldo Briano // Texas Instruments, Inc // June 2010 // Built with Code Composer Studio v4 //*************************************************************************************** #include

#define LED0 BIT0 #define LED1 BIT6 #define BUTTON BIT3

int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= (LED0 + LED1); // Set P1.0 to output direction // P1.3 must stay at input P1OUT &= ~(LED0 + LED1); // set P1.0 to 0 (LED OFF) P1IE |= BUTTON; // P1.3 interrupt enabled

P1IFG &= ~BUTTON; // P1.3 IFG cleared

__enable_interrupt(); // enable all interrupts for(;;) {} }

// Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { P1OUT = 0x40; // green light __delay_cycles(1000000); // delay 3 seconds

P1OUT = 0x00; //red light __delay_cycles(100000); // delay 3 seconds

P1OUT = 0x01; //red light __delay_cycles(1000000000); // delay 3 seconds P1IFG &= BUTTON; // P1.3 IFG cleared P1IES ^= BUTTON; // toggle the interrupt edge, // the interrupt vector will be called // when P1.3 goes from HitoLow as well as // LowtoHigh }

OK, I tried again with this program but even if I stop pushin the button, the two LED switch on even if normally, they should stop.

Can someone help me because I do not know where is the problem. I have take a look to this link to understand how is working the push button :

formatting link
but apparently, with this sequence, it does not stop.

Thanks for your advices.

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

I have no idea what your code is doing. You set the lights to different colors with different delays every time the button is pushed.

I don't see a need for an interrupt. To get something working try making a loop that continually tests the push button. Once pressed start a timer. The easiest way to do that is to have a free running timer and you just store the value to a variable. When the button is released this stored timer value is compared to the current timer value and a decision is made about which time period has elapsed. The appropriate LED is lit. All of this can be done in the loop.

If you have a reason to use an interrupt, this code would go in the interrupt routine with the loop as the main routine with nothing in it.

As someone has mentioned, you will need to debounce the switch input. That would be a third time interval (a short one) that precludes any action.

sooo..... here is some pseudo code...

forever loop { current_button 2 sec { turn on green LED old_timer

Reply to
rickman

Thanks, to be honest... I do not know either what my code was doing except telling me to ask for a big help :D

What do you mean by current button and old button ? What sort of clock do I have to use ? The sub-main clock ?

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

current_button and old_button are just variables. read_button() is some function that returns the current value of the button. In my notation a

Reply to
rickman

Thanks for you help Rick but I do not know how to make a program with this. I have tried since two hours and it doesn't work. Do you have some similar code to understand how does it work ?

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

If you can't grasp how this program should work, you need to start with something simpler. Can you construct a program that reads the push button and turns the LED on when the button is pushed, off when it is released?

--

Rick
Reply to
rickman

Yes,I have seen this one and I tried to adapt it in my last program (4-5 message above) with the function "delay cycles" but as I explained, the LEDs did not stop.

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

That program was not logically correct.

It is actually a well established development/debug technique to start with something that works and incrementally add code in small pieces.

So start with the code to turn on/off the LEDs. Then adapt it so each press of the button will change the state of the LED from off to on and back to off. Push to turn on, push to turn off. Remember to debounce your button presses. When you have it working, post your code.

--

Rick
Reply to
rickman

Do you have a website with some tutorials to explain me how to debounce the presses button ?

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

I have this code program that I adapted from

formatting link
but the turn on/off do not work anytime.

#include

#define LED_0 BIT0 #define LED_1 BIT6 #define LED_OUT P1OUT #define LED_DIR P1DIR #define BUTTON BIT3

unsigned int blink = 0;

void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer LED_DIR |= (LED_0); // Set P1.0 and P1.6 to output direction LED_OUT &= ~(LED_0); // Set the LEDs off P1REN |= BUTTON; //Enables a puller-Resistor on the button-pin P1OUT |= BUTTON; //Writes a "1" to the portpin, tellling the resistor to pullup

P1IES |= BUTTON; //Triggers when you PRESS the button :: Pick one... //P1IES &= ~BUTTON; // Triggers when you RELEASE the button :: ...or pick the other

P1IE |= BUTTON; //Enables the selector-mask for generating interrupts on the relevant pin

__enable_interrupt(); // Interrupts get enabled *here* - they were disabled thus far..

for (;;) {

if(blink > 0) { P1OUT ^= (LED_0); // Toggle P1.0 and P1.6 using exclusive-OR

} }

}

// Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { blink ^= 0x01; P1IFG &= ~BUTTON; // P1.3 IFG cleared LED_OUT &= ~(LED_0); // Clear the LEDs so they start in OFF state

}

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

The easy way to debounce a button press in software is to take action when you see the first edge. You then ignore all subsequent edges until some time has elapsed, say 100 ms.

This does not deal with noise though. To kill two birds with one stone another way is to detect the first edge and wait some time and verify the level is still the same. Or to be *really* cautious once the first edge is detected the state is checked repeatedly looking for the same state for a time duration.

--

Rick
Reply to
rickman

If you don't use the interrupt you can debug the code with the debugger. Instead of letting the interrupt detect when the button is pushed, just check the button state in your loop. When the state changes treat it like you had a button press. Use your debugger and single step or set breakpoints in the code to figure out what the code is doing.

Rick

--

Rick
Reply to
rickman

I do not understand, can you show me the code ?

--------------------------------------- Posted through

formatting link

Reply to
Iron-Sim

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.