单片机综合实验 - 04 | 键盘数码管与点阵管实验
Posted Neutionwei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单片机综合实验 - 04 | 键盘数码管与点阵管实验相关的知识,希望对你有一定的参考价值。
一、实验目的和任务
1、掌握80C51单片机矩阵键盘的构成原理和软件编程。
2、掌握80C51单片机数码管动态显示原理和编程。
3、掌握80C51单片机LED点阵的构成原理和软件编程。
二、实验原理简介
ZSC-1实验箱为MCU1配置了2*8矩阵键盘、8位LED数码管、8*8LED点阵管等人机对话设备,相关电路如下图所示。为提高口线利用率,单片机P0口分时用作矩阵键盘的行扫描口、数码管的段码口以及点阵管的列驱动口,P2.2、P2.1、P2.0与74HC138译码器及2片74HC240反相驱动器相配合,用作数码管的位码口和点阵管的行驱动口。
P3.4、P3.5用于读取矩阵键盘的列状态,P2.3、P4.4的作用为:P2.3=1时,数码管和点阵管均关闭显示,P2.3=0而P4.4=1或0时,数码管或点阵管开通显示。
三、实验内容和步骤
1、在8个数码上顺序显示0-7八个数字。程序代码如下所示:
#include <reg52.h>
unsigned lettern[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(int n)
{
int i;
int j;
for(i=0; i<n; i++)
for(j=700; j>=0; j--);
}
void display(unsigned char *dis,unsigned char n)
{
unsigned char i,x;
for (i=0;i<n;i++)
{
P2=i;
x=*dis;
dis++;
P0=~lettern[x];
delay(500);
}
}
void main(void)
{
unsigned char dis[8]={0,1,2,3,4,5,6,7};
while(1)
{
display(dis,8);
}
}
2、用8*8LED点阵循环显示0-9。程序代码如下所示:
#include <reg52.h>
sfr P4 = 0xC0;
sfr P4SW = 0xBB;
sbit P44=P4^4;
unsigned char code led88n[16][8]=
{
{0x00,0x18,0x24,0x24,0x24,0x24,0x18,0x00}, //'0'
{0x00,0x08,0x18,0x08,0x08,0x08,0x1C,0x00}, //'1'
{0x00,0x18,0x24,0x04,0x08,0x10,0x3C,0x00}, //'2'
{0x00,0x3C,0x08,0x10,0x08,0x04,0x24,0x18}, //'3'
{0x00,0x08,0x18,0x28,0x28,0x3C,0x08,0x08}, //'4'
{0x00,0x3C,0x20,0x38,0x04,0x24,0x18,0x00}, //'5'
{0x00,0x18,0x20,0x38,0x24,0x24,0x18,0x00}, //'6'
{0x00,0x3C,0x04,0x08,0x10,0x10,0x10,0x00}, //'7'
{0x00,0x18,0x24,0x24,0x18,0x24,0x24,0x18}, //'8'
{0x00,0x18,0x24,0x24,0x1C,0x04,0x24,0x18}, //'9'
{0x0C,0x12,0x12,0x12,0x1E,0x12,0x12,0x00}, //'A'
{0x0E,0x09,0x09,0x0E,0x09,0x09,0x09,0x0E}, //'B'
{0x00,0x1C,0x20,0x20,0x20,0x20,0x1C,0x00}, //'C'
{0x0E,0x1C,0x12,0x12,0x12,0x12,0x1C,0x00}, //'D'
{0x00,0x1E,0x10,0x1E,0x10,0x10,0x1E,0x00}, //'E'
{0x00,0x1E,0x10,0x10,0x1E,0x10,0x10,0x00}, //'F'
};
void delay(int n)
{
int i;
int j;
for(i=0; i<n; i++)
for(j=700; j>=0; j--);
}
void display88(unsigned char *chs) //显示8*8LED点阵
{
unsigned char i;
P44=0;
for(i=0;i<8;i++)
{
P0=~chs[i]; //每行值
P2=i; //第几行
delay( 1 );
P2=0xff;
}
P44=1;
}
void main(void)
{
unsigned char dis[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char i;
unsigned int j;
P4SW|=0x10; //将P44设为IO
while(1)
{
for(i=0;i<16;i++)
for(j=0;j<100;j++)
display88(led88n[dis[i]]);
}
}
3、从矩阵键盘获得按键值,对0-9的值同时用数码管和LED点阵进行显示。
#include <reg52.h>
sbit P34=P3^4;
sbit P35=P3^5;
sfr P4 = 0xC0;
sfr P4SW = 0xBB;
sbit P44=P4^4;
unsigned lettern[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code led88n[16][8]=
{
{0x00,0x18,0x24,0x24,0x24,0x24,0x18,0x00}, //'0'
{0x00,0x08,0x18,0x08,0x08,0x08,0x1C,0x00}, //'1'
{0x00,0x18,0x24,0x04,0x08,0x10,0x3C,0x00}, //'2'
{0x00,0x3C,0x08,0x10,0x08,0x04,0x24,0x18}, //'3'
{0x00,0x08,0x18,0x28,0x28,0x3C,0x08,0x08}, //'4'
{0x00,0x3C,0x20,0x38,0x04,0x24,0x18,0x00}, //'5'
{0x00,0x18,0x20,0x38,0x24,0x24,0x18,0x00}, //'6'
{0x00,0x3C,0x04,0x08,0x10,0x10,0x10,0x00}, //'7'
{0x00,0x18,0x24,0x24,0x18,0x24,0x24,0x18}, //'8'
{0x00,0x18,0x24,0x24,0x1C,0x04,0x24,0x18}, //'9'
{0x0C,0x12,0x12,0x12,0x1E,0x12,0x12,0x00}, //'A'
{0x0E,0x09,0x09,0x0E,0x09,0x09,0x09,0x0E}, //'B'
{0x00,0x1C,0x20,0x20,0x20,0x20,0x1C,0x00}, //'C'
{0x0E,0x1C,0x12,0x12,0x12,0x12,0x1C,0x00}, //'D'
{0x00,0x1E,0x10,0x1E,0x10,0x10,0x1E,0x00}, //'E'
{0x00,0x1E,0x10,0x10,0x1E,0x10,0x10,0x00}, //'F'
};
void delay(int n)
{ int i;
int j;
for(i=0;i<n;i++)
for(j=700;j>=0;j--);
}
unsigned char getkey(void)
{
P0 = 0x00;
if(P34 == 1 && P35 == 1)
return 0xFF;
delay(10);
P0 = 0xfe;
if(P35 == 0) return 0; // K0
if(P34 == 0) return 8; // K8
P0 = 0xfd;
if(P35 == 0) return 1; // K1
if(P34 == 0) return 9; // K9
P0 = 0xfb;
if(P35 == 0) return 2; // K2
if(P34 == 0) return 10; // KEYESC
P0 = 0xf7;
if(P35 == 0) return 3; // K3
if(P34 == 0) return 11; // KEYENTER
P0 = 0xef;
if(P35 == 0) return 4; // K4
if(P34 == 0) return 0xF1;// KEYF1
P0 = 0xdf;
if(P35 == 0) return 5; // K5
if(P34 == 0) return 0xF2;// KEYF2
P0 = 0xbf;
if(P35 == 0) return 6; // K6
if(P34 == 0) return 0xF3;// KEYF3
P0 = 0x7f;
if(P35 == 0) return 7; // K7
if(P34 == 0) return 0xF4;// KEYF4
return 0xFF;
}
void display88(unsigned char *chs) //显示8*8LED点阵
{
unsigned char i;
P44=0;
for(i=0;i<8;i++)
{
P0=~chs[i]; //每行值
P2=i; //第几行
delay(1);
P2=0xff;
} P44=1;
}
void main(void)
{
unsigned char ch1,ch=0;
unsigned char x[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
P4SW|=0x10; //将P44设为IO
while(1)
{
ch1=getkey();
if(ch1<10) ch=ch1;
if(ch<10)
{
display(&x[ch],1);
display88(led88n[ch]);
}
}
}
以上是关于单片机综合实验 - 04 | 键盘数码管与点阵管实验的主要内容,如果未能解决你的问题,请参考以下文章