SlideShare a Scribd company logo
1 of 13
Basic Standard Calculator 
(Using ATMega16 Microcontroller) 
Aim of this project is to design a calculating device capable of 
performing basic calculations (add, subtract, multiply, divide) on two 
operands. 
Project overview 
Basic Standard Calculator
Hardware components required 
1. ATMega 16 microcontroller 
2. Basic calculator keypad 
3. 2x16 LCD 
Keypad and LCD are connected with microcontroller to make an easy man-machine 
interface to make the calculator capable of performing the desired 
function. 
 ATMega16 microcontroller: This is the brain of system which is 
programmed to take input from user through keypad, perform the 
desired operation and then display the result on the provided 2x16 LCD. 
 Basic calculator keypad: This is a 4x4 (having 4 rows and 4 columns) 
keypad which is interfaced with microcontroller with its each key 
assigned a specific no. or operator defined in the program. 
 2x16 LCD: this is the liquid crystal display module capable of 
displaying 32 characters in two rows (16 in each row). Microcontroller 
displays characters on it while taking inputs from user and to display the 
result to user.
Interfacing keypad with microcontroller 
Keyboard Connections to PORTS 
With matrix keypads 16 keys can function with 8 pins (4 rows and 4 columns) 
of microcontroller’s either same or different ports as convenient.
Scanning and identifying the key pressed
Function to identify a key pressed 
//connect colum with lower nibble and rows with upper nibble 
#define key_port PORTA 
#define key_ddr DDRA 
#define key_pin PINA 
unsigned char keypad[4][4]={'7','8','9','/', 
'4','5','6','*', 
'1','2','3','-', 
'c','0','=','+'}; 
char takekey() 
{ 
unsigned char row,colum; 
char key; 
key_ddr=0xf0; 
key_port=0xff; 
do 
{ 
key_port&=0x0f; 
colum=(key_pin&0x0f); 
}while(colum!=0x0f); 
do 
{ 
do 
{ 
_delay_ms(1); 
key_port&=0x0f; 
colum=(key_pin&0x0f); 
}while(colum==0x0f); 
_delay_ms(1); 
key_port&=0x0f;
colum=(key_pin&0x0f); 
}while(colum==0x0f); 
while(1) 
{ 
key_port=0xef; 
colum=(key_pin&0x0f); 
if(colum!=0x0f) 
{ 
row=0; 
break; 
} 
key_port=0xdf; 
colum=(key_pin&0x0f); 
if(colum!=0x0f) 
{ 
row=1; 
break; 
} 
key_port=0xbf; 
colum=(key_pin&0x0f); 
if(colum!=0x0f) 
{ 
row=2; 
break; 
} 
key_port=0x7f; 
colum=(key_pin&0x0f); 
row=3; 
break; 
}
if(colum==0x0e) 
key=keypad[row][0]; 
else if(colum==0x0d) 
key=keypad[row][1]; 
else if(colum==0x0b) 
key=keypad[row][2]; 
else 
key=keypad[row][3]; 
return(key); 
} 
Interfacing 2x16 LCD with microcontroller (in 4 bit mode) 
LCD interfacing with atmega 16
LCD functions in accordance with above figure: 
#define en PA2 // enable signal 
#define rw PA1 // read/write signal 
#define rs PA0 // register select signal 
void lcd_cmd(unsigned char cmd) 
{ 
DDRA=0xff; 
PORTA=0; 
PORTA=cmd&0xf0; 
lcd &=~(1<<rs); 
lcd &=~(1<<rw); 
lcd |= (1<<en); 
_delay_ms(1); 
lcd &=~(1<<en); 
_delay_ms(1); 
PORTA=((cmd<<4)&0xf0); 
lcd &=~(1<<rs); 
lcd &=~(1<<rw); 
lcd |= (1<<en); 
_delay_ms(1); 
lcd &=~(1<<en); 
_delay_ms(1); 
return; 
} 
void lcd_data(unsigned char data) 
{ 
PORTA=(data&0xf0); 
lcd |=(1<<rs); 
lcd &=~(1<<rw); 
lcd |= (1<<en);
_delay_ms(1); 
lcd &=~(1<<en); 
_delay_ms(1); // delay to get things executed 
PORTA= ((data<<4)&0xf0); 
lcd |=(1<<rs); 
lcd &=~(1<<rw); 
lcd |= (1<<en); 
_delay_ms(1); 
lcd &=~(1<<en); 
return ; 
} 
void ini_lcd(void) 
{ 
_delay_ms(5); 
lcd_cmd(0x02); 
_delay_ms(1); 
lcd_cmd(0x28); 
_delay_ms(1); 
lcd_cmd(0x01); // clear LCD 
_delay_ms(1); 
lcd_cmd(0x0E); // cursor ON 
_delay_ms(1); 
lcd_cmd(0x84); 
_delay_ms(1); 
return; 
}
Calculator 
The functioning of calculator s as follows: 
i. Enter first operand 
ii. Enter operator 
iii. Enter second operand 
iv. Press result key 
Main program: 
#include<avr/io.h> 
#include<util/delay.h> 
#include"4bitlcd.h" 
#include"keypad.h" 
#define key_port PORTA 
#define key_ddr DDRA 
#define key_pin PINA 
void lcd_cmd(unsigned char cmd); 
void lcd_data(unsigned char data); 
void ini_lcd(void); 
void main() 
{ 
unsigned char ch=0,op=0; 
long o1=0,o2=0,o3=0,ch1=0; 
PORTB=0XFF; 
DDRB=0xff; 
_delay_ms(5); 
ini_lcd(); 
while(1) 
{
lcd_data('k'); 
lcd_cmd(0x01); 
_delay_ms(20); 
ch=0; 
op=0; 
ch1=0; 
o1=0; 
o2=0; 
o3=0; 
lcd_cmd(0x80); 
while((ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) //get first operand and operator 
{ 
ch=takekey(); 
ch1=ch; 
if((ch!='c')&(ch!='=')&(ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) 
{ 
lcd_data(ch); 
o1=((o1*10)+(ch1-0x30)); 
} 
else 
{ 
op=ch; 
lcd_data(ch); 
break; 
} 
} 
ch=0; 
ch1=0; 
lcd_cmd(0xc0); 
while((ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) //get second operand
{ 
ch=takekey(); 
ch1=ch; 
if((ch!='c')&(ch!='=')&(ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) 
{ 
lcd_data(ch); 
o2=((o2*10)+(ch1-0x30)); 
} 
else 
{ 
lcd_cmd(0x01); 
break; 
} 
} 
switch(op) //apply operation 
{ 
case '+': 
o3=o1+o2; 
lcd_data('a'); 
_delay_ms(5); 
break; 
case '-': 
o3=o1-o2; 
lcd_data('s'); 
_delay_ms(5); 
break; 
case '*': 
o3=o1*o2;
lcd_data('m'); 
_delay_ms(5); 
break; 
case '/': 
o3=o1/o2; 
lcd_data('d'); 
_delay_ms(5); 
break; 
default: 
lcd_cmd(0x01); 
_delay_ms(5); 
} 
lcd_cmd(0xc2); 
lcd_cmd(0x01); 
lcd_cmd(0xce); 
while(o3) //print result 
{ 
lcd_data((o3%10)+0x30); 
lcd_cmd(0x10); 
lcd_cmd(0x10); 
o3=o3/10; 
} 
ch=takekey(); 
lcd_cmd(0x01); 
_delay_ms(5); 
} 
return ; 
}

More Related Content

What's hot

What's hot (20)

DAC Interfacing with 8051.pdf
DAC Interfacing with 8051.pdfDAC Interfacing with 8051.pdf
DAC Interfacing with 8051.pdf
 
8051 programming in c
8051 programming in c8051 programming in c
8051 programming in c
 
Unit i-fundamentals of programmable DSP processors
Unit i-fundamentals of programmable DSP processorsUnit i-fundamentals of programmable DSP processors
Unit i-fundamentals of programmable DSP processors
 
CPLD xc9500
CPLD xc9500CPLD xc9500
CPLD xc9500
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
1st Semester M Tech CMOS VLSI Design (Dec-2013) Question Papers
1st Semester M Tech CMOS VLSI Design (Dec-2013) Question Papers1st Semester M Tech CMOS VLSI Design (Dec-2013) Question Papers
1st Semester M Tech CMOS VLSI Design (Dec-2013) Question Papers
 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in pic
 
Ec8791 lpc2148 timer unit
Ec8791 lpc2148 timer unitEc8791 lpc2148 timer unit
Ec8791 lpc2148 timer unit
 
Delays in verilog
Delays in verilogDelays in verilog
Delays in verilog
 
Embedded c program and programming structure for beginners
Embedded c program and programming structure for beginnersEmbedded c program and programming structure for beginners
Embedded c program and programming structure for beginners
 
programmable logic controller presentation
programmable logic controller presentationprogrammable logic controller presentation
programmable logic controller presentation
 
Microcontroller presentation
Microcontroller presentationMicrocontroller presentation
Microcontroller presentation
 
Digital Communication: Information Theory
Digital Communication: Information TheoryDigital Communication: Information Theory
Digital Communication: Information Theory
 
Plc report with project
Plc report with projectPlc report with project
Plc report with project
 
Humidity & Temperature monitoring using arduino
Humidity & Temperature monitoring using arduinoHumidity & Temperature monitoring using arduino
Humidity & Temperature monitoring using arduino
 
Fpga architectures and applications
Fpga architectures and applicationsFpga architectures and applications
Fpga architectures and applications
 
Unit 2 - Single Purpose Processors
Unit 2 - Single Purpose ProcessorsUnit 2 - Single Purpose Processors
Unit 2 - Single Purpose Processors
 
Pipelining approach
Pipelining approachPipelining approach
Pipelining approach
 
Security system using Arduino
Security system using ArduinoSecurity system using Arduino
Security system using Arduino
 
BPSK modulation using CD 4016
BPSK modulation using CD 4016 BPSK modulation using CD 4016
BPSK modulation using CD 4016
 

Similar to Basic standard calculator

Combine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdfCombine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdf
forwardcom41
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
azhar557
 

Similar to Basic standard calculator (20)

Password based door locksystem
Password  based door locksystemPassword  based door locksystem
Password based door locksystem
 
131080111003 mci
131080111003 mci131080111003 mci
131080111003 mci
 
Automatic room light controller with visible counter
Automatic room light controller with visible counterAutomatic room light controller with visible counter
Automatic room light controller with visible counter
 
SIMPLE Frequency METER using AT89c51
SIMPLE Frequency METER using AT89c51 SIMPLE Frequency METER using AT89c51
SIMPLE Frequency METER using AT89c51
 
Lcd & keypad
Lcd & keypadLcd & keypad
Lcd & keypad
 
LCD_Example.pptx
LCD_Example.pptxLCD_Example.pptx
LCD_Example.pptx
 
2022-BEKM 3453 - LCD and keypad.pdf
2022-BEKM 3453 - LCD and keypad.pdf2022-BEKM 3453 - LCD and keypad.pdf
2022-BEKM 3453 - LCD and keypad.pdf
 
ESD -DAY 24.pptx
ESD -DAY 24.pptxESD -DAY 24.pptx
ESD -DAY 24.pptx
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Customizable Microprocessor design on Nexys 3 Spartan FPGA Board
Customizable Microprocessor design on Nexys 3 Spartan FPGA BoardCustomizable Microprocessor design on Nexys 3 Spartan FPGA Board
Customizable Microprocessor design on Nexys 3 Spartan FPGA Board
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDAC
 
Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 Card
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
 
Combine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdfCombine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdf
 
The functional design scheme of the dedicated keyboard chip kb core based on ...
The functional design scheme of the dedicated keyboard chip kb core based on ...The functional design scheme of the dedicated keyboard chip kb core based on ...
The functional design scheme of the dedicated keyboard chip kb core based on ...
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
 
Experiment 16 x2 parallel lcd
Experiment   16 x2 parallel lcdExperiment   16 x2 parallel lcd
Experiment 16 x2 parallel lcd
 
PIC and LCD
PIC and LCDPIC and LCD
PIC and LCD
 
Switch Control and Time Delay - Keypad
Switch Control and Time Delay - KeypadSwitch Control and Time Delay - Keypad
Switch Control and Time Delay - Keypad
 

More from UVSofts Technologies

More from UVSofts Technologies (16)

Arduino dtmf controlled robot
Arduino dtmf controlled robotArduino dtmf controlled robot
Arduino dtmf controlled robot
 
Arduino bluetooth controlled robot
Arduino bluetooth controlled robotArduino bluetooth controlled robot
Arduino bluetooth controlled robot
 
Vehicle tracting system
Vehicle tracting systemVehicle tracting system
Vehicle tracting system
 
GPS based tracking system
GPS based tracking systemGPS based tracking system
GPS based tracking system
 
Password based door locksystem
Password  based door locksystemPassword  based door locksystem
Password based door locksystem
 
Calculator
CalculatorCalculator
Calculator
 
Bluetooth controlled robot
Bluetooth controlled robotBluetooth controlled robot
Bluetooth controlled robot
 
Pc controlled robot
Pc controlled robotPc controlled robot
Pc controlled robot
 
Bluetooth controlled robot
Bluetooth controlled robotBluetooth controlled robot
Bluetooth controlled robot
 
Pc controlled robot
Pc controlled robotPc controlled robot
Pc controlled robot
 
Mobile controll robot
Mobile controll robotMobile controll robot
Mobile controll robot
 
Mobile controlled robot
Mobile controlled robotMobile controlled robot
Mobile controlled robot
 
Edge detector robot
Edge detector robotEdge detector robot
Edge detector robot
 
Line follower robot
Line follower robotLine follower robot
Line follower robot
 
Line follower robot
Line follower robotLine follower robot
Line follower robot
 
Edge detector & avoider robot
Edge detector & avoider robotEdge detector & avoider robot
Edge detector & avoider robot
 

Recently uploaded

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (20)

Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 

Basic standard calculator

  • 1. Basic Standard Calculator (Using ATMega16 Microcontroller) Aim of this project is to design a calculating device capable of performing basic calculations (add, subtract, multiply, divide) on two operands. Project overview Basic Standard Calculator
  • 2. Hardware components required 1. ATMega 16 microcontroller 2. Basic calculator keypad 3. 2x16 LCD Keypad and LCD are connected with microcontroller to make an easy man-machine interface to make the calculator capable of performing the desired function.  ATMega16 microcontroller: This is the brain of system which is programmed to take input from user through keypad, perform the desired operation and then display the result on the provided 2x16 LCD.  Basic calculator keypad: This is a 4x4 (having 4 rows and 4 columns) keypad which is interfaced with microcontroller with its each key assigned a specific no. or operator defined in the program.  2x16 LCD: this is the liquid crystal display module capable of displaying 32 characters in two rows (16 in each row). Microcontroller displays characters on it while taking inputs from user and to display the result to user.
  • 3. Interfacing keypad with microcontroller Keyboard Connections to PORTS With matrix keypads 16 keys can function with 8 pins (4 rows and 4 columns) of microcontroller’s either same or different ports as convenient.
  • 4. Scanning and identifying the key pressed
  • 5. Function to identify a key pressed //connect colum with lower nibble and rows with upper nibble #define key_port PORTA #define key_ddr DDRA #define key_pin PINA unsigned char keypad[4][4]={'7','8','9','/', '4','5','6','*', '1','2','3','-', 'c','0','=','+'}; char takekey() { unsigned char row,colum; char key; key_ddr=0xf0; key_port=0xff; do { key_port&=0x0f; colum=(key_pin&0x0f); }while(colum!=0x0f); do { do { _delay_ms(1); key_port&=0x0f; colum=(key_pin&0x0f); }while(colum==0x0f); _delay_ms(1); key_port&=0x0f;
  • 6. colum=(key_pin&0x0f); }while(colum==0x0f); while(1) { key_port=0xef; colum=(key_pin&0x0f); if(colum!=0x0f) { row=0; break; } key_port=0xdf; colum=(key_pin&0x0f); if(colum!=0x0f) { row=1; break; } key_port=0xbf; colum=(key_pin&0x0f); if(colum!=0x0f) { row=2; break; } key_port=0x7f; colum=(key_pin&0x0f); row=3; break; }
  • 7. if(colum==0x0e) key=keypad[row][0]; else if(colum==0x0d) key=keypad[row][1]; else if(colum==0x0b) key=keypad[row][2]; else key=keypad[row][3]; return(key); } Interfacing 2x16 LCD with microcontroller (in 4 bit mode) LCD interfacing with atmega 16
  • 8. LCD functions in accordance with above figure: #define en PA2 // enable signal #define rw PA1 // read/write signal #define rs PA0 // register select signal void lcd_cmd(unsigned char cmd) { DDRA=0xff; PORTA=0; PORTA=cmd&0xf0; lcd &=~(1<<rs); lcd &=~(1<<rw); lcd |= (1<<en); _delay_ms(1); lcd &=~(1<<en); _delay_ms(1); PORTA=((cmd<<4)&0xf0); lcd &=~(1<<rs); lcd &=~(1<<rw); lcd |= (1<<en); _delay_ms(1); lcd &=~(1<<en); _delay_ms(1); return; } void lcd_data(unsigned char data) { PORTA=(data&0xf0); lcd |=(1<<rs); lcd &=~(1<<rw); lcd |= (1<<en);
  • 9. _delay_ms(1); lcd &=~(1<<en); _delay_ms(1); // delay to get things executed PORTA= ((data<<4)&0xf0); lcd |=(1<<rs); lcd &=~(1<<rw); lcd |= (1<<en); _delay_ms(1); lcd &=~(1<<en); return ; } void ini_lcd(void) { _delay_ms(5); lcd_cmd(0x02); _delay_ms(1); lcd_cmd(0x28); _delay_ms(1); lcd_cmd(0x01); // clear LCD _delay_ms(1); lcd_cmd(0x0E); // cursor ON _delay_ms(1); lcd_cmd(0x84); _delay_ms(1); return; }
  • 10. Calculator The functioning of calculator s as follows: i. Enter first operand ii. Enter operator iii. Enter second operand iv. Press result key Main program: #include<avr/io.h> #include<util/delay.h> #include"4bitlcd.h" #include"keypad.h" #define key_port PORTA #define key_ddr DDRA #define key_pin PINA void lcd_cmd(unsigned char cmd); void lcd_data(unsigned char data); void ini_lcd(void); void main() { unsigned char ch=0,op=0; long o1=0,o2=0,o3=0,ch1=0; PORTB=0XFF; DDRB=0xff; _delay_ms(5); ini_lcd(); while(1) {
  • 11. lcd_data('k'); lcd_cmd(0x01); _delay_ms(20); ch=0; op=0; ch1=0; o1=0; o2=0; o3=0; lcd_cmd(0x80); while((ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) //get first operand and operator { ch=takekey(); ch1=ch; if((ch!='c')&(ch!='=')&(ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) { lcd_data(ch); o1=((o1*10)+(ch1-0x30)); } else { op=ch; lcd_data(ch); break; } } ch=0; ch1=0; lcd_cmd(0xc0); while((ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) //get second operand
  • 12. { ch=takekey(); ch1=ch; if((ch!='c')&(ch!='=')&(ch!='+')&(ch!='-')&(ch!='*')&(ch!='/')) { lcd_data(ch); o2=((o2*10)+(ch1-0x30)); } else { lcd_cmd(0x01); break; } } switch(op) //apply operation { case '+': o3=o1+o2; lcd_data('a'); _delay_ms(5); break; case '-': o3=o1-o2; lcd_data('s'); _delay_ms(5); break; case '*': o3=o1*o2;
  • 13. lcd_data('m'); _delay_ms(5); break; case '/': o3=o1/o2; lcd_data('d'); _delay_ms(5); break; default: lcd_cmd(0x01); _delay_ms(5); } lcd_cmd(0xc2); lcd_cmd(0x01); lcd_cmd(0xce); while(o3) //print result { lcd_data((o3%10)+0x30); lcd_cmd(0x10); lcd_cmd(0x10); o3=o3/10; } ch=takekey(); lcd_cmd(0x01); _delay_ms(5); } return ; }