#include "r_lcd1602drv.h"
#include "r_matrixKeypad.h"
u8 keyValueTab[][2]={
{0xee,'1'},{0xed,'2'},{0xeb,'3'},{0xe7,'+'},
{0xde,'4'},{0xdd,'5'},{0xdb,'6'},{0xd7,'-'},
{0xbe,'7'},{0xbd,'8'},{0xbb,'9'},{0xb7,'*'},
{0x7e,'.'},{0x7d,'0'},{0x7b,'='},{0x77,'/'}
};
u8 GetKeyValue(u32 scanCode)
{
int i;
for(i=0;i<16;i++)
if(scanCode==keyValueTab[i][0])
return keyValueTab[i][1];
return 0;
}
int main()
{
r_lcd1602drv lcd;
r_matrixKeypad keypad;
uchar buf[20];
u32 scanCode;
float f=0,f1=0;
u8 datBuf[20];
u8 datBuf1[20];
u8 datBufIdx=0;
u8 datBufIdx1=0;
u8 keyValue;
u8 isPoint=0;
u8 isadd=0;
u8 isde=0;
u32 pins[]={Pin3,Pin4,Pin5,Pin6,Pin7,Pin8,Pin9,Pin10};
u32 rowPins[]={Pin5,Pin6,Pin7,Pin8};
u32 colPins[]={Pin1,Pin2,Pin3,Pin4};
InitSystem();
lcd.SetRsPin(GPIOB,Pin0);
lcd.SetRwPin(GPIOB,Pin1);
lcd.SetEnPin(GPIOB,Pin2);
lcd.SetDataPins(GPIOB,pins,8);
lcd.Init();
keypad.SetRowPort(GPIOA,rowPins,4);
keypad.SetColPort(GPIOA,colPins,4);
while(1)
{
scanCode=keypad.GetScanCode();
if(scanCode!=0)
{
if(isde==1)
{
isde=0;
datBufIdx=0;
datBufIdx1=0;
lcd.Init();
lcd.SetRowCol(0,0);
for(u8 i=0;i<20;i++)
{
datBuf[i]=0;
datBuf1[i]=0;
}
}
if((isadd=='+')|(isadd=='-')|(isadd=='*')|(isadd=='/'))
{
keyValue=GetKeyValue(scanCode);
if(keyValue>='0'&&keyValue<='9')
{
datBuf1[datBufIdx1++]=keyValue;
lcd.DispChar(keyValue);
}
if(keyValue=='.')
{
if(isPoint==0)
{
datBuf1[datBufIdx1++]=keyValue;
lcd.DispChar(keyValue);
isPoint=1;
}
}
}
if(isadd==0)
{
keyValue=GetKeyValue(scanCode);
if(keyValue>='0'&&keyValue<='9')
{
datBuf[datBufIdx++]=keyValue;
lcd.DispChar(keyValue);
}
if(keyValue=='.')
{
if(isPoint==0)
{
datBuf[datBufIdx++]=keyValue;
lcd.DispChar(keyValue);
isPoint=1;
}
}
if((keyValue=='+')|(keyValue=='-')|(keyValue=='*')|(keyValue=='/'))
{
isadd=keyValue;
isPoint=0;
lcd.DispChar(keyValue);
}
}
if(keyValue=='=')
{
sscanf((char *)datBuf,"%f",&f);
sscanf((char *)datBuf1,"%f",&f1);
switch(isadd)
{
case '+':f=f1+f;break;
case '-':f=f-f1;break;
case '*':f=f*f1;break;
case '/':f=f/f1;break;
}
lcd.SetRowCol(1,0);
sprintf((char *)buf,"%.2f",f);
lcd.DispString(buf);
isPoint=0;
datBufIdx=0;
datBufIdx1=0;
isde=1;
isadd=0;
}
}
}
} |