4.1 C Programming For Embedded
4.1 C Programming For Embedded
microcontroller systems.
ABBOTT.o COSTELLO.o
… … Idunno, whosonfirst
MOVE R1, (idunno) …
CALL whosonfirst whosonfirst:
… …
Unknown addresses
Linker
Linker
HAHA.exe
…
MOVE R1, 2388 ➢ Functions are merged
CALL 1547
… ➢ Relative addresses used
1547 MOVE R5, R1
…
2388 (value of idunno)
Linker/Locator
➢ Cross-Compiler: avr-gcc
➢ Linker/Locator: avr-ld
➢ Cross-Assembler: avr-as
➢ Programmer: avrdude
+ error-prone
+ difficult to understand
+ difficult to modify
Similarities of C to java
/* Comments */
Variable declarations
if / else statements
for loops
while loops
function definitions (like methods)
Main function starts program
Differences between C and java
C does not have objects
There are “struct”ures
C is a functional programming language
C allows pointer manipulation
Input / Output with C
Output with printf function
Input with scanf function
C Fundamentals
First program
#include <stdio.h>
main()
{
printf(“To C, or not to C: that is the question”);
}
C Fundamentals
Compiling and Linking
Preprocessing: the program is given to a preprocessor,
which obeys commands that begin with #(directives)
add things to the program and make modifications
% cc –o pun pun.c
% gcc –Wall –o pun pun.c
C Fundamentals
Keywords
main() {
// initialization code
while (1) {
// main code
}
}
void main( )
{
printf(“Hello World!..”);
}
Here the things given inside /* */ are called
comments which will be ignored by the compiler
during compilation time
When we write the pre-processor directive
#include<stdio.h> our pre-processor will also
include the header file stdio.h which contains the
defenitions for standard input output functions.
C Syntax Basics
➢Comments: single line //, multiline /* … */
➢Semicolon used to terminate all statements
➢# indicates a compiler directive (include, define, etc.)
➢Brackets { … } group lines of code for execution
➢Blankspace between lines is not important in C
•Still need to separate tokens
int a, b, c;
int a,b,c;a=b+c; inta,b,c;a=b+c;
a = b + c;
long int x;
scanf(“%ld”, &x);
printf(“%ld”, x);
Floating Types
float single-precision floating-point
double double-precision floating-point
long double extended-precision floating-point
long int
double
unsigned int
float int
Type Conversion
char c;
short int s;
int i;
unsigned int u;
long int l;
unsigned long int ul;
float f;
double d;
long double ld;
i = i + c; /* c is converted to int */
i = i + s; /* s is converted to int */
u = u +i; /* i is converted to unsigned int */
l = l + u; /* u is converted to long int */
ul =ul + l; /* l is converted to unsigned long int */
f = f + ul; /* ul is converted to float */
d = d + f; /* f is converted to double */
ld = ld + d; /* d is converted to long double */
Casting
( type-name ) expression
float f, frac_part;
frac_part = f – (int) f;
float quotient;
int dividend, divisor;
quotient = (float) dividend / divisor;
short int i;
int j = 1000;
i = j * j; /* WRONG */
Basic Operators in C:
•Arithmetic Operators A=10, B=20
Operat
Description Example
or
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by denumerator B / A will give 2
% Modulus Operator and remainder of B % A will give 0
after an integer division
++ Increment operator, increases integer A++ will give 11
value by one
-- Decrement operator, decreases integer A-- will give 9
value by one
•Logical / Relational Operators:
Operat
Description Example
or
== Checks if the value of two operands is equal or (A == B) is not true.
not, if yes then condition becomes true.
!= Checks if the value of two operands is equal or (A != B) is true.
not, if values are not equal then condition
becomes true.
> Checks if the value of left operand is greater than (A > B) is not true.
the value of right operand, if yes then condition
becomes true.
< Checks if the value of left operand is less than the (A < B) is true.
value of right operand, if yes then condition
becomes true.
>= Checks if the value of left operand is greater than (A >= B) is not true.
or equal to the value of right operand, if yes then
condition becomes true.
Logical / Relational Operators:
<= Checks if the value of left operand is less than or equal to the value of (A <= B) is true.
right operand, if yes then condition becomes true.
&&
Called Logical AND operator. If both the (A && B) is true.
operands are non-zero then then condition
becomes true.
||
Called Logical OR Operator. If any of the two (A || B) is true.
operands is non-zero then then condition
becomes true.
!
Called Logical NOT Operator. Use to reverses !(A && B) is false.
the logical state of its operand. If a condition
is true then Logical NOT operator will make
false.
• Logical Operators
• A logical operator operated between two relational or logical
expressions and returns a logical value.
•
• && - Logical AND
• || - Logical OR
•! - Logical Negation / NOT
Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100 – Bitwise ANDing
A|B = 0011 1101 – Bitwise ORing
A^B = 0011 0001 – Bitwise XORing
~A = 1100 0011 – Bitwise Negation operator
<< - Left shift operator
>> - Right Shift Operator
NB: Please make yourself very clear about bitwise operators as we use them
much frequently while programming the microcontrollers.
sizeof( ) – operator is used to get the size (in bytes) which a variable or a data
type takes in memory.
Type Definitions
typedef int BOOL
BOOL flag; /* same as int flag; */
i = 1; … i = 1;
➢Base 10 is default
➢Base can be specified with a prefix before the number
➢Binary is 0b, Hexadecimal is 0x
Ex. char x = 0b00110011;
char x = 0x33;
➢Binary is useful to show each bit value
➢Hex is compact and easy to convert to binary
➢1 hex digit = 4 binary digits
Types of Statements
➢Assignment – =
➢Relational - <, >, ==, !=
➢Control Flow – if, for, while, do, switch
➢Arithmetic Operations - +, -, *, /, %
➢Logical Operations - &&, ||, !
➢Bitwise Logical Operations - &, |, ^
Logical Operators
8/30/2021
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) => false
if( i != j || k < j) => true
if( j<= k || i > k) => true
if( j == k && m) => false
if(i) => true
if(m || j && i ) => true
8/30/2021
Control Structures:
Sequential Control Structure
Eg: Usual programs in which we execute statements
one after the other.
For Loop
for(initialisation; condition ; updation)
{
--Loop Body--
}
Eg:
for(int i = i; i<=10 ; i++)
{ /*Printing Multiplication Table of 5 */
printf(“%d x 5 = %d”, i, i*5);
}
FOR loop structure
FOR loop structure
FOR structure example
FOR structure example
C functions
Functions
Functions are one of the most commonly used features in the
C Programming language. It helps users to separate blocks
of codes which could be reused again and again.
A program using functions may look something like this:
#include<stdio.h>
int myFunction(int , int);
void main( )
{
---------
---------
}
What is a pointer?
A pointer is a variable which contains the address in memory of another variable.
8/30/2021
Declaring a pointer variable
int * pointer;
char * name;
How to obtain the address of a variable?
int x = 0x2233;
pointer = &x;
where & is called address of operator.
X pointer
0x5200
33 22 00 00
0x5200 0x5203
8/30/2021
swap the value of two variables
8/30/2021
Why is the left one not working?
swap x, y
x, y, a, b are
all local
variables
main a, b
8/30/2021
Why is the right one working?
8/30/2021
Pointers and Arrays
Pointers and arrays are very closely linked in C.
Array elements arranged in consecutive memory
locations
8/30/2021
Books recommended
The C Programming Language, Brian Kernighan and Dennis Ritchie.
Second edition. Prentice-Hall, 1988. (C Bible)
The C++ Programming Language, Bjarne Stroustrup. Third edition.
Addison-Wesley, 1997. (C++ Bible)
Advanced Programming in the UNIX Environment, W. Richard Stevens,
Addison-Wesley, 1992. (APUE)
8/30/2021
Some on-line C tutorials
http://www.cprogramming.com/tutorial/c-tutorial.html
http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/
http://www.iu.hio.no/~mark/CTutorial/CTutorial.html
http://www2.its.strath.ac.uk/courses/c/