void key_task();
extern uchar condition;
//È«¾Ö±äÁ¿
char yun_flag='+';//ÔËËã·ûºÅ yun_flag £¨Ä¬ÈÏΪ+ £©
char data c_num[15]=" ";//c_num1 ×Ö·ûÐ͵ÚÒ»²Ù×÷Êý (Êý×é²»¶¨Òå´óСÓëÆäËû±äÁ¿³åÍ»)
//char data c_num2[15]=" ";//c_num2 ×Ö·ûÐ͵ڶþ²Ù×÷Êý
char error;//ÔËËã·¨Ôò´íÎó£»
void refresh();
//=====================================================
void DelayUs(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1; /*12MHzƵÂÊ*/
while(--uscnt);
}
/******************************************************************/
/* ºÁÃ뺯ÊýÉùÃ÷ */
/******************************************************************/
void DelayMs(unsigned char ms)
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
//==============================================
long my_num3 = 0;
long my_num2 = 0;
long my_num1 = 0;
char my_num_len = 1;
char num_flag = 0;
unsigned char operation_num = 0;
char long_to_char(long a,char* p)
{
char length;
sprintf(p,"%ld",a);
length = strlen (p);
return length;
}
void refresh(){
//LCD_write_str(0,1,"1234567890123456");
LCD_write_str(0,0," ");
LCD_write_str(0,1," ");
my_num_len = long_to_char(my_num2,c_num);//num2תΪ×Ö·ûÐÍ
if(error==0)
{
if(num_flag == 0){
//my_num_len = long_to_char(my_num2,c_num);//num2תΪ×Ö·ûÐÍ
LCD_write_str(16-my_num_len,2,c_num);
}else if(num_flag == 1){
LCD_write_str(16-my_num_len,2,c_num);
my_num_len = long_to_char(my_num1,c_num);//num1תΪ×Ö·ûÐÍ
LCD_write_str(16-my_num_len ,0,c_num);
LCD_write_char(1,15,yun_flag);
}else{
my_num_len = long_to_char(my_num1,c_num);//num1תΪ×Ö·ûÐÍ
LCD_write_str(0 ,0,c_num);
LCD_write_char(my_num_len ,0,yun_flag);
yun_flag = '=';
long_to_char(my_num2,c_num);
LCD_write_str(my_num_len + 1 ,0,c_num);
my_num_len = long_to_char(my_num3,c_num);//num1תΪ×Ö·ûÐÍ
LCD_write_str(16-my_num_len ,1,c_num);
LCD_write_char(16-my_num_len-1,1,'=');
}
}else
{
LCD_write_str(0,0,"error!!!");
}
}
void my_operation2(char keynum)//ÊäÈëÊý×Ö
{
unsigned char Data1;
if(keynum==1)Data1=1;//·ÖÎöÊäÈëÊý×Ö
if(keynum==2)Data1=2;
if(keynum==3)Data1=3;
if(keynum==5)Data1=4;
if(keynum==6)Data1=5;
if(keynum==7)Data1=6;
if(keynum==9)Data1=7;
if(keynum==10)Data1=8;
if(keynum==11)Data1=9;
if(keynum==14)Data1=0;
my_num2=my_num2*10+Data1;
refresh();
}
void my_operation_clear()//ÊäÈëclear all
{
my_num1=0;// num1ÇåÁã
my_num2=0;//num2ÇåÁã
operation_num = 0;
yun_flag=' ';// yun_flag(Ϊ'+')
error=0;//Çåerror
num_flag = 0;
refresh();//¸üÐÂÒº¾§
}
void my_operation(char keynum)//°´Ï ¡¯+¡¢-¡¢*¡¢/´¦Àí
{
if(operation_num > 1)//Éϴΰ´¼üΪ Êý×Ö¡¢Ð¡Êýµã¡¢+/-¡¢sqrt
{
if(yun_flag=='+')my_num2=my_num1+my_num2; //°´ÏµÄÊÇ¡®+¡¯
if(yun_flag=='-')my_num2=my_num1-my_num2;//°´ÏµÄÊÇ¡®-¡¯
if(yun_flag=='*')my_num2=my_num1*my_num2;//°´ÏµÄÊÇ¡®*¡¯
if(yun_flag=='/')//°´ÏµÄÊÇ¡®/¡¯
{
if(my_num2!=0){
my_num2=my_num1/my_num2;
}
else{
error=1;
}
}
}
if(yun_flag=='='){
my_num1 = my_num3;
}else{
my_num1 = my_num2;
}
if(keynum==4)yun_flag='+';//yun_flag¸üÐÂ
if(keynum==8)yun_flag='-';
if(keynum==12)yun_flag='*';
if(keynum==16)yun_flag='/';
my_num2=0;
//len=0;
//key_last=0;//key_last¸üÐÂ
refresh();
}
void operation_for_equal()//ÊäÈë'='
{
if(yun_flag=='+')my_num3=my_num1+my_num2; //°´ÏµÄÊÇ¡®+¡¯
if(yun_flag=='-')my_num3=my_num1-my_num2;//°´ÏµÄÊÇ¡®-¡¯
if(yun_flag=='*')my_num3=my_num1*my_num2;//°´ÏµÄÊÇ¡®*¡¯
if(yun_flag=='/')//°´ÏµÄÊÇ¡®/¡¯
{
if(my_num2!=0){
my_num3=my_num1/my_num2;
}
else{
error=1;
}
}
//yun_flag='=';//yun_flag¸üÐÂ
operation_num = 0;
refresh();
}
void xiaoshucong_key_task(unsigned char key){
switch(key){
case 4 :
case 8 :
case 12:
case 16://+¡¢-¡¢*¡¢/
if(error==0){
num_flag = 1;
operation_num++;
my_operation(key);
}
break;
case 1 : //Êý×Ö1
case 2 : //Êý×Ö2
case 3 : //Êý×Ö3
case 5 : //Êý×Ö4
case 6 : //Êý×Ö5
case 7 : //Êý×Ö6
case 9 : //Êý×Ö7
case 10 : //Êý×Ö8
case 11 : //Êý×Ö9
case 14 : //Êý×Ö0
if(error==0){
if(yun_flag == '='){
yun_flag = ' ';
my_num2 =0;
num_flag = 0;
}
my_operation2(key);
}
break;
case 13 :// clear all
my_operation_clear();
break;
case 15 :// '='
if(error==0){
num_flag = 2;
operation_for_equal();
}
break;
}
}
void main(){
char key_num = 0;
LCD_init();
refresh();
while(1){
key_num = KeyDriver();
if(key_num){
xiaoshucong_key_task(key_num);
}
}
}