Matrix keyboard program under stc89c52 microcontroller

The STC89C52 is a microcontroller from the 51 and 52 series, similar to the AT89C52. There are only minor differences between them, and it can be directly used as a replacement. One of its key features is the built-in EEPROM, which allows for program downloading via the serial port. Additionally, its instruction execution speed is twice that of the standard 8051 architecture.

The STC89C52 is a low-voltage, high-performance CMOS 8-bit microprocessor that includes 8K bytes of Flash memory for program storage. It is manufactured using Atmel's advanced non-volatile memory technology and is fully compatible with the industry-standard MCS-51 instruction set and pinout configuration. This makes it ideal for a wide range of embedded applications where reliability and performance are essential.

Matrix keyboard program under stc89c52 microcontroller

Below is an example of a matrix keyboard program implemented on the STC89C52 microcontroller. In this example, the keyboard is connected to the P0 port, and the output is displayed on the P2 port.

#include <reg52.h>

#define uchar unsigned char

#define uint unsigned int

sbit key1 = P3^2;

sbit key2 = P3^3;

uchar code tab[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00}; // Display patterns for 8 LEDs

uchar code wep[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; // Row values for scanning

void delay(uint t) {

while (--t);

}

void main() {

uchar row, col, key;

while (1) {

P0 = 0xf0; // Set upper 4 bits high for scanning

row = P0; // Read P0 value

row &= 0xf0; // Mask lower 4 bits

if (row != 0xf0) { // Key pressed

delay(125 * 5); // Debounce delay

if (row != 0xf0) {

col = P0 & 0xf0; // Read column value

col |= 0x0f; // Set lower 4 bits to 1

P0 = col; // Write back to P0

row = P0; // Read again

row &= 0x0f; // Mask upper 4 bits

col &= 0xf0;

key = row + col;

}

}

switch (key) {

case 0xee: P2 = tab[0]; break;

case 0xde: P2 = tab[1]; break;

case 0xbe: P2 = tab[2]; break;

case 0x7e: P2 = tab[3]; break;

case 0xed: P2 = tab[4]; break;

case 0xdd: P2 = tab[5]; break;

case 0xbd: P2 = tab[6]; break;

case 0x7d: P2 = tab[7]; break;

case 0xeb: P2 = tab[8]; break;

case 0xdb: P2 = tab[9]; break;

case 0xbb: P2 = tab[10]; break;

case 0x7b: P2 = tab[11]; break;

case 0xe7: P2 = tab[12]; break;

case 0xd7: P2 = tab[13]; break;

case 0xb7: P2 = tab[14]; break;

case 0x77: P2 = tab[15]; break;

}

}

}

German Warehouse

Longhua Manxueling Trading Company , https://www.mxlvape.com

Posted on