C-PR Ogra Ming For Micr Ocon Troll Ers
C-PR Ogra Ming For Micr Ocon Troll Ers
o
f
g
n
i
m
s
a
r
r
e
g
l
l
o
o
r
r
P
t
n
Co
c
o
r
c
i
M
Introduction
C - Language is very efficient language to use especially
with the hardware. It is similar to the languages we used to
write before with little bit differences. And it has also
1)
2)
3)
4)
5)
6)
Loops.
Conditional Statements.
Switch Cases.
Variables and Data types.
Objects.
Arrays.
Outline
1)
Software installation
2)
3)
4)
5)
Arrays
6)
Conditional statements
7)
8)
9)
Methods
10)
Operators
11)
Examples
This software is called MikroC for PIC which you can download from
the following link
http://www.mikroe.com/eng/products/view/228/mikroc-pro-for-avr/
import java.io.*;
In the previous java courses we used this line to import
some predefined classes in java to be used in the active
class we are working through
But, How we could do the same in C- Language?
#include <h_file> ;
It could be done using the #include statement to include one
of the header files (h_files) that have all the microcontrollers
predefined functions and register names
(2) cont.
#include <file.h>
y
r
a
r
b
C- Li
Header File
Predefined elements
are ready for the
class
C- Class
(2) cont.
Also You Can FIND in C- Language
#define X Y ;
It is used to define variable called X to have the same value
or behavior as variable Y and this can be realized in
Example (2.1)
#include <pic16f877a.h>
(4) cont.
(4) cont.
Variables and Data Types
(4) cont.
Integer
int is used to define integer numbers
Int
count = 5;
float
FLOAT is used to define floating point numbers
float
count = 5.6;
char
char is used to define characters
char
letter = x;
(5) Arrays
Arrays is the group of elements saved in one group and each element has an
index of its location that contains its value
int [6] x ;
/* Creates empty array of length 6 cells */
int [6] x = {12,45,3,7} ;
/* Creates array of length 6 cells shown in
this page where the cells values are the
values between the
12
0
0
3
0
7
0
0
(5) cont.
12
20
10
to
make
branches
in
the
(6) cont.
switch
If-else
int month= 6 ;
int num = 6 ;
switch (month)
{
if ( num < 0)
case 1: printf(JAN);break; {
case 2: printf(FEB);break;
printf(negative);
. . .
}
case 12: printf(DEC);break; else
default: printf(error);
{
break;
printf(positive);
}
}
Abscence of break will prevent the instruction to be printed and it will print the
statement that has the first break.
More than one statement inside the if part or else part will need curly braces for
the instructions
Initialization
2)
Condition
3)
(7) cont.
while
int x = 5;
while ( x > 0 )
{
printf(positive);
}
for Do-loop
operation
iteration
evaluates
of
the
process
that
condition
before
looping
int num = 6 ;
{
printf(positive);
}
do ( x < 0 )
for (int x = 5 ; x < 0 ; x --)
{
printf(positive);
}
Normal
condition
The
simplest
form
of
the
PORTX is used to define the data of port X and it is the register carrying the data
of this port
PORTXbits.RX4 is used to define the data of port X bit number 4 for example
(9) Methods
Sometimes you need to break your program to smaller parts in order to
have flexible and reliable program because the first step to solve any
problem is to break it down to solvable smaller problems
So we have the main method the organizer to the class that will invoke
any other method created inside the class
Take Care that : creating and calling methods in C is a little bit different
from JAVA
(9) Cont.
#include <16f877a.h> ;
int add (int x , int y);
// the definition of the method should be written here
Void main ( void )
{
int a = 5;
int b = 7 ;
printf(the result is = + add(a , b));
}
// method to be invoked by the main method in C- class
int add (int s , int r )
{
int z = s+r;
return z ;
}
(10) Operators
Assume we have to operands a and b
Logical operators
Comparison operators
Bitwise operators
(10) Cont.
Assume we have to operands a and b
Compound operators
Arithmetic operators
for ( ; ; )
// repeat forever
{
PORTB = 0x55;
PORTB = 0xAA;
}
}
like while(ture)
u
o
Y
k
n
a
h
T