The mini calculator is developed using PIC16f877 microcontroller with LCD display. The programming language is mikroC. This prject will help the students to learn the designing of microcontroller based project, interfacing of keypad with microcontroller, interfacing the LCD with microcontrollers. handling the strings and their conversions to numbers will be discussed in this project.pic 16f877a calculator
The operation of the calculator is as under:
The display shows a message " CALCULATOR" on powering up the microcontroller on LCD first. Then after a few second display is changed and new message is printed " No1:" at first line of the LCD. This is clear indication that the microcontroller is ready to operate and waiting to feed first number as input.YOu can download the 16f877a microcontroller c code of the calculator project
The user should feed a positive number like 219 etc.
To feed second number press once the key "=" at the keypad. The display is updated on second line of LCD with message "No2:", yes the microcontroller based calculator is ready to accept the second number. using the keypad, user will enter the second number like 2987 etc.
Now, after entering these two number, the turn come for operator. So, the user will press key "=" once. Now the LCD is again updated with new message like "OP" at first line of LCD. You should feed required operator by pressing right key on keypad, like "+","-","*" or "\"
The result will be displayed like "res = 456" etc.
The circuit diagram of PIC Microcontroller 16f877 based calculator with LCD is as shown below:
The code or program for PIC16f877 is written in MikroC language and is presented here:
/**************************************************************
In this project a 4 x 4 keypad is connected to PORTB of a PIC18F877
microcontroller. Also an LCD is connected to PORTC. The project is a simple
calculator which can perform integer arithmetic.
The keys on keypad are labeled as follows:
.............
7 8 9 /
4 5 6 *
1 2 3 -
C 0 = +
In this program mikroC built-in functions are used.
The crystal frequency for microcontroller PIC16f877 in this project is 8MHz
****************************************************************/
#define Enter 0x0C// here it is the key "=" on keypad
#define Plus 0x0F// The "+" key
#define Minus 0x0E // The "-" key
#define Multiply 0x0B // The "*" key
#define Divide 0x0D // The "/"
unsigned key [17]={0,0x07,0x08,0x09,0x0D,0x04,0x05,
0x06,0x0B,0x01,0x02,0x03,0x0E,0x00,0x0A,0x0C,0x0F};
void main()
{
unsigned char MyKey, i,j,lcd[5],op[12];
unsigned long Calc, Op1, Op2;
TRISC = 0; // PORTC are outputs (LCD)
//
// Configure LCD
//
Lcd_Init(&PORTC); // LCD is connected to PORTC
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"CALCULATOR"); // Display CALCULATOR
Delay_ms(2000);
Lcd_Cmd(LCD_CLEAR);
//
// Configure KEYPAD
//
Keypad_Init(&PORTB); // Keypad on PORTB
//
// Program loop
//
for(;;) // Endless loop
{
MyKey = 0;
Op1 = 0;
Op2 = 0;
Lcd_Out(1,1,"No1: "); // Display No1:
while(1)
{
do // Get first number
MyKey = key[Keypad_Released()];
while(!MyKey);
// MyKey = key[MyKey];
if(MyKey == Enter)break; // If ENTER pressed
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
//Lcd_Chr_Cp(key[MyKey] + '0');
Op1 = 10*Op1 + MyKey;
}
Lcd_Out(2,1,"No2: "); // Display No2:
while(1) // Get second no
{
do
MyKey = key[Keypad_Released()]; // Get second number
while(!MyKey);
if(MyKey == Enter)break; // If ENTER pressed
if(MyKey == 10)MyKey = 0; // If 0 key pressed
Lcd_Chr_Cp(MyKey + '0');
Op2 = 10*Op2 + MyKey;
}
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"Op: "); // Display Op:
do
MyKey = key[Keypad_Released()]; // Get operation
while(!MyKey);
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"Res="); // Display Res=
switch(MyKey) // Perform the operation
{
case Plus:
Calc = Op1 + Op2; // If ADD
break;
case Minus:
Calc = Op1 - Op2; // If Subtract
break;
case Multiply:
Calc = Op1 * Op2; // If Multiply
break;
case Divide:
Calc = Op1 / Op2; // If Divide
break;
}
LongToStr(Calc, op); // Convert to string
//
// Remove leading blanks
//
j=0;
for(i=0;i<=11;i++) { if(op[i] != ' ') // If a blank { lcd[j]=op[i]; j++; } } Lcd_Out_Cp(lcd); // Display result Delay_ms(5000); // Wait 5 seconds Lcd_Cmd(LCD_CLEAR); } }




0 comments:
Post a Comment
Hi: This is PICINF.blogspot.com comments section. Here you are requested to write your question or problems in detail. Write the question in such a way that, it have all necessary information, sothat i can understand and reply.If you want to send pictures and codes then e-mail me (rghkk@hotmail.com).Thanks.