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.
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++;
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
case '+':jisuan(n,l,'+'); break; // + 18
case '-':jisuan(n,l,'-');break;// - 19
case '*':jisuan(n,l,'*');break; // * 20
case '/':jisuan(n,l,'/');break; // / 21
Reference address:Simple calculator based on 51 single chip microcomputer 12864
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?
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)
{
}
}
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])
{
}
}
else
{
write_string(0,1,"ERROR");
}
}
delayms(1);
}
while(1);
}
This is the end of the 51 program of the simple calculator
Previous article:51 single chip breathing light
Next article:Based on 51 single chip microcomputer 12864 LCD display digital function
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
Guess you like
- OpenHarmony 3.0 porting to Raspberry Pi 4B - learning record
- High internal resistance design reference, don’t understand?
- Calculation of the ripple rate r of the second inductor of the SEPIC circuit
- Common problems in designing microcontroller peripheral circuits
- Smart connected lighting and the Internet of Things
- [Chuanglong TLA40i-EVM development board] +01. Unboxing (zmj)
- Can the 66AK2L06 SoC enable miniaturization of test and measurement equipment?
- Does ThreadX open source have a big impact on other RTOS?
- Relay switching contacts do not open
- Some experience records of BLE-CC2541 project