Microprocessor and
Interfacing
Ch4_lec1_ Basic C
programming Structure
Kassahun Tamir 1
Outline
C Programming IDEs
Micro-controller header files
Compiler data types
Different types of variables
Arithmetic operations
Bit parallel logical operations
Shift and relational operations
Structures
✗ If-then-else
✗ Switch
✗ While and do while
✗For
Functions, Arrays and Pointers
Kassahun Tamir 2
C Programming IDEs
We interconnect the Hardware/Electronics parts using hardware
simulators such as, Proteus
Then we need to write an Embedded C or Assembly code, and
then upload the .hex file into the micro-controller
To simulate the Embedded C program: MPLAB or MikroC is
recommended
Kassahun Tamir 3
Micro-controller header files
An embedded program in its simplest form:
Header Files,
Macros, and
Function and
Global Variable Declaration
Configuring Registers
Local Variables Declaration
Loop Forever
Kassahun Tamir 4
Different Types of Variables
A variable is declared by the reserved word indicating its type
and size followed by an identifier.
Kassahun Tamir 5
Arithmetic Operations
Binary Arithmetic Operators:
+ - * / %
Unary Operators:
++ --
Kassahun Tamir 6
Bit Parallel Logical Operations
Logical Operators:
&& AND
|| OR
! NOT
Bit-wise Operators:
& Bit-wise AND
| Bit-wise OR
^ Exclusive OR (XOR)
~ Complement
>> Shift to the Right
<< Shift to the Left
Kassahun Tamir 7
Shift and Relational Operations
Shift Operators:
>> Shift Right
<< Shift Left
Relational Operators:
==
!=
<
>
<=
>=
Kassahun Tamir 8
Control Structures: If-then-else
The “if” selection statement:
The “if … else” selection statement:
“If … else’s ” equivalent(using Conditional Operator)
Kassahun Tamir 9
Control Structures: If-then-else
Nested “if…else” statements:
Kassahun Tamir 10
Control Structures: switch case
Kassahun Tamir 11
Functions
Why Functions?
“Divide and conquer”
Reusability
Avoid repeating code in a program
Kassahun Tamir 12
Functions
Function Definition:
Function Definition
Example:
Function Call:
Kassahun Tamir 13
Arrays: One Dimensional Arrays
Defining Arrays:
Size of arrays are given during declaration either
explicitly or implicitly
Arrays can be initialized:
✗
During declaration
✗
Using “loop”
Kassahun Tamir 14
Arrays: One Dimensional Arrays
Examples:
Kassahun Tamir 15
Arrays: One Dimensional Arrays
Using character arrays to store and manipulate
Strings
Assign String values to character arrays
Explicitly give characters using braces { }, put every
character in single quotes ‘ ’ and separate them using
comma (,)
Kassahun Tamir 16
Arrays: One Dimensional Arrays
Passing Arrays to Functions:
They are passed by
reference implicitly
Kassahun Tamir 17
Arrays: Multidimensional Arrays
Kassahun Tamir 18
Pointers
A variable that holds a memory address
This address is the location of another address in the
memory
Pointers as an address indicates where to find an
object: pointers contain memory addresses as their
values
Kassahun Tamir 19
&, * and Declaring variables of pointer type
& is used to reference the memory address of a
variable
When we declare a variable, 3 things happened
✗ Computer memory is set aside for variable
✗ Variable name is linked to that location in memory
✗ Value of variable is placed into memory that was set
aside
Kassahun Tamir 20
&, * and Declaring variables of pointer type
Data-type *name;
* is a unary operator, also called as indirection
operator
Data-type is the type of object which the pointer is
pointing
Any type of pointer can point to any where in the
memory
* is used to declare a pointer and also to dereference
a pointer Kassahun Tamir 21
&, * and Declaring variables of pointer type
int *m;
✗ Declaring variable m which holds the value at address of int
type
✗ compiler assumes that any address that it holds points to an
integer type
int count = 10;
✗ Assigning the literal value of 10
m = &count;
✗ It means memory address of count variable is stored into m
✗ So it means m receives the address of count
& is unary operator that returns them memory address
int deref = *m
✗ Printf(“%d\n”,deref);
Kassahun Tamir 22
✗ Output will be 10
Pointer Initialization
You need to set proper values (address) to point like
you set values for variables, just before you can use
them
Initialize them at the time of declaration with of a
known variable:
int *ptr =&count
If not initialized, it can later be assigned an address of
any variable of the same type, before using it:
int *ptr;
ptr = &count
Kassahun Tamir 23
Pointers and Arrays
When we pass the entire array as an argument to
functions, functions get complete access to the
original array
✗ Means that it directly affects the original value
When we pass array element by element, functions
get a copy of the original value
✗ Means that it does not affect the original value
Kassahun Tamir 24
Pointer to Constants
const int *ptr = &count // int const* ptr; also possible
✗ (*ptr) = 10; //error
✗ (*ptr) ++; //error
✗ in j = (*ptr)+56; //OK as we are not modifying count
Kassahun Tamir 25
Pointers to Functions
Pointers can be used as a function argument
void myFunc(int *ptr);
In the function body, use ptr, as if it has been properly
defined and initialized
To call the aforementioned function:
myFunc(&count);
We may also pass pointer to int(not address of pointer
to int)
Int count=20, *m;
m = &count;
myFunc(m)
Kassahun Tamir 26
Example 1
Kassahun Tamir 27
Example 2
Kassahun Tamir 28
Questions?
Kassahun Tamir 29