EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

A beginners programming tutorial for MSP430

Started by v_shukla72 January 5, 2009
??? to who does this seem clearer? What is a single bit shift but a
multiplication by 2? Why would something visually more complex be
clearer? The objective is to define single bit values NOT perform a
shift operation. No such operation actually or symbolically exists.

Al

Johan Compen wrote:

>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.
>
>

Beginning Microcontrollers with the MSP430

It's a matter of style. I prefer to write (1 << 27) instead of 134217728.

Johan.
On Fri, Jan 9, 2009 at 12:18 AM, Onestone wrote:
> ??? to who does this seem clearer? What is a single bit shift but a
> multiplication by 2? Why would something visually more complex be
> clearer? The objective is to define single bit values NOT perform a
> shift operation. No such operation actually or symbolically exists.
>
> Al
>
> Johan Compen wrote:
>
>>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.
>>
>>
what's wrong with hex? 0x08000000, or, since we are talking about a 16
bit machine simply 0x0800? Much clearer?

Al

Johan Compen wrote:

>It's a matter of style. I prefer to write (1 << 27) instead of 134217728.
>
>Johan.
>On Fri, Jan 9, 2009 at 12:18 AM, Onestone wrote:
>
>
>>??? to who does this seem clearer? What is a single bit shift but a
>>multiplication by 2? Why would something visually more complex be
>>clearer? The objective is to define single bit values NOT perform a
>>shift operation. No such operation actually or symbolically exists.
>>
>>Al
>>
>>Johan Compen wrote:
>>
>>
>>
>>>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.
>>>
>>>
>>>
>>>
>>>
>>>
I wrote this tutorial for beginners. I have simplified and removed the
errors ( as far as I think).

I will also add up more pages when I get time.

Please check

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

and onwards.

The tutorial is not intended to replace a book. Its purpose is to get
a totally newbie to get started with a "hello world" of embedded
world. he can then pick up some standard book and formal programming
technique.

I do want to add some more pages including ADC, Serial Ports, Concept
of Interrupt - when I get time.
Why do you need to change:
#include

And, why change to:
#include

Why do you also add:
#define BIT0=0x01

--OCY

--- In m..., "v_shukla72" wrote:
>
> I wrote this tutorial for beginners. I have simplified and removed the
> errors ( as far as I think).
>
> I will also add up more pages when I get time.
>
> Please check
>
> http://referencedesigner.com/tutorials/msp430/msp430_04.php
>
> and onwards.
>
> The tutorial is not intended to replace a book. Its purpose is to get
> a totally newbie to get started with a "hello world" of embedded
> world. he can then pick up some standard book and formal programming
> technique.
>
> I do want to add some more pages including ADC, Serial Ports, Concept
> of Interrupt - when I get time.
>

--- In m..., Onestone wrote:
>
> ??? to who does this seem clearer? What is a single bit shift but a
> multiplication by 2? Why would something visually more complex be
> clearer? The objective is to define single bit values NOT perform a
> shift operation. No such operation actually or symbolically exists.
>
> Al

Ha! When I first saw that 1 << 0 stuff, I first did a double take,
then oh, I thought wow that's bizarre, it was like, someone somewhere
has a sole purpose in life to make things more complex then they need
to be.

"bungalow_steve" :

> --- In m..., Onestone wrote:
>>
>> ??? to who does this seem clearer? What is a single bit shift but a
>> multiplication by 2? Why would something visually more complex be
>> clearer? The objective is to define single bit values NOT perform a
>> shift operation. No such operation actually or symbolically exists.
>>
>> Al
>
> Ha! When I first saw that 1 << 0 stuff, I first did a double take,
> then oh, I thought wow that's bizarre, it was like, someone somewhere
> has a sole purpose in life to make things more complex then they need
> to be.

Ha! This is nothing. Real master can create nearly unreadable code using C:

http://www.ioccc.org/

M.
> Ha! This is nothing. Real master can create nearly unreadable code using
> C:
>
> http://www.ioccc.org/

quick link: http://www.de.ioccc.org/years.html

M.
"Johan Compen" :

> 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
>
> You are right, my bad.

This discussion is a good example that using shift operations for constants
is producing more headache than the simple hex approach. :-)

M.
Guys,

Database guru C. J. Date from the '70s once said something
like "There is no computer problem that cannot be solved by the addition of a few more levels of indirection."

Later,
Mike Raines

________________________________
From: m... [mailto:m...] On Behalf Of Matthias Weingart
Sent: Friday, January 09, 2009 10:27 AM
To: m...
Subject: [msp430] Re: A beginners programming tutorial for MSP430
> Ha! This is nothing. Real master can create nearly unreadable code using
> C:
>
> http://www.ioccc.org/

quick link: http://www.de.ioccc.org/years.html

M.



The 2024 Embedded Online Conference