Professional Documents
Culture Documents
• laboratory: 30%
References
• Muhammad Ali Mazidi, Sarmad Naimi, Sepehr Naimi, “the AVR
microcontroller and embedded system: using assembly and C”
SYLLABUS OF MICROCONTROLLER COURSE
RAM ROM
Timer Interrupt
Introduction
Microprocessor vs. Microcontroller
Introduction
Microprocessor:
only processing of data
Faster
General purpose
Expensive
CPU is stand-alone, RAM, ROM, I/O, timer are
separate.
Designer can control the amount of ROM, RAM and
I/O ports.
It is cheaper compared to the microcontroller with
the same features.
Introduction
Microcontroller:
Slower.
Stand alone operation.
Cheaper.
CPU, RAM, ROM, I/O and timer are all on a
single chip(but fixed amount).
for applications in which cost, power and
space are critical.
Single Operation.
history
AVR family
AVR family First AVR family
CPU
Memories
Input/output
ports
Timer/counter
Parallel ports
Interrupt sources
PWM function
(A/D , D/A)
converters
microcontroller components
An auxiliary register
1. Register: is a quickly accessible location available to a
processor and is one of a small set of data holding places that
may hold an instruction, a storage address, or any kind of data
Accumulator register: the accumulator is a register in which
intermediate arithmetic and logic results are stored
ROM(ROM)
PROM(Programmable ROM).
EPROM (Erasable PROM by UV
light).
EEPROM(Electrically Erasable
PROM).
Flash Memory:
○ It is non-volatile computer memory that
can be electrically erased and
reprogrammed. It is a technology that is
primarily used in memory cards and USB
flash drives for general storage and
transfer of data between computers and
other digital products. It is a specific type
of EEPROM (Electrically Erasable
Programmable Read-Only Memory) that is
erased and programmed in large blocks (it
is faster than EEPROM)
I/O: Input/output ports
Parallel input/output ports are mainly used to
drive/interface various devices such as LCD’S,
LED’S, printers, memories, etc to a microcontroller
T/C: Timers/counters
The timers and counters provide all timing and counting
functions inside the microcontroller. The major operations
of this section are performed clock functions,
modulations, pulse generations, frequency measuring,
making oscillations, etc. This also can be used for
counting external pulses. A microcontroller may have
more than one timer and counters.
Parallel ports
provide various serial interfaces between a
microcontroller and other peripherals like
parallel ports
interrupts sources
Interrupts are the events that temporarily suspend
the main program and are provided by interrupts
sources
PWM function block
Is a built-in timer that can be used to generate
a PWM signal.
External
A clock used by related fuse bits
NC Extal 2
Extal1
Atmega 32/16 Clock sources
external RC oscillator
min(C ) 22 pF NC
Extal 2
1
f Extal1
3RC
Atmega 32/16 Clock sources
30 pF
Extal 2
Extal1
30 pF
quartz crystal
external crystal oscillator with frequency of
which is determined by quartz crystal (32.768
kHz)
ATmega16 Memories
a a b; , a a b;
switch (var) {
case 1:
doThing1();
break; case 2:
doThing2();
break; }
C language review
x 0b 11111111; //Binary
x 0 xFF ; //Hexadecimal
C language review
C program basics
C language review
A sample program
C Review:
Variable Declaration:
Type Size (Bits) Range
bit 1 0,1
char 8 -128 to 127
unsigned char 8 0 to 255
signed char 8 -128 to 127
int 16 -32768 to 32767
short int 16 -32768 to 32767
unsigned int 16 0 to 65535
signed int 16 -32768 to 32767
C Review:
Variable Declaration:
Type Size (Bits) Range
long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
signed long int 32 -2147483648 to 2147483647
float 32 ±1.175e-38 to ±3.402e38
double 32 ±1.175e-38 to ±3.402e38
C Review:
Global and Local variables
Global variables:
The variables declared outside any function are called global
variables. They are not limited to any function. Any function
can access and modify global variables. Global variables are
generally written before main() function.
Local variables:
The variables which are declared inside the function,
compound statement (or block) are called Local variables. is
one that occurs within a specific scope. They exist only in the
function where they are created.
An example for local variables
C Review:
Storage class variables
Const or flash variables: data will be constant and put in read only
memory like flash
e.g. const int var=1; or flash float pi=3.14
this command is used for declaring the address of stored data in SRAM
e.g. int y@0x80; storing y in 80H address
C Review:
Arrays:
Char arreyName=[arreySize];
Char arr1=[5];
Char arreyName[arreySize]={arreyLegend};
Char arr1[5]={1,2,3,4,5};
or char arr1[5];
arr1[0]=1; arr1[1]=2; arr1[2]=3; arr1[3]=4; arr1[4]=5;
from arr1[0] to arr1[4]
arr1[4]=5;
Indexed by integer.
Tow dimensional array x[3][4];
Unsigned char highschool [2][3];
Unsigned char highschool [2][3]={12,15,9}, {10,11,8};
C Review:
Operators:
Arithmetic operators
( x+y ,- , *, /, %)
Increment/decrement operator ++ , --
Increment operator: increases integer value by one
decrement operator: decreases integer value by one
Relation operators
Equal operator (==)
Less than (<) … less than or equal (<=)
Greater than (>) … greater than or equal (>=)
Not equal (!=)
C Review:
Operators:
Logical operators:
And (&&)
Or (||)
Not (!)
Bitwise operators
Bitwise AND (&)
Bitwise OR(|)
Bitwise XOR(^)
Complement (~)
Right shifted (>>) a = 0001000 b = 2
Left shifted (<<) a << b = 0100000
a >> b = 0000010
C Review:
Assignment operator (= , +=, -=, /=, *=, %=)
C Review:
Preprocessors:
#include: Inserts a particular header from another file.
Other useful header files e.g. (delay.h, lcd.h, spi.h, I2C.h, gray.h,…etc.)
#define FILE_SIZE 42
e.g. #undef FILE_SIZE
C in functions
Loops in c language
for
while
Do while
Conditional statements
if
Else if
Switch case
C Review:
The For statement
For(initial; condition ; increment) If there are no braces, the next
statement is the only statement
{
under the for statement
Do whatever here…
}
count = 1;
for (x=5 ; count < 1000; count++)
e.g.2
Do
{ while(i<10){
PORTA=-PORTA;
}while(condition);
delay_ms(400);
i++
}
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
C Review:
Goto statement
goto is a jumping statement in c language, which
transfer the program’s control from one statement to
another statement (where label is defined)
: (colon) should be used after the label_name
label_name:
goto lable_name;
e.g.
label-name:
statement1;
statement2;
.
.
if(any-test-condition)
goto label-name;
C Review:
C functions
Function general form declaration.
Parameter list
A function may return a value. The return_type is the data type of the value
the function returns.
Some functions perform the desired operations without returning a value. In
this case, the return_type is the keyword void.
An example for c function:
C Review:
Delay function
delay_ms(): this function is used to give a delay in the code and it is
in terms of its input argument
fist the preprocessors must be included:
#include< delay.h>
Ex: Write a program to read the data of port D and put it in port B
#include<mega16.h>
void main (void) {
DDRD=0X00;
PORTD=0XFF;
while(1){
PORTB=PIND;
}
}
ATmega16 I/O Ports problems:
#include<mega16.h>
#include<delay.h>
void main (void) {
DDRB=0XFF;
PORTB=0X00;
while(1){
PORTB~= PORTB;
delay_ms(500)
}
}
ATmega16 I/O Ports problems:
EX: Program atmega16 to read number on port A and save it to port B if the
number is 5.
#include<mega16.h>
#include<delay.h>
unsigned char a;
void main (void) {
DDRA=0X00;
PORTA=0XFF;
DDRB=0XFF;
while(1) {
a=PINA;
if (a==5) {
PORTB=PINA;
delay_ms(1000)
}
}
}
ATmega16 I/O Ports problems:
EX: Write a function to read the first bit of port D and if it is one,
turn on the second bit of port D for 1s
#include<mega16.h>
#include<delay.h>
void Blink (void) {
if (PIND.0 ==1) { Note: in if command, it is
PIND.1=1; possible to check one pin of
a port but reading the whole
delay_ms(1000);
port is impossible
PIND.1=0;
}
}
void main (void) {
DDRD.0=0;
DDRD.1=1;
while(1) {
Blink();
}
}
LCD
LCDs are used for displaying status or parameters
in embedded systems meaning that they used to
show a program's printed output. LCDs using
related chips and codes interface microcontrollers
and converts the received data's to the characters
or letters that have in their memory
LCD types:
1. alphanumeric LCD: they display numbers,
letters( A-Z and a-z ) and some other
characters that they have stored in their
memories
2. graphic LCD : they also display images.
The alphanumeric LCDs are divided in terms of row and
column as: 16 1 , 16 2 , 40 4 , for example 16 2
means that LCD has 16 column and 2 row
LCD
LCD 16 2 is a 16 pin device which has 8 data pins (D0-D7) and
3 control pins (RS,RW,E). The remaining 5 pins are for supply
and backlight for the LCD
LCD
15 16
A K
LCD
Vss: Ground pin
Vdd: Supply pin
Vee: Contrast regulation pin; contrast of data on LCD is done by
connecting the variable side of potentiometer to pin 3 of LCD and
connecting 2 other side of that to supply and ground, and
varying potentiometer
RS: Register selection pin (either data or command)
RS=0:command register, RS=1:data register
RW: Selects read or write operation. RW=0: for write, RW=1:for
read
E: Enable pin: makes enable pin from high to low; by changing
the pulse level from one to zero, data and commands placed in
D0 -D7 enters to LCD
D0-D7: when E=0, , data and commands are put in D0-D7
A: anode: the light of background of LCD
K: cathode: the light of background of LCD
LCD
Interfacing modes of LCD with a micro controller
16×2 LCD can be interfaced with a microcontroller in 8 Bit
or 4 Bit mode. In 8 Bit mode, character data and LCD
command are sent through the data lines D0 to D7. That is 8
bit data is send at a time and data strobe is given through E
of the LCD.
But 4 Bit mode uses only 4 data lines D4 to D7. In this 8 bit
data is divided into two parts and are sent sequentially
through the data lines. The idea of 4 bit communication is
introduced to save pins of microcontroller. 4 bit
communication is bit slower than 8 bit but this speed
difference has no significance as LCDs are slow speed
devices. Thus 4 bit mode data transfer is most commonly
used.
LCD
alphanumeric LCD and AVR interface
4 Bit mode
LCD
15 16
A K
LCD memories:
ROM: include the ascci codes of characters, numbers, and
English letters that by declaring each of them to LCD, the
intended phrase will be displayed
DROM: it is volatile memory for displaying the data
RAM: it is 64 bit memory that can sore new characters
LCD
LCD functions:
LCD functions are in the library <lcd.h> that with wizard setting
they are added to the program #include <lcd.h>
1. LCD is waiting for the data
lcd_ready();
void lcd_ready(void)
2. To initialize the lcd
EX
lcd_init() lcd_init(16)
Void lcd_init(unsigned int)
lcd_clear()
void lcd_clear(void)
LCD
4. To Set the position of the cursor for subsequent text entry
Lcd_gotoxy(x,y)
void lcd_gotoxy(unsigned int x, unsigned int y) lcd_goto xy(5,0)
column row Sixth column and first row
in C the character values are stored as ascii values. to convert int to ascii we
can add the ascii of character “ ” with the integer. In numeral variables ,
the value of variable must be converted to its ascci code. In following
example, to print the variable of value, in which the ambient temperature
has been saved, the convert commands to ascci must be used
.equ_lcd_port=0x15; PORTC
void main(void)
lcd_ready();
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(“Micro”);
delay_ms(1000);
}
}