Stc52 single-chip keyboard schematic and program introduction

The STC89C52RC is a low-power, high-performance CMOS 8-bit microcontroller from STC with 8K bytes of system-programmable Flash memory. The STC89C52 uses the classic MCS-51 core, but has made a lot of improvements to make the chip have the features that the traditional 51 microcontroller does not have. On a single chip, with a smart 8-bit CPU and in-system programmable Flash, the STC89C52 provides a highly flexible, ultra-efficient solution for many embedded control applications.

Stc52 microcontroller keyboard schematic

Stc52 single-chip keyboard schematic and program introduction

Description:

1. Independent keyboard section

S2~S5 are 4 independent keyboards, which are connected to P3.4~P3.7 of MCU respectively.

2. Matrix keyboard section

S6~S21 are 16 matrix keyboards, and 8 lines are respectively connected with the P3 port of the single chip microcomputer. The 4 rows of the matrix keyboard are respectively connected with the P3.0~P3.3 of the single chip microcomputer, and the 4 columns of the matrix keyboard are respectively connected with the P3 of the single chip microcomputer. 4~P3.7 are connected.

Detailed programming
1. Use the first two digits of the digital tube to display a decimal number with a range of 00-59.
Program features:

Use the first two digits of the digital tube to display a decimal number. The range of change is 00-59. At the beginning, 00 is displayed. Each time the S2 button is pressed, the value is incremented by one. Each time the S3 button is pressed, the value is decreased by one. Each press of S4 Once the key is pressed, the value is reset to zero; press the S5 key once, use the timer function to increment the value by 1 per second, press the S5 key again, and the value stops automatically incrementing by 1, keeping the original number.

Program source code:

#include

#define uchar unsigned char

#define uint unsigned int

Sbit key1 = P3 ^ 4;

Sbit key2 = P3 ^ 5;

Sbit key3 = P3 ^ 6;

Sbit key4 = P3 ^ 7;

Sbit dula = P2 ^ 6;

Sbit wela = P2 ^ 7;

Uchar code table[] = {

0x3f, 0x06, 0x5b, 0x4f,

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

0x7f, 0x6f, 0x77, 0x7c,

0x39, 0x5e, 0x79, 0x71

};

Void init();

Void keyscan();

Void display(uchar);

Void delayms(uint);

Uchar t0, num;

Void main()

{

Init();

While (1)

{

Keyscan();

Display(num);

}

}

Void init()

{

TMOD = 0x01;

TH0 = (65536 - 45872) / 256;

TL0 = (65536 - 45872) % 256;

EA = 1;

ET0 = 1;

}

Void keyscan()

{

If (key1 == 0)

{

Delayms(10);

If (key1 == 0)

{

If (num == 60)

Num = 0;

Num++;

While (!key1)

Display(num);

}

}

If (key2 == 0)

{

Delayms(10);

If (key2 == 0)

{

If (num == 0)

Num = 60;

Num--;

While (!key2)

Display(num);

}

}

If (key3 == 0)

{

Delayms(10);

If (key3 == 0)

{

Num = 0;

While (!key3)

Display(num);

}

}

If (key4 == 0)

{

Delayms(10);

If (key4 == 0)

{

TR0 = ~TR0;

While (!key4)

Display(num);

}

}

}

Void display(uchar numDis)

{

Dula = 1;

P0 = table[numDis / 10];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfe;

Wela = 0;

Delayms(5);

Dula = 1;

P0 = table[numDis % 10];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfd;

Wela = 0;

Delayms(5);

}

Void delayms(uint xms)

{

Uint i, j;

For (i = xms; i 》 0; i--)

For (j = 110; j 》 0; j--)

;

}

Void T0_timer() interrupt 1

{

TH0 = (65536 - 45872) / 256;

TL0 = (65536 - 45872) % 256;

T0++;

If (t0 == 20)

{

T0 = ​​0;

Num++;

If (num == 60)

Num = 0;

}

}

Summary of the program:

a. Keyboard button detection needs to be done twice (two ifs for each keyboard button)

b. Keyboard button exit also needs to be detected (each time button is used to exit a while (!key))

c. This program contains the button plus 1, the button minus 1, the button to zero, the button to start counting, then press to stop counting

2. Press the matrix keyboard to display 0~F on the digital tube, and the 6 digital tubes can be displayed statically at the same time. Program function

Power-on does not display, press the matrix keyboard, display 0~F on the digital tube, and 6 digital tubes can be displayed statically at the same time.

Program source code

#include

#define uchar unsigned char

#define uint unsigned int

Sbit dula = P2 ^ 6;

Sbit wela = P2 ^ 7;

Uchar code table[] = {

0x3f, 0x06, 0x5b, 0x4f,

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

0x7f, 0x6f, 0x77, 0x7c,

0x39, 0x5e, 0x79, 0x71

};

Void delayms(uint);

Void display(uchar);

Void matrixkeyscan();

Void main()

{

Dula = 1;

P0 = 0;

Dula = 0;

Wela = 1;

P0 = 0xc0;

Wela = 0;

While (1)

{

Matrixkeyscan();

}

}

Void delayms(uint xms)

{

Uint i, j;

For (i = xms; i 》 0; i--)

For (j = 110; j 》 0; j--)

;

}

Void display(uchar num)

{

Dula = 1;

P0 = table[num];

Dula = 0;

}

Void matrixkeyscan()

{

Uchar temp, key;

P3 = 0xfe;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xee:

Key = 0;

Break;

Case 0xde:

Key = 1;

Break;

Case 0xbe:

Key = 2;

Break;

Case 0x7e:

Key = 3;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

Display(key);

}

}

P3 = 0xfd;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xed:

Key = 4;

Break;

Case 0xdd:

Key = 5;

Break;

Case 0xbd:

Key = 6;

Break;

Case 0x7d:

Key = 7;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

Display(key);

}

}

P3 = 0xfb;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xeb:

Key = 8;

Break;

Case 0xdb:

Key = 9;

Break;

Case 0xbb:

Key = 10;

Break;

Case 0x7b:

Key = 11;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

Display(key);

}

}

P3 = 0xf7;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xe7:

Key = 12;

Break;

Case 0xd7:

Key = 13;

Break;

Case 0xb7:

Key = 14;

Break;

Case 0x77:

Key = 15;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

Display(key);

}

}

}

Program summary

a. First send the line low level, detect the column line signal

b. Debounce by delay

c. Need to check release

3. The first three digits of the digital tube display a stopwatch, running program functions from 000 to 999 at 1% seconds .

When the independent keyboard is pressed, the stopwatch stops, and when the hand is released, the stopwatch continues to run. Design the table with a timer.

Program source code

#include

#define uint unsigned int

#define uchar unsigned char

Sbit dula = P2 ^ 6;

Sbit wela = P2 ^ 7;

Sbit s2 = P3 ^ 4;

Uchar code table[] = {

0x3f, 0x06, 0x5b, 0x4f,

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

0x7f, 0x6f, 0x77, 0x7c,

0x39, 0x5e, 0x79, 0x71,

0x76, 0x79, 0x38, 0x3f, 0

};

Uchar flag, t0, bai, shi, ge;

Uint shu;

Void init();

Void display(uchar aa, uchar bb, uchar cc);

Void delayms(uint);

Void main()

{

Init();

While (1)

{

Display(bai, shi, ge);

If (s2 == 0)

{

Delayms(10);

If (s2 == 0)

{

TR0 = 0;

While (!s2)

Display(bai, shi, ge);

TR0 = 1;

}

}

}

}

Void init()

{

TMOD = 0x01;

TH0 = (65536 - 10000) / 256;

TL0 = (65536 - 10000) % 256;

EA = 1;

ET0 = 1;

TR0 = 1;

}

Void T0_timer() interrupt 1

{

TH0 = (65536 - 10000) / 256;

TL0 = (65536 - 10000) % 256;

T0++;

If (t0 == 1)

{

T0 = ​​0;

Shu++;

If (shu == 1000)

Shu = 0;

Bai = shu / 100;

Shi = shu % 100 / 10;

Ge = shu % 10;

}

}

Void display(uchar aa, uchar bb, uchar cc)

{

Dula = 1;

P0 = table[aa];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfe;

Wela = 0;

Delayms(1);

Dula = 1;

P0 = table[bb];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfd;

Wela = 0;

Delayms(1);

Dula = 1;

P0 = table[cc];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfb;

Wela = 0;

Delayms(1);

}

Void delayms(uint xms)

{

Uint i, j;

For (i = xms; i 》 0; i--)

For (j = 110; j 》 0; j--)

;

}

Program summary

a. Release the check while (!s2)

b. 1% second speed operation: Select timing base is 1000 (ie 10ms), timing multiple is 1, multiply to 10ms

Explanation: Since the interval time is too short, the problem here is that the low-level data display is not clear. You can use the timing multiplier to 10 (but the timing number becomes 100ms, which may not match the meaning of the question)

4. The first three digits of the digital tube display a stopwatch, running the program function from 000 to 999 at 1% seconds .

When the first independent keyboard is pressed, the stopwatch stops, and when the hand is released, the stopwatch continues to run.

The timing stops when the second independent keyboard is pressed.

Timing starts when the third independent keyboard is pressed.

Count value is cleared from the beginning when the third independent keyboard is pressed

Program source code

#include

#define uint unsigned int

#define uchar unsigned char

Sbit dula = P2 ^ 6;

Sbit wela = P2 ^ 7;

Sbit s2 = P3 ^ 4;

Sbit s3 = P3 ^ 5;

Sbit s4 = P3 ^ 6;

Sbit s5 = P3 ^ 7;

Uchar code table[] = {

0x3f, 0x06, 0x5b, 0x4f,

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

0x7f, 0x6f, 0x77, 0x7c,

0x39, 0x5e, 0x79, 0x71,

0x76, 0x79, 0x38, 0x3f, 0

};

Uchar flag, t0;

Uint shu;

Void init();

Void display(uint num);

Void delayms(uint);

Void keyscan();

Void main()

{

Init();

While (1)

{

Display(shu);

Keyscan();

}

}

Void init()

{

TMOD = 0x01;

TH0 = (65536 - 10000) / 256;

TL0 = (65536 - 10000) % 256;

EA = 1;

ET0 = 1;

TR0 = 1;

}

Void keyscan()

{

If (s2 == 0)

{

Delayms(10);

If (s2 == 0)

{

TR0 = 0;

While (!s2)

Display(shu);

TR0 = 1;

}

}

If (s3 == 0)

{

Delayms(10);

If (s3 == 0)

{

TR0 = 0;

While (!s3)

Display(shu);

}

}

If (s4 == 0)

{

Delayms(10);

If (s4 == 0)

{

TR0 = 1;

While (!s4)

Display(shu);

}

}

If (s5 == 0)

{

Delayms(10);

If (s5 == 0)

{

Shu = 0;

While (!s5)

Display(shu);

}

}

}

Void T0_timer() interrupt 1

{

TH0 = (65536 - 10000) / 256;

TL0 = (65536 - 10000) % 256;

T0++;

If (t0 == 1)

{

T0 = ​​0;

Shu++;

If (shu == 1000)

Shu = 0;

}

}

Void display(uint num)

{

Dula = 1;

P0 = table[num / 100];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfe;

Wela = 0;

Delayms(1);

Dula = 1;

P0 = table[num % 100 / 10];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfd;

Wela = 0;

Delayms(1);

Dula = 1;

P0 = table[num % 100 % 10];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfb;

Wela = 0;

Delayms(1);

}

Void delayms(uint xms)

{

Uint i, j;

For (i = xms; i 》 0; i--)

For (j = 110; j 》 0; j--)

;

}

Program summary

1. Press the display (shu) button to exit the test, otherwise the first two digits of the digital tube are blank.

2. Press the button to exit the sequence of detection and execution actions

1) If you want to perform an action immediately by a button, you should perform the action first, then press the button to exit the test.

That is:

Shu = 0;

While (!s5)

Display(shu);

2) If you need to press the button to exit and then perform the action, you should first check the button to exit and then execute the action.

That is:

While (!s5)

Display(shu);

Shu = 0;

5. Press 16 matrix keyboards to display the 1 to 16 square program function on the first three digital tubes in turn .

Press 16 matrix keyboards, and display the squares of 1~16 on the first three digital tubes in turn, press the first display 1, press the second display 4, ... press the 16th display 16*16 ( 256)

Program source code

#include

#define uchar unsigned char

#define uint unsigned int

Sbit dula = P2 ^ 6;

Sbit wela = P2 ^ 7;

Uchar code table[] = {

0x3f, 0x06, 0x5b, 0x4f,

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

0x7f, 0x6f, 0x77, 0x7c,

0x39, 0x5e, 0x79, 0x71

};

Void delayms(uint);

Void display(uint);

Void matrixkeyscan();

Void main()

{

While (1)

{

Matrixkeyscan();

}

}

Void delayms(uint xms)

{

Uint i, j;

For (i = xms; i 》 0; i--)

For (j = 110; j 》 0; j--)

;

}

Void display(uint num)

{

Dula = 1;

P0 = table[num / 100];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfe;

Wela = 0;

Delayms(1);

Dula = 1;

P0 = table[num % 100 / 10];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfd;

Wela = 0;

Delayms(1);

Dula = 1;

P0 = table[num % 100 % 10];

Dula = 0;

P0 = 0xff;

Wela = 1;

P0 = 0xfb;

Wela = 0;

Delayms(1);

}

Void matrixkeyscan()

{

Uchar temp, key;

P3 = 0xfe;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xee:

Key = 1;

Break;

Case 0xde:

Key = 2;

Break;

Case 0xbe:

Key = 3;

Break;

Case 0x7e:

Key = 4;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

}

}

P3 = 0xfd;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xed:

Key = 5;

Break;

Case 0xdd:

Key = 6;

Break;

Case 0xbd:

Key = 7;

Break;

Case 0x7d:

Key = 8;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

}

}

P3 = 0xfb;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xeb:

Key = 9;

Break;

Case 0xdb:

Key = 10;

Break;

Case 0xbb:

Key = 11;

Break;

Case 0x7b:

Key = 12;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

}

}

P3 = 0xf7;

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Delayms(10);

Temp = P3;

Temp = temp & 0xf0;

If (temp != 0xf0)

{

Temp = P3;

Switch(temp)

{

Case 0xe7:

Key = 13;

Break;

Case 0xd7:

Key = 14;

Break;

Case 0xb7:

Key = 15;

Break;

Case 0x77:

Key = 16;

Break;

}

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

}

}

Display(key * key);

}

Program summary

1. display(key * key) can't be placed immediately after each button is exited. If you do that, only the last digit (ie, one digit) will be displayed on the digital tube.

in other words,

The program can't do this:

While (temp != 0xf0)

{

Temp = P3;

Temp = temp & 0xf0;

}

Display(key*key);

Instead, it should be placed at the end of the program.

6Gang Switch And Multi Function Socket

6Gang Switch And Multi Function Socket,6Gang Switch,Multi Function Socket,Multi Function Socket Wrench

ZHEJIANG HUAYAN ELECTRIC CO.,LTD , https://www.huayanelectric.com

This entry was posted in on