1. Write an AVR C program to interface a 7-segment display.
Display numbers from 0 to 9 in a
loop with a delay of 1 second.
Ans:
Assumption:
Microcontroller: ATmega16 / ATmega32
7-segment type: Common Cathode
7-segment connected to PORTB
Clock frequency = 8 MHz
✅ AVR C Program
#define F_CPU 8000000UL // Define CPU frequency as 8 MHz
#include <avr/io.h> // Include AVR input-output header file
#include <util/delay.h> // Include delay library
// Lookup table for common cathode 7-segment display (0–9)
unsigned char segment_code[10] =
{
0x3F, // Binary pattern for digit 0
0x06, // Binary pattern for digit 1
0x5B, // Binary pattern for digit 2
0x4F, // Binary pattern for digit 3
0x66, // Binary pattern for digit 4
0x6D, // Binary pattern for digit 5
0x7D, // Binary pattern for digit 6
0x07, // Binary pattern for digit 7
0x7F, // Binary pattern for digit 8
0x6F // Binary pattern for digit 9
};
int main(void)
{
DDRB = 0xFF; // Set PORTB as output (connected to 7-segment)
while(1) // Infinite loop
{
for(int i = 0; i < 10; i++) // Loop from 0 to 9
{
PORTB = segment_code[i]; // Send corresponding pattern to 7-segment
_delay_ms(1000); // Wait for 1 second
}
}
}
✅ Segment Connection (Common Cathode)
Segment AVR Pin (PORTB)
a PB0
b PB1
c PB2
d PB3
e PB4
f PB5
g PB6
dp PB7 (optional)
Common pin → GND
2. Write an AVR C program to display your name on the first row and "Hello!" on the second
row of a 16x2 LCD.
Answer:
Below is a complete AVR Embedded C program (for ATmega16/ATmega32) to:
Display your name on the first row
Display "Hello!" on the second row
LCD interfaced in 4-bit mode
✅ Assumptions (Connections)
LCD Pin AVR Pin
RS PD0
EN PD1
D4 PD4
D5 PD5
D6 PD6
D7 PD7
RW GND
Clock Frequency = 8 MHz
✅ AVR C Program (With Comments on Every Line)
#define F_CPU 8000000UL // Define CPU frequency as 8 MHz
#include <avr/io.h> // Include AVR input-output library
#include <util/delay.h> // Include delay library
#define RS PD0 // Define RS pin connected to PD0
#define EN PD1 // Define EN pin connected to PD1
// Function to send command to LCD
void lcd_command(unsigned char cmd)
{
PORTD = (cmd & 0xF0); // Send higher nibble to LCD (D4-
D7)
PORTD &= ~(1<<RS); // RS = 0 for command
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
PORTD = (cmd << 4); // Send lower nibble to LCD
PORTD &= ~(1<<RS); // RS = 0 for command
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
}
// Function to send data (character) to LCD
void lcd_data(unsigned char data)
{
PORTD = (data & 0xF0); // Send higher nibble
PORTD |= (1<<RS); // RS = 1 for data
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
PORTD = (data << 4); // Send lower nibble
PORTD |= (1<<RS); // RS = 1 for data
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
}
// Function to initialize LCD in 4-bit mode
void lcd_init()
{
DDRD = 0xFF; // Set PORTD as output
_delay_ms(20); // Wait for LCD to power up
lcd_command(0x02); // Initialize LCD in 4-bit mode
lcd_command(0x28); // 2 lines, 5x7 matrix
lcd_command(0x0C); // Display ON, cursor OFF
lcd_command(0x06); // Auto increment cursor
lcd_command(0x01); // Clear display
_delay_ms(2); // Delay after clear
}
// Main function
int main()
{
lcd_init(); // Initialize LCD
lcd_command(0x80); // Move cursor to first row, first
column
char name[] = "YourName"; // Replace with your actual name
for(int i = 0; name[i] != 0; i++) // Loop until end of string
{
lcd_data(name[i]); // Display each character
}
lcd_command(0xC0); // Move cursor to second row
char message[] = "Hello!"; // Second row message
for(int i = 0; message[i] != 0; i++) // Loop through message
{
lcd_data(message[i]); // Display each character
}
while(1); // Infinite loop
}
✅ Output on LCD
YourName
Hello!
3. Write an AVR C program to display the message "Hello, World!" on a 16x2 LCD using 4 bit
mode.
Ans:
✅ Assumptions
Microcontroller: ATmega16 / ATmega32
Clock Frequency: 8 MHz
LCD in 4-bit mode
Connections:
LCD Pin AVR Pin
RS PD0
EN PD1
D4 PD4
D5 PD5
D6 PD6
D7 PD7
RW GND
✅ AVR C Program (With Comments in Every Line)
#define F_CPU 8000000UL // Define CPU frequency as 8 MHz
#include <avr/io.h> // Include AVR I/O header file
#include <util/delay.h> // Include delay library
#define RS PD0 // RS pin connected to PD0
#define EN PD1 // EN pin connected to PD1
// Function to send command to LCD
void lcd_command(unsigned char cmd)
{
PORTD = (cmd & 0xF0); // Send higher nibble (D4-D7)
PORTD &= ~(1<<RS); // RS = 0 for command
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
PORTD = (cmd << 4); // Send lower nibble
PORTD &= ~(1<<RS); // RS = 0 for command
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
}
// Function to send data (character) to LCD
void lcd_data(unsigned char data)
{
PORTD = (data & 0xF0); // Send higher nibble
PORTD |= (1<<RS); // RS = 1 for data
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
PORTD = (data << 4); // Send lower nibble
PORTD |= (1<<RS); // RS = 1 for data
PORTD |= (1<<EN); // Enable = 1
_delay_ms(1); // Small delay
PORTD &= ~(1<<EN); // Enable = 0
}
// Function to initialize LCD in 4-bit mode
void lcd_init()
{
DDRD = 0xFF; // Set PORTD as output
_delay_ms(20); // Wait for LCD power ON
lcd_command(0x02); // Initialize LCD in 4-bit mode
lcd_command(0x28); // 2 lines, 5x7 matrix in 4-bit mode
lcd_command(0x0C); // Display ON, cursor OFF
lcd_command(0x06); // Auto increment cursor
lcd_command(0x01); // Clear display
_delay_ms(2); // Delay after clear
}
// Main function
int main(void)
{
lcd_init(); // Initialize LCD
lcd_command(0x80); // Move cursor to first row, first
column
char message[] = "Hello, World!"; // Store message
for(int i = 0; message[i] != 0; i++) // Loop until end of string
{
lcd_data(message[i]); // Display each character
}
while(1); // Infinite loop
}
✅ Expected LCD Output
Hello, World!
4. Write an AVR C program to display a counter that increments every second on the LCD. Use
Timer1 to generate a 1-second delay.
Ans:
✅ Assumptions
Microcontroller: ATmega16 / ATmega32
Clock Frequency: 8 MHz
LCD in 4-bit mode
Connections:
LCD Pin AVR Pin
RS PD0
EN PD1
D4 PD4
D5 PD5
D6 PD6
D7 PD7
RW GND
Timer1 prescaler = 256
1 second overflow calculation:
8,000,000
𝑇𝑖𝑚𝑒𝑟 𝐶𝑜𝑢𝑛𝑡𝑠 = = 31250
256
𝐼𝑛𝑖𝑡𝑖𝑎𝑙 𝑣𝑎𝑙𝑢𝑒 = 65536 − 31250 = 34286
✅ AVR C Program
#define F_CPU 8000000UL // Define CPU frequency as 8 MHz
#include <avr/io.h> // Include AVR I/O library
#include <avr/interrupt.h> // Include interrupt library
#include <util/delay.h> // Include delay library
#define RS PD0 // RS pin connected to PD0
#define EN PD1 // EN pin connected to PD1
volatile unsigned int counter = 0; // Global counter variable
//---------------- LCD FUNCTIONS ----------------//
void lcd_command(unsigned char cmd) // Function to send command to LCD
{
PORTD = (cmd & 0xF0); // Send higher nibble
PORTD &= ~(1<<RS); // RS = 0 (command mode)
PORTD |= (1<<EN); // Enable high
_delay_ms(1);
PORTD &= ~(1<<EN); // Enable low
PORTD = (cmd << 4); // Send lower nibble
PORTD &= ~(1<<RS);
PORTD |= (1<<EN);
_delay_ms(1);
PORTD &= ~(1<<EN);
}
void lcd_data(unsigned char data) // Function to send data to LCD
{
PORTD = (data & 0xF0);
PORTD |= (1<<RS); // RS = 1 (data mode)
PORTD |= (1<<EN);
_delay_ms(1);
PORTD &= ~(1<<EN);
PORTD = (data << 4);
PORTD |= (1<<RS);
PORTD |= (1<<EN);
_delay_ms(1);
PORTD &= ~(1<<EN);
}
void lcd_init() // Initialize LCD in 4-bit mode
{
DDRD = 0xFF; // Set PORTD as output
_delay_ms(20);
lcd_command(0x02); // 4-bit mode
lcd_command(0x28); // 2 lines, 5x7 matrix
lcd_command(0x0C); // Display ON
lcd_command(0x06); // Auto increment cursor
lcd_command(0x01); // Clear display
_delay_ms(2);
}
void lcd_print_number(unsigned int num) // Function to print number
{
char buffer[6]; // Buffer to store digits
itoa(num, buffer, 10); // Convert integer to string
for(int i=0; buffer[i]!=0; i++)
lcd_data(buffer[i]); // Display each digit
}
//---------------- TIMER1 INTERRUPT ----------------//
ISR(TIMER1_OVF_vect) // Timer1 overflow interrupt service
routine
{
TCNT1 = 34286; // Reload timer for 1 second delay
counter++; // Increment counter
}
//---------------- TIMER INITIALIZATION ----------------//
void timer1_init()
{
TCCR1A = 0x00; // Normal mode
TCCR1B = (1<<CS12); // Prescaler = 256
TCNT1 = 34286; // Load initial value for 1 sec
TIMSK = (1<<TOIE1); // Enable Timer1 overflow interrupt
sei(); // Enable global interrupts
}
//---------------- MAIN FUNCTION ----------------//
int main(void)
{
lcd_init(); // Initialize LCD
timer1_init(); // Initialize Timer1
while(1)
{
lcd_command(0x80); // Move cursor to first row
lcd_print_number(counter); // Display counter value
_delay_ms(100); // Small delay to avoid flicker
}
}
✅ LCD Output Example
0
1
2
3
...
Counter increments every 1 second automatically using Timer1 interrupt.
5. Write an AVR c Program to interface a 16*2 LCD (in 4-bit mode) with an AVR
microcontroller (eg. ATmega16/ATmega32). The program should a) Initialize the LCD in 4-bit
mode. b) Display the message ‘AVR LCD TEST’.
Ans:.
The program:
✅ Initializes LCD in 4-bit mode
✅ Displays “AVR LCD TEST”
✅ Assumptions
Microcontroller: ATmega16 / ATmega32
Clock Frequency: 8 MHz
LCD Connections:
LCD Pin AVR Pin
RS PD0
EN PD1
D4 PD4
D5 PD5
D6 PD6
D7 PD7
RW GND
✅ AVR C Program
#define F_CPU 8000000UL // Define CPU frequency as 8 MHz
#include <avr/io.h> // Include AVR I/O header file
#include <util/delay.h> // Include delay library
#define RS PD0 // Define RS pin connected to PD0
#define EN PD1 // Define EN pin connected to PD1
//--------------- Function to Send Command ----------------//
void lcd_command(unsigned char cmd)
{
PORTD = (cmd & 0xF0); // Send higher nibble (D4-D7)
PORTD &= ~(1<<RS); // RS = 0 (Command mode)
PORTD |= (1<<EN); // EN = 1
_delay_ms(1);
PORTD &= ~(1<<EN); // EN = 0
PORTD = (cmd << 4); // Send lower nibble
PORTD &= ~(1<<RS); // RS = 0
PORTD |= (1<<EN); // EN = 1
_delay_ms(1);
PORTD &= ~(1<<EN); // EN = 0
}
//--------------- Function to Send Data ----------------//
void lcd_data(unsigned char data)
{
PORTD = (data & 0xF0); // Send higher nibble
PORTD |= (1<<RS); // RS = 1 (Data mode)
PORTD |= (1<<EN); // EN = 1
_delay_ms(1);
PORTD &= ~(1<<EN); // EN = 0
PORTD = (data << 4); // Send lower nibble
PORTD |= (1<<RS); // RS = 1
PORTD |= (1<<EN); // EN = 1
_delay_ms(1);
PORTD &= ~(1<<EN); // EN = 0
}
//--------------- LCD Initialization in 4-bit Mode ----------------//
void lcd_init()
{
DDRD = 0xFF; // Set PORTD as output
_delay_ms(20); // Wait for LCD to power ON
lcd_command(0x02); // Initialize LCD in 4-bit mode
lcd_command(0x28); // 2 lines, 5x7 matrix in 4-bit mode
lcd_command(0x0C); // Display ON, Cursor OFF
lcd_command(0x06); // Entry mode (cursor increment)
lcd_command(0x01); // Clear display
_delay_ms(2); // Delay after clear
}
//--------------- Main Function ----------------//
int main(void)
{
lcd_init(); // Initialize LCD
lcd_command(0x80); // Move cursor to first row, first column
char message[] = "AVR LCD TEST"; // Store message
for(int i = 0; message[i] != 0; i++) // Loop until end of string
{
lcd_data(message[i]); // Display each character
}
while(1); // Infinite loop
}
✅ Expected LCD Output
AVR LCD TEST
(Display appears on first row of LCD)