EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Switch lazy on Lunchpad, or wrong way to programm?

Started by ulix_s November 29, 2011
Hello everybodey,

after I fixed my problem with the LED's turning on, my next programm code workes. But the switch doesn't everytime turns on the led, is this the right way to programm this?

#include

int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;

P1OUT = 0;

P1DIR = BIT0;

while (1)

{

while ( (P1IN & BIT3) != BIT3)

{
P1OUT = BIT0 ;
}
P1OUT = 0;

}

return 0;
}

Beginning Microcontrollers with the MSP430

On Tue, Nov 29, 2011 at 2:11 PM, ulix_s wrote:

> after I fixed my problem with the LED's turning on, my next programm code workes. But the switch doesn't everytime turns on the led, is this the right way to programm this?

All mechanical switches make and break contact several times whenever
the switch is opened or closed. This property is known as "bounce"
and you need to de-bounce your switch input. This can be done in
hardware or software. There is plenty of information about bounce and
how to deal with it on the web. Here is a link to one of the best and
most comprehensive documents:

http://www.eng.utah.edu/~cs5780/debouncing.pdf

-p.
ulix_s wrote:
> Hello everybodey,
>
> after I fixed my problem with the LED's turning on, my next programm code workes. But the switch doesn't everytime turns on the led, is this the right way to programm this?
>
> while (1)
>
> {
>
> while ( (P1IN& BIT3) != BIT3)
>
> {
> P1OUT = BIT0 ;
> }
> P1OUT = 0;
>
> }
Hi,
You should work from concept to code, don't try to create concept from
code. So you want to sense the switch 'then' handle it. More properly
you would handle an interrupt on the change at P1.3 so you don't keep
your main loop hanging and/or checking for an event. And as pointed out,
you would have the opportunity to put the machine to sleep. And even
without an interrupt you don't want to have a wait for an event in the
main loop, it makes it useless when you go to extend your program. It is
stuck while the switch is off.

So you want to handle an event in the main loop.

for( ; ; )
{
//check and handle
if( P1IN & BIT3 )
P1OUT|= BIT0;
else
P1OUT&= ~BIT0;

//More stuff can be handled.
}

Now the program doesn't hang on your switch test.
P1IN & BIT3 is your truth. You don't need to compare it to anything.
Set/reset only the output bit of the port or you make the other pins of
the port worthless as outputs. You still have that green LED on P1.6?
What happens if you want to control it independently from the P1.0 LED?
Answer: if you 'P1OUT = BIT0' you shut off P1.6 if it were on.

If you don't know what |= and &= and ~ do, google'm.
Also, in a real world app you will probably want to debounce the switch
too, just something to think about, you don't need it here.

As simple as this program is, if you had have started with a flow chart
and written code from there, it may have worked better.

Best, Dan.

Just another hint for beginners: learn by example. TI has many very small
examples on the web (also reading of switches and switching LED on/off) for
the MSP430. (ti.com/msp430), browse through it - try to understand how it is
working - go through the code using the debugger - step by step - take a look
in the registers with the debugger and see how it is changing.
If you need to solve a certain problem: search for the example with the
nearest solution and change it to your needs. E.g. proper initialisiation of
the UART from the doc is a nightmare (tooo many options) - it is a quick
solution just to take it from the example. :-)

Maybe somebody here can suggest a good book for MSP430 newbies - I can only
suggest a German one.

M.

"ulix_s" :

> Hello everybodey,
>
> after I fixed my problem with the LED's turning on, my next programm
> code workes. But the switch doesn't everytime turns on the led, is this
> the right way to programm this?
>
> #include int main( void )
> {
> // Stop watchdog timer to prevent time out reset
> WDTCTL = WDTPW + WDTHOLD;
>
> P1OUT = 0;
>
> P1DIR = BIT0;
>
> while (1)
>
> {
>
> while ( (P1IN & BIT3) != BIT3)
>
> {
> P1OUT = BIT0 ;
> }
> P1OUT = 0;
>
> }
>
>
> return 0;
> }

In the links section of this group down the bottom is a link to a rapid
share of a really good introductory (and beyond) book on the msp430

http://tech.groups.yahoo.com/group/msp430/links

Regards

Michael Dalton


--- In m..., Michael Dalton wrote:
>
> In the links section of this group down the bottom is a link to a rapid
> share of a really good introductory (and beyond) book on the msp430
>
> http://tech.groups.yahoo.com/group/msp430/links
>
> Regards
>
> Michael Dalton
>
>

Well I know the book by davies. Got it as PDF.
Where I do get my tutorials from is this page: mspsci.blogspot.com, sometimes very hard to understand, because it's not for c beginners, buts ok.

--- In m..., "ulix_s" wrote:
>
> --- In m..., Michael Dalton wrote:
> >
> > In the links section of this group down the bottom is a link to a rapid
> > share of a really good introductory (and beyond) book on the msp430
> >
> > http://tech.groups.yahoo.com/group/msp430/links
> >
> > Regards
> >
> > Michael Dalton
> >
> >
> >
> > Well I know the book by davies. Got it as PDF.
> Where I do get my tutorials from is this page: mspsci.blogspot.com, sometimes very hard to understand, because it's not for c beginners, buts ok.
>

Actually, as for C programming, his code is not very convoluted, or obscure, but it does assume/require a level of familiarity with bitwise operation, and the underlying 16-bit addresses. Personally, I've found most texts on C skim over the subject, and what I've learned, which is nowhere near what it should be, has been based on stepping through with the debugger and looking at the associated assembly code.

Bob


Memfault Beyond the Launch