Wa0012.

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 64

1 DEPT.

OF ELECTRONICS ENGINEERING

GOVERNMENT
POLYTECHNIC COLLEGE
MATTANUR-670702

LABORATORY RECORD

NAME ATHUL PP

PROGRAMME ELECTRONICS ENGINEERING

COURSE EMBEDDED SYSTEMS LAB

REG. No 2201041483

YEAR 2024-25

Certified that this is the bonafide record of work done


by ATHUL PP Reg. No 2201041483 of Electronics Engineering Department in
Embedded Systems Lab, during 2024-25.

GPC MATTANNUR
2 DEPT. OF ELECTRONICS ENGINEERING

Staff in Charge Head of Department


Internal Examiner External Examiner

GPC MATTANNUR
3 DEPT. OF ELECTRONICS ENGINEERING

INDEX

Exp.. NAME OF EXPERIMENT Page No. Date Remarks


No.
1 FAMILIARIZATION OF AVR STUDIO 5 19/06/24

2 AVR HARDWARE SET UP AND ISP USB PROGRAMMING 11 19/06/24

3 15 19/06/24
READ A SWITCH AND DISPLAY STATUS ON LED
4 INTERFACING RELAY 17 21/06/24

5 INTERFACING SEVEN SEGMENT DISPLAY 21 21/06/24

6 INTERFACING DC MOTOR 23 2/07/24

7 INTERFACING STEPPER MOTOR 27 24/07/24

8 INTERFACING 16x2 LCD 31 7/08/24

9 INTERFACING ADC 37 14/08/24

10 TEMPERATURE SENSOR INTERFACING WITH AVR 39 14/08/24

11 MATRIX KEYPAD INTERFACING 45 01/10/24

12 MULTIPLEXED SEVEN SEGMENT DISPLAY INTERFACING 53 09/10/24

13 OPEN PROJECT - 57 09/10/24

Exp No. 1 Date: 18/06/24


FAMILIARIZATION OF AVR STUDIO
Aim: To familiarize with features of AVR Studio, and practice AVR Embedded C.

GPC MATTANNUR
4 DEPT. OF ELECTRONICS ENGINEERING

Theory: AVR Studio is an integrated development environment (IDE) for developing and
debugging AVR microcontroller applications. AVR Studio provides a complete set of features
including project file management, a C/C++ editor, navigation and code completion; a debugger
supporting run control including source and instruction-level stepping; registers, memory and I/O
views; and target configuration and management. AVR Studio supports all 8 and 32-bit AVR
microcontrollers, and connects seamlessly to debuggers and development kits.

Procedure:

Opening AVR Studio

● After installation, open AVR Studio 5 from Start → All Programs → Atmel AVR Tools → AVR Studio
5.0
● After opening, you will see a Start Page like this. Click on “New Project…“.

AVR Studio 5 Start Page

1. In new projects page ensure that C/C++ is selected in Installed Templates and
AVRGCC C Executable Project is selected. Give a suitable project name (here name
given as Exp0). Click OK.

GPC MATTANNUR
5 DEPT. OF ELECTRONICS ENGINEERING

AVR Studio New Project

● Now, you will see the Device Selection dialog box. As you can see, AVR Studio supports all the
AVR MCUs. We choose ATmega32. Click OK.

AVR Studio Device Selection


● Now, you will see the following screen. Note that the basic program structure is already
prepared for you. You simply need to initialize your variables and place your code inside the
while(1) loop.
● Type in the following code and press save button as shown

GPC MATTANNUR
6 DEPT. OF ELECTRONICS ENGINEERING

Now select Build->Build Exp0 to build the project and generate Hex File. If build is successful without
any error, we get a Build successful message in Output window.

● Hex file with same name as project will be generated inside Debug folder in the location where
we saved the project during Step 2. This Hex file will be used for Proteus simulation.

GPC MATTANNUR
7 DEPT. OF ELECTRONICS ENGINEERING

Sample code

We will write the code for blinking an LED connected in pin c.0 with a delay in between.

#define F_CPU 8000000UL /* Define CPU frequency here 8MHz */


#include <avr/io.h>
#include <util/delay.h> // for _delay_ms()

int main(void)
{
DDRA = 0x01; // initialize port A.0 as o/p while(1)
{ // LED on
PORTA = 0b00000001; // PA0 = High = Vcc _delay_ms(500);
// wait 500 milliseconds

//LED off
PORTC = 0b00000000; // PA0 = Low = 0v _delay_ms(500);
// wait 500 milliseconds
}
}

Result:
Familiarized with AVR studio and created HEX file.

GPC MATTANNUR
8 DEPT. OF ELECTRONICS ENGINEERING

BASIC HARDWARE SETUP FOR ATMEGA 32

GPC MATTANNUR
9 DEPT. OF ELECTRONICS ENGINEERING

HARDWARE ARRANGEMENT FOR PROGRAMMER

GPC MATTANNUR
10 DEPT. OF ELECTRONICS ENGINEERING

Exp No.2 Date: 18/06/24 AVR


HARDWARE SET UP AND ISP USB PROGRAMMING
AIM:
1. Set up hardware connection for AVR (ATmage 32).
2. Familiarize USB programmer (ISP) for AVR and its hardware connections.
3. Install the driver for USB programmer and the HEX file loader (khazama).
4. Practice downloading hex file from AVR Studio to AVR flash.

PROCEDURE:
1) The basic hardware connection for AVR has been assembled (Vcc, Ground, Reset, External crystal
for Clock). This setup on a single PCB with port terminals is called target board or development
board.
2) The driver software for USB programmer is installed .
(3) The software for HEX file loader [Here khazama], has been installed. [Prog. ISP, PonyProg,
Extreme burner, AVR dude, are some of the other popular GUI for hex file loading for different
programmers].
(4) The application program is compiled using AVR studio, while compiling, hex file is created in the
Project folder
5) The hardware connections are made and all the hardware arrangements are done for programming.
6) Khazama is opened and hex file of the program is loaded from the project folder [Browsing].
(7) Then Hex file is written into the AVR flash.
(8) The programmer is removed and the output is observed.

HARDWARE NEEDED
1. Computer system with windows OS.
2. Target board with interfacing circuit.
3. USB ASP programmer.
4. USB type B cable, FRC cable.

SOFTWARE NEEDED
1. AVR Studio (To edit, assemble and debug the program).
2. Khazama, to load hex file and to burn.
3. Driver Software for USB ASP programmer.

SAMPLE PROGRAM TO TOGGLE ALL BITS OF PORTA WITH A VISIBLE DELAY

#define F_CPU 16000000UL /* Define CPU frequency here 16MHz */


#include <avr/io.h>
#include <util/delay.h>

int main(void)
{

GPC MATTANNUR
11 DEPT. OF ELECTRONICS ENGINEERING

DDRA = 0xFF; /* Make all pins of PORTA as output pins */


while (1) /* Blink PORTA infinitely
*/
{
PORTA = 0x00;

CIRCUIT DIAGRAM FOR LED INTERFACING

DESIGN

R=V/I
In the LED circuit, total voltage V=5V.
Assume that voltage drop across LED=2.5V, and LED current = 10mA.

GPC MATTANNUR
12 DEPT. OF ELECTRONICS ENGINEERING

So voltage drop across resistor = 5V-2.5V= 2.5V.


There for R=2.5V/10mA = 250Ω.
Select nearest higher standard value; R = 330Ω .

_delay_ms(500); /* Delay of 500 milli second */


PORTA = 0xFF;
_delay_ms(500);
} }

● #include <avr/io.h> is a header files which provides various i/o operations like DDRx, PINx,
PORTx, etc.
● #include <util/delay.h> is a header file which provides inbuilt delay functions like _delay_loop_1(),
_delay_loop_2(), _delay_ms(), _delay_us(), etc.
● _delay_ms(xxx) provides a delay xxx milliseconds whereas _delay_us(xxx) provides a delay of xxx
microseconds.

After typing the program in AVR studio, and saving it, build and generate hex file.
After setting up the AtMega 32 development BOARD and USB ISP programmer as shown in figure,
download the hex file to microcontroller using Khazama program.

Open Khazama, select chip, select the he x file to be copied to microcontroller, and select auto program
to write the chip.

After writing the chip, make connections to LED as per circuit and verify the program.

Result:

Written embedded c program to toggle LEDs connected to PORTA, generated he x file, downloaded to
microcontroller, and verified using target board.

GPC MATTANNUR
13 DEPT. OF ELECTRONICS ENGINEERING

CIRCUIT DIAGRAM FOR SWITCH INTERFACING

GPC MATTANNUR
14 DEPT. OF ELECTRONICS ENGINEERING

Exp No.3 Date: 18/06/24

READ A SWITCH AND DISPLAY STATUS ON LED


AIM:
To read the status (ON/OFF) of a switch and display the status by turning ON/OFF an LED.
THEORY:
PORTD is configured as an input port and PORTC.0 is configured as an output port. Switch is connected to
PORTD Pin0 and LED connected to PORTC Pin 0. HARDWARE NEEDED:
1. Computer system with windows OS.
2. Target board with interfacing circuit.
3. USB ASP programmer.
4. USB type B cable, FRC cable.

SOFTWARE NEEDED:
1. AVR Studio (To edit, assemble and debug the program).
2. Khazama, to load hex file and to burn.
3. Driver Software for USB ASP programmer. ALGORITHM:
1.Set PORTC.0 as output by initializing DDRC.0 register with 0x01
2.Set PORTD as input by initializing DDRD register with 0x00
3.Read status of switch by reading value of PORTD Pin0. If it is high (Switch is closed), Turn ON
LED by outputting 0x01 on PORTC. If it is low(Switch is OFF), Turn OFF LED by outputting 0x00 on
PORTC. CODE:
#include <avr/io.h>
#include <util/delay.h> int main(void)
{
DDRC = 0x01;//Makes first pin of PORTC as Output DDRD
= 0x00;// //Makes all pins of PORTD input while(1)
//infinite loop
{ if(PIND==0x01)//checking the status of PIN, if it is '1', turns on
the LED {
PORTC = 0x01;//Turns ON LED
}
else if(PIND==0x00)// checking the status of PIN, if it is '0', turns off
the LED
{
PORTC=0x00;// Turning off the LED
}
}

RESULT: Status of switch has been read and displayed on

LED.
GPC MATTANNUR
15 DEPT. OF ELECTRONICS ENGINEERING

Hardware arrangement

GPC MATTANNUR
16 DEPT. OF ELECTRONICS ENGINEERING

Exp No. 4 Date: 20/06/24

INTERFACING RELAY
AIM:

Set up Atmega 32 based system to control a 230V/AC bulb using a switch connected to PD 7 pin.

THEORY:

A microcontroller can’t operate a 230 V device directly. Generally a relay is used to control such devices.

A relay is an electromagnetic switch used to control High Voltage or Current using low power circuits.
Electromagnetic relays uses an electromagnet to operate a switching mechanism. It also provides
isolation between low power circuit and high power circuits.

Normally a SPDT relay has 5 terminals. Two of them are used to energies the electromagnet and other
three are COM, NO, NC. COM stands for Common, NO stands for Normally Open and NC stands for
Normally Closed. When the electromagnet is not energized, the armature will be connected to NC
contact. Thus COM and NC will be connected. When the electromagnet is energized, the electromagnet
attracts the iron armature and it will be connected to NO contact. Thus COM and NO will be connected.

A relay should not be connected directly to a microcontroller as microcontroller is not able to supply
current required for the working of a relay, and back emf produced by relay coil may damage
microcontroller. A driver using transistor or ICs used to control relays.

Embedded C program

#include <avr/io.h>
#include <util/delay.h> int main(void)
{
DDRA= 0x80; // PA.7 as out put.
DDRD= 0x00; // PD.7 as input.
PORTD=0X00; // PORT D.7 internal pull up turned on.
while(1)
{
int a= PIND; // Monitor pin D.7 for low.
if(a == 0 )
{
PORTA=0x80; // If switch pressed turn on light (PA.7).

GPC MATTANNUR
17 DEPT. OF ELECTRONICS ENGINEERING

Hardware arrangement

GPC MATTANNUR
18 DEPT. OF ELECTRONICS ENGINEERING

else {
PORTA=0x00; // else turn off the light.

}
}
}

RESULT

Program written in embedded C, compiled, downloaded to target board, and implemented with
electromagnetic relay and 230V bulb.

GPC MATTANNUR
19 DEPT. OF ELECTRONICS ENGINEERING

CIRCUIT DIAGRAM

3&8 -ve

H G F E D C B A Data Digits
0 0 1 1 1 1 1 1 0x3F 0
0 0 0 0 0 `1 1 0 0x06 1
0 1 0 1 1 0 0 1 0x5B 2
0 1 0 0 1 1 1 1 0x4F 3
0 1 1 0 0 1 1 0 0X66 4
0 1 1 0 1 1 0 1 0x6D 5
0 1 1 1 1 1 0 1 0x7D 6
GPC MATTANNUR
20 DEPT. OF ELECTRONICS ENGINEERING

0 0 0 0 0 1 1 1 0X07 7
0 1 1 1 1 1 1 1 0x7F 8
0 1 1 0 1 1 1 1 0x6F 9

Exp No. 5 Date: 24/06/24

INTERFACING SEVEN SEGMENT DISPLAY


AIM
To display ‘0-9-0’ continuously on seven segment common cathode display with segments connected to
PA0 to PA7 with a suitable delay in between.

C Program

#define F_CPU 1600000UL //clock frequency

#include <avr/io.h> // standard AVR header

#include <util/delay.h> // delay loop function

int main(void)

DDRA = 0xff; // setting PORTA as output

while (1) // indefinite loop

PORTA = 0x3f; // setting value for PORTA for 0

_delay_ms(1000); //delay

PORTA = 0x06; // setting value for PORTA for 1

_delay_ms(1000); //delay

PORTA = 0x5B; // setting value for PORTA for 2

_delay_ms(1000); //delay

PORTA = 0x4F; // setting value for PORTA for 3

_delay_ms(1000); //delay

PORTA = 0x66; // setting value for PORTA for 4

_delay_ms(1000); //delay

GPC MATTANNUR
21 DEPT. OF ELECTRONICS ENGINEERING

PORTA = 0x6D; // setting value for PORTA for 5


_delay_ms(1000); //delay

PORTA = 0x7D; // setting value for PORTA for 6

_delay_ms(1000); //delay

PORTA = 0x07; // setting value for PORTA for 7

_delay_ms(1000); //delay

PORTA = 0x7F; // setting value for PORTA for 8

_delay_ms(1000); //delay
PORTA = 0x6F; // setting value for PORTA for 9

_delay_ms(1000); //delay

}
}

RESULT

Embedded C program for 7 segment display is written with delay routine, compiled the program,
downloaded in target board, and implemented.

Circuit for Program 1

GPC MATTANNUR
22 DEPT. OF ELECTRONICS ENGINEERING

Vcc1 : Logic Input Voltage


Vcc2: Driver/Motor Supply
A : Input
Y : Output
1, 2 EN : Enable Of First Pair 1, 2
3, 4 EN : Enable of Second Pair 3, 4

L293D

Control Signals and Motor Status


PC0/IN1 PC1/IN2 Motor Status
LOW LOW Stops
LOW HIGH Clockwise
HIGH LOW Anti-Clockwise
HIGH HIGH Stops

Exp No. 6 Date: 24/06/24

INTERFACING DC MOTOR
AIM
Write embedded c program to control direction of rotation of DC motor and implement in
hardware.
Write embedded c program to control direction of rotation of DC motor using switch, and
implement in hardware.

THEORY

A DC Motor can’t connect directly to a microcontroller. ATmega32 Microcontroller can source or


sink currents up to 40mA but a DC Motor needs current very much more than that. The back emf of the
motor may affect the proper functioning of the microcontroller. The operating voltage of the DC Motor
may be much higher than the operating voltage of the microcontroller.

GPC MATTANNUR
23 DEPT. OF ELECTRONICS ENGINEERING

Generally DC motors are connected with transistorized H Bridge drivers. ICs such as L293D or L293 are
popular H Bridges. L293D is a Quadruple Half H-Bridge driver commonly used for motor driving. All the
four outputs of this IC are TTL compatible and output clamp diodes are provided to drive inductive loads.
L293D can provide up to 600mA current, in the voltage raging from 4.5 to 36v. L293 is a similar IC which
can provide up to 1A in the same voltage range.L293 or L293D contains four Half H Bridge drivers and are
enabled in pairs. We can drive two DC Motors with one L293Ds.

Program 1

#define F_CPU 1000000UL // clock speed


#include <avr/io.h>
#include <util/delay.h> int
main(void) {

DDRC = 0xFF; //PORTC as Output while(1)


{
PORTC = 0x01; //00000001 //Rotates Motor in Anticlockwise for
5sec.
_delay_ms(5000);
PORTC = 0x00; //00000000 //Stops Motor
_delay_ms(2000);
PORTC = 0x02; //00000010 //Rotates Motor in Clockwise
_delay_ms(5000)
PORTC = 0x00; //00000011 //Stops Motor
_delay_ms(4000);
}
}

Working of L293D H-bridge

GPC MATTANNUR
24 DEPT. OF ELECTRONICS ENGINEERING

Circuit for program 2

Program 2

#define F_CPU 1000000UL // clock speed


#include <avr/io.h>
#include <util/delay.h> int
main(void)
{
DDRC = 0xFF; //PORTC as Output
while(1)
{ if((PIND&0x01)==0
x01)
PORTC = 0x01; //Rotates Motor in Anticlockwise for 5sec.
elseif((PIND&0x01)==0x00)
PORTC = 0x02 //Rotates Motor in Clockwise else
PORTC = 0x00; //Stops Motor
}
}

RESULT

Embedded C program for controlling direction of rotation of DC motor is written, compiled the program,
downloaded in target board, and implemented.

GPC MATTANNUR
25 DEPT. OF ELECTRONICS ENGINEERING

Stepper Motor Interfacing

Full step sequence Half step sequence

Step A B C D Step A B C D
1 1 1 0 0 1
1 1 0 0
0 2 1 0 0 0
2 1 1 0 3 1 1 0 0
0

3 0 1 1
4 0 1 0 0
GPC MATTANNUR
5 0 1 1 0
4 0 0 1
6 0 0 1 0
26 DEPT. OF ELECTRONICS ENGINEERING

7 0 0 1 1
8 0 0 0 1

Exp No. 7 Date: 02/07/24

INTERFACING STEPPER MOTOR


AIM
To write embedded c program for interfacing a unipolar stepper motor and implement it using
ULN 2003 driver IC.
THEORY
Stepper motor is a brushless DC motor that divides the full rotation angle of 360° into a number of equal
steps. The motor is rotated by applying a certain sequence of control signals. The speed of rotation can be
changed by changing the rate at which the control signals are applied.
Stepper motors are classified in to Unipolar, and bipolar depending upon winding arrangement.

Unipolar Stepper Motor

Unipolar Stepper Motor has centre tapped winding with 5 (If both centers are connected internally) or 6
wires as shown in figure. Generally, these centre tapped connections are connected to the power supply.
By providing ground path to the winding leads we can allow the flow of current through each half of coil
which create magnetic poles. By altering poles sequentially, we can rotate rotor accordingly. A
microcontroller can be used to apply different control signals to the motor to make it rotate according to
the need of the application.

ULN2003 driver is used to the driving stepper motor. The ULN2003A is an array of seven NPN Darlington
transistors capable of 500mA, 50 V output. It features common-cathode flyback diodes for switching
inductive loads.

Two step sequences are used to rotate Stepper Motor.


● Full Step Sequence
Here motor moves through its basic step angle. At a time two coils are excited. ●
Half Step Sequence

GPC MATTANNUR
27 DEPT. OF ELECTRONICS ENGINEERING

Here motor moves half of its basic step angle. Half step can be achieved by exciting both current
and next coil.
Interfacing Stepper Motor with ATmega 32
ATmega32 program to rotate the stepper motor clockwise by FULL step sequence.
#define F_CPU 8000000UL /* Define CPU Frequency 8MHz */
#include<avr/io.h> /* Include AVR std. library file
*/
#include<util/delay.h> /* Include delay header file */
void main(void)
{
DDRD = 0xFF; while /* Make PORTD pins as output */ (1)

GPC MATTANNUR
28 DEPT. OF ELECTRONICS ENGINEERING

{ PORTD = 0x09;
_delay_ms(100);
PORTD = 0x03;
_delay_ms(100);
PORTD = 0x06;
_delay_ms(100);
PORTD = 0x0C;
_delay_ms(100);
}
}
Stepper motor direction control using switch connected to pin A-7
#define F_CPU 8000000UL /* Define CPU Frequency 8MHz */
#include<avr/io.h> /* Include AVR std. library file */
#include<util/delay.h> /* Include delay header file */
voidmain(void)
{
DDRA=0X00; /* Make PORTA pins as input */
PORTA=0XFF; /* Make PORTA internal pull up */ DDRD =
0xFF; /* Make PORTD pins as output */
while (1)
{ if((PINA&0x80)==0)

GPC MATTANNUR
29 DEPT. OF ELECTRONICS ENGINEERING

{
PORTD = 0x09;
_delay_ms(100);
PORTD = 0x03;
_delay_ms(100);
PORTD = 0x06;
_delay_ms(100);
PORTD = 0x0C;
delay_ms(100);
}
else
{
PORTD = 0x06;
_delay_ms(100);
PORTD = 0x03;
_delay_ms(100);
PORTD = 0x09;
_delay_ms(100);
PORTD = 0x0C;
delay_ms(100);
}
}
}

RESULT

Stepper motor interfaced with forward and reverse rotation.

GPC MATTANNUR
30 DEPT. OF ELECTRONICS ENGINEERING

Exp No. 8 Date: 23/07/24

INTERFACING 16x2 LCD


AIM
To interface 16x2 LCD with ATmega 32.

GPC MATTANNUR
31 DEPT. OF ELECTRONICS ENGINEERING

THEORY

Liquid Crystal Display (LCD) is widely used in various electronics’ applications to show different status and
parameters. LCD16x2 has 2 lines with 16 characters in each line. Each character is made up of 5x8
(column x row) pixel matrix. LCD 16x2 is a 16 pin device which has 8 data pins (D0-D7) and 3 control pins
(RS, RW, EN). The remaining 5 pins are for supply and backlight for the LCD. The control pins help us
configure the LCD in command mode or data mode.

To use LCD, need to initialize the LCD, by sending some commands. Commonly
Used LCD16x2 Commands
Code Execution
Command to LCD
(HEX) Time
0x01 Clear the display screen 1.64ms

Shift the cursor right (e.g. data gets written in an incrementing order, left to
0x06 40 us
right)
0x0C Display on, cursor off 40 us
0x0E Display on, cursor blinking 40 us
0x80 Force the cursor to the beginning of the 1st line 40 us
0xC0 Force the cursor to the beginning of the 2nd line 40 us
0x10 Shift cursor position to the left 40 us
0x14 Shift cursor position to the right 40 us
0x18 Shift entire display to the left 40 us
0x1C Shift entire display to the right 40 us
0x38 2 lines, 5x8 matrix, 8-bit mode 40 us
0x28 2 lines, 5x8 matrix,4-bit mode 40 us
0x30 1 line, 8-bit mode 40us
0x20 1 line, 4-bit mode 40us

C Program
// Code for LCD Interfacing with ATmega32 AVR microcontroller
#define F_CPU 16000000UL /* Define CPU Frequency e.g. here
16MHz */
#include <avr/io.h> /* Include AVR std. library file
*/
#include <util/delay.h> /* Include inbuilt defined
Delay header file */

#define LCD_DATA PORTD //PORT D as data line


GPC MATTANNUR
32 DEPT. OF ELECTRONICS ENGINEERING

#define LCD_RS 6 //of PORT C


#define LCD_EN 7 //of PORT C

//------function declaration---------
void lcd_command(char c); void
lcd_data(char d); void lcd_puts(const
char *s);

//------main function---------------- main()


{
DDRC=0XFF; //PORT C as o/p line
DDRD=0XFF; //PORT D as o/p line
_delay_ms(20);

//-------passing commands to lcd for initialization------


lcd_command(0X38); //2 lines, 5x8 matrix, 8-bit mode
lcd_command(0X80); // cursor to the beginning of the
1st line
lcd_command(0X0C); // Display on, cursor off
lcd_command(0X06); // Shift the cursor right
lcd_command(0X01); // Clear the display screen
_delay_us(100); lcd_puts("GPC MATTANUR");
// first line of data
lcd_command(0XC0); // cursor to the beginning of the
2nd line lcd_puts("ELECTRONICS ENGG "); // second
line of data
_delay_us(5);
while(1); // endless loop }

//-----------function lcd command--------- void


lcd_command(char c)
{
LCD_DATA=c;
PORTC&=~(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

GPC MATTANNUR
33 DEPT. OF ELECTRONICS ENGINEERING

//-----------function lcd data--------- void


lcd_data(char d)
{
LCD_DATA=d;
PORTC|=(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

GPC MATTANNUR
34 DEPT. OF ELECTRONICS ENGINEERING

//-----------function lcd puts---------

void lcd_puts(const char *s)


{
while(*s)
{
lcd_data(*s++);
_delay_us(5);
}
}

RESULT

Interfacing program for 16x2 LCD is written in embedded C, compiled, downloaded to target board, and
implemented LCD interfacing.

GPC MATTANNUR
35 DEPT. OF ELECTRONICS ENGINEERING

CIRCUIT DIAGRAM

Exp No. 9 Date: 06/08/24

INTERFACING ADC
AIM
To familiarize ADC interfacing in ATmega 32.
THEORY

AtMega 32 has an inbuilt 8 channel ADC with 10 bit resolution. It has features like selectable
reference voltage, single ended or differential channel, gain controller, sampling prescalar, etc.
these can be selected using ADCMU and ADCSRA registers.
A potentiometer connected to PORTA.0 to develop a variable voltage. LEDs connected to PORTB
and PORTD to indicate the binary equivalent. Change the potentiometer position and observe
the change in binary pattern.
C Program

#include <avr/io.h>

int main(void)
{
DDRD = 0xFF; //port D output
GPC MATTANNUR
36 DEPT. OF ELECTRONICS ENGINEERING

DDRB = 0xFF; //port B output


DDRA = 0; //port A input. LM 34 connected to ADC0
ADCSRA = 0x87; //ADC enable, ck/128,
ADMUX = 0x40; //Vcc V ref, ADC0 single ended right justified data
while(1)
{
ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF))==0); //wait for EOC flag ADIF
PORTD = ADCL; //low byte to port D
PORTB = ADCH; //high byte to port B
} return 0;
}
RESULT

Familiarized with AtMega 32 ADC programming.

CIRCUIT DIAGRAM

GPC MATTANNUR
37 DEPT. OF ELECTRONICS ENGINEERING

Exp No. 10 Date: 21/08/24

TEMPERATURE SENSOR INTERFACING WITH AVR USING ADC


AIM

To interface Temperature sensor with AVR microcontroller using ATmega32 ADC and display on
LCD
THEORY
To use ATmega32 ADC, the analog input need to be connected to PORTA pins which are
multiplexed as ADC input pins (labeled as ADC0 to ADC7). LM35 Temperature sensor (Pin 2)
is connected to PORTA Pin 0(ADC0 pin) of ATmega32. PORTA is configured as an input port
and PORTD and C as out put ports.is configured as an output port. Data lines of LCD are
connected to PORTD, and PORTC.6 and 7 are used as RS and E. RW is connected to ground.
LM35 series are precision integrated-circuit temperature sensors whose output voltage is
linearly proportional to the temperature. It outputs 10 mV for each degree of temperature.
The ADC has 10-bit resolution. So number of steps will be 1024. We use the internal 2.56 V
reference voltage as Vref, so the step size would be 2.56 V/1024 = 2.5 mV. LM35 sensor
produces 10 mV for each degree of temperature change and the ADC step size is 2.5 mV. So
each degree of temperature change corresponds to 4 ADC steps(10 mV/2.5 mV = 4).This
makes the binary output number for the ADC four times the real temperature. So the 10-bit
output of the ADC is divided by 4 to get the real temperature. To divide the 10-bit output of
the ADC by 4 we choose the left-justified option and only read the ADCH register. It is same
as shifting the result 2 bits right (division by 4 in binary).

C Program

// Code for Interfacing LM 35 and LCD with ATmega32 AVR microcontroller

#define F_CPU 16000000UL /* Define CPU Frequency e.g. here


16MHz */
#include <avr/io.h> /* Include AVR std. library file
*/
#include <util/delay.h> /* Include inbuilt defined Delay
header file */
#define LCD_DATA PORTD //PORT D as data line
#define LCD_RS 6 //of PORT C
#define LCD_EN 7 //of PORT C

GPC MATTANNUR
38 DEPT. OF ELECTRONICS ENGINEERING

//------function declaration---------
void lcd_command(char c); void
lcd_data(char d); void lcd_puts(const
char *s);

//------main function---------------- main()


{
unsigned int ADATA = 0; //storing digital output of ADC char
GPC MATTANNUR
39 DEPT. OF ELECTRONICS ENGINEERING

DIGITS [3]; //for holding string of digits

DRC=0XFF; //PORT C as o/p line


DDRD=0XFF; //PORT D as o/p line DDRA
= 0; // PORTA as input. _delay_ms(20); ADMUX |
=(1<<REFS0)|(1<<REFS1) |(1<<ADLAR);
//setting the reference of ADC as 2.56V,left justified
data
ADCSRA |=(1<<ADEN)|(1<<ADATE)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);
//enabling the ADC, setting free running mode, setting prescalar 128
//-------passing commands to lcd for initialization------
lcd_command(0X38); //2 lines, 5x8 matrix, 8-bit mode
lcd_command(0X80); // cursor to the beginning of the
1st line
lcd_command(0X0C); // Display on, cursor off
lcd_command(0X06); // Shift the cursor right
lcd_command(0X01); // Clear the display screen
_delay_us(100);
ADCSRA |=(1<<ADSC); //start conversion while(1)
{
ADATA = ADCH; //devide adc data by four.(10mV/2.5mV=4)=
ADCH
lcd_puts("ROOM TEMPERATURE"); // first line lcd_command(0XC0);
// cursor to the beginning of the
2nd line
lcd_puts("Degree C:"); // second line itoa(ADATA,DIGITS,10);
//binary data to digits in a string lcd_puts (DIGITS); in LCD
//display digits of temperature
lcd_command(0X80); //retuning to first
line first column

_delay_us(5);
}
}

GPC MATTANNUR
40 DEPT. OF ELECTRONICS ENGINEERING

//-----------function lcd command--------- void


lcd_command(char c)
{
LCD_DATA=c;
PORTC&=~(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

//-----------function lcd data---------

void lcd_data(char d)
{
LCD_DATA=d;
PORTC|=(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

//-----------function lcd puts--------- void


lcd_puts(const char *s)
{
while(*s)
{
lcd_data(*s++);
_delay_us(5);
}
}

GPC MATTANNUR
41 DEPT. OF ELECTRONICS ENGINEERING

RESULT
Interfaci
ng
program
for LM
35 with
16x2
LCD is
written
in
embedd
ed C,
compile
d,
downlo
aded to
target
board,
and
implem
ented.

GPC MATTANNUR
42 DEPT. OF ELECTRONICS ENGINEERING

Exp No. 11 Date: 07/09/24

MATRIX KEYPAD INTERFACING


AIM
To interface 4x4 matrix keypad with AVR microcontroller and display key on LCD.
THEORY
Matrix keypad offers more inputs with the lesser I/O pins. A matrix keypad consists of a set of push-
button or switches which are arranged in a matrix format of rows and columns.
The CPU accesses both rows and columns through ports. When a key is pressed, a row and a column
make a contact. otherwise, there is no connection between rows and columns.
All the column pins (Col1 – Col4) are connected to the inputs pins and all the row (Ro1 –Ro4), pins are
connected to the output pins of the microcontroller. In the normal case, all the column pins are pulled up
(HIGH state) by internal or external pull-up resistors. Now we can read the status of each switch through
scanning.
1. A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
2. Now each Column is scanned. If any switch belongs to the 1 st row is pressed the corresponding
column will pull down (logic LOW) and we can detect the pressed key.
3. This process is repeated for all rows.

GPC MATTANNUR
43 DEPT. OF ELECTRONICS ENGINEERING

From the row column pattern it is possible to identify the pressed key with the help of a look up table.
Debouncing can be done either with software or hardware.
PROCEDURE

Row and column lines of keypad are connected to PORTA. LCD is used to display the pressed key. PORTD is
used as data lines, and PORTC 6 and 7 lines are used as RS and EN lines of LCD. C Program

#define F_CPU 16000000UL /* Define CPU Frequency e.g. here


16MHz */
#include <avr/io.h> /* Include AVR std. library file
*/
#include <util/delay.h> /* Include inbuilt defined Delay
header file */
#define LCD_DATA PORTD //PORT D as data line

#define LCD_RS 6 //of PORT C

#define LCD_EN 7 //of PORT C


//------function declaration---------
void lcd_command(char c); void
lcd_data(char d); void lcd_puts(const
char *s); //------main function------
---------- main() {
char i,chr;

DDRC=0XFF; //PORT C as o/p line


DDRD=0XFF; //PORT D as o/p line
DDRA=0XF0; //PORT A0-3 as i/p line, 4-7 as o/p
PORTA=0xff; //PORT A pull up enabled

GPC MATTANNUR
44 DEPT. OF ELECTRONICS ENGINEERING

//-------passing commands to lcd for initialization------


lcd_command(0X38); //2 lines, 5x8 matrix, 8-bit mode
lcd_command(0X80); // cursor to the beginning of the
1st line lcd_command(0X0C); // Display on, cursor
off lcd_command(0X06); // Shift the cursor
right lcd_command(0X01); // Clear the display
screen
_delay_us(100); lcd_puts("CHECK KEYS"); //
first line while(1) {
_delay_us(5);
lcd_command(0X8B);
_delay_us(5);
PORTA=0x7f; // row1 =0 i=PINA
& 0X0F;

//-------reading columns-------------
switch(i)
{
case 0x0E:
chr='0';
lcd_data(chr);
break;
case 0x0D:
chr='4';
lcd_data(chr);
break;
case 0x0B:
chr='8';
lcd_data(chr);
break;
case 0x07:
chr='C';
lcd_data(chr);
break;
}
PORTA=0XBF; //row2=0 i=PINA
& 0X0F;
switch(i)
{
case 0x0E:
chr='1';
lcd_data(chr);
break;

GPC MATTANNUR
45 DEPT. OF ELECTRONICS ENGINEERING

case 0x0D:
chr='5';
lcd_data(chr);
break; case
0x0B: chr='9';
lcd_data(chr);
break;

GPC MATTANNUR
46 DEPT. OF ELECTRONICS ENGINEERING

case 0x07: chr='D';


lcd_data(chr); break;
}

PORTA=0XDF; //row3=0 i=PINA&


0X0F;
switch(i)
{
case 0x0E:
chr='2';
lcd_data(chr);
break;
case 0x0D:
chr='6';
lcd_data(chr);
break;
case 0x0B:
chr='A';
lcd_data(chr);
break;
case 0x07:
chr='E';
lcd_data(chr);
break;
}

PORTA=0XEF; //row4=0
i=PINA& 0X0F; switch(i)
{
case 0x0E:
chr='3';
lcd_data(chr);
break;
case 0x0D:
chr='7';
lcd_data(chr);
break;
case 0x0B:
chr='B';
lcd_data(chr);
break;
case 0x07:
chr='F';

GPC MATTANNUR
47 DEPT. OF ELECTRONICS ENGINEERING

lcd_data(chr);
break;
}
}

return(0);
}

GPC MATTANNUR
48 DEPT. OF ELECTRONICS ENGINEERING

//-----------function lcd command--------- void


lcd_command(char c)
{
LCD_DATA=c;
PORTC&=~(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

//-----------function lcd data--------- void


lcd_data(char d)
{
LCD_DATA=d;
PORTC|=(1<<LCD_RS);
PORTC|=(1<<LCD_EN);
_delay_us(5);
PORTC&=~(1<<LCD_EN);
_delay_ms(1);
}

//-----------function lcd puts--------- void


lcd_puts(const char *s)
{
while(*s)
{
lcd_data(*s++);
_delay_us(5);
}

RESULT

Interfaced Matrix Keypad with ATMEGA32 and observed the key pressed on 16x2 LCD.

CIRCUIT DIAGRAM

GPC MATTANNUR
49 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
50 DEPT. OF ELECTRONICS ENGINEERING

Exp No.12 Date: 01/10/24


MULTIPLEXED SEVEN SEGMENT DISPLAY INTERFACING
AIM:
To interface 3 seven segment displays in multiplexed fashion with AVR microcontroller and display
continuous count from 000 to 999.

THEORY
With minimum number of o/p pins, multiple & segment displays can be connected, either
using a display driver or with Multiplexing. This technique is based on the principle of
Persistence of Vision of eyes. If the frames change at a rate of 25 ( or more) frames per second,
human eye can’t detect that visual change. Each display appears turned on continuously.

PORTA is configured as output port. 3 common cathode 7 segment displays are connected to
PORTA in a multiplexed fashion. PORTA Pins 0 to 7 are connected to D1to D8 segment pins of 7
segment display which correspond to segments a to g and dot in 7 segment display. PORTD is
also configured as output port .Pin 2 of Port D is connected to common cathode point of 1 st 7
segment display, Pin 1 to 2 nd and Pin 0 to 3rd respectively through switching transistors. This is
done to control the displays individually. The 7 segment displays are switched on and off
sequentially with a delay of 1 ms and due to human persistence of vision they appear as stable.

ALGORITHM

1. Set PORTA as output by initializing DDRA register with 0xFF


2. Set PORTD as output by initializing DDRD register with 0xFF
3. 8 bit hex codes to display digits 0 to 9 in 7 segment display are stored in an array. The
program loops through the array continuously and outputs the code to PORTA.
4. Hundred’s, ten’s and One’s places are displayed using nested for loops. Outer for loop
corresponds to first 7 segment display (100’s place). The next inner for loop corresponds to
second 7 segment display (10’s place) and the next inner for loop corresponds to third 7
segment display (1’s place).
5. In the inner most for loop we first enable only the first 7 segment display by driving PORTD
Pin 2 high and output the hex code corresponding to 100’s place value from the array to
PORTA and call 10 ms delay.
6. Then sequentially we enable only the second 7 segment display by driving PORTD Pin 1 high
and output the hex code corresponding to 10’s place value from the array to PORTA and call
10 ms delay.
7. And then sequentially we enable only the third 7 segment display by driving PORTD Pin 0
high and output the hex code corresponding to 1’s place value from the array to PORTA and
call 10 ms delay.

GPC MATTANNUR
51 DEPT. OF ELECTRONICS ENGINEERING

CODE

#define F_CPU 1000000UL //1 MHz clock #include <avr/io.h>


#include <util/delay.h>
#include <avr/io.h> /* Include AVR std. library file */

char seg_7[10] =
{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char hun,ten,one,dig; int main(void)
{
DDRA = 0xFF; // setting PORTA as output
DDRD= 0xFF; // setting PORTD as output
PORTD = 0x00; // initially disable all 3 seven segment
displays PORTA = 0x00; while (1) // indefinite loop
{ for(hun = 0; hun<10; hun++) //for loop for 100's
place
{ for(ten = 0;ten<10;ten++) //for loop for 10's
place
{ for(one=0; one<10; one++) //for loop for 1's
place
{ for (dig =0;
dig<10;dig++)
{
PORTD = 0x04; // enable 1st 7 segment display
PORTA = seg_7[hun]; // output hex code for 100's place value
_delay_ms(10); // 10 msec delay

PORTD = 0x02; // enable 2nd 7 segment display


PORTA = seg_7[ten]; //
_delay_ms(10); // 10 msec delay

PORTD = 0x01; // enable 3rd 7 segment display


PORTA = seg_7[one]; //
_delay_ms(10); // 10 msec delay
}
}
}
} }
return
0;
}

GPC MATTANNUR
52 DEPT. OF ELECTRONICS ENGINEERING

RESULT:

3 seven segment displays in multiplexed fashion have been interfaced with AVR microcontroller and
displayed continuous count from 000 to 999.

CIRCUIT DIAGRAM

GPC MATTANNUR
53 DEPT. OF ELECTRONICS ENGINEERING

Exp No.13 Date: 08/10/24

OPEN PROJECT

LED CONTROLLER USING LDR

AIM:
To interface light dependent resistor (LDR) with AVR microcontroller and control the led light intensity

THEORY:
A light dependent resistor is a resistor whose resistance changes with the intensity of incident light. The
working principle of light dependent resistor is photoelectric effect. A light dependent resistor is made of
a high resistance semiconductor. If the energy of the incident light is greater than the band gap of the
semiconductor, electron-hole pairs are generated .The photo generated electron-hole pair transits the
device giving rise to photo conductivity. Here PORTB is configured as output.

CODE

#include <avr/io.h> //Include AVR std. library file


#include <avr/interrupt.h> //Enabling interrupts
#include <util/delay.h> // Include inbuilt defined Delay header file
#define THRESHOLD 500 //setting threshold value #define
LED_PIN PB0 //setting PORTA as output int main()
// Initialize ports
{
DDRB = (1 << LED_PIN); // LED output
DDRC = 0; // LDR input
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
ADMUX = (1 << REFS0); // Initialize ADC
while (1) // indefinite loop
{
unsigned int ldr_value = ADC_Read(PC0); //Read LDR value

GPC MATTANNUR
54 DEPT. OF ELECTRONICS ENGINEERING

if (ldr_value < THRESHOLD) // Check threshold


{
PORTB |= (1 << LED_PIN); // Night mode: Turn on LED
} else
{
PORTB &= ~(1 << LED_PIN); // Day mode: Turn off LED
}
_delay_ms(100) // 10 msec delay
}

return 0;
}

GPC MATTANNUR
55 DEPT. OF ELECTRONICS ENGINEERING

Explanation:

- Initialize LDR as analog input (PC0) and LED as digital output (PB0).
- Read LDR value using ADC.
- Compare LDR value to threshold.
- Turn on/off LED based on threshold comparison.

Pin Functions:

- PC0: Analog input for LDR


- PB0: Digital output for LED

GPC MATTANNUR
56 DEPT. OF ELECTRONICS ENGINEERING

RESULT:
Interfaced the LDR with AVR microcontroller and controlled the LED using light intensity

GPC MATTANNUR
57 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
58 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
59 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
60 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
61 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
62 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
63 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR
64 DEPT. OF ELECTRONICS ENGINEERING

GPC MATTANNUR

You might also like