How to build a calculator using Pic16f877 microcontroller


How to build a calculator using Pic16f877 microcontroller

Calculator using pic microcontroller – Project code

Code portion is little bit difficult. But if you are familiar with syntax of C++ language and did some good work in c++ then the code below is easy for you to understand and to modify. First htc.h header file is included in the project. This header file must be included in every project that is using HIGH-TECH C compiler for compiling the code. Since i am using HIGH-TECH C compiler so i included it. Then the statement _XTAL_FREQ 20e6 is defining our crystal frequency. Which is 20 MHz. Next i defined Port-C & D pins for 16Γ—2 lcd and keypad interface. Each port pin is also given a meaning full name. After that some functions are defined. Then some strings are defined. These strings are displayed on 16Γ—2 lcd display. The strings are used to communicate with the user.

Functions which are part of pic microcontroller calculator code

void main()
Main Function the heart of the code. Main function executes first. All other functions executes after it.
void lcdcmd  (unsigned char)
This function sends commands and controls lcds registers to execute the command properly.  
void lcddata (unsigned char)
This function sends data to lcd display and controls lcds registers to display data on 16Γ—2 lcd.
void disp_num(float num)
This function displays calculated value or output result on 16Γ—2 lcd display efficiently. 
int get_num  (char ch)
This function converts character value to integer. To display integer value on 16Γ—2 lcd it must first be converted in to character format. On 16Γ—2 lcd we can display a character 8-bit value.
void lcdinit ()
This function initializes the character lcd display. 16Γ—2 lcd, font-size 7Γ—5, cursor blinking etc.
char scan_key(void)
This functions checks which keypad key is pressed by the user.

Each and every Statement of the code is well commented. Go through the code and if you feel any problem in any statement just leave your queries in the comments section below.

  #define _XTAL_FREQ 20e6
  #define rs RD6
  #define en RD7
  #define r0 RC0
  #define r1 RC1
  #define r2 RC2
  #define r3 RC3
  #define c0 RC4
  #define c1 RC5
  #define c2 RC6
  #define c3 RC7
  void lcdcmd (unsigned char);
  void lcddata (unsigned char);
  void disp_num(float num);
  int get_num (char ch);
  void lcdinit ();
  char scan_key(void);
  unsigned char s[]={β€œENTER 1 NO= β€œ};
  unsigned char s1[]={β€œENTER 2 NO= β€œ};
  unsigned char s2[]={β€œOPERATOR = β€œ};
  unsigned char s3[]={β€œ***RESULT***β€œ};
  void lcdinit(){
  __delay_ms(400); lcdcmd(0x30); __delay_ms(400); lcdcmd(0x30);
  __delay_ms(400); lcdcmd(0x30); __delay_ms(400); lcdcmd(0x38);
  lcdcmd(0x0F); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80);
  void main (void)
  TRISC=0xF0; //Rows Output, Coulombs Input
  TRISB=0x00; //Port-B as Output
  TRISD6=0; //Port-D PIN-6 as Output
  TRISD7=0; //Port-D PIN-7 as Output
  unsigned int count=0;
  int k2,k1;
  char ke,key,key1;
  lcdinit(); //Initializing Lcd
  while(s[count]!=β€˜\0β€˜) //Displaying String s on LCD
  ke=scan_key(); //Scan the First Digit
  k2=get_num(ke); //Converting Char into number
  lcdcmd(0x01); //Clear Lcd
  while(s2[count]!=β€˜\0β€˜) //Displaying String s2 on LCD
  key=scan_key(); //Scaning operator
  lcdcmd(0x01); //Cleat Lcd
  while(s1[count]!=β€˜\0β€˜) //Displaying String s1 on LCD
  key1=scan_key(); //Scan Second digit
  k1=get_num(key1); //Converting Char into number
  lcdcmd(0x01); //Clear Lcd
  lcdcmd(0x82); //Start displying data on lcd at position Row=1 Coulomb=3
  while(s3[count]!=β€˜\0β€˜) //Displaying String s3 on LCD
  lcdcmd(0xC0); //Jump to second Line of Lcd
  lcddata(β€˜ β€˜);
  case β€˜+β€˜:
  case β€˜β€“β€˜:
  case β€˜*β€˜:
  case β€˜/β€˜:
  void lcdcmd(unsigned char value) //Sending Commands to Lcd
  PORTB = value;
  rs = 0;
  en = 1;
  en = 0;
  void lcddata(unsigned char value) //Sending Data to Lcd
  PORTB = value;
  rs = 1;
  en = 1;
  en = 0;
  char scan_key() //Scan the Pressed Key by user
  unsigned char c=β€˜sβ€˜;
  if(c0==1 && r0==0){ lcddata(β€˜7β€˜);__delay_ms(500);return β€˜7β€˜;c=β€˜aβ€˜;}
  if(c1==1 && r0==0){ lcddata(β€˜8β€˜);__delay_ms(500);return β€˜8β€˜;c=β€˜aβ€˜;}
  if(c2==1 && r0==0){ lcddata(β€˜9β€˜);__delay_ms(500);return β€˜9β€˜;c=β€˜aβ€˜;}
  if(c3==1 && r0==0){ lcddata(β€˜/β€˜);__delay_ms(500);return β€˜/β€˜;c=β€˜aβ€˜;}
  if(c0==1 && r1==0){ lcddata(β€˜4β€˜);__delay_ms(500);return β€˜4β€˜;c=β€˜aβ€˜;}
  if(c1==1 && r1==0){ lcddata(β€˜5β€˜);__delay_ms(500);return β€˜5β€˜;c=β€˜aβ€˜;}
  if(c2==1 && r1==0){ lcddata(β€˜6β€˜);__delay_ms(500);return β€˜6β€˜;c=β€˜aβ€˜;}
  if(c3==1 && r1==0){ lcddata(β€˜*β€˜);__delay_ms(500);return β€˜*β€˜;c=β€˜aβ€˜;}
  if(c0==1 && r2==0){ lcddata(β€˜1β€˜);__delay_ms(500);return β€˜1β€˜;c=β€˜aβ€˜;}
  if(c1==1 && r2==0){ lcddata(β€˜2β€˜);__delay_ms(500);return β€˜2β€˜;c=β€˜aβ€˜;}
  if(c2==1 && r2==0){ lcddata(β€˜3β€˜);__delay_ms(500);return β€˜3β€˜;c=β€˜aβ€˜;}
  if(c3==1 && r2==0){ lcddata(β€˜β€“β€˜);__delay_ms(500);return β€˜β€“β€˜;c=β€˜aβ€˜;}
  if(c1==1 && r3==0){ lcddata(β€˜0β€˜);__delay_ms(500);return β€˜0β€˜;c=β€˜aβ€˜;}
  if(c3==1 && r3==0){ lcddata(β€˜+β€˜);__delay_ms(500);return β€˜+β€˜;c=β€˜aβ€˜;}
  return 0;
  int get_num(char ch) //converting character into integer
  case β€˜0β€˜: return 0; break;
  case β€˜1β€˜: return 1; break;
  case β€˜2β€˜: return 2; break;
  case β€˜3β€˜: return 3; break;
  case β€˜4β€˜: return 4; break;
  case β€˜5β€˜: return 5; break;
  case β€˜6β€˜: return 6; break;
  case β€˜7β€˜: return 7; break;
  case β€˜8β€˜: return 8; break;
  case β€˜9β€˜: return 9; break;
  return 0;
  void disp_num(float num) //Displays calculated value on LCD
  unsigned char UnitDigit = 0; //Contains unit digit of calculated value
  unsigned char TenthDigit = 0; //contains 10th digit of calculated value
  unsigned char decimal = 0;
  int j,numb;
  numb = –1*numb; // Make number positive
  lcddata(β€˜β€“β€˜); // Display a negative sign on LCD
  TenthDigit = (numb/10); // Findout Tenth Digit
  if( TenthDigit != 0) // If it is zero, then don’t display
  lcddata(TenthDigit+0x30); // Make Char of TenthDigit and then display it on LCD
  UnitDigit = numb – (TenthDigit*10);
  lcddata(UnitDigit+0x30); // Make Char of UnitDigit and then display it on LCD
  decimal=(j%10)+0x30; //Display If any value after Decimal Point
More projects about calculators involving different other microcontrollers. Each project is made with 16Γ—2 lcd display, 4Γ—4 keypad and an intelligent unit, the microcontroller. Every project is open source. One can modify and use code according to his/her needs.  
Download the project files, code(C, Hex) and simulation. Code is written is C Language using MP-LAB IDE and High-tech C compiler is used to compile the code. Simulation is made in Proteaus 8.0. Watch Project Video Below. Plz Give us your feed back on the project.

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.