You are on page 1of 26

Chapter 9

Programming Microcontrollers
in C
9.1 Introduction

Comments
For a single line of comments use double back slashes as in
// Blinky.c
For multiline comments, begin them with /* and end them with */ as
in:
/* Blinky.c is a really great first program for microcontrollers it
causes eight LEDs to scan back and forth like a Cylon’s eyes */
Include Files:
#include <avr/delay.h>
#include <avr/io.h>
The ‘#include’ is a preprocessor directive that instructs the compiler
to find the file in the <> brackets and tack it on at the head of the file
you are about to compile.

Microcomputer principles and applications


9.1 Introduction

Statements:
Statements control the program flow and consist of keywords,
expressions, and other statements. A semicolon ends a statement.
For example:
TempInCelsius = 5 * (TempInFahrenheit-32)/9;
Blocks:
Blocks are compound statements grouped by open and close
braces { }. For example:

f o r ( i n t i = 1 ; i <= 128; i = i ∗ 2 )
{
PORTD = ~ i ;
Delayloop ( 3 0 0 0 0 ) ;
}

Microcomputer principles and applications


9.1 Introduction

The Main() Thing :


All C programs must have a ‘main’ function that contains the code
that is first run when the program begins
i n t main ( )
{
/ / Do something
}

Microcomputer principles and applications


9.2 C Types, Operators, and Expressions

Data Types and Sizes

• char: The name of this data is short for character representing a


signed value from -127 to +127.
• int: An integer is usually the size of the basic unit of storage for
the machine. An int must be at least 16 bits wide.
• float: A single precision floating-point number.
• double: A double precision floating-point number.

Microcomputer principles and applications


9.2 C Types, Operators, and Expressions

Additional qualifiers

• short: modifies an int, and is a variable whose width is no


greater than that of the int. For example, with a compiler with a
32 bit int a short int could be 16 bits.
• long: modifies an int, and is a variable size whose width is no
less than that of an int. For example, on a 16-bit machine, an int
might be 16 bits, and a long int could be 32 bits.
• signed: modifies all integral numbers and produces a range of
numbers that contains both positive and negative numbers.
• unsigned: modifies all integral numbers and produces a range
of numbers that are positive only.

Microcomputer principles and applications


9.2 C Types, Operators, and Expressions

Arithmetic Operators
+, - , * , /, %
Logical Operators
< (less than)
<= (less than or equal to)
> (greater than)
>= (greater than or equal to)
== (is equal to)
!= (is not equal to)

Microcomputer principles and applications


9.2 C Types, Operators, and Expressions

Bitwise Operators
& bitwise AND
>> right shift
| bitwise Inclusive OR
<< left shift
∧ bitwise Exclusive OR
∼ one’s complement
Increment and Decrement Operators
++, - -
Assignment Operators

i = i +2;
i += 2 ;
x = x < <1;
x < <=1;
Microcomputer principles and applications
9.2 C Types, Operators, and Expressions

Bit manipulation:
Set a bit P1OUT = P1OUT | BIT3
Clear a bit P1OUT &= BIT3
Toggle a bit P1OUT ^= BIT3

Microcomputer principles and applications


9.3 Program Flow and Control

The Conditional Expression

i f ( expression )
statement1 ;
else
statement2 ;

Microcomputer principles and applications


9.3 Program Flow and Control

Microcomputer principles and applications


9.3 Program Flow and Control

If statement example
void main ( ) {

i n t a = 100;

/ ∗ check th e boolean c o n d i t i o n ∗ /
i f ( a < 20 ) {
/ ∗ i f c o n d i t i o n i s t r u e then do th e f o l l o w i n g ∗ /
...
}
else {
/ ∗ i f c o n d i t i o n i s f a l s e then do th e f o l l o w i n g ∗ /
...
}
}

Microcomputer principles and applications


9.3 Program Flow and Control

The Conditional Expression

switch ( e x p r e s s i o n ) {
case c o n s ta n t −e x p r e s s i o n :
s ta te m e n t ( s ) ;
break ; / ∗ o p t i o n a l ∗ /
case c o n s ta n t −e x p r e s s i o n :
s ta te m e n t ( s ) ;
break ; / ∗ o p t i o n a l ∗ /
/ ∗ you can have any number o f case s ta te m e n ts ∗ /
default : /∗ Optional ∗/
s ta te m e n t ( s ) ;
}
Note: The constant-expression for a case must be the same data
type as the variable in the switch, and it must be a constant or a
literal.
Microcomputer principles and applications
9.3 Program Flow and Control

Microcomputer principles and applications


9.3 Program Flow and Control

Break statement
When a break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the
next statement following the loop.

Microcomputer principles and applications


9.3 Program Flow and Control

Microcomputer principles and applications


9.3 Program Flow and Control

Switch statement example

void main ( ) {
char grade = ’B ’ ;
switch ( grade ) {
case ’A ’ :
...
break ;
case ’B ’ :
case ’C ’ :
...
break ;
default :
...
}
}

Microcomputer principles and applications


9.3 Program Flow and Control

The While Statement


while ( c o n d i t i o n ) {
s ta te m e n t ( s ) ;
}

Microcomputer principles and applications


9.3 Program Flow and Control

Microcomputer principles and applications


9.3 Program Flow and Control

While loop example


void main ( ) {
i n t guess , i ;
i =1;
guess = 5 ;

while ( guess ! = i ) {
i = guess ;
guess = ( i + (1 0 0 0 0 / i ) ) / 2 ;
}

Microcomputer principles and applications


9.3 Program Flow and Control

The For loop


f o r ( i n i t ; c o n d i t i o n ; in c re m e n t ) {
s ta te m e n t ( s ) ;
}

Microcomputer principles and applications


9.3 Program Flow and Control

Microcomputer principles and applications


9.3 Program Flow and Control

For loop example


void main ( ) {

int a ;

/ ∗ f o r lo o p e x e c u t i o n ∗ /
f o r ( a = 1 0 ; a < 2 0 ; a++ ) {
...
}

Microcomputer principles and applications


9.3 Program Flow and Control

The Do While Statement


do {
s ta te m e n t ( s ) ;
} while ( c o n d i t i o n ) ;

Microcomputer principles and applications


9.3 Program Flow and Control

Microcomputer principles and applications


9.3 Program Flow and Control

For loop example


void main ( ) {

/∗ local variable d e fi n it io n ∗/
i n t a = 10;

/ ∗ do lo o p e x e c u t i o n ∗ /
do {
p r i n t f ( " v a lu e o f a : %d \ n " , a ) ;
a = a + 1;
} while ( a < 20 ) ;

Microcomputer principles and applications

You might also like