Introduction to
Embedded System
Semester : 5th
Course Code : ES314
TP Sr No: 16, CLO:2, PLO 2, C4
Fair Use Notice
The material used in this presentation i.e., pictures/graphs/text, etc. is solely
intended for educational/teaching purpose, offered free of cost to the students for
use under special circumstances of Online Education due to COVID-19 Lockdown
situation and may include copyrighted material - the use of which may not have
been specifically authorized by Copyright Owners. It’s application constitutes Fair
Use of any such copyrighted material as provided in globally accepted law of many
countries. The contents of presentations are intended only for the attendees of the
class being conducted by the presenter.
Why Programming AVR in C
◉Microcontrollers have limited amount of
Memory on chip, ATMEGA16 has 16KB of
Programmable Memory.
◉Compilers generate an HEX file, that is to be
uploaded to microcontroller Chip.
◉Assembly language produces HEX file that is
much smaller in size than C.
◉Programming in assembly language is slow and
time consuming.
◉Programming in C language is less time
consuming and easy to write but HEX file size is
much larger than assembly.
Major Advantages of writing program in C
◉ It is easier and less time consuming to write in C than in Assembly.
◉ C is easier to modify and update.
◉ You can use code available in function libraries.
◉ C code is portable to other microcontrollers with little or no modification
AVR C Programming
Unsigned Characters
◉ The unsigned char is an 8-bit data type that takes value in
the range of 0-255 (00-FFH)
◉Due to limited number of registers and data RAM locations
in AVR microcontroller, unsigned character saves memory;
hence is the most preference in AVR applications
◉In default, C compilers use the signed char so we need to put
unsigned in front of the char
Sending data to a port
Write an AVR C program to send values 00-FF to Port B.
#include <avr/io.h> //standard AVR header
int main(void) //standard AVR header
{
unsigned char z;
DDRB = 0xFF;
for(z = 0; z <= 255; z++)
PORTB = z;
return 0;
}
//Notice that the program never exits the for loop because if you
//increment an unsigned char variable when it is 0xFF, it will
//become zero.
Sending data to a port
Example 7-2: Write an AVR C program to send hex values for
ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D to Port B.
#include <avr/io.h> //standard AVR header
int main(void) //the code starts from here
{
unsigned char myList[]= "012345ABCD";
unsigned char z;
DDRB = OxFF;
for(z=0; z<10; z++) //repeat 10 times and increment z
PORTB = myList[ z]; //send the character to PORTB
while(1); //needed if running on a trainer
return 0;
}
Arithmetic and Logic Operations
◉Logical Operators ◉Arithmetic Operators:
AND (&&) ADD (+)
OR (||) Subtract (-)
NOT (!) Multiply (*)
◉Bitwise Operators Divide (/)
AND (&) Remainder (%)
OR (|)
EX-OR(^)
Inverter (~)
Shift Right (>>)
Shift Left (<<)
Arithmetic and Logic Operations
Shift Operators
General formula: data shift left/right operator no of shifts/ bit position
(a) 1<<7 = 0000 0001 << 7 = 1000 0000
(b) 3<<2 = 0000 0011 << 2 = 0000 1100
Examples of Shift Operators
= 1000 0000
= 0000 0100
= 0001 0000
= 1101 1111
= 1111 0111
= 1111 1101
Bit size IO
#include <avr/io.h>
int main(void) //standard AVR header
{
DDRB = 0xFF; //make Port B output
DDRC = 0xFF; //make Port C output
DDRD = 0xFF; //make Port D output
PORTB = 0x35 & 0x0F;//ANDing
PORTC = 0x04 | 0x68;//Oring
PORTD = 0x54 ^ 0x78;//XORing
PORTB = ~0x55; //inverting
while (1);
return 0;
}
Bit size IO
Write an AVR C program to monitor bit 5 of port C. If it is HIGH, send 55H to Port B;
otherwise, send AAH to Port B.
#include <avr/io.h> //standard AVR header
int main(void)
{
DDRB = 0xFF; //PORTB is output
DDRC = 0x00; //PORTC is input
DDRD = 0xFF; //PORTB is output
while(1)
{
if (PINC & 0b00100000) //check bit 5 (6th bit) of PINC
PORTB = 0x55;
else
PORTB = 0xAA;
)
return 0;
}
Compound statements
OR the data of PORT A and also send on PORT A
PORTA| = 0AH
PORTA= PORTA | 0AH
A door sensor is connected to bit 1 of Port B, and an LED is connected to bit 7 of Port C. Write
an AVR C program to monitor the door sensor and, when it opens, turn on the LED.
#include <avr/io.h> //standard AVR header
int main(void)
{
DDRB = DDRB & 0b11111101; //pin 1 of Port B is input
DDRC = DDRC | 0bl0000000; //pin 7 of Port C is output
while(1)
{
if (PINB & 0b00000010) //check pin 1 (2nd pin) of PINB
PORTC = PORTC | 0b10000000; //set pin 7 (8th pin) of PORTC
else
PORTC = PORTC & 0b0lllllll; //clear pin 7 (8th pin) of PORTC
}
return 0;
}
}
Thanks!
Any questions ?