Programming Microcontrollers in C PDF
Programming Microcontrollers in C PDF
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.
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 ) ;
}
Additional qualifiers
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)
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
i f ( expression )
statement1 ;
else
statement2 ;
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 ∗ /
...
}
}
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
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.
void main ( ) {
char grade = ’B ’ ;
switch ( grade ) {
case ’A ’ :
...
break ;
case ’B ’ :
case ’C ’ :
...
break ;
default :
...
}
}
while ( guess ! = i ) {
i = guess ;
guess = ( i + (1 0 0 0 0 / i ) ) / 2 ;
}
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++ ) {
...
}
/∗ 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 ) ;