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
Project with "IF" function and timer
Started by ●May 27, 2015
Reply by ●May 31, 20152015-05-31
Reply by ●May 31, 20152015-05-31
>I've already given you code and you didn't understand that. > >-- > >RickAfter 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
Reply by ●May 31, 20152015-05-31
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
Reply by ●June 8, 20152015-06-08
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éfinit le terme "GREEN_LED" comme étant la sortie P1.6 #define RED_LED 0x01 // Définit le terme "RED_LED" comme étant la sortie P1.0 #define LEDS 0x41 // Définit le terme "LEDS" comme étant la sortie P1.0 et P1.6. #define LEDS_ETEINT 0x00 // Définit le terme "LEDS_ETEINT" comme étant aucune sortie donc LEDs fermées. #define BUTTON BIT3 // Définit le terme" BUTTON" comme étant l'entrée P1.3 long tps; //Définit ma variable du temps void main(void) //Fonction principale lancée au reset { WDTCTL = WDTPW + WDTHOLD; // Arrête timer du Watchdog P1DIR = LEDS; //Configure le port P1.0 et P1.6 en sortie P1OUT = LEDS_ETEINT; //LEDs éteinte au démarrage BCSCTL2 = SELS; // SMCLK = LFXT1CLK (quartz) TA0CTL = MC_1 + TASSEL_2 + TACLR + ID_2; // On sélectionne le mode monté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é {} //… rien ne se passe if ((P1IN & BUTTON) == 0) // Tant que le bouton n'est pas enfoncé {__delay_cycles(5000);} // Délai anti-rebonds while ((P1IN & BUTTON) == 1) // Tant que le bouton est enfoncé {} while ((P1IN & BUTTON) == 0) // Tant que le bouton est enfoncé { if ((P1OUT == GREEN_LED)||(P1OUT == RED_LED)) // Si l'une des LEDs est allumé { P1OUT = LEDS_ETEINT; // ... On les é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
Reply by ●June 8, 20152015-06-08
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éfinit le terme "GREEN_LED" comme > étant la sortie P1.6 > #define RED_LED 0x01 // Définit le terme "RED_LED" comme étant > la sortie P1.0 > #define LEDS 0x41 // Définit le terme "LEDS" comme étant la > sortie P1.0 et P1.6. > #define LEDS_ETEINT 0x00 // Définit le terme "LEDS_ETEINT" comme > étant aucune sortie donc LEDs fermées. > #define BUTTON BIT3 // Définit le terme" BUTTON" comme étant > l'entrée P1.3 > long tps; //Définit ma variable du temps > > void main(void) //Fonction principale lancée au reset > > { > WDTCTL = WDTPW + WDTHOLD; // Arrête timer du Watchdog > P1DIR = LEDS; //Configure le port P1.0 et P1.6 en sortie > P1OUT = LEDS_ETEINT; //LEDs éteinte au démarrage > > BCSCTL2 = SELS; // SMCLK = LFXT1CLK (quartz) > TA0CTL = MC_1 + TASSEL_2 + TACLR + ID_2; // On sélectionne le mode > monté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é > {} //… rien ne se passe > if ((P1IN & BUTTON) == 0) // Tant que le bouton n'est pas enfoncé > {__delay_cycles(5000);} // Délai anti-rebonds > while ((P1IN & BUTTON) == 1) // Tant que le bouton est enfoncé > {} > while ((P1IN & BUTTON) == 0) // Tant que le bouton est enfoncé > { > if ((P1OUT == GREEN_LED)||(P1OUT == RED_LED)) // Si l'une des LEDs est > allumé > { > P1OUT = LEDS_ETEINT; // ... On les é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
Reply by ●June 11, 20152015-06-11
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 secondsWHY THE HELL ARE YOU DELAYING 3seconds IN AN INTERRUPT HANDLER?> > P1OUT = 0x00; //red light > __delay_cycles(100000); // delay 3 secondsAND IF __delay_cycles(100000); IS 3seconds, then> > P1OUT = 0x01; //red light > __delay_cycles(1000000000); // delay 3 secondsthis 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
Reply by ●June 14, 20152015-06-14
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
Reply by ●June 15, 20152015-06-15
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. > > -- > > Rickyes, 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
Reply by ●June 15, 20152015-06-15
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
Reply by ●June 16, 20152015-06-16
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