Thread: How much of C do you have to know before programming microcontrollers?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    40

    How much of C do you have to know before programming microcontrollers?

    I'm learning C on my own time. I'm going at a pretty fast pace, but I'm highlighting most of the important things or at least, what I think is important so I can refer back to it later. I struggle with most of the back of the chapter exercises mainly because I'm going through it really fast and most of the time, I have to end up looking at the solutions.

    How much of C do you have to know before you start programming microcontrollers for basic circuits needs such as controlling the elements in a circuit, motors, etc... Not too advanced programming stuff, but stuff that requires programming.

    I'm like 3/4 of the way through the book and I can see where some of this would apply, but if I were given a microcontroller and told to program it so it would behave (control a circuit) a certain way, I wouldn't even know where to start.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It depends on the microcontroller - what functions it offers, what mechanisms are available to program it, what peripherals it supports, what type of CPU it has, what I/O ports it supports, what operating system it supports (if any), and things like that.

    Quite a few microcontrollers support instructions with behaviour for which there is no general counterpart in C - so they require, at best, a non-standard dialect. Some microcontrollers have firmware with an interpreter or another programming language, so it is not possible to program them at all using C (unless you want to hack the firmware, which is not a task for the fainthearted). Some will only be programmable using some (controller-specific) assembler, whether that is because it supports funky instructions, or because the vendor or developer has not bothered to implement or support another means of programming it.

    All you can really do, once you have picked a microcontroller, is read the documentation. The vendor will usually provide information about how to program them, whether that involves C or not.

    Knowing some C may be helpful in reading the documentation. Or it may not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    I will be using an arduino microcontroller. They have their own language, but they said I can use C.
    It's for a mechatronics class I am taking next semester and we'll be doing a project. However, I do not know the exact details.


    Do you have any experience with an arduino?

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    "Arduino language" is just C with their own preprocessor (eg. the IDE adds include files for you). It's just C/C++ for all intents and purposes.

    Usually in a microcontroller you would talk to the outside world using voltage levels. For example, the circuit can be designed so that when it sees 5V from the microcontroller, it runs a motor, otherwise stop. Arduino has functions to set output pins to high or low voltage.

    On the input side, you'd have to design a circuit that amplifies and conditions a signal from a sensor to, for example, 0V-5V range (this part has nothing to do with programming), and wire it up to one of the analog input pins. Then, Arduino has functions to read the voltage levels of analog input pins.

    These are the most basic ways. Once you get into, for example, high speed digital communication, things get quite a bit more complicated, but you probably don't want to worry about that for now.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Depends on a lot of things. If you just want to spin a few motors back and forth, probably very little (it can be a cut-and-paste exercise).

    If you're talking timing, control, recovery, interaction with other computers, networking, GPIO, memory interactions, etc. etc. etc. then you could need to know the language inside out.

    If you can write a program that "does" what you need on a computer (i.e. it can run a function "spinning motor 0 now" that you have some idea how to write on the microcontroller), then you can do it on a microcontroller. It will still take work (learning the quirks of embedded compilers, embedded machines, limited memory, etc.) but you shouldn't have any "C" problems.

    If you wouldn't even know how to go about writing that sort of program, then obviously you're going to struggle until you do.

    Embedded programming in C is no different to programming in C in terms of the skill needed. Experience, and knowledge of the tools, yes, but not really skill.

    If you can program something in C, you can program that in C on an embedded device, in general.

    But you know the best way to find out? Try.

    P.S. Though it might seem like you're "learning" by skipping through a book and looking at the answers, I assure you that it's not the same when you come down to actually programming something complex. The knowledge is fleeting and not ingrained and you will struggle. As someone who went through the entire education system and came out with a degree with NOT ONE SINGLE PERIOD OF REVISION EVER, I can honestly attest to this. You can pass tests that way, but having the skill to do something can't be acquired without actually challenging your brain rather than just soaking it gently in knowledge and hoping it won't all dry out. That doesn't mean "do every exercise in the book", but you have to APPLY that knowledge, and challenge your brain, for it to become ingrained and proper rather than being an "expert" on what C can and can't do or how best to use it without ever having found out what that actually means in real life.

    Again, the best way to find out if this will affect you? Try to do what you are wanting to do.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    None. You can learn C with a microcontroller.

    The good thing about learning C through tutorials on your microcontroller, is that you are learning how to use the microcontroller at the same time.

    gooligum is a good PIC tutorial
    avrfreaks are a node of information for the AVR
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Click_here View Post
    The good thing about learning C through tutorials on your microcontroller, is that you are learning how to use the microcontroller at the same time.
    The bad thing is that you may think you are learning C when, in reality, you are learning about the microcontroller (or vice versa). The only way to be sure, if you do it that way, is try to apply what you have learned on a range of other computing platforms.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by grumpy View Post
    The bad thing is that you may think you are learning C when, in reality, you are learning about the microcontroller (or vice versa). The only way to be sure, if you do it that way, is try to apply what you have learned on a range of other computing platforms.
    That's good advice grumpy

    That is how I ended up writing C on computers afterall!
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by Click_here View Post
    None. You can learn C with a microcontroller.

    The good thing about learning C through tutorials on your microcontroller, is that you are learning how to use the microcontroller at the same time.

    gooligum is a good PIC tutorial
    avrfreaks are a node of information for the AVR
    A PIC is a req for the project I'm doing. Can you program a PIC microcontroller with C?

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by pyroknife View Post
    A PIC is a req for the project I'm doing. Can you program a PIC microcontroller with C?
    Yes

    There is a forum for programming PICs at Microchip

    As for tutorials, I suggest that you look at these series:
    http://www.gooligum.com.au/tutorials...IC_Mid_C_1.pdf

    The first few tutorials are free, but they now charge for the more advance ones. Gooligum have a development kit which you can use, but I've always just used a breadboard and plugged things into that.

    ICSP is a must -> This stands for "In Circuit Serial Programming" and allows you to program (I think...) all PIC devices; search microchip.com for "PICkit 3"

    As for the C language itself, this forum's tutorials is probably the best approach. C Tutorial - Learn C - Cprogramming.com
    I suggest you get Setting up Code::Blocks and MINGW, A Free C and C++ Compiler, on Windows - Cprogramming.com or similar, and work your way through a few problems in C

    Enjoy
    Last edited by Click_here; 01-16-2013 at 07:46 PM.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    My opinion is that it's better to learn 'C' first, before transitioning to microcontrollers.

    Obviously, 'C' programming has a learning curve. Getting a microcontroller to work has its own learning curve. It is better, I think, to tackle one before the other. Otherwise, when things aren't going right, you'll be utterly flummoxed.

    For instance, suppose you write MCU code to make eight LEDs blink in succession. You write the code, burn it into the chip ... and nothing happens. Is it a problem associated with 'C' (i.e. the loops you wrote aren't effective, or you left something important out of the code), or a problem associated with the device (i.e. you didn't clear or disable the WDT, or didn't initialize your ports properly)?

    I struggle with most of the back of the chapter exercises mainly because I'm going through it really fast and most of the time, I have to end up looking at the solutions.
    I appreciate (and applaud) your enthusiasm, but maybe you should slow down a little bit if this is the case. When you start to write your own programs, there won't be a "solution" to look up - you have to sit, think about it for a while, and figure it out yourself. (Side note: If memory serves me correctly, the most time-consuming bug I ever had to find and fix took me several hours.)

    --------

    However, my cautious warnings are not intended to scare you away from this kind of project! It might be a while before you can write sophisticated code for an MCU - but if you understand basic loops, there's a lot of fun you can have with microcontrollers while riding the learning curve.

    Best of luck!
    Last edited by Matticus; 01-16-2013 at 08:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-04-2010, 12:30 PM
  2. Microcontrollers in C
    By zyxx_66 in forum C Programming
    Replies: 4
    Last Post: 02-22-2009, 11:55 PM
  3. Programing microcontrollers in c
    By gorabhat in forum C Programming
    Replies: 2
    Last Post: 09-09-2008, 10:40 AM
  4. Embedded microcontrollers?
    By confuted in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-09-2006, 03:28 PM
  5. C Programming for Microcontrollers
    By CB4 in forum C Programming
    Replies: 4
    Last Post: 03-07-2005, 08:34 AM