微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > MSP430单片机的4X4矩阵键盘C语言程序

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

时间:11-30 来源:互联网 点击:
#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;
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top