Learning C with the MSP430

Discussion in 'Programmer's Corner' started by Sparky49, Jan 22, 2013.

  1. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Hi everyone.

    I've nearly come to the end of my January exams, so I think it is time I started getting back into the swing of electronics. I'm going to pick up where I left off from the Texas Instruments MSP430 dev kit.

    I know a few members have/promote the kit, and I presume many more know C, so hopefully you'll be able to help.

    I have tried to use the development wiki for the MSP430 to learn C. However, it only explains the very simple aspects. With it, I've only been able to make programs to change the little leds which come on the board.

    I've copied the simple blink the led on and off program, and I understand how it works. I've changed the duration of on and off. I've figured out how to get both red and green leds to flash on and off, at the same time alternately, etc.

    But moving on from there, I cannot see how to do it. :/

    I have been watching YouTube tutorials for C, have have learnt the basic logical operators and how one can use them, and I have read through some C tutorials online. However, the problem I'm having is that they all seem to presume some prior knowledge.

    Furthermore, I'm not sure which things are compatible my compiler, with TI components, the MSP430 etc, so that confuses me too. :/

    Can anyone point me to a good website? I have really tried hard to get on with it, but I'm finding it hard to find good examples I can look at, tweak, play around with, etc.

    Perhaps some could offer suggestions? I know learning to program isn't something that happens overnight, but I just don't know how to progress from here.

    Thanks very much for your time, guys.

    I really appreciate it. :)

    Sparky
     
  2. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Buy a book about C language. Simply put.

    Figure out what kind of circuits you want to build with this small microcontroller.
     
    Sparky49 likes this.
  3. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Thanks for the quick reply!

    Is C a universal language? There are no variations in the way it is used?
     
  4. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
  5. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    The spelling is the all the same basically.

    The memory layout is of course different, as well the I/O capabilities.

    You can use 32bits long int on a small 8 bit chip- the comiler will generate the neccessary machine code. This is what makes C so comfortable (on 8bit microcontrollers). You can use 16 bit or 32 bit when you need it.

    For instance the book "The C language" makes well sense as a reference no matter what platform you use.

    I never used any books to learn C language, so it is possible. But I had previous exposure to assembler, as well I used the MSDN VC++ documentation. Which also is a moderately good language reference.

    Look at the webpage by the C++ inventor Bjarne Strostroup, well well the original C language researcher's web pages. They all contain bits and pieces of useful information.

    Nonetheless you need to make yourself familiar with the datasheet of the controller you are using.

    All this stuff is often very abstract, so you need creativity to build some circuits, and experience, or both.

    Use schematics if you like- myself I don't use them much at all, except for SMPS circuits. Of course I take a look, but I never really used a schematic for my controller circuits.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    For the most part, C will be C, but there will be odd little things that each compiler does to make C work on a microcontroller...

    Example code is helpful, the chip's datasheet is a must, and the compiler's user guide will explain the quirks.
     
  7. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    I think its safe to say C is a universal language as C could be run almost on any platform(microcontrollers or microprocessors) we have today.Yes their are some variations on different compilers but its not in the language its the way its used.You don't need to learn C++ for programming microcontrollers or microprocessors.C++'s OOP feature is useful when programming under some operating system ,on system level C is the language you need.

    Dennis Ritchie was the person who created the C language
    http://cm.bell-labs.com/who/dmr/
    http://en.wikipedia.org/wiki/Dennis_Ritchie

    I have learnt C language under windows to create windows application but that knowledge was enough for programming microcontrollers(actually too much you just need a bit of that),so you can do it in my way download VisualStudio C++ Express Edition its Free ,it contains a C/C++ compiler which will do the job ,or you can use GNU C compiler search google.Remember with these compilers you will be able to write codes only for windows not for any microcontroller,but its will be help full as most online tutorial use windows application to teach C.

    A free C book
    http://publications.gbdirect.co.uk/c_book/
    A C tutorial
    http://www.iu.hio.no/~mark/CTutorial/CTutorial.html

    Good Luck
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I think this is good advice. If you don't know C yet, it would be twofold for also trying to learn microcontrollers too... I think you should learn C, then learn C on microcontrollers...
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    ANSI C is standard. I was not aware of such a thing as C for microcontrollers.

    What you may encounter are different implementations of C and differences in compilers.
    Along with that, library functions are extensions of C and may or may not be available depending on the programming platform of the software vendor.
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You can find many books about C. Most of them will cover what you need in MCU programming. Spend some time and find a book you like. Use the library, Google Books, or browse some book store.
    If you get an LCD display. You can build a lot of cool things. Like a clock or a simple digital voltmeter to mention some. The UART is also fun to play with
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    The first thing you want to do is write a program to flash the red and green LEDs.

    The next step is to move the MSP430 chip to a breadboard and connect the programming cable over to the breadboard. I can show you how to do that if you don't know how.

    The put some more LEDs on the breadboard and program away.

    Step 3. Get a character LCD and interface to the MSP430 chip.

    Program the mcu to display characters.
    Next display hex values.
    Followed by 16-bit integer values in decimal notation.
    No shortage of good old fashioned programming exercises.
     
    absf likes this.
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    The syntax and most of the "Standard Library" functions for C are the same across platforms.

    The big differences shine through with what other libraries are included. This is an area where Arduino has a very large base of libraries, where one doesn't need to know I2C communication details to talk to a device, the programmer just includes the Wire library, which does all the under the hood details for the programmer.

    Different compilers offer different standard library functions, the biggest difference is C on unix has standard output and standard error output (console devices). A Microcontroller has no default standard output, so functions like printf are implemented in the LCD library, for example, rather than being a standard library function.

    The syntax is straightforward, the biggest concept people get confused with is when working with pointers and doing pointer math. The rest of it is essentially knowing what library functions are available and how to use them.
     
  13. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    This is the point I am trying to make if you wish to become an embedded microcontroller expert.

    If your goal is to get your project completed as efficiently as possible, ignore having to learn ASM and get the Arduino.

    If your objective is to become an expert, learn ASM on the MSP430 (or Atmel or Freescale mcu) and write all of your own library functions, using ASM or C.
     
    thatoneguy likes this.
  14. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    Also program with the mcu datasheet in front of you, and try to understand the Oscillation settings... Timing is essential.
     
  15. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Thanks for all the replys guys.

    I have taken a major crash course in C programming on the computer.

    Now I'm going to start moving over to implement my newfound knowledge on uC's.

    After that, I'll certainly be delving into asm for micros.

    Thanks again for your brilliant replies. :)

    Sparky
     
  16. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    Which crash course? Care to share?
     
  17. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    834
    417
    Sure, I used this website:

    http://www.codingunit.com/the-history-of-the-c-language

    And just went over everything really quickly, writing out each program, changing and adding, subtracting things, etc.

    Any things I didn't know or understand I googled and read up on, like &, %, etc...

    Just making sure I understood what, why and how things work.

    I find that sort of thing really satisfying. :)

    Sparky
     
    thatoneguy likes this.
  18. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    That website is great CodingUnit.
     
  19. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Yes, it is. I normally refer people to the GNU C Tutorial

    That is but one site hosting the GNU C tutorial, but it is very thorough.

    The benefit CodingUnit has over the GNU C tutorial is ease of locating basic topics, you don't need to drill down from a couple dozen initial topics to find a sub link that takes you to the link you are looking for. Google is handy that way, though, just google <topic> site:crasseux.com/books/ctutorial.

    Same technique can be used here by searching for <topic> site:forum.allaboutcircuits.com which finds better results through their algorithm than the forum search function typically does. It also saves server load for the AAC site. :)
     
  20. JMac3108

    Active Member

    Aug 16, 2010
    349
    66
    Sparky49,

    You got some great answers to your questions and some suggestions. Most of this will therefore be repeat - but maybe it will help.

    The MSP430 is a great chip to learn with. Its inexpensive, highly capable, and used in industry. You can get an MSP430 Launchpad development board which includes the programmer for only $4.30! In my opinion this is a better learning platform than the Aurduino because you are right down at the hardware level with nothing in-between.

    I don't agree with the suggestion to learn C on a PC first, then on a micro. Just jump into the micro, if you're into electronics, then this is where you want to be.

    C is standard with small exceptions that you likely won't run into.

    You will see lots of references inside a C program for a microcontroller that do not look like standard C. On a microcontroller you have to be able to talk to the hardware. Rather than having to know the harware addresses of all the registers inside your micro that are used to setup and run the peripherals such as timers, ADCs, serial ports, etc, your compiler will provide an .h file such as io430.h in the case of the MSP430. This file names and defines the registers and the bits inside them to make it easier for you to access them. For example, on the MSP430F5342, UCA0BR0 is the SPI port bit clock control register. Its used to set the clock divisor for a SPI communication port. So if I write this line of code, UCA0BR0 = 4; , this means to set the SPI clock to the system clock divided by 4. You need to have the User's Guide for your particular microcontroller with you at all times when programming because it explains what all these registers and their bits do.

    There are also other things you might see that do not look like standard C. For example you might see some functions defined with an underscore in front of the name such as __delay_cycles(); These are intrinsic functions provided by the compiler that do low level stuff on the microcontroller.

    Blinking LEDs and reading switches is a great place to start learning.

    Learning to make an interrupt work is a great next step.

    Operating a keypad or a character display is a good place to go next.

    Making a serial port work is a great exercise and very useful too. Once this is working, you can connect your microcontroller to a PC serial port and send messages or commands back and forth to help debug your programs. You use a terminal emulator program on the PC such as HyperTerm or RealTerm or TeraTerm. Also you'll need an RS-232 level shifter chip such as MAX232 to change the digital levels on your microcontroller serial port to the RS-232 levels on the PC serial port.

    Another good project is to get the SPI port working. Once you have this, you can use it to talk to all kinds of interesting chips that have SPI interfaces such as DACs, Temperature sensors, and flash memory.

    Make good use of the sample code provided by the microcontroller manufacturer. Don't blindly copy it, but use it as an example to figure out how to set things up. By far, the biggest challenge in programming microcontrollers is not the C language. Its figuring out how to setup the registers in your microcontroller to make something work (such as a timer or ADC).

    Everything you do builds on what you did before. Eventually you'll have yourself a nice library of code to do lots of things and you won't have to start over from scratch every time you want to write an application.

    Oh, one more thing. You said you used the MSP430 to blink LEDs so you're aware of the "super loop" type of microcontroller application. Basically this is just an endless loop in your main program that has all of your code. There are a lot of different ways to implement this and you'll eventually want to get into them. But for your initial learning, just stick with the most basic way of doing this - just like the LED blinker. Later you can get more sophisticated by following examples you can find online, or maybe even use an RTOS or write your own.

    Hope this helps.

    Jim
     
    thatoneguy likes this.
Loading...