On 5/29/2015 4:44 PM, Iron-Sim wrote:>> 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 ?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 <= b means assign a the value of b. read_timer() is some function that returns the current value of any free running timer/counter/time of day clock. I don't care what clock you use for this. You just need to be able to measure time with it in units of milliseconds to seconds. If it is a 16 bit timer running from a MHz clock that will roll over too fast to measure seconds. A 32 bit clock will do nicely if the rate is less than 16 MHz. You will need to read up on what you have libraries for or at least read up on the peripherals for the MCU you are using. You may need to understand modulo arithmetic to deal with the always present rollover issue. I assume you are programming in C with tools from TI? I had a small project not too different from this and decided to install Mecrisp Forth in a TI ARM target (also available for the MSP430). It worked well, I much prefer Forth to C. If you have the time to learn Forth you might want to give it a try. Unfortunately Mecrisp doesn't come with much documentation so it might not be the best to learn with. Forth, Inc has some free tools that support MSP430s and should be easier to get up. -- Rick
Project with "IF" function and timer
Started by ●May 27, 2015
Reply by ●May 29, 20152015-05-29
Reply by ●May 30, 20152015-05-30
>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><= b means assign a the value of b. > >read_timer() is some function that returns the current value of any free>running timer/counter/time of day clock. I don't care what clock you >use for this. You just need to be able to measure time with it in units>of milliseconds to seconds. If it is a 16 bit timer running from a MHz >clock that will roll over too fast to measure seconds. A 32 bit clock >will do nicely if the rate is less than 16 MHz. You will need to read >up on what you have libraries for or at least read up on the peripherals>for the MCU you are using. You may need to understand modulo arithmetic>to deal with the always present rollover issue. > >I assume you are programming in C with tools from TI? I had a small >project not too different from this and decided to install Mecrisp Forth>in a TI ARM target (also available for the MSP430). It worked well, I >much prefer Forth to C. If you have the time to learn Forth you might >want to give it a try. Unfortunately Mecrisp doesn't come with much >documentation so it might not be the best to learn with. Forth, Inc has>some free tools that support MSP430s and should be easier to get up. > >-- > >RickThanks 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 http://www.EmbeddedRelated.com
Reply by ●May 30, 20152015-05-30
On 5/30/2015 11:31 AM, Iron-Sim wrote:>> 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 > >> <= b means assign a the value of b. >> >> read_timer() is some function that returns the current value of any free > >> running timer/counter/time of day clock. I don't care what clock you >> use for this. You just need to be able to measure time with it in units > >> of milliseconds to seconds. If it is a 16 bit timer running from a MHz >> clock that will roll over too fast to measure seconds. A 32 bit clock >> will do nicely if the rate is less than 16 MHz. You will need to read >> up on what you have libraries for or at least read up on the peripherals > >> for the MCU you are using. You may need to understand modulo arithmetic > >> to deal with the always present rollover issue. >> >> I assume you are programming in C with tools from TI? I had a small >> project not too different from this and decided to install Mecrisp Forth > >> in a TI ARM target (also available for the MSP430). It worked well, I >> much prefer Forth to C. If you have the time to learn Forth you might >> want to give it a try. Unfortunately Mecrisp doesn't come with much >> documentation so it might not be the best to learn with. Forth, Inc has > >> some free tools that support MSP430s and should be easier to get up. >> >> -- >> >> Rick > > 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 ?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 by ●May 30, 20152015-05-30
>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? > >-- > >RickYes,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 http://www.EmbeddedRelated.com
Reply by ●May 30, 20152015-05-30
On 5/30/2015 11:56 AM, Iron-Sim wrote:>> 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 > > 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.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 by ●May 30, 20152015-05-30
Do you have a website with some tutorials to explain me how to debounce the presses button ? --------------------------------------- Posted through http://www.EmbeddedRelated.com
Reply by ●May 30, 20152015-05-30
>Do you have a website with some tutorials to explain me how to debounce >the presses button ?I have this code program that I adapted from http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_Drive_LED but the turn on/off do not work anytime. #include <msp430.h> #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 http://www.EmbeddedRelated.com
Reply by ●May 30, 20152015-05-30
On 5/30/2015 1:03 PM, Iron-Sim wrote:> Do you have a website with some tutorials to explain me how to debounce > the presses button ?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 by ●May 30, 20152015-05-30
On 5/30/2015 1:49 PM, Iron-Sim wrote:>> Do you have a website with some tutorials to explain me how to debounce >> the presses button ? > > > I have this code program that I adapted from > http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_Drive_LED but the > turn on/off do not work anytime. > > #include <msp430.h> > > > #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 > > }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 by ●May 31, 20152015-05-31
I do not understand, can you show me the code ? --------------------------------------- Posted through http://www.EmbeddedRelated.com