Counter based on STC51 microcontroller

Publisher:自由探索者Latest update time:2024-04-15 Source: elecfansKeywords:STC51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Design requirements:#

Write a driver for the matrix keyboard


Press the corresponding keys respectively. The digital tube will accumulate the corresponding values ​​of the keyboard and display


Design Overview:

According to the design requirements, the required single-chip microcomputer chip is STC89C52, and the hardware tool used is an intelligent car based on STC89C52 developed by Huaqing Yuanjian. The car is equipped with the required matrix key module and digital tube module. The matrix key module is controlled by P3 port, the digital tube module bit selection is controlled by P2.7 port, the segment selection is controlled by P2.6 port, and the digital display is controlled by P0 port. STC89C52 is a low-power, high-performance 8-bit microcontroller, which is an enhanced version of the 80C51 single-chip microcomputer.


Matrix keyboard: 4x4 matrix keyboard has 16 keys. The left end of each key is connected to a line to form a row line, and the right end of each key is also connected to a line to form a column line. The lower 4 bits of P3 port are connected to the row line, and the upper 4 bits are connected to the column line. The keys in the matrix keyboard are accurately identified by scanning the level changes at both ends of the keys.


source code:#

#include

#define uint unsigned int

#define uchar unsigned char


sbit WELA = P2^7; //define bit select IO port

sbit DULA = P2^6; //define segment select IO port


void matrix_key_scan();

void delay_ms(uint ms);

void sum_key();

void display_num(unsigned int num);

void display_digit(unsigned char wela,unsigned char dula);


uint key_value = 0; //Define global variables to save key values

uint keynum1 = 0;

uint keynum2 = 0;


//Digital tube 0-9 segment selection code

uchar code Du[] = {0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f};


/*-----Matrix keyboard scanning function-----*/

void matrix_key_scan()

{

uchar temp;

uchar a = 0;

P3 = 0x0f; // Pull all row levels high and column levels low to perform row scanning

temp = P3;

if(temp != 0x0f) // Check if a key is pressed

{

delay_ms(5); //delay debounce

if(temp != 0x0f) // confirm the button is pressed again

{

P3 = 0x0f; // Pull all row levels high and column levels low to perform row scanning

temp = P3;

switch(temp)

{

case 0x0e:key_value = 0; //Line 1

break;

case 0x0d:key_value = 4; //Line 2

break;

case 0x0b:key_value = 8; //Line 3

break;

case 0x07:key_value = 12; //Line 4

break;

}

P3 = 0xf0; //Pull all column levels high and row levels low to scan columns

temp = P3;

switch(temp)

{

case 0xe0:key_value = key_value + 0; //Column 1

break;

case 0xd0:key_value = key_value + 1; // Column 2

break;

case 0xb0:key_value = key_value + 2; //Column 3

break;

case 0x70:key_value = key_value + 3; // Column 4

break;

//Here a delay of 200ms is required because the matrix keyboard scanning function is continuously executed in the while loop

//If you do not release the key after pressing it or release it slowly, the keynum2 variable will continue to accumulate,

delay_ms(200);

keynum1 = key_value;![image](https://img2020.cnblogs.com/blog/2427665/202109/2427665-20210902095257265-732916178.jpg)

keynum2 = keynum2 + keynum1;


}

}

while((a < 50)&&(temp != 0xf0))//Wait for the key to be released

{

a++;

}

}


/*-----Millisecond delay function-----*/

void delay_ms(uint ms)

{

volatile uint i,j;

for(i = ms;i > 0;i--)

{

for(j=0;j<110;j++);

}

}


/*--------Digital tube display digital function---------*/

void display_num(unsigned int num)

{

unsigned int div = 0;

unsigned char rema = 0;

unsigned char index = 0;

while((div = num / 10) > 0)

{

rema = num % 10;

display_digit(index,rema);

num = div;

index++;

delay_ms(2);

}

rema = num % 10;

display_digit(index,rema);

}


/*-------Digital tube position selection function-------*/

void display_digit(unsigned char wela,unsigned char dula)

{

WELA = 1; // The bit select latch is set high and data is sent

P0 = 0xFF; //Erasing

P0 &= ~(1 << (7 - wela));//1000 0000 0111 1111

WELA = 0; // Set the bit select latch low to save data


DULA = 1; // The segment select latch is set high and data is sent

P0 = Du[dula]; //Send data

DULA = 0; // The segment select latch is set low to save data

}


void main()

{

while(1)

{

matrix_key_scan();

display_num(keynum2);

}

}

Schematic diagram of the car: #

image

image


Keywords:STC51 Reference address:Counter based on STC51 microcontroller

Previous article:Software simulation to implement the iic protocol (51 as an example)
Next article:Fan based on STC51 microcontroller

Recommended ReadingLatest update time:2024-11-16 09:54

STC51 from entry to mastery (compilation) ~~~ Lecture 1: Overview
1.1 The development history of single chip microcomputer: Microcontroller: A fully functional microcomputer that integrates a microprocessor, semiconductor memory, I/O interface and interrupt system on a single silicon chip. The development of single chip microcomputer can be summarized into the following aspects:
[Microcontroller]
STC51 from entry to mastery (compilation) ~~~ Lecture 1: Overview
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号