Simple calculator based on 51 single chip microcomputer 12864

Publisher:asdfrewqppLatest update time:2015-10-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I finally wrote the calculator. Actually, I had written it a long time ago, but I was busy with exams and lab projects, so I didn't have time to write a blog. Now I am at home during the winter vacation, and I can finally learn something quietly.
   Below I will first write about the process of writing this program. In fact, this is a final project for the microcontroller class, but few people can write it well. In fact, a calculator is very complicated to consider carefully. But we only need to make a simple calculator, so my calculator only has integer addition, subtraction, multiplication and division, and negative number function. After testing, it is still practical.
The overall idea of ​​this program is very simple, and it mainly involves the processing of some details.
The idea is to first scan the matrix keyboard, monitor the operation symbols pressed, store the two operands, calculate the result, and display the whole process in real time.
First, matrix keyboard scanning. I used the method taught by Guo Tianxiang. This method is easy to understand, but the disadvantage is that the code is too long, so I went online to find a simple matrix keyboard scanning. (Compared to thinking hard, sometimes it is also good to go online to find methods)
void keyscan()
{
uchar h,l;
if(keypress())
{
 
P3=0X0F;
_nop_(); 
h=P3;
h&=0X0F; //Read the row where the key is located
 
P3=0XF0;
_nop_();
_nop_();
l=P3;
l&=0XF0; //Read the key column
key=h+l; //Finally add as the switch case selection
//This scanning process is very clear, right?
 switch(key)
 {
       case 0xEE:key=1;Input[i]='1';break;// 1
   case 0xDE:key=4;Input[i]='4';break;// 4
   case 0xBE:key=7;Input[i]='7';break;// 7
   case 0x7E:key=0;Input[i]='0';break; /// 0
   case 0xED:key=2;Input[i]='2';break;// 2
   case 0xDD:key=5;Input[i]='5';break;// 5
   case 0xBD:key=8;Input[i]='8';break; // 8
   case 0x7D:key=16;i--;break; // = 16
   case 0xEB:key=3;Input[i]='3';break; // 3
   case 0xDB:key=6;Input[i]='6';break; // 6
   case 0xBB:key=9;Input[i]='9';break; // 9
   case 0x7B: clear();break; // c 17
   case 0xE7:key=18;Input[i]='+';n=i;break; // + 18
   case 0xD7:key=19;Input[i]='-';n=i;break;// - 19
   case 0xB7:key=20;Input[i]='*';n=i;break; // * 20
   case 0x77:key=21;Input[i]='/';n=i;break; // /   21
   default:key=22;  
 
 i++;   
while(keypress());
}
}
My keyscan() function came out...
By storing the number of keys pressed each time into an array and then continuously displaying the array members, you can achieve the effect of displaying the input numbers in real time.
void jisuan(int n,int l,uchar yusuan)
{
int a=0,b=0,c=0,result;
uchar x;
for(x=0;x
{
a+=(Input[x]-'0')*pow(10,nx-1);
}
for(x=n+1;x
{
b+=(Input[x]-'0')*pow(10,lx-1);
}
// write_num(0,1,a);
// write_num(0,2,b);
switch(yusuan)
{
 
   case '+':result=a+b;write_num(0,1,result); break; // + 18
   case '-':result=ab;write_num(0,1,result);break;// - 19
   case '*':result=a*b;write_num(0,1,result);break; // * 20
   case '/':result=a/b;write_num(0,1,result);break; // /   21
}
}
This is the calculation function.
a+=(Input[x]-'0')*pow(10,nx-1);
b+=(Input[x]-'0')*pow(10,lx-1);
This is used to calculate the input value, which should be understandable
Finally, the main function:
void main()
Lcd_Init(); 
// write_string(0,3,"check ok");
while(1)
{
keyscan();
write_string(0,0,Input);
if(key==16)
{
l=i;  
if(n>0&&n!=i-1)
{
switch(Input[n])
{  
   case '+':jisuan(n,l,'+'); break; // + 18
   case '-':jisuan(n,l,'-');break;// - 19
   case '*':jisuan(n,l,'*');break; // * 20
   case '/':jisuan(n,l,'/');break; // /   21
}
}
else
{
write_string(0,1,"ERROR");
}  
}
delayms(1);
}
while(1);
}
 
 
This is the end of the 51 program of the simple calculator
Reference address:Simple calculator based on 51 single chip microcomputer 12864

Previous article:51 single chip breathing light
Next article:Based on 51 single chip microcomputer 12864 LCD display digital function

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号