【51 MCU】Comprehensive experiment of seven-segment digital tube and matrix keyboard——Calculator

Publisher:mu22Latest update time:2024-04-07 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

[1] [2]
Reference address:【51 MCU】Comprehensive experiment of seven-segment digital tube and matrix keyboard——Calculator

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

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号