Microcontrollers and C programming advice

Thread Starter

Spark1985

Joined Dec 1, 2016
7
Hi all,
I'm currently studying HND electrical/electronic engineering at college I'm applying to university for next year with third year entry. I have spoken to a number of people regarding the transition process and if there is any extra work and studying I can do now to make the transition easier, and have repeatedly been told that the programming is the most difficult part. I know they use C Programming language and I plan to try and familiarise myself with it. I have also been told to try and get some practice with microcontrollers and embedded systems. I have had arduino mentioned to me.
This is where I get a bit lost, as there are a number of different versions, and I can't seem to get a straight answer as to whether it is Arduinos they use. I'm also not sure what compiler to use and if it really matters. I've sort of been lead to believe it doesn't really matter which compiler or microcontroller to use and that it's more about getting familiar with the processes.
There are several others looking to do the same transition as I am and I would like to be able to give everyone a clearer idea and what we can do or ideally try and get the university to provide us with some support materials to go through. Not just for myself and others this year but in years to come.
I feel as though the information I have been given is a bit vague and would like more detailed guidance. I was wondering if anyone here can give me any advice as to what sort of information I need to get or if what they have already said is substantial enough?

Sorry for my essay, this is my first post on here. Thank you in advice for any and all help and guidance provided.
 

shteii01

Joined Feb 19, 2010
4,644
I will share my experience.

My uC class used Intel 8051 uC development board and Keil software. The reason I say it this way is that I did not have to setup anything, everything was provided. All I had to do is write the code in C on pc and send it to the board, and watch it run (turn on/off led, show text on the lcd, "do stuff" when I press a button). Here is the thing... Keil offers programming software for free, it is limited, but it is good for learning.

My senior design project for graduation I used Arduino Due. Again, since I bought Due, I had a complete assembled board. Downloaded Arduino software, followed guide to how get Arduino software to talk to Due. Programming language is C like, but I think they might use straight C now.

Both of the above a complete software packages. Both use C.
 

djsfantasi

Joined Apr 11, 2010
9,151
All you need to start is a basic board - Arduino Uno. Then, the Arduino site has a download for the IDE. IDE is an acronym for integrated development environment. It contains a context sensitive editor, compiler and uploader in one program.

The Arduino site is one of the most useful in general that I have found. I will point out one useful content. Under "Learning" there is a reference link. I used it often when learning and still refer to it.
 

thumb2

Joined Oct 4, 2015
122
If you will buy Arduino, get the datasheet of the microcontroller used on the board and program using AVR C libraries or a little bit of Assembly (instructions set are usually on the datatasheet, and is very useful to learn how the micro and architecture works), and waste all the porky code libraries that Arduino staff will suggest to use (i.e. digitalWrite and similars ).
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,375
I've been writing code since 1970 on so many platforms in so many languages I could never remember them all. When I got to college I was learning my 3rd or 4th language and found it trivially easy because I already understood how to wrangle the code to do useful. Now that work was on computer systems (even mainframes) so I did not have any inputs or outputs to control till years later.

There are some basic tasks you need to get down first when entering the world of micros, like reading a button, lighting a light, reading a voltage, and such. What will serve you best are training wheels in the form of a prebuilt tested board with some tutorials. You start by entering known good code and seeing your results match the tutorials. Then you can go onto writing your own code, and designing your own boards.

Training wheels are great things when starting because you need to write good code for good hardware with a good programmer, and if any link in the chain goes bad you will be fairly clueless how to proceed.

The is simply me best board to start with. Start with any board you can afford that has free software tools for downloading and go from there.

One day you will want something else, and will either buy it or design it from scratch with your new found experience.
 

Thread Starter

Spark1985

Joined Dec 1, 2016
7
Wow guys, thank you so much for all the comments and advice. I can honestly there's some useful information in all of your replies.
So I had a look at the Arduino site as some of you recommended and I noticed there was a tutorial on building a breadboard Arduino, that really appeals to me. As some of you already said, it will probably help me to understand a bit more about what is going on. I've used breadboads with basic ICs on before so it's not really something that worries me. It's really the programming side of things that I'm a bit unsure on.
I've got a book from the university library that explains all the basics of programming and computer systems, so I plan on working through that.
I suppose I'm just a bit worried about spending money on stuff and I end up hardly using it, or needing something different.
I don't really understand anything about the setting up and programming process. I've read a bit on the Arduino site and I'm sort of starting to get there but still quite lost with it all.
 

MrChips

Joined Oct 2, 2009
30,467
There is nothing wrong with Arduino. If you want to eventually become an expert at embedded microcontroller systems at some point you need to learn about assembler language.

Which do you learn first, C or ASM?
I believe in learning to crawl before you walk.
My preference would be to learn ASM first. On which micro? On any micro.
My preference would be Atmel AVR, Freescale S908 or TI MSP430 LaunchPad.
(Freescale is now owned by NXP).
(Atmel is now owned by Microchip).

For a starting tutorial, this would be my recommended reading:

http://www.nxp.com/assets/documents/data/en/reference-manuals/M68HC05TB.pdf
 

shteii01

Joined Feb 19, 2010
4,644
Wow guys, thank you so much for all the comments and advice. I can honestly there's some useful information in all of your replies.
So I had a look at the Arduino site as some of you recommended and I noticed there was a tutorial on building a breadboard Arduino, that really appeals to me. As some of you already said, it will probably help me to understand a bit more about what is going on. I've used breadboads with basic ICs on before so it's not really something that worries me. It's really the programming side of things that I'm a bit unsure on.
I've got a book from the university library that explains all the basics of programming and computer systems, so I plan on working through that.
I suppose I'm just a bit worried about spending money on stuff and I end up hardly using it, or needing something different.
I don't really understand anything about the setting up and programming process. I've read a bit on the Arduino site and I'm sort of starting to get there but still quite lost with it all.
If you build your own arduino board, you will use "raw" ATmega328 chip (that is the actual uC chip). And you will run into your first problem. The raw chip needs a special device called Programmer (note my use of capital p) to write your code into the chip memory. This device can be expansive and with Arduino setup (note use of capital A, I am not talking about board, I am talking about organization called Arduino Foundation or some such) you only need it one time.

How does arduino board get around not using Programmer, you may ask. If you look and read about arduino boards, you will note that all you have to do is connect the board to pc, and then tell Arduino IDE to send code to the board. The way they do it is that each board has ATmega328 chip that has a special piece of software preloaded. This piece of software is called Bootloader, it resides on the chip. You run into chicken-egg problem. You need Programmer to write bootloader onto your raw chip, but once it is done, you will never need Programmer again. So, kind of waste of money.

You have a couple choices:
1. Buy ATmega328 chip, but don't buy raw chip, buy one that is already has bootloader on it. Some places sell them that way. Then do the breadboard build using this chip.
2. Buy actual arduino board (Uno is very popular and cheap) and either use it for your learning, or use it as Programmer (yes, you read it right, for this application a cheap 10 dollar arduino board replaces expansive 40-200 dollar Programmer).
 

ErnieM

Joined Apr 24, 2011
8,375
Building your own board makes a great SECOND project. Use a pre built and tested board for your first time out, least your first project also be your last project because "nothin worked so this is too hard for someone to do on their own."
 

Thread Starter

Spark1985

Joined Dec 1, 2016
7
Wow thank you guys yet again for all of your help. You've all helped me to understand things a bit more, I did read about the bootloader process I wasn't sure exactly what was required for it, so thank you for that.
Mr Chips I wasn't quite sure what the difference is between ASM and C?
The tutorial you've recommended looks really good and very similar to the book I got out.
The issue I have as we will do one unit on programming this year but not in C, I'm not sure what it is in. But going to university next year I know that the students we will be joining in third year have already done a fair amount of programming in C in second year. So therefore we will have a fair amount of ground to catch up on. Myself and a view others are trying to do what we can to make that gap a lot smaller so it isn't such a jump.
I have asked the department head that teaches the embedded systems unit at the university in third year, and he has not been very willing to go into much detail about what we can do to decrease the gap at all. Speaking to a student thats currently studying there I know that they do assembly language in the embedded systems module in third year...so as you I want to walk before running. I have asked someone else about it as well and they said he should have been more willing, and they will speak to him.
There is an agreement between the college and this particular university for a degree link 2+2 so therefore the two are supposed to work together to make the transition as easy as possible for students. Therefore I'm trying to find out what can be done, so that hopefully it won't only just benefit us this year but other students in the future. I also help out at the college in some classes, so I know who to speak to at the college or can find out. I know they are in the process of trying to update the microcontroller programming unit that is done at college currently.
 

shteii01

Joined Feb 19, 2010
4,644
Wow thank you guys yet again for all of your help. You've all helped me to understand things a bit more, I did read about the bootloader process I wasn't sure exactly what was required for it, so thank you for that.
Mr Chips I wasn't quite sure what the difference is between ASM and C?
ASM (assembly) is not interpreted language.
C is interpreted language.

What does this mean?
In ASM you tell what you want done and how to do it. There is no ambiguity, compiler does not interpret your code (note the use of word interpret), it simply translates it into machine code.

In C, you tell compiler what you want done, then compiler interprets how to do it. Note the difference? You, the designer, have no control over how your code will be translated/interpreted into machine code.

In pc world this is not very important because we have gigabytes of storage and memory. In uC world this can be very important because the storage is limited, often very limited. And the bigger the storage on uC chip, the more expansive it is. To you, a student, this may not matter. Imagine you are Samsung executive and you are going to sign to produce millions of phones or remote controls or some other gadget that will have uC inside. By using cheaper (smaller storage) uC you will save millions or even billions of dollars over the years and insure nice bonus for yourself.
 

MrChips

Joined Oct 2, 2009
30,467
ASM (assembly) is not interpreted language.
C is interpreted language.

What does this mean?
In ASM you tell what you want done and how to do it. There is no ambiguity, compiler does not interpret your code (note the use of word interpret), it simply translates it into machine code.

In C, you tell compiler what you want done, then compiler interprets how to do it. Note the difference? You, the designer, have no control over how your code will be translated/interpreted into machine code.

In pc world this is not very important because we have gigabytes of storage and memory. In uC world this can be very important because the storage is limited, often very limited. And the bigger the storage on uC chip, the more expansive it is. To you, a student, this may not matter. Imagine you are Samsung executive and you are going to sign to produce millions of phones or remote controls or some other gadget that will have uC inside. By using cheaper (smaller storage) uC you will save millions or even billions of dollars over the years and insure nice bonus for yourself.
That is not right. There is a difference between an interpreter and a compiler.

Firstly, let me expand for the benefit of the TS, the difference between ASM and C.
Don't get hung up on which programming language to learn. There is no problem with learning multiple languages. The more you learn the easier the next one becomes.

If I can use a crude analogy, the difference between ASM and C is like the difference between doing long division by hand or with a calculator.

The native language of the microcontroller unit (MCU) is machine code, i.e. binary zeros and ones. ASM is the language a programmer uses which will be assembled into machine code. By learning ASM you learn intimately how the MCU works.

C is an example of a higher level language (HLL). There are many HLL besides C, such as Pascal, BASIC, Java, Pearl, etc. All the nitty gritty of how the MCU works is hidden from you, just like how in using a calculator the baby steps are already done for you.

HLL can be interpreted or complied. We'll leave that distinction for another time.

The point is a HLL takes you to a higher plane of abstraction. Here you focus more on program algorithms and data structures. You are then able to tackle much more complex tasks and can develop software solutions much more efficiently than in ASM.

The point is, learn the basics first before moving to the next step. If you start off with C there is less of an incentive and motivation to go back to the starting point.
 

shteii01

Joined Feb 19, 2010
4,644
That is not right. There is a difference between an interpreter and a compiler.

Firstly, let me expand for the benefit of the TS, the difference between ASM and C.
Don't get hung up on which programming language to learn. There is no problem with learning multiple languages. The more you learn the easier the next one becomes.

If I can use a crude analogy, the difference between ASM and C is like the difference between doing long division by hand or with a calculator.

The native language of the microcontroller unit (MCU) is machine code, i.e. binary zeros and ones. ASM is the language a programmer uses which will be assembled into machine code. By learning ASM you learn intimately how the MCU works.

C is an example of a higher level language (HLL). There are many HLL besides C, such as Pascal, BASIC, Java, Pearl, etc. All the nitty gritty of how the MCU works is hidden from you, just like how in using a calculator the baby steps are already done for you.

HLL can be interpreted or complied. We'll leave that distinction for another time.

The point is a HLL takes you to a higher plane of abstraction. Here you focus more on program algorithms and data structures. You are then able to tackle much more complex tasks and can develop software solutions much more efficiently than in ASM.

The point is, learn the basics first before moving to the next step. If you start off with C there is less of an incentive and motivation to go back to the starting point.
Fortunately I am not talking about interpreter.
 

MrChips

Joined Oct 2, 2009
30,467
ASM (assembly) is not interpreted language.
C is interpreted language.
I am referring to your statements on interpreted language. C is usually compiled into machine code. It is not interpreted.
There is a huge difference between an interpreted language and a complied language.

A compiler takes program text and converts it into machine code (binary zeros and ones).
We can make a distinction between an assembler and a compiler as follows.
An assembler converts ASM text code into machine code.
A compiler takes HLL text such as a C program and converts it into ASM text. The assembler takes over and creates machine code.
Hence in general, C program text is compiled to machine code which is then executed.

An interpreter does things differently. An interpreter examines the program code and figures out what needs to be done and does it by invoking a huge library of built-in functions. Generally, an interpreter does this parsing on the fly, one line at a time. Hence it is possible to edit a single line on the fly and code execution can continue on from where it left off. Some interpreters have an intermediate stage where the program text is translated into an intermediate language call p-code. With this scheme you can design the language such that the program text is capable of running on any machine using a virtual machine, i.e. the interpreted language is designed for portability. In general, interpreted languages take longer to execute than code that is compiled into the native machine code.
 

Thread Starter

Spark1985

Joined Dec 1, 2016
7
Thank you very much to everyone yet again.
I have been led to believe that I do not need to teach myself assembly language as this will be taught to us at university, it's just programming microcontrollers using C that I need to be able to do.
I understand where you are coming from, regarding understanding how the MCU works. However I'm not sure I have the time, or confidence to teach myself machine code :/.
I have done a small amount of programming in school but, that was over 15years ago and at a very basic level.
I would even know where to start with assembly language.
I have another sort of related question, regarding the output voltage from an arduino, if I wanted to use it as a control circuit to control a power circuit, what would be the best way to go about this?
Could I use it to power a contactor or would it need to go via a relay or transformer?
Essentially I'm looking to design a timer type control circuit with motion detection as well, which would switch on and off a 230V power circuit. Possibly a couple of switches as well.
I noticed on one of the tutorials it mentioned the output being 5V I wondered if that was the maximum output voltage?
 

shteii01

Joined Feb 19, 2010
4,644
I have another sort of related question, regarding the output voltage from an arduino, if I wanted to use it as a control circuit to control a power circuit, what would be the best way to go about this?
Could I use it to power a contactor or would it need to go via a relay or transformer?
Essentially I'm looking to design a timer type control circuit with motion detection as well, which would switch on and off a 230V power circuit. Possibly a couple of switches as well.
I noticed on one of the tutorials it mentioned the output being 5V I wondered if that was the maximum output voltage?
Arduino uses two voltage systems. What I call regular system, 5 volts. And low voltage system, 3.3 volts. What it means is that output of any pin is limited to those voltages.

Example, Uno is 5 volt system. The digital pins of Uno will output either 0 volts (digital 0) or 5 volts (digital 1). ADC on Uno will read voltage from 0 to 5 volts and convert them into digital values. DAC on Uno will output voltages from 0 to 5 volts based on the digital value that is sent to the DAC.

Example, Due is 3.3 volt system. The digital pins will output either 0 or 3.3 volts. ADC will read/digitize signals from 0 to 3.3 volts. DAC will output voltages from 0 to 3.3 volts (and when I was using Due it had a design defect, the DAC output was from 0.5 to 2.7 volts, I am not sure if they have fixed it since 2012)

If you use transistor for switching something on and off, it is commonly recommended to use logic level mosfet (Metal Oxide Silicon Field Effect Transistor) transistor. Logic level means that you don't need to figure out how to interface uC to mosfet. Uno uses 5 volt logic, Due uses 3.3 volt logic. Pick logic level mosfet that fits the logic of your uC, and you can connect the logic level mosfet directly to the digital I/O pin of the uC. When pin is Digital 0 (0 volts), the logic level mosfet is Off. When pin is Digital 1 (5 volts for Uno), the logic level mosfet that uses 5 volt logic will be On.
 

MrChips

Joined Oct 2, 2009
30,467
I have another sort of related question, regarding the output voltage from an arduino, if I wanted to use it as a control circuit to control a power circuit, what would be the best way to go about this?
Could I use it to power a contactor or would it need to go via a relay or transformer?
Essentially I'm looking to design a timer type control circuit with motion detection as well, which would switch on and off a 230V power circuit. Possibly a couple of switches as well.
I noticed on one of the tutorials it mentioned the output being 5V I wondered if that was the maximum output voltage?
You must recognize and acknowledge that you are just starting out on this journey. A journey of 1000 miles begins with the first step. There is so much to learn along the way that you cannot learn it all at once. Then you never stop learning.

The output of a controller might be limited to 5V. You can use this to control another voltage that can be any voltage, -1000V, +1000V, 10kV, AC or DC.
There are various ways of doing this. You can use a mechanical relay, a solid-state relay, a transistor or other semiconductor devices. A mechanical relay can provide galvanic isolation. So can a solid-state relay if it is designed using optical isolators.
 

djsfantasi

Joined Apr 11, 2010
9,151
Depends on the particular Arduino model you use, but the maximum voltage can't exceed the supply voltage for the board. Typically 5v or 3.3v. If you need a higher voltage, you need a driver. Maybe just a transistor circuit.

In addition, there's a limit on the current that the Arduino can have. Again, the Uno is limited to 40ma on each pin and 200ma total. Check the datasheet.
 

nsaspook

Joined Aug 27, 2009
12,778
Thank you very much to everyone yet again.
I have been led to believe that I do not need to teach myself assembly language as this will be taught to us at university, it's just programming microcontrollers using C that I need to be able to do.
I understand where you are coming from, regarding understanding how the MCU works. However I'm not sure I have the time, or confidence to teach myself machine code :/.
I have done a small amount of programming in school but, that was over 15years ago and at a very basic level.
I would even know where to start with assembly language.
Learning at least one simple machine language to the point where you can design/code basic I/O and memory operation functions should be a prerequisite for embedded C language programming (we built our own little processor using TTL in college). I don't recommend this because you need to be a ASM programmer, it's because you need to be able to think instinctively in abstract machine operations when using low-level C so you need a solid foundation to build those abstractions of data and state sequence flows. You can peek and poke until it works most of the time or you can analyze the problem down to structured software to hardware interfaces that are easy to program and follow 5 years later from the C source code.

This is my best advice once you become a good embedded programmer:
1. Read the datasheet.
2. Read the datasheet.
3. Don’t trust the datasheet.
 
Top