Electronics > Microcontrollers

MSP430G2231 LaunchPad - Beginner Needs Help

<< < (5/5)

baljemmett:

--- Quote from: jproject on June 01, 2011, 08:08:34 pm --- P1DIR = BIT0; // got rid of OR assignment "|="
 P1OUT &= BIT0; // got rid of "~"
 P1IE = BIT3; // got rid of OR assignment "|="

Now, why does this do the exactly the same thing as the original code?

--- End quote ---

Well, to be honest, rather than answer your question it might be better to ask you one in return: under what conditions would those changes result in no change in behaviour?  An understanding of the bitwise operators is, as has been pointed out, essential to the sort of register manipulations you'll need to be doing when you work with a microcontroller.  Since you seem to understand already what the various bitwise and assignment operators do, it shouldn't be too tricky to reason about the differences your changes made :)

jproject:

--- Quote from: baljemmett on June 01, 2011, 09:34:07 pm ---
--- Quote from: jproject on June 01, 2011, 08:08:34 pm --- P1DIR = BIT0; // got rid of OR assignment "|="
 P1OUT &= BIT0; // got rid of "~"
 P1IE = BIT3; // got rid of OR assignment "|="

Now, why does this do the exactly the same thing as the original code?

--- End quote ---

Well, to be honest, rather than answer your question it might be better to ask you one in return: under what conditions would those changes result in no change in behaviour?  An understanding of the bitwise operators is, as has been pointed out, essential to the sort of register manipulations you'll need to be doing when you work with a microcontroller.  Since you seem to understand already what the various bitwise and assignment operators do, it shouldn't be too tricky to reason about the differences your changes made :)

--- End quote ---



BIT0 = 0x0001 = 0000 0001
BIT3 = 0x0008 = 0000 1000


Original:
P1DIR |= BIT0;   //I'm assuming since all pins are set on input on default, P1DIR will be 0000 0000
       //P1DIR |= BIT0 will equal P1DIR = 0000 0000 | 0000 0001 which equals 0000 0001
P1OUT &= ~BIT0;    //Since BIT0 is set to output, I'm assuming P1OUT = 0000 0001
       //P1OUT &= ~BIT0 equals P1OUT = 0000 0001 & 1111 1110 which equals 0000 0000
P1IE |= BIT3;    //*I am not sure what P1IE value is at the beginning but I'm assuming it would be
       //either 0000 0000 or 0000 1000


My Editted Version:
P1DIR = BIT0;   // got rid of OR assignment "|="
P1OUT &= BIT0;  // got rid of "~"
P1IE = BIT3;    // got rid of OR assignment "|="

Which also means:
P1DIR = 0000 0001
P1OUT = P1OUT & 0000 0001  //Does P1OUT == 0000 0001? If so then the final P1OUT is 0000 0001
P1IE = 0000 1000


Please do correct me if I am wrong! As always, thank you.

Mechatrommer:

--- Quote from: jproject on June 01, 2011, 09:13:50 pm ---Any good sites or ebooks you would recommend for MCU programming? I am currently just reading over the MSP430 Microcontroller Basics ebook.

--- End quote ---
ere let me google for you, type "binary arithmetic operation bitwise etc". some random search...
http://atrevida.comprenica.com/atrtut02.html
http://www.doc.ic.ac.uk/~eedwards/compsys/arithmetic/index.html
and then read your C/C++ book (on bitwise section), buy one if you still dont have. and then your MSP430#!@$@!#$ datasheet. and then the example you already found out. and then alot of practice! good luck ;)

Mechatrommer:
ok i see where's you getting now. i suggest you try to simulate the value in your pc environment. try to make a pc program that will show the binary value during debugging. so you can check your mistake faster.
P1OUT &= ~BIT0 ... p1out = p1out and (not bit0)

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version