PROGRAMMING WITH PIC MICROCOTROLLERS
Prasad Manjula Wijesinghe
(BSc Eng, AMIE)
What is a Microcontroller
NUMBER SYATEMS
Decimal number system
Binary number system
Hexadecimal number system
NUMBER SYATEMS
Decimal Number System
NUMBER SYATEMS
Binary Number System
NUMBER SYATEMS
Hexadecimal Number System
DATA
BIT
Basic unit of information
Use only two digits 1 and 0
Some applications use as High and Low
DATA
BYTE
Group of Eight Bits
Largest decimal number in a Byte is 255
Largest Hexa value in a byte is FF
LOGIC GATES
AND GATE
LOGIC GATES
OR GATE
LOGIC GATES
NOT GATE
LOGIC GATES
XOR GATE
REGISTER
register or a memory cell is an electronic circuit which can memorize the state of one byte
REGISTER
SFR Register
Special Function Registers
Predetermined function by manufacturer.
Connected to internal circuits like timers, A/D converters, Oscillators..etc.
Eg :- TRIS , ANSEL
REGISTER
INPUT / OUTPUT PORTS
Registers connected to the microcontroller pins.
Analog and Digital inputs are available.
Digital outputs are 0 and 5 V with 20mA maximum current.
Eg :- PORTA, PORTB
OSCILLATOR
Oscillators are used to time calculation in Miro controller
Some microcontrollers have internal oscillator eg:- pic12f629
Most popular oscillator type is crystal oscillator.
Crystal oscillators have higher stability in frequency with temperature and other factors.
Programming Microcontrollers
Programming Microcontrollers
MPLAB X XC8
Programming with C Language
Data types in C Language
Data type Description Size (# of bits) Range of values
char Character 8 0 to 255
int Integer 16 -32768 to 32767
float Floating point 32 ±1.2 x 10^-38 to ±3.4x10^+38
Double precision floating
double 64 ±2.3 x 10^-308 to ±1.7x10^+308
point
Programming with C Language
Variables in C Language
Variables are acting as a vessel or container that can store in various data type.
Each variable has unique name address and data type.
The data inside a variable can be change by manually or by the programme.
Variable name can include characters A-Z (a-z), the digits 0-9 and the underscore character '_‘
Name should not start with a digit.
Name should not use compiler key words.
Programming with C Language
Mikro C Key words
Programming with C Language
Declaration and assignment of Variables
int number ;
number = 5;
int a,b,c,d;
int age = 0;
Double temp = 37.5;
Programming with C Language
Arithmetic operators
Programming with C Language
Assignment operators
Programming with C Language
Increment & Decrement operators
Programming with C Language
Relational operators
Programming with C Language
Conditional operators – if else
Programming with C Language
Conditional operators – switch case
Programming with C Language
Program Loop – While loop
while(expression){
commands
...
}
while(1){
... // Expressions enclosed within curly
brackets will be
... // endlessly executed (endless loop).
}
Programming with C Language
Program Loop – For loop
for(initial_expression; condition_expression;
change_expression) {
operations
...
}
Programming with C Language
Program Loop – Do while loop
do
operation
while (check_condition);
a = 0; // Set initial value
do
a = a+1 // Operation in progress
while (a <= 1E06); // Check condition
Programming with C Language
ARRAYS
A group of variables of the same type is called an array.
component_type array_name [number_of_components];
unsigned char calendar [12] = {31,28,31,30,31,30,31,31,30,31,30,31};