#include<stdlib.h>
#include<string.h>
volatile char *col_pinC=0x28; // Pointer to access rows of the keypad (Port C)
volatile char *rows_pinA=0x22; // Pointer to access columns of the keypad (Port A)
void delay() //delay function to waste time
{
volatile long i;
for(i=0;i<50000;i++);
}
void outdata(volatile char out) // sending data to lcd
{
volatile char *outputport_f;
outputport_f=0x31; //f port (d0-d7)
*outputport_f=out; //sending data on port f
}
void outcontrol(volatile char control)//to control command or data and enable
{
volatile char *outputport_k;
outputport_k=0x108;// k port (0-pin RS and 1-Enable )
*outputport_k=control; //send control data
}
void LCD_CMD(volatile char c) //send cammand to lcd
{
outdata(c); //writing on to pins
outcontrol(0x02); //RS=0,E=1
delay();
outcontrol(0x00); //RS=0,E=0
delay();
}
void init_lcd(void)//initialing lcd
{
LCD_CMD(0x38); //enable all pixels
LCD_CMD(0X0E); //display on cursor blinking
LCD_CMD(0X01); //Clear display screen
LCD_CMD(0x80); //force cursor to beginning of 1st line
}
void init_LCD_pin() //configurate the pins of lcd
{
volatile char *outputport_f,*outputport_k;
outputport_f=0x30; outputport_k=0x107;
*outputport_f=0xFF; //f port as output
*outputport_k=0x03; //k -1(RS) and 2(E) pin as output
}
void LCD_DATA(volatile char c)//to send character on lcd
{
outdata(c); //writing on pins
outcontrol(0x03);//RS=1,E=1
delay();
outcontrol(0x01);//RS=1,E=0
delay();
}
void init_keypad_pin()//configurate the pins of keypad
{
//making port C first four pins as ouput
volatile char *col_dirC;
col_dirC=0x27;
*col_dirC=0x00;
//making port A frist four as input
volatile char *row_dirA;
row_dirA=0x21;
*row_dirA=0x0F;
}
volatile char keypad_scan()//scaning row and colums to know which key is pressed
{
volatile long i;
for(i=0;i<4;i++)
{
*rows_pinA=1<<i;//set one row at a time
int a=*col_pinC;//reading colum values
switch(i)
{
case 0:
if(a==1) return '1';
if(a==2) return '2';
if(a==4) return '3';
if(a==8) return '+';
case 1:
if(a==1) return '4';
if(a==2) return '5';
if(a==4) return '6';
if(a==8) return '-';
case 2:
if(a==1) return '7';
if(a==2) return '8';
if(a==4) return '9';
if(a==8) return '*';
case 3:
if(a==1) return 'c';
if(a==2) return '0';
if(a==4) return '=';
if(a==8) return '/';
}
}
}
void setup() {
// put your setup code here, to run once:
volatile char a[10],b[10];
volatile long i,j=0,k,num[10],result=0,c,temp,sign=1,l=0;
volatile char op[10];
init_keypad_pin(); //pin configuration for key pad
init_LCD_pin();//pin configuration for LCD
init_lcd();//intialing lcd
while(1)
{
*rows_pinA=0x0F; //set all rows high
*col_pinC=0x00;//set all rows low
while(*col_pinC==0);//wait for key press
delay();//debouncing delay
while(*col_pinC==0);//to stabalize
a[i]=keypad_scan();//get the pressed key
if(a[i]=='c')// if pressed key is ac to clear everything
{
i=0;
j=0;
l=0;
result=0;
sign=1;
strcpy(op,"");
strcpy(a,"");
strcpy(b,"");
init_lcd();
}
else{
LCD_DATA(a[i]);//display the pressed key on lcd
i++;
if((i==1)&&(j==0)&&(a[i-1]=='-')) //to check frist number is negative
{
sign=-1;
strcpy(a,"");
i=0;
}
if((a[i-1]=='+')||(a[i-1]=='-')||(a[i-1]=='*')||(a[i-1]=='/')||(a[i-1]=='='))
{
int k=0;
num[j] = 0;
while(a[k]>='0'&&a[k]<='9')//convert character to number
{
num[j]=num[j]*10+(a[k]-48);
k++;
}
num[j]=num[j]*sign;//apply sign to number
sign=1;
if(a[i-1]!='=')
{
op[j]=a[i-1];
strcpy(a,"");
i=0;
j++;
}
else
{
result=num[0];
for(i=0;i<j;i++)
{
if(op[i]=='+')
{
result=result+num[i+1];
}
else if(op[i]=='-')
{
result=result-num[i+1];
}
else if(op[i]=='*')
{
result=result*num[i+1];
}
else if(op[i]=='/')
{
result=result/num[i+1];
}
}
k=0;
temp=result;
LCD_CMD(0x01);//clear lcd
if(temp==0)//display 0 if result is zero
{
LCD_DATA('0');
}
if(temp<0)//display - sign if result is negative
{
LCD_DATA('-');
temp=-temp;
}
while(temp)
{
c=temp%10;
b[k]=c+48;//convert number to character
temp=temp/10;
k++;
}
for(j=k-1;j>=0;j--)
{
LCD_DATA(b[j]);
}
i=0;
j=0;
l=0;
num[j]=result;//store result to further calculation
j++;
strcpy(op,"");
strcpy(a,"");
strcpy(b,"");
}
}
}
}
}
void loop() {
// put your main code here, to run repeatedly:
}