Preface
The four articles in the series are the four learning records I have written since I started learning microcontrollers. After understanding the above knowledge, I have also mastered some of the programming ideas of the 80C51 microcontroller. Of course, the 80C51 can mount many different chips and devices, and there is still a lot to learn. For now, I have a basic understanding of the use of I/O devices, and I usually write programs while learning, which makes my small goal - Make a calculator , I have a certain foundation. So I took advantage of the time today to record the process of implementing this calculator.
[51 single-chip microcomputer] matrix keyboard line-by-line scanning method simulation experiment + ultra-detailed Proteus simulation and Keil operation steps and [51 single-chip microcomputer] seven-segment digital tube display experiment + detailed explanation , which will not be repeated here.
2. Software Program
Most of the program is similar to the line reversal method + dynamic display program. The main changes are the keyboard scanning program and the display program, and some codes are optimized. The following are the changed parts.
The code is as follows:
1) Initialization
#include
int ans=0;
int newnum=0;
char op='';
int b[4]={12,12,12,0};
int cs[4]={0x07,0x0B,0x0D,0x0E};
int digit[13]={0x3f,0x06,0x5B,0x4f,0x66,0x6D,0x7D,0x07,0x7f,0x6f,0x71,0x40,0x00}; //Display 0~9, 'F', '-' and no display on the digital tube
int p1line[4]={0xf7,0xfb,0xfd,0xfe}; //The row value of the keyboard in the progressive scanning method
2) Main program (keyboard scanning program)
void main(void){
unsigned int key1=0;
unsigned int key2=0;
unsigned int key=0;
int i;
show(0);
while (1){
key=key1=key2=0;
P1=0xf0;
key1=P1&0xf0;
if(key1!=0xf0){
delayms(1);
for(i=0;i<4;i++){
P1=p1line[i];
key2=P1&0xf0;
if(key2!=0xf0){
key=(p1line[i]&0x0f)|key1;
break;
}
}
while((P1&0xf0)!=0xf0);
act(key);
}
}
}
3) Define the function of the button
void act(){
switch(key){
case 0x77:clear();turnLight();break;//clear key, clear + marquee
case 0xB7:savenum(0);break;//Press number 0, save number 0
case 0xD7:output();break;//Press the equal sign to output the result
case 0xE7:saveop('+');break;//Press the operator and save the symbol
case 0x7B:savenum(1);break;
case 0xBB:savenum(2);break;
case 0xDB:savenum(3);break;
case 0xEB:saveop('-');break;
case 0x7D:savenum(4);break;
case 0xBD:savenum(5);break;
case 0xDD:savenum(6);break;
case 0xED:saveop('*');break;
case 0x7E:savenum(7);break;
case 0xBE:savenum(8);break;
case 0xDE:savenum(9);break;
case 0xEE:saveop('/');break;
}
}
4) Other functions
The main programs are the main program, keyboard scanning program, interrupt service program and key function configuration function. Other functions can be modified according to your needs.
① Delay program:
Previous article:[51 MCU] DAC0832 makes a signal generator (square wave, triangle wave, sawtooth wave)
Next article:[51 MCU] Seven-segment digital tube display experiment + detailed explanation
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- ONENET platform DTLS encryption mentioned the boot machine and access machine. What are the boot machine and access machine? What is the difference between them?
- 【phyBOARD-i.MX 8M Plus Development Board】Part 2: Powering on the Development Board and Evaluating the Development Environment
- 22 Ways to Prevent EMI When Designing Power Supplies
- 【NXP Rapid IoT Review】Hello Touch
- RISC-V MCU Application Development Series Tutorial CH32V103
- Let's take a look
- 【TI Wireless】Micro Dual-Mode Wireless Receiver
- Let’s talk about whether Huawei can survive in the end.
- Can the network cable be directly soldered to the PCB without a crystal plug or can the network cable be plugged into the circuit board with terminals?
- [Revenge RVB2601 creative application development] helloworld_beginner's guide to debugging methods and processes