您的位置 首页 电路

51单片机矩阵键盘与左右流水灯操控C程序

本程序所用的原理图下载:点这里,单片机芯片使用的stc89c52;找到本程序所使用的相应部分即可这是一整个单片机开发板的电路图其他的忽略h

本程序所用的原理图下载:点这里,单片机芯片运用的stc89c52;找到本程序所运用的相应部分即可.这是一整个单片机开发板的电路图其他的疏忽.

hex文件及其工程文件下载:http://www.51hei.com/f/jzzyou.rar 以下是测验ok的源代码:

/*
*功用:运用矩阵按键使得按键按下时数码管上显现各自对应的数字的平方数;
* 且运用守时器0中止使得五颜六色流水灯先以20毫秒的速度左移活动4秒后,
* 然后使得五颜六色流水灯以20毫秒的速度右移活动;
*日期:2013-05-02-16:46
*作者:徐冉
*特别阐明:本程序代码现已经过调试,仅供学习运用;
*
*/
/***********AT89C52-RC单片机- 51hei-5 试验板***********/
/*****************51hei开发板*********************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
sbit FM = P2^3;
uchar code table[] = {
0x3F, //”0″
0x06, //”1″
0x5B, //”2″
0x4F, //”3″
0x66, //”4″
0x6D, //”5″
0x7D, //”6″
0x07, //”7″
0x7F, //”8″
0x6F //”9″
};
uint key1 = 0;
uchar counter = 0, x = 0, flag = 0;
void display(uint num);
void delay(uint xms);
void Marix_keyscan();
void init();
//主程序
void main()
{
init();//守时器0初始化
while(1)
{
if(counter == 200)//守时器守时左移活动4秒
{
counter = 0;
flag= 1;
TR0 = 0;
TH0 = 0xB8;
TL0 = 0X00;
TR0 = 1;
x = 0;
}
Marix_keyscan();
display(key1);
}
}
//守时器0初始化子程序
void init()
{
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0X00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
//数码管优化显现子程序
void display(uint num)
{
uchar bai, shi, ge;
bai = num / 100 % 10;
shi = num / 10 % 10;
ge = num % 10;
if(num < 10)
{
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
}
else if(num < 100)
{
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
}
else
{
dula = 1;
P0 = table[bai];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x00;
delay(1);
}
}
//延时子程序
void delay(uint xms)
{
uint i, j;
for(i = xms; i > 0; i–)
for(j = 125; j > 0; j–);
}
//矩阵按键检测子程序
void Marix_keyscan()
{
uchar temp; //界说一个变量寄存P3口的值
//榜首次矩阵按键检测
P3 = 0xfe; //给P3口赋一个值,使得矩阵按键的榜首行置为低电平,其他口置为高电平;
temp = P3; //将P3口的值赋给temp
temp &= 0xf0; //经过与0xf0&来检测矩阵按键的各列经过队伍检测与检测矩阵按键
if(temp != 0xf0)
{
//检测有矩阵按键按下
delay(5);//消抖
temp = P3;//再次将P3口的值赋给temp,即再次赋初值检测矩阵按键;
temp &= 0xf0; //再次按位与检测矩阵按键队伍
if(temp != 0xf0)
{ //承认有键按下
FM = 0;//蜂鸣器发声
temp = P3; //将此刻的P3口的值赋给temp
switch(temp)
{
case 0xee: key1 = 1 * 1;
break; //检测到key1按下
case 0xde: key1 = 2 * 2;
break; //检测到key2按下
case 0xbe: key1 = 3 * 3;
break; //检测到key3按下
case 0x7e: key1 = 4 * 4;
break; //检测到key4按下
default:
break;
}
//按键开释
while(temp != 0xf0)
{
//从头赋初值检测矩阵按键
temp = P3;
temp &= 0xf0;
}
delay(5);//开释去抖
//按键开释
while(temp != 0xf0)
{
//从头赋初值检测矩阵按键
temp = P3;
temp &= 0xf0;
}
//按键开释封闭蜂鸣器
FM = 1;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/dianlu/254010.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部