您的位置 首页 ADAS

MSP430单片机的4X4矩阵键盘C言语程序

includemsp430x24xh>defineucharunsignedchardefineuintunsignedintuchartable[]={0xfe,0xfd,0xfb,0xf7,

#include <msp430x24x.h>

#define uchar unsigned char
#define uint unsigned int

uchar table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

void delay(unsigned int i) //延时子程序
{
while(i–);
}

uchar keyvalue()
{
uchar key;
uchar np10,np11,np12,np13;
P1DIR=0x0f;
//第一排
P1OUT=~BIT3;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=0;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=1;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=2;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=3;
}
//第二行
P1OUT=~BIT2;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=4;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=5;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=6;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=7;
}
//第三行
P1OUT=~BIT1;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=8;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=9;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=10;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=11;
}
//第四行
P1OUT=~BIT0;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=12;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=13;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=14;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=15;
}
P1OUT=0X00;
return key;
while(1)
{
if((P1IN&0X0F)==0x0f)
break;
}
}

void main()
{
uchar key_value;
WDTCTL=WDTPW+WDTHOLD;
P1DIR=0X0F;
P2DIR=0XFF;
P2OUT=0XFF;
while(1)
{
if((P1IN&0XF0)!=0XF0)
{
delay(100);
if((P1IN&0XF0)!=0XF0)
{
delay(100);
if((P1IN&0XF0)!=0XF0)
{
key_value=keyvalue();
}
}
}
P2OUT=~key_value;
}
}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部