咱直接贴代码 因为水平本就不高 希望大家能加不好的地方指出
main.c
#include <msp430x14x.h>
#include "Config.h"
#include "delay.h"
void Port_Init()
{
LED8SEL = 0x00;
LED8DIR = 0xFF;
LED8PORT = 0xFF;
KEYSEL = 0x00;
KEYDIR = 0xf0;
KEYPORT= 0x0f;
}
uchar Key_Scan(void)
{
uchar statevalue,key_checkin,key_check;
KEYPORT=0x0f;
KEYPORT=0x0f;
key_checkin=KEYIN;
if(key_checkin!=0x0f)
{
Delay_Nms(20);
key_checkin=KEYIN;
if(key_checkin!=0x0f)
{
KEYPORT=0xef;
KEYPORT=0xef;
key_check=KEYIN;
switch (key_check)
{
case 0xee:key=1;break;
case 0xed:key=2;break;
case 0xeb:key=3;break;
case 0xe7:key=4;break;
}
KEYPORT=0xdf;
KEYPORT=0xdf;
key_check=KEYIN;
switch (key_check)
{
case 0xde:key=5;break;
case 0xdd:key=6;break;
case 0xdb:key=7;break;
case 0xd7:key=8;break;
}
KEYPORT=0xbf;
KEYPORT=0xbf;
key_check=KEYIN;
switch (key_check)
{
case 0xbe:key=9;break;
case 0xbd:key=10;break;
case 0xbb:key=11;break;
case 0xb7:key=12;break;
}
KEYPORT=0x7f;
KEYPORT=0x7f;
key_check=KEYIN;
switch (key_check)
{
case 0x7e:key=13;break;
case 0x7d:key=14;break;
case 0x7b:key=15;break;
case 0x77:key=16;break;
}
}
}
else
{
statevalue=0xff;
key=statevalue;
return(key);
}
return(key);
}
void main(void)
{
WDT_Init();
Port_Init();
while(1)
{
Key_Scan();
if(key!=0xff)
{
{
switch(key)
{
case 1: LED8PORT = BIT0 + BIT1 ;break;
case 2: LED8PORT = BIT2 + BIT3 ;break;
case 3: LED8PORT = BIT4 + BIT5 ;break;
case 4: LED8PORT = BIT6 + BIT7 ;break;
case 5: LED8PORT = BIT1 + BIT2 ;break;
case 6: LED8PORT = BIT1 + BIT3 ;break;
case 7: LED8PORT = BIT1 + BIT4 ;break;
case 8: LED8PORT = BIT1 + BIT5 ;break;
case 9: LED8PORT = BIT1 + BIT6 ;break;
case 10: LED8PORT = BIT0 + BIT1;break;
case 11: LED8PORT = BIT0 + BIT2;break;
case 12: LED8PORT = BIT0 + BIT3 ;break;
case 13: LED8PORT = BIT0 + BIT4 ;break;
case 14: LED8PORT = BIT0 + BIT5 ;break;
case 15: LED8PORT = BIT0 + BIT6 ;break;
case 16: LED8PORT = BIT0 + BIT7 ;break;
}
}
}
else
{
LED8PORT = 0XFF ;
}
}
}
#define CPU_F ((double)8000000)
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define baud 9600
#define baud_setting (uint)((ulong)CPU_F/((ulong)baud))
#define baud_h (uchar)(baud_setting>>8)
#define baud_l (uchar)(baud_setting)
#define LED8PORT P2OUT
#define LED8SEL P2SEL
#define LED8DIR P2DIR
#define KEYPORT P1OUT
#define KEYSEL P1SEL
#define KEYDIR P1DIR
#define KEYIN P1IN
uchar key;
void WDT_Init()
{
WDTCTL = WDTPW + WDTHOLD;
}
```c
#ifndef __DELAY
#define __DELAY
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
void Delay_Nus(uint n);
void Delay_1ms(void);
void Delay_Nms(uint n);
#endif
```c
/********************************************
文件名:delay.c
编写者:xdq
描述:延时程序。用于14xx系列。
MCLK:12m SCLK:4kHz ACLK:
版本:2012-6
********************************************/
#include <msp430x14x.h>
#include "delay.h"
/*******************************************
函数名称:Delay_Nus
功 能:延时N个us的时间
参 数:n--延时长度
返回值 :无
********************************************/
void Delay_Nus(uint n)
{
uchar i;
for(i = n;i > 0;i--)
_NOP();
}
/*******************************************
函数名称:Delay_1ms
功 能:延时约1ms的时间
参 数:无
返回值 :无
********************************************/
void Delay_1ms(void)
{
uchar i;
for(i = 150;i > 0;i--) _NOP();
}
/*******************************************
函数名称:Delay_Nms
功 能:延时N个1ms的时间
参 数:n--延时长度
返回值 :无
********************************************/
void Delay_Nms(uint n)
{
uint i;
for(i = n;i > 0;i--) Delay_1ms();
}
上面的代码对于初学者更加易懂 下面是改进版原理是一样的 首先,确保您的单片机P1.0~P1.3是否有上拉电阻 才能直接使用本程序 根据您的开发板来确定哪个I/O口连接4x4矩阵按键
#define u8 unsigned charu8 b[]={0xef, 0xdf, 0xbf, 0x7f};
void IO_Init()
{
P1DIR = 0xf0;
}
void Key_Board(void)
{
u8 j,n;
for(n=0; n<4; n++)
{
P1OUT = b[n];
if((P1IN&0X0f) !=0x0f)
{
delay_ms(2);
if((P1IN&0X0f) != 0x0f)
{
j = P1IN&0X0f;
switch(j)
{
case 0x0e: temp =0+n*4; break;
case 0x0d: temp =1+n*4; break;
case 0x0b: temp =2+n*4; break;
case 0x07: temp =3+n*4; break;
}
}
}
}
}
|