//1 bit calculator
#include "lcd.h"
#include "operation.h"
void setup() {
// put your setup code here, to run once:
init();
}
void loop() {
// put your main code here, to run repeatedly:
volatile char *port_d =(volatile char*)0x2A;
volatile char *port_a =(volatile char*)0x21;
volatile char *port_c =(volatile char*)0x27;
volatile char *row =(volatile char*)0x2B;
volatile char *col =(volatile char*)0x20;
volatile char *seg =(volatile char*)0x28;
volatile char x,j,k,count=0,num1,op,num2,result=0,rs;
volatile long i;
volatile int arr[]={0x01,0x02,0x04,0x08};
volatile int arr1[]={1,2,3,'+'};
volatile int arr2[]={4,5,6,'-'};
volatile int arr3[]={7,8,9,'*'};
volatile int arr4[]={'=','0','0','/'};
volatile int digit[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d};
x = *col;
while(1)
{
for(k=0;k<4;k++) //row scan
{
*row = 1 << k; //row scan
if(x=*col) {count++;
if (*row == 1)
for(j=0;j<4;j++)
{
x = *col;
if((x & arr[j]) == arr[j])
{
if(count == 1){ num1 = arr1[j];}
if(count == 2){ op = arr1[3];}
if(count == 3){ num2 = arr1[j];}
}
}
if (*row == 2)
for(j=0;j<4;j++)
{
x = *col;
if((x & arr[j]) == arr[j])
{
if(count == 1){ num1 = arr2[j];}
if(count == 2){ op = arr2[3];}
if(count == 3){ num2 = arr2[j];}
}
}
if (*row == 4)
for(j=0;j<4;j++)
{
x = *col;
if((x & arr[j]) == arr[j])
{
if(count == 1){ num1 = arr3[j];}
if(count == 2){ op = arr3[3];}
if(count == 3){ num2 = arr3[j];}
}
}
if (*row == 8)
for(j=0;j<4;j++)
{
x = *col;
if((x & arr[j]) == arr[j])
{
if(count == 2){ op = arr4[3];}
if(count >=4){ rs = arr4[0];}
}
}
if(count == 0)*seg = digit[0];
if(count == 1)*seg = digit[1];
if(count == 2)*seg = digit[2];
if(count == 3)*seg = digit[3];
if(count == 4)*seg = digit[4];
lcd_configuration(0x02);
lcd_configuration(0x01);
lcd_configuration(0x0c);
display(num1+0x30);
lcd_configuration(0x0c);
display(op);
lcd_configuration(0x0c);
display(num2+0x30);
lcd_configuration(0x06);
display(rs);
if(op == arr1[3])result = add(num1,num2);
if(op == arr2[3])result = sub(num1,num2);
if(op == arr3[3])result = mul(num1,num2);
if(op == arr4[3])result = division(num1,num2);
if(rs == arr4[0])
{
lcd_configuration(0x06);
if(result >= 10)
{
volatile char temp;
temp = result;
result=temp/10;
display(result+0x30);
result=temp%10;
display(result+0x30);
break;
}
display(result+0x30);
}
}
}
}
}