A beginners programming tutorial for MSP430

Started by v_shukla72 January 5, 2009
This programming tutorial will be helpful for beginners and newbies.

http://referencedesigner.com/tutorials/msp430/msp430_01.php

Beginning Microcontrollers with the MSP430

> This programming tutorial will be helpful for beginners and newbies.
> http://referencedesigner.com/tutorials/msp430/msp430_01.php

I have seen this before but parts of it seems to be broken. For
example
http://referencedesigner.com/tutorials/msp430/msp430_09.php
is not available for me.
Martin
This "tutorial" has some major bugs in example code. For example:

http://referencedesigner.com/tutorials/msp430/msp430_04.php
P6DIR |= (1< P6OUT|= (1< P6OUT&=~(1<
This code uses P6.1 and not P6.0 because (1<
The code should be:
P6DIR |= BIT0;
P6OUT |= BIT0; // P6.0 High
P6OUT &= ~BIT0; // P6.0 Low

In general, the BITx defines should not be used in a (1 << BITx) form
as they are already defined as follows:
#define BIT0 (1 << 0) // 1
#define BIT1 (1 << 1) // 2
#define BIT2 (1 << 2) // 4
#define BIT3 (1 << 3) // 8
#define BIT4 (1 << 4) // 16
etc.

Somewhere else on the same page:
"Similary, the statement P1DIR |= (1<<0x03), will assign P1.2 to be an
output pin.:

This is also incorrect. (1<<0x03) is 11 in binary, which means the
statement sets bits 0 and 1. Thus P1.0 and P1.1 are set to output mode
and not P1.2.

Further reading:
http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c

regards, Johan.
On Mon, Jan 5, 2009 at 11:46 PM, v_shukla72 wrote:
> This programming tutorial will be helpful for beginners and newbies.
>
> http://referencedesigner.com/tutorials/msp430/msp430_01.php

"Johan Compen" :

> In general, the BITx defines should not be used in a (1 << BITx) form
> as they are already defined as follows:
> #define BIT0 (1 << 0) // 1
> #define BIT1 (1 << 1) // 2
> #define BIT2 (1 << 2) // 4
> #define BIT3 (1 << 3) // 8
> #define BIT4 (1 << 4) // 16

I always ask me, what is the advantage of this?

Why not this:
#define BIT0 (1)
#define BIT1 (2)
#define BIT2 (4)
#define BIT3 (8)
?

M.

I do not use c. I believe some c programmers defines BIT1 this way:

#ifndef BIT1_defined
#ifdef BIT0_defined
#define BIT1 (BIT0<<1)
#else
#define BIT1 (2)
#endif
#define BIT1_defined
#endif

--OCY

--- In m..., "Johan Compen" wrote:
>
> This "tutorial" has some major bugs in example code. For example:
>
> http://referencedesigner.com/tutorials/msp430/msp430_04.php
> P6DIR |= (1< > P6OUT|= (1< > P6OUT&=~(1< >
> This code uses P6.1 and not P6.0 because (1< >
> The code should be:
> P6DIR |= BIT0;
> P6OUT |= BIT0; // P6.0 High
> P6OUT &= ~BIT0; // P6.0 Low
>
> In general, the BITx defines should not be used in a (1 << BITx) form
> as they are already defined as follows:
> #define BIT0 (1 << 0) // 1
> #define BIT1 (1 << 1) // 2
> #define BIT2 (1 << 2) // 4
> #define BIT3 (1 << 3) // 8
> #define BIT4 (1 << 4) // 16
> etc.
>
> Somewhere else on the same page:
> "Similary, the statement P1DIR |= (1<<0x03), will assign P1.2 to be an
> output pin.:
>
> This is also incorrect. (1<<0x03) is 11 in binary, which means the
> statement sets bits 0 and 1. Thus P1.0 and P1.1 are set to output mode
> and not P1.2.
>
> Further reading:
>
http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c
>
> regards, Johan.
> On Mon, Jan 5, 2009 at 11:46 PM, v_shukla72 wrote:
> > This programming tutorial will be helpful for beginners and newbies.
> >
> > http://referencedesigner.com/tutorials/msp430/msp430_01.php
> >
>
--- In m..., "Johan Compen" wrote:
> > This programming tutorial will be helpful for beginners and newbies.
> >
> > http://referencedesigner.com/tutorials/msp430/msp430_01.php
> >
> >
> This "tutorial" has some major bugs in example code. For example:
>

Or instead of debugging free stuff from the web you can get a book from
amazon without major bugs, that has fifteen chapters with solved
exercises ;-)

http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%
2Fwww.amazon.com%2FMSP430-State-Machine-Programming-ES2274%2Fdp%
2F0975475924%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1214397954%26sr%3D8-
1&tag=softcom-20&linkCode=ur2&camp89&creative25

Or if you like tiny links:

http://tinyurl.com/softbaugh-book3

Tom

Getting a book will not solve problems. You still have to read it,
under stand it, apply what you learned and solve problems.

--- In m..., "Tom Baugh" wrote:
>
> --- In m..., "Johan Compen" wrote:
> > > This programming tutorial will be helpful for beginners and newbies.
> > >
> > > http://referencedesigner.com/tutorials/msp430/msp430_01.php
> > >
> > >
> >
> >
> > This "tutorial" has some major bugs in example code. For example:
> > Or instead of debugging free stuff from the web you can get a book from
> amazon without major bugs, that has fifteen chapters with solved
> exercises ;-)
>
> http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%
> 2Fwww.amazon.com%2FMSP430-State-Machine-Programming-ES2274%2Fdp%
> 2F0975475924%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1214397954%26sr%3D8-
> 1&tag=softcom-20&linkCode=ur2&camp89&creative25
>
> Or if you like tiny links:
>
> http://tinyurl.com/softbaugh-book3
>
> Tom
>

> Somewhere else on the same page:
> "Similary, the statement P1DIR |= (1<<0x03), will assign P1.2 to be an
> output pin.:
>
> This is also incorrect. (1<<0x03) is 11 in binary, which means the
> statement sets bits 0 and 1. Thus P1.0 and P1.1 are set to output mode
> and not P1.2.

Just to clarify, and unless I'm terribly mistaken, this statement of
yours is also incorrect. When left-shifting, the MSP fills the new
bits with 0's. It is equivalent to multiplying by a power of 2.
Only when right-shifting a signed integer variable (char, int, etc) in
which a negative value is stored, does the MSP use 1's to fill the new
bits. It is equivalent to dividing by a power of 2.
This means that (1<<0x03) left-shifts 1 by 3 bits, or multiplies 1 by
2^3, which is 1000 in binary or 0x08. I've no idea how you came up
with 11 in binary.

P1DIR |= (1<<0x03), will configure the direction of P1.3 to be an output.

Michael K.
On Thu, Jan 8, 2009 at 4:25 PM, tintronic wrote:
>> Somewhere else on the same page:
>> "Similary, the statement P1DIR |= (1<<0x03), will assign P1.2 to be an
>> output pin.:
>>
>> This is also incorrect. (1<<0x03) is 11 in binary, which means the
>> statement sets bits 0 and 1. Thus P1.0 and P1.1 are set to output mode
>> and not P1.2.
>
> Just to clarify, and unless I'm terribly mistaken, this statement of
> yours is also incorrect. When left-shifting, the MSP fills the new
> bits with 0's. It is equivalent to multiplying by a power of 2.
> Only when right-shifting a signed integer variable (char, int, etc) in
> which a negative value is stored, does the MSP use 1's to fill the new
> bits. It is equivalent to dividing by a power of 2.
> This means that (1<<0x03) left-shifts 1 by 3 bits, or multiplies 1 by
> 2^3, which is 1000 in binary or 0x08. I've no idea how you came up
> with 11 in binary.
>
> P1DIR |= (1<<0x03), will configure the direction of P1.3 to be an output.

You are right, my bad.

Johan.

On Wed, Jan 7, 2009 at 3:49 PM, Matthias Weingart
wrote:
> "Johan Compen" :
>
>> In general, the BITx defines should not be used in a (1 << BITx) form
>> as they are already defined as follows:
>> #define BIT0 (1 << 0) // 1
>> #define BIT1 (1 << 1) // 2
>> #define BIT2 (1 << 2) // 4
>> #define BIT3 (1 << 3) // 8
>> #define BIT4 (1 << 4) // 16
>
> I always ask me, what is the advantage of this?
>
> Why not this:
> #define BIT0 (1)
> #define BIT1 (2)
> #define BIT2 (4)
> #define BIT3 (8)
> ?

The version with the bitwise left shift operator makes it more clear
that are single bit (1) is shifted N places to the left.
That's all.

Johan.