找回密码
 立即注册
查看: 15|回复: 3

6-1 矩阵键盘,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版

[复制链接]
  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 09:19:55 | 显示全部楼层 |阅读模式
6-1 矩阵键盘,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
主程序main.C代码如下:


#include <REGX52.H>
#include "Delay.h"                //包含Delay头文件
#include "LCD1602.h"        //包含LCD1602头文件
#include "MatrixKey.h"        //包含矩阵键盘头文件

unsigned char KeyNum;

void main()
{
        LCD_Init();                                                        //LCD初始化
        LCD_ShowString(1,1,"MatrixKey:");        //LCD显示字符串
        while(1)
        {
                KeyNum=MatrixKey();                                //获取矩阵键盘键码
                if(KeyNum)                                                //如果有按键按下
                {
                        LCD_ShowNum(2,1,KeyNum,2);        //LCD显示键码
                }
        }
}


回复

使用道具 举报 送花

  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 09:21:12 | 显示全部楼层
6-1 矩阵键盘,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
延时Delay.C代码如下:



void Delay(unsigned int xms)
{
        unsigned char i, j;
        while(xms--)
        {
                i = 2;
                j = 239;
                do
                {
                        while (--j);
                } while (--i);
        }
}


回复

使用道具 举报 送花

  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 09:22:17 | 显示全部楼层
6-1 矩阵键盘,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
矩阵键盘MatrixKey.C代码如下:


#include <REGX52.H>
#include "Delay.h"

/**
  * @brief  矩阵键盘读取按键键码
  * @param  无
  * @retval KeyNumber 按下按键的键码值
                        如果按键按下不放,程序会停留在此函数,松手的一瞬间,返回按键键码,没有按键按下时,返回0
  */
unsigned char MatrixKey()
{
        unsigned char KeyNumber=0;
       
        P1=0xFF;
        P1_3=0;
        if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}
        if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}
        if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}
        if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}
       
        P1=0xFF;
        P1_2=0;
        if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}
        if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}
        if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}
        if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}
       
        P1=0xFF;
        P1_1=0;
        if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}
        if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}
        if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}
        if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}
       
        P1=0xFF;
        P1_0=0;
        if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}
        if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}
        if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}
        if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}
       
        return KeyNumber;
}


回复

使用道具 举报 送花

  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 09:23:40 | 显示全部楼层
6-1 矩阵键盘,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
LCD1602.C代码如下:


#include <REGX52.H>

//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0

//函数定义:
/**
  * @brief  LCD1602延时函数,12MHz调用可延时1ms
  * @param  无
  * @retval 无
  */
void LCD_Delay()
{
        unsigned char i, j;

        i = 2;
        j = 239;
        do
        {
                while (--j);
        } while (--i);
}

/**
  * @brief  LCD1602写命令
  * @param  Command 要写入的命令
  * @retval 无
  */
void LCD_WriteCommand(unsigned char Command)
{
        LCD_RS=0;
        LCD_RW=0;
        LCD_DataPort=Command;
        LCD_EN=1;
        LCD_Delay();
        LCD_EN=0;
        LCD_Delay();
}

/**
  * @brief  LCD1602写数据
  * @param  Data 要写入的数据
  * @retval 无
  */
void LCD_WriteData(unsigned char Data)
{
        LCD_RS=1;
        LCD_RW=0;
        LCD_DataPort=Data;
        LCD_EN=1;
        LCD_Delay();
        LCD_EN=0;
        LCD_Delay();
}

/**
  * @brief  LCD1602设置光标位置
  * @param  Line 行位置,范围:1~2
  * @param  Column 列位置,范围:1~16
  * @retval 无
  */
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
        if(Line==1)
        {
                LCD_WriteCommand(0x80|(Column-1));
        }
        else if(Line==2)
        {
                LCD_WriteCommand(0x80|(Column-1+0x40));
        }
}

/**
  * @brief  LCD1602初始化函数
  * @param  无
  * @retval 无
  */
void LCD_Init()
{
        LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
        LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
        LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
        LCD_WriteCommand(0x01);//光标复位,清屏
}

/**
  * @brief  在LCD1602指定位置上显示一个字符
  * @param  Line 行位置,范围:1~2
  * @param  Column 列位置,范围:1~16
  * @param  Char 要显示的字符
  * @retval 无
  */
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
        LCD_SetCursor(Line,Column);
        LCD_WriteData(Char);
}

/**
  * @brief  在LCD1602指定位置开始显示所给字符串
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  String 要显示的字符串
  * @retval 无
  */
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
        unsigned char i;
        LCD_SetCursor(Line,Column);
        for(i=0;String!='\0';i++)
        {
                LCD_WriteData(String);
        }
}

/**
  * @brief  返回值=X的Y次方
  */
int LCD_Pow(int X,int Y)
{
        unsigned char i;
        int Result=1;
        for(i=0;i<Y;i++)
        {
                Result*=X;
        }
        return Result;
}

/**
  * @brief  在LCD1602指定位置开始显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~65535
  * @param  Length 要显示数字的长度,范围:1~5
  * @retval 无
  */
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
        unsigned char i;
        LCD_SetCursor(Line,Column);
        for(i=Length;i>0;i--)
        {
                LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
        }
}

/**
  * @brief  在LCD1602指定位置开始以有符号十进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:-32768~32767
  * @param  Length 要显示数字的长度,范围:1~5
  * @retval 无
  */
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
        unsigned char i;
        unsigned int Number1;
        LCD_SetCursor(Line,Column);
        if(Number>=0)
        {
                LCD_WriteData('+');
                Number1=Number;
        }
        else
        {
                LCD_WriteData('-');
                Number1=-Number;
        }
        for(i=Length;i>0;i--)
        {
                LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
        }
}

/**
  * @brief  在LCD1602指定位置开始以十六进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~0xFFFF
  * @param  Length 要显示数字的长度,范围:1~4
  * @retval 无
  */
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
        unsigned char i,SingleNumber;
        LCD_SetCursor(Line,Column);
        for(i=Length;i>0;i--)
        {
                SingleNumber=Number/LCD_Pow(16,i-1)%16;
                if(SingleNumber<10)
                {
                        LCD_WriteData(SingleNumber+'0');
                }
                else
                {
                        LCD_WriteData(SingleNumber-10+'A');
                }
        }
}

/**
  * @brief  在LCD1602指定位置开始以二进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  * @param  Length 要显示数字的长度,范围:1~16
  * @retval 无
  */
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
        unsigned char i;
        LCD_SetCursor(Line,Column);
        for(i=Length;i>0;i--)
        {
                LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
        }
}


回复

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2026-7-24 06:53 , Processed in 0.067040 second(s), 51 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表