EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Project with "IF" function and timer

Started by Iron-Sim May 27, 2015
On 5/31/2015 2:49 AM, Iron-Sim wrote:
> I do not understand, can you show me the code ?
I've already given you code and you didn't understand that. -- Rick
>I've already given you code and you didn't understand that. > >-- > >Rick
After some headaches, I finally found the solution. #include <msp430g2553.h> #define LED BIT6 #define BUTTON BIT3 void main(void) { WDTCTL = WDTPW|WDTHOLD; P1DIR = LED; P1REN = BUTTON; P1OUT = BUTTON; while(1){ if((P1IN & BUTTON) == 0x00){ __delay_cycles(5000); if((P1IN & BUTTON) == 0x00){ P1OUT ^= LED; while((P1IN & BUTTON) == 0x00); }}} } Now, I just have to adapt it with the timer with the LED (GREEN if < 1sec; RED if > 2sec) --------------------------------------- Posted through http://www.EmbeddedRelated.com
On 5/31/2015 1:25 PM, Iron-Sim wrote:
>> I've already given you code and you didn't understand that. >> >> -- >> >> Rick > > After some headaches, I finally found the solution. > > #include <msp430g2553.h> > > #define LED BIT6 > #define BUTTON BIT3 > > void main(void) > { > WDTCTL = WDTPW|WDTHOLD; > P1DIR = LED; > P1REN = BUTTON; > P1OUT = BUTTON; > > while(1){ > if((P1IN & BUTTON) == 0x00){ > __delay_cycles(5000); > if((P1IN & BUTTON) == 0x00){ > P1OUT ^= LED; > while((P1IN & BUTTON) == 0x00); > }}} > } > > Now, I just have to adapt it with the timer with the LED (GREEN if < 1sec; > RED if > 2sec)
Can you explain exactly what the above code is doing? Add some comments to explain what the various lines are doing and why. How long is your delay in ms? -- Rick
Hello,

I am back ad after some study, I found this code for my program.

To answer to you Rick, the delay is 5ms for the deboune and at each push
on the button, the bit are 0 (no light) or 6 (light).

Can someone check and tell me where I am wrong because I am quite lost.

Thanks a lot.

#include <msp430g2553.h>

 #define GREEN_LED 0x40       		// D&eacute;finit le terme "GREEN_LED" comme
&eacute;tant la sortie P1.6
 #define RED_LED 0x01       		// D&eacute;finit le terme "RED_LED" comme &eacute;tant
la sortie P1.0
 #define LEDS 0x41       		// D&eacute;finit le terme "LEDS" comme &eacute;tant la
sortie P1.0 et P1.6.
 #define LEDS_ETEINT 0x00       		// D&eacute;finit le terme "LEDS_ETEINT" comme
&eacute;tant aucune sortie donc LEDs ferm&eacute;es.
 #define BUTTON BIT3		  	// D&eacute;finit le terme" BUTTON" comme &eacute;tant
l'entr&eacute;e P1.3
 long tps;				//D&eacute;finit ma variable du temps

 void main(void)			  	//Fonction principale lanc&eacute;e au reset

  {
  WDTCTL = WDTPW + WDTHOLD; 		// Arr&ecirc;te timer du Watchdog
  P1DIR = LEDS;		  		//Configure le port P1.0 et P1.6 en sortie
  P1OUT = LEDS_ETEINT;			//LEDs &eacute;teinte au d&eacute;marrage

  BCSCTL2 = SELS; 			// SMCLK = LFXT1CLK (quartz)
  TA0CTL = MC_1 + TASSEL_2 + TACLR + ID_2; // On s&eacute;lectionne le mode
mont&eacute;e / La sub-main clock / TAR Clear / On divise l'horloge par 4

 while (1) 				//Boucle infinie
  {
  while ((P1IN & BUTTON) == 1)	// Tant que le bouton est enfonc&eacute;
	  {}				//&hellip; rien ne se passe
  if ((P1IN & BUTTON) == 0) 	// Tant que le bouton n'est pas enfonc&eacute;
	  {__delay_cycles(5000);}	// D&eacute;lai anti-rebonds
  while ((P1IN & BUTTON) == 1)	// Tant que le bouton est enfonc&eacute;
	  {}
	  while ((P1IN & BUTTON) == 0)	// Tant que le bouton est enfonc&eacute;
  {
	if ((P1OUT == GREEN_LED)||(P1OUT == RED_LED)) // Si l'une des LEDs est
allum&eacute;
	  {
		  P1OUT = LEDS_ETEINT; 	// ... On les &eacute;teint
	  }
		  else				//Sinon
	  {tps++; 				//... on met en route le compteur
		  if(tps <= 8192) { 		//Et pendant ce temps si T<1sec
		  		P1OUT = GREEN_LED ; //... On allume la LED verte
		  					}
		  if (tps >= 16384) { 		// Si T>2 sec
		  		P1OUT = RED_LED ;//... On allume la LED rouge
		  			}
	  	  	  }
  	  	  }
  	  }
  }

---------------------------------------
Posted through http://www.EmbeddedRelated.com
On 6/8/2015 11:55 AM, Iron-Sim wrote:
> Hello, > > I am back ad after some study, I found this code for my program. > > To answer to you Rick, the delay is 5ms for the deboune and at each push > on the button, the bit are 0 (no light) or 6 (light). > > Can someone check and tell me where I am wrong because I am quite lost. > > Thanks a lot. > > #include <msp430g2553.h> > > #define GREEN_LED 0x40 // D&eacute;finit le terme "GREEN_LED" comme > &eacute;tant la sortie P1.6 > #define RED_LED 0x01 // D&eacute;finit le terme "RED_LED" comme &eacute;tant > la sortie P1.0 > #define LEDS 0x41 // D&eacute;finit le terme "LEDS" comme &eacute;tant la > sortie P1.0 et P1.6. > #define LEDS_ETEINT 0x00 // D&eacute;finit le terme "LEDS_ETEINT" comme > &eacute;tant aucune sortie donc LEDs ferm&eacute;es. > #define BUTTON BIT3 // D&eacute;finit le terme" BUTTON" comme &eacute;tant > l'entr&eacute;e P1.3 > long tps; //D&eacute;finit ma variable du temps > > void main(void) //Fonction principale lanc&eacute;e au reset > > { > WDTCTL = WDTPW + WDTHOLD; // Arr&ecirc;te timer du Watchdog > P1DIR = LEDS; //Configure le port P1.0 et P1.6 en sortie > P1OUT = LEDS_ETEINT; //LEDs &eacute;teinte au d&eacute;marrage > > BCSCTL2 = SELS; // SMCLK = LFXT1CLK (quartz) > TA0CTL = MC_1 + TASSEL_2 + TACLR + ID_2; // On s&eacute;lectionne le mode > mont&eacute;e / La sub-main clock / TAR Clear / On divise l'horloge par 4 > > while (1) //Boucle infinie > { > while ((P1IN & BUTTON) == 1) // Tant que le bouton est enfonc&eacute; > {} //&hellip; rien ne se passe > if ((P1IN & BUTTON) == 0) // Tant que le bouton n'est pas enfonc&eacute; > {__delay_cycles(5000);} // D&eacute;lai anti-rebonds > while ((P1IN & BUTTON) == 1) // Tant que le bouton est enfonc&eacute; > {} > while ((P1IN & BUTTON) == 0) // Tant que le bouton est enfonc&eacute; > { > if ((P1OUT == GREEN_LED)||(P1OUT == RED_LED)) // Si l'une des LEDs est > allum&eacute; > { > P1OUT = LEDS_ETEINT; // ... On les &eacute;teint > } > else //Sinon > {tps++; //... on met en route le compteur > if(tps <= 8192) { //Et pendant ce temps si T<1sec > P1OUT = GREEN_LED ; //... On allume la LED verte > } > if (tps >= 16384) { // Si T>2 sec > P1OUT = RED_LED ;//... On allume la LED rouge > } > } > } > } > }
What part of this code are you having trouble with? Can you translate the comments for me? -- Rick
On Friday, May 29, 2015 at 3:18:20 PM UTC-4, 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
WHY THE HELL ARE YOU DELAYING 3seconds IN AN INTERRUPT HANDLER?
> > P1OUT = 0x00; //red light > __delay_cycles(100000); // delay 3 seconds
AND IF __delay_cycles(100000); IS 3seconds, then
> > P1OUT = 0x01; //red light > __delay_cycles(1000000000); // delay 3 seconds
this is 10000 times longer!!!!!!!!!!!!!!!!!!!!!
> 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 > } >
My advice is to STAY AWAY from using interrupt handlers until you understand what they really are. rickman's suggestion is one you should follow. ed
On 6/11/2015 9:48 AM, Ed Prochak wrote:
> On Friday, May 29, 2015 at 3:18:20 PM UTC-4, 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 > > WHY THE HELL ARE YOU DELAYING 3seconds IN AN INTERRUPT HANDLER? > >> >> P1OUT = 0x00; //red light >> __delay_cycles(100000); // delay 3 seconds > > AND IF __delay_cycles(100000); IS 3seconds, then >> >> P1OUT = 0x01; //red light >> __delay_cycles(1000000000); // delay 3 seconds > > this is 10000 times longer!!!!!!!!!!!!!!!!!!!!! > > >> 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 >> } >> > > My advice is to STAY AWAY from using interrupt handlers until you understand what they really are. rickman's suggestion is one you should follow.
I don't think he is trying to learn really. I expect this is an assignment and he is trying to get something to work as quickly as possible. It's not a difficult assignment really, but he needs to *learn* about the issues involved rather than just copying code from the Internet. -- Rick
On Sunday, June 14, 2015 at 5:50:34 PM UTC-4, rickman wrote:
[]
> > I don't think he is trying to learn really. I expect this is an > assignment and he is trying to get something to work as quickly as > possible. It's not a difficult assignment really, but he needs to > *learn* about the issues involved rather than just copying code from the > Internet. > > -- > > Rick
yes, he seems to be doing on the job learning, poorly. That seems to be the modern mentality: "I don't need to understand how it works. I'll just throw code at it until something happens." I hope he follows your advice. ed
On 6/15/2015 1:13 PM, Ed Prochak wrote:
> On Sunday, June 14, 2015 at 5:50:34 PM UTC-4, rickman wrote: > [] >> >> I don't think he is trying to learn really. I expect this is an >> assignment and he is trying to get something to work as quickly as >> possible. It's not a difficult assignment really, but he needs to >> *learn* about the issues involved rather than just copying code from the >> Internet. >> >> -- >> >> Rick > > yes, he seems to be doing on the job learning, poorly. That seems to be the modern mentality: "I don't need to understand how it works. I'll just throw code at it until something happens." > > I hope he follows your advice.
I remember being in a hurry myself when I was in school. I thought I was learning everything I needed and the actual practice of the craft was just an "exercise". lol. I'm still learning the practice of the craft. -- Rick
On Monday, June 15, 2015 at 10:44:07 PM UTC-4, rickman wrote:
> On 6/15/2015 1:13 PM, Ed Prochak wrote: > > On Sunday, June 14, 2015 at 5:50:34 PM UTC-4, rickman wrote: > > [] > >> > >> I don't think he is trying to learn really. I expect this is an > >> assignment and he is trying to get something to work as quickly as > >> possible. It's not a difficult assignment really, but he needs to > >> *learn* about the issues involved rather than just copying code from the > >> Internet. > >> > >> -- > >> > >> Rick > > > > yes, he seems to be doing on the job learning, poorly. That seems to be > > the modern mentality: "I don't need to understand how it works. I'll > > just throw code at it until something happens." > > > > I hope he follows your advice. > > I remember being in a hurry myself when I was in school. I thought I > was learning everything I needed and the actual practice of the craft > was just an "exercise". lol. I'm still learning the practice of the > craft. >
Thirty plus years doing software and I'm still learning. 8^) Hey Iron Slim, is this helping? ed

The 2024 Embedded Online Conference