Chapter 03 – A SMALL PROGRAMMING PRIMER

THE MICROCONTROLLER PROGRAMMING IN BRIEF

The microcontroller is programmed using the C language. A program, for the people that are not used to programming, is a list of instructions that is executed in the right order to get the expected result. To make it really simple, the preparation of a recipe in your kitchen is a program.

This chapter can be useful for all the people that want to get some basic knowledge on programming and need some guidance with the microcontroller IDE. If you find the concepts a bit not easy to understand, you don’t have to worry, you can start going through the different experiments and use the example codes that are provided. In addition, the website of arduino – Arduino.cc – is a quite complete resource, or if you need more details on how the C Language is defined you can buy one of the first books written on the subject, if not the first one, “The C Programming Language” (the authors are Brian Kernighan and Dennis Ritchie, can be found in different languages).

STRUCTURE

Each program (is normally called a sketch) has two required functions (called also routines).

void setup() { }

Contains all the code, between two curly brackets, that will be run once when your Arduino program is started.

void loop() { }

This function is run after the setup function has finished. After it has run once it will be executed again, and again like a loop, until the power is removed from the microcontroller.

SYNTAX

One of the most important elements of the C is its formatting requirements (this makes it very powerful, but for beginners is not easy to use). If you remember the following you should be alright.

// (single line comment)

Is useful to write notes to yourself as you need to put a remark to a line of code. Type two forward slashes and everything until the end of the line will be ignored by the program.

/*    */ (multi line comment)

Normally is used when one line of comment is not enough, for example to describe in general terms how your program is working. Everything between these two symbols will be ignored in the program. Should be reminded that /* cannot have another /* but should be terminated first with the /* symbol.

{ } (curly brackets)

Is used to define when a block of code starts and ends (is normally used in functions and in loops).

; (semicolon)

Each line of code must be ended with a semicolon (a missing semicolon is a quite common reason for a program not compiled).

VARIABLES

A program is nothing more than a set of instructions to move data around the RAM of the microcontroller. Variables are used to store one or more information. Below are listed all the different type of data that can be used to program the microcontroller. The most important information that should be kept in mind when writing programs microcontrollers, is that normally the memory / space available is very limited, this means that should always used the smallest type variable (in bits).

int (integer)

Is the most important and used type of variable, it stores numbers using 2 bytes (16 bits). Has no decimal places and store a values between -32,768 and 32,767.

long (long)

When an integer is not big enough, the long is the next option possible. It takes 4 bytes (32 bits) of RAM and has a range between -2,147,483,648 and 2,147,483,647.

boolean (boolean)

Is a simple variable that contain a True or False variable. It’s useful because it only require one bit of RAM.

float (float)

Used for floating point math (with decimals). It takes 4 bytes (32 bits) of RAM and has a range between -3.4028235E+38 and 3.4028235E+38.

char (character)

Stores one character using the ASCII code (ie ‘A’ = 65). Uses one byte (8 bits) of RAM. The language handles strings as an array of char’s.

String (contains several characters)

It stores several characters and is easy to use for beginners. The main disadvantage is that requires a lot of memory and in some versions of the programming IDE can break the execution of your program.

MATH OPERATORS

Operators are used to manipulate numbers. (they work like the simple math that you study at school).

=    (assignment) makes something equal to something else (eg. x = 10 * 2 (x now equals 20)).

% (modulo) gives the remainder when one number is divided by another (ex. 12 % 10 (gives as result 2))

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

++ (increment) The variable is incremented of one unit. (ex. if i has value 2 the code i++; after the semicolon i will have value 3)

– (decrement)  The variable is decremented of one unit. (ex. if i has value 2 the code i–; after the semicolon i will have value 1)

COMPARISON OPERATORS

Operators used for logical comparison between variables.

==    (equal to) (eg. 12 == 10 is FALSE or 12 == 12 is TRUE)

!=    (not equal to) (eg. 12 != 10 is TRUE or 12 != 12 is FALSE)

<    (less than) (eg. 12 < 10 is FALSE or 12 < 12 is FALSE or 12 < 14 is TRUE)

>    (greater than) (eg. 12 > 10 is TRUE or 12 > 12 is FALSE or 12 > 14 is FALSE)

CONTROL STRUCTURE

Programs are based on controlling structures that define what runs next, some basic control control elements (there are many more online) are below. To reduce the space used in this chapter we wrote the control structures in one line, remind that the C language allows it.The format in one line as you can imagine is not the most readable, in the examples used with the experiments you will notice a more readable program.

if(condition){ } else if( condition ){ } else { }

Execute the code between the curly brackets if the condition is true, if not true it will test the else if condition if that is also false the else code will execute. Is possible to use several else if, but is important to remind that some time if the conditions are not well written the code can work improperly.

for(int i = 0; i < number_of_repetitions; i++){    }

If you would like to repeat a part of code a defined number of times you should use the for control structure (can count up i++ or down i– or use any variable)

INPUT AND OUTPUT

The microcontroller, like a small computer, allows to interact with external resources, this allows our programs to read or execute some physical action outside the microcontroller. The type of interaction can be DIGITAL or ANALOG. Digital means that some data like 01010010 are sent over one line, analog is when an audio is played like in the old style tapes.  Some examples are: to switch on or off a light, to read a temperature, play a melody or to send some data over a network.

DIGITAL

pinMode(pin, mode);

Is used to set the functionality of a pin, pin is the pin number that you would like to address 0-19 (analog 0- 5 are 14-19). The mode can either be INPUT or OUTPUT.

digitalWrite(pin, value);

When a pin is set as an OUTPUT, it can be set either HIGH (pulled to +5 volts) or LOW (pulled to ground, GND).

int digitalRead(pin);

When the pin is set as an INPUT, the informationr read can beHIGH (pulled to +5 volts) or LOW (pulled to ground, GND).

ANALOG

The microcontroller is a digital machine but it has also the ability to operate in the analog realm (using some tricks).

pinMode(pin, mode);

Is used to set the functionality of a pin, pin is the pin number that you would like to address 0-19 (analog 0- 5 are 14-19). The mode can either be INPUT or OUTPUT.

int analogWrite(pin, value);

Some of the Arduino’s pins support a special functionality that is the pulse width modulation (3, 5, 6, 9, 10, 11). This turns the pin on and off very quickly making it act like an analog output. The value is any number between 0 (0% duty cycle ~0v) and 255 (100% duty cycle ~5 volts).

int analogRead(pin);
When the analog input pins are set, is possible to read their voltage. A value between 0 (for 0 volts) and 1024 (for 5 volts) is returned.

3 comments

  1. [...] 01 – INTRO Chapter 02 – ELECTRONICS IN BRIEF Chapter 03 – A SMALL PROGRAMMING PRIMER Chapter 04 – THE BREADBOARD Chapter 05 – HARDWARE INSTALLATION Chapter 06 – INSTALL THE [...]

  2. Very helpful beginners guide.

  3. Hello to every , because I am actually eager of reading this webpage’s post to be
    updated on a regular basis. It contains good material.

Leave a Reply to Ross M. Betchley Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


istanbul evden eve nakliyat istanbul evden eve nakliyat istanbul evden eve nakliyat sarıyer evden eve nakliyat beylikdüzü evden eve nakliyat şişli evden eve nakliyat