EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Project with "IF" function and timer

Started by Iron-Sim May 27, 2015
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 <msp430g2553.h> 

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 <= 32768))  {
		P1OUT = 0x40}
else
{
	if(((P1IN & BIT3) == 0)) && (CCR0 >= 65536)){
		P1OUT=0x01)}}
}

Someone can help me ?

Thanks by advance.


---------------------------------------
Posted through http://www.EmbeddedRelated.com
On Wed, 27 May 2015 17:32:52 -0500, Iron-Sim wrote:

> 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 <msp430g2553.h> > > 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 <= 32768)) { > P1OUT = 0x40} > else { > if(((P1IN & BIT3) == 0)) && (CCR0 >= 65536)){ > P1OUT=0x01)}} > } > > Someone can help me ? > > Thanks by advance. > > > --------------------------------------- > Posted through http://www.EmbeddedRelated.com
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 http://www.wescottdesign.com
On Wednesday, May 27, 2015 at 3:32:56 PM UTC-7, Iron-Sim wrote:
> 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 <msp430g2553.h> > > 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 <= 32768)) { > P1OUT = 0x40} > else > { > if(((P1IN & BIT3) == 0)) && (CCR0 >= 65536)){ > P1OUT=0x01)}} > }
Your code probably would not even compile, with the extra ')' and '}'.
On 28.5.15 01:32, Iron-Sim wrote:
> 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 <msp430g2553.h> > > 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 <= 32768)) { > P1OUT = 0x40} > else > { > if(((P1IN & BIT3) == 0)) && (CCR0 >= 65536)){ > P1OUT=0x01)}} > } > > Someone can help me ? > > Thanks by advance. > > > --------------------------------------- > Posted through http://www.EmbeddedRelated.com >
Homework? -- -TV
On 28/05/15 00:45, Tim Wescott wrote:
> On Wed, 27 May 2015 17:32:52 -0500, Iron-Sim wrote: > >> 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 <msp430g2553.h> >> >> 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 <= 32768)) { >> P1OUT = 0x40} >> else { >> if(((P1IN & BIT3) == 0)) && (CCR0 >= 65536)){ >> P1OUT=0x01)}} >> } >> >> Someone can help me ? >> >> Thanks by advance. >> >> >> --------------------------------------- >> Posted through http://www.EmbeddedRelated.com > > 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. >
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.
>On 28/05/15 00:45, Tim Wescott wrote: >> On Wed, 27 May 2015 17:32:52 -0500, Iron-Sim wrote: >> >>> 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 <msp430g2553.h> >>> >>> 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 <= 32768)) { >>> P1OUT = 0x40} >>> else { >>> if(((P1IN & BIT3) == 0)) && (CCR0 >= 65536)){ >>> P1OUT=0x01)}} >>> } >>> >>> Someone can help me ? >>> >>> Thanks by advance. >>> >>> >>> --------------------------------------- >>> Posted through http://www.EmbeddedRelated.com >> >> 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. >> > >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.
Thanks for all your answers and advice. I will try this and give you a feedback. --------------------------------------- Posted through http://www.EmbeddedRelated.com
On Thursday, May 28, 2015 at 12:30:29 PM UTC-4, Iron-Sim wrote:
[]
> > Thanks for all your answers and advice. I will try this and give you a > feedback.
Please do.
//***************************************************************************************
// 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 <msp430x20x2.h>

#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 :
http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_PushButton#Code
but apparently, with this sequence, it does not stop.

Thanks for your advices.


---------------------------------------
Posted through http://www.EmbeddedRelated.com
On 5/29/2015 3:18 PM, Iron-Sim wrote:
> //*************************************************************************************** > // 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 <msp430x20x2.h> > > #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 : > http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_PushButton#Code > but apparently, with this sequence, it does not stop. > > Thanks for your advices.
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 <= read_button(); if current_button <> old_button { current_timer <= read_timer(); interval = current_timer - old_timer; if current_button = pressed { if interval > 250 ms { turn off all LEDs old_timer <= current_timer; } } else { if interval > 250 ms { if interval > 2 sec { turn on green LED old_timer <= current_timer; } if interval < 1 sec { turn on red LED old_timer <= current_timer; } } } old_button <= current_button; } } -- Rick
>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 <= read_button(); > if current_button <> old_button { > current_timer <= read_timer(); > interval = current_timer - old_timer; > > if current_button = pressed { > if interval > 250 ms { > turn off all LEDs > old_timer <= current_timer; > } > } > else { > if interval > 250 ms { > if interval > 2 sec { > turn on green LED > old_timer <= current_timer; > } > if interval < 1 sec { > turn on red LED > old_timer <= current_timer; > } > } > } > > old_button <= current_button; > } > } > >-- > >Rick
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 http://www.EmbeddedRelated.com

Memfault Beyond the Launch