MSP430G2231 LaunchPad - Beginner Needs Help - Page 1
Forum section: electronics / general

author: jproject
author_rank: Contributor
timestamp: May 30, 2011, 07:13:09 am
content: Hello everyone!I recently got my own LaunchPad and I am very new to microcontrollers (this is actually my very first microcontroller). I just finished writing some codes in C and making it blink in various delay time but I'm having trouble understanding the coding for this microcontroller. I checked the header file msp430g2231.h but that just made me even more confused. I checked the user's manual for msp430 series but its almost 700 pages so... I was wondering if anyone can help me understand the coding for msp430 without me reading the whole manual. Right now, I just want to do simple projects like get a light sensor and make it turn on an LED when there is no light (a night light). Also, what kind of sensor (the specs of the sensor) would I need?Thank you!

author: johnboxall
author_rank: Unknown
timestamp: May 30, 2011, 07:33:37 am
content: You can find good articles and some tutorials as well and a MSP430 forum athttp://www.43oh.com/

author: jproject
author_rank: Contributor
timestamp: May 30, 2011, 07:50:19 am
content: Thank you for the quick reply!Here is a good link I found for beginners from the website you recommended:http://www.43oh.com/forum/viewtopic.php?f=8&t=449&p=6346#p6346There are many different people suggesting various ebooks and websites to get you started.

author: jproject
author_rank: Contributor
timestamp: May 30, 2011, 06:23:41 pm
content: I'm still having quite a bit of trouble understanding the coding. These examples are not based off of LaunchPad and they don't explain the actual coding (like the logical operators). Can anyone help?Thank you.P.S. Do I need to learn all the architect of the microcontroller and the board to do small projects? I currently just finished first year electrical engineering and these microcontrollers seem like 4th year stuff. My instructor recommended me to test this out rather than playing around with arduino because it will be better in the end but it looks like it will take the whole summer to understand the LaunchPad.

author: Bored@Work
author_rank: Super Contributor
timestamp: May 30, 2011, 06:34:11 pm
content: Quote from: jproject on May 30, 2011, 06:23:41 pmYes.

author: jproject
author_rank: Contributor
timestamp: May 30, 2011, 06:42:35 pm
content: Alright! Time to read 300 more pages on the microcontroller... yay!

author: TheDirty
author_rank: Frequent Contributor
timestamp: May 31, 2011, 01:09:15 pm
content: Did you try googling msp430 tutorial or launchpad msp430 tutorial? It looks like there are quite a few out there.http://www.egarante.net/2010/09/msp430-launchpad-tutorial-part-1-basics.htmlhttp://e2e.ti.com/videos/m/microcontroller/208276.aspxIf you are moving from another microcontroller, it should not be difficult at all to look up the specifics of the peripherals in the F2xxx user guide. If you are starting from scratch, working with any microcontroller without abstract libraries like an Arduino is going to be difficult to get up to speed on.I would also suggest using TI's sample peripheral code as a base when trying out something new.http://focus.ti.com/mcu/docs/mcuprodcodeexamples.tsp?sectionId=96&tabId=1468

author: tsmz
author_rank: Contributor
timestamp: June 01, 2011, 01:22:39 am
content: I don't think you need to read the microcontroller manuals yet. They're recommended reading for sure, especially the family user's guide. However, before reading it, make sure you find out exactly which modules your microcontroller has, as the family user's guide is rather large and you wouldn't want to read everything on modules you'll never be able to use.I get the impression you don't know terribly much about C programming. This is absolutely required before doing anything with microcontrollers, even with systems like the arduino, which is basically just a set of very high-level hardware-abstracted C/C++ libraries. You can find many C tutorials online, and it's important to know your C when getting into microcontrollers.TI's examples are a mixed bag, I don't personally find them very informative at times. However, there's a lot of code on the web and TI's launchpad wiki contains links to other ressources, which you should check out.

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 01:59:51 am
content: Quote from: TheDirty on May 31, 2011, 01:09:15 pmThe Enrico Garante site has helped me a lot! Understood it so easily. Thank you.Quote from: tsmz on June 01, 2011, 01:22:39 amI am familiar with the C/C++ language (studied up to the point where I am able to make a search/store database program) but there are some codes that I have never seen before such as these bitwise assignment operators (|=, &=, ^=). Why do we need these?. Also, I have never seen these "~" in C programming but I've seen them quite a bit in the MSP430 source files in front of the pin addresses. I decided to experiment and ran the source code without "~" in front of the addresses and it made no difference so I'm confused why they are needed in the first place.As for the module, I currently have the MSP430G2231 LaunchPad (like the title of the topic says) so that should explain everything.Yes, I checked the TI video tutorials but it is quite confusing at times because they tell you to look at the example and the TI manual without any reference to page number or section of the manual. I did find them after few minutes of reading through the titles of the chapters but not helpful at all unless if you are familiar with the architect of the whole MSP430!

author: 8086
author_rank: Super Contributor
timestamp: June 01, 2011, 02:11:09 am
content: Quote from: jproject on June 01, 2011, 01:59:51 am"~" means invert all the bits in the operand, I believe.

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 02:26:29 am
content: So if I do ~0x001 it will equal 11...10? I still don't understand because I programmed my MCU with an example code after taking out the "~" and it worked perfectly fine. GRRR so confusing!

author: 8086
author_rank: Super Contributor
timestamp: June 01, 2011, 02:30:43 am
content: It may be that the line of code did not alter anything in the program's main functionality.Could you post the line in question, including a few on either side of it?

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 02:44:47 am
content: Here it is, it's fromhttp://www.egarante.net/2010/09/msp430-launchpad-tutorial-part-1-basics.html#include <msp430g2231.h>int main(void) //Main program{WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerP1DIR |= BIT0; // Set P1.0 to output and P1.3 to input directionP1OUT &= ~BIT0; // set P1.0 to OffP1IE |= BIT3; // P1.3 interrupt enabledP1IFG &= ~BIT3; // P1.3 interrupt flag cleared__bis_SR_register(GIE); // Enable all interruptswhile(1) //Loop forever, we'll do our job in the interrupt routine...{}}#pragma vector=PORT1_VECTOR__interrupt void Port_1(void){P1OUT ^= BIT0; // Toggle P1.0P1IFG &= ~BIT3; // P1.3 interrupt flag cleared}If you are not familiar with the header file, BIT0 to BIT# meansBIT0 = 0x0001u (this is the address for the LED)BIT1 = 0x0002uBIT2 = 0x0004uBIT3 = 0x0008u (this is the address for the switch button)etc...

author: TheDirty
author_rank: Frequent Contributor
timestamp: June 01, 2011, 05:03:13 pm
content: ~ inverts the number, soBIT3 = 00001000~BIT3 = 11110111So (11110111 & variable) clears the bit in variable.I'm not certain what happens if you don't clear the interrupt flag. I guess nothing if it still works fine, but without clearing the interrupt flag you can't check to see which interrupt initiated the ISR since the flag is left high.

author: jahonen
author_rank: Super Contributor
timestamp: June 01, 2011, 07:38:07 pm
content: If the port interrupt flag is not cleared at end of the ISR on a MSP430, the MCU will immediately re-enter the ISR upon return from it.Regards,Janne

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 08:08:34 pm
content: This is the code I used to programme the MCU and it works exactly like the original code posted above (push the switch to toggle the red LED).#include <msp430g2231.h>int main(void) //Main program{WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerP1DIR = BIT0; // got rid of OR assignment "|="P1OUT &= BIT0; // got rid of "~"P1IE = BIT3; // got rid of OR assignment "|="P1IFG &= ~BIT3; // P1.3 interrupt flag cleared__bis_SR_register(GIE); // Enable all interrupts//Got rid of while loop}#pragma vector=PORT1_VECTOR__interrupt void Port_1(void){P1OUT ^= BIT0; // Toggle P1.0P1IFG &= ~BIT3; // P1.3 interrupt flag cleared}Now, why does this do the exactly the same thing as the original code?

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 01, 2011, 09:02:40 pm
content: Quote from: jproject on June 01, 2011, 01:59:51 amno! you are not familiar with C, esp you are not familiar with binary operation. you maybe an expert database programmer, but to become mcu programmer, you need to master number crunching in binary! and i'm not the oneonce you've done with binary operation, then yea! you can read the manual of what bit in the register do what.

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 09:13:50 pm
content: Quote from: Mechatrommer on June 01, 2011, 09:02:40 pmHaha yeeeee! I am very new to MCU programming. I must get "dirty" as my instructor would say and learn up to the binary parts of the MCU. Any good sites or ebooks you would recommend for MCU programming? I am currently just reading over the MSP430 Microcontroller Basics ebook.

author: Unknown
author_rank: Unknown
timestamp: June 01, 2011, 09:17:16 pm
content: I agree with the other people that a good grasp on C, especially the bitwise operators, is critical for MCU programming in C. Learning aspects of C on a PC is usually easier than on a micro because debugging and testing on a PC tend to be much easier.This tutorial on bitwise operatorsmight be a good read. Bitwise operators have been a standard part of C since K&R C, so they work the same in all compilers on all platforms. Nothing AVR specific about that tutorial.

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 09:31:35 pm
content: Quote from: alm on June 01, 2011, 09:17:16 pmThank you for the link! I am getting directed in the right direction which is very good. Here is also a link I would like to add for bitwise in C:Pretty much the same as the forum except extra explanation and video format.

author: baljemmett
author_rank: Unknown
timestamp: June 01, 2011, 09:34:07 pm
content: Quote from: jproject on June 01, 2011, 08:08:34 pmWell, 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

author: jproject
author_rank: Contributor
timestamp: June 01, 2011, 09:56:49 pm
content: Quote from: baljemmett on June 01, 2011, 09:34:07 pmBIT0 = 0x0001 = 0000 0001BIT3 = 0x0008 = 0000 1000Original: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 0001P1OUT &= ~BIT0; //Since BIT0 is set to output, I'm assuming P1OUT = 0000 0001//P1OUT &= ~BIT0 equals P1OUT = 0000 0001 & 1111 1110 which equals 0000 0000P1IE |= BIT3; //*I am not sure what P1IE value is at the beginning but I'm assuming it would be//either 0000 0000 or 0000 1000My 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 0001P1OUT = P1OUT & 0000 0001 //Does P1OUT == 0000 0001? If so then the final P1OUT is 0000 0001P1IE = 0000 1000Please do correct me if I am wrong! As always, thank you.

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 01, 2011, 10:13:00 pm
content: Quote from: jproject on June 01, 2011, 09:13:50 pmere let me google for you, type "binary arithmetic operation bitwise etc". some random search...http://atrevida.comprenica.com/atrtut02.htmlhttp://www.doc.ic.ac.uk/~eedwards/compsys/arithmetic/index.htmland 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

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 01, 2011, 10:18:44 pm
content: 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)