以下是74HC165的键盘扫描,但是在lcd1602上显示不了。我保证lcd1602程序正常。
/********************************************************
*HC74165.c 74HC165 for MSP430各系列单片机
*74HC165:8位并入串出移位寄存器,可以接8个按键
*by 追梦 2011-3-19
*P1.0 QH输出;P1.1 SHIFT/LOAD;P1.2 CLOCK & CLOCK INHIBIT
********************************************************/
#include "msp430.h"
#include "lcd1602.c"
unsigned char tishi[]={"PRESS KEY IS :"};
unsigned char table[]={"0123456789"};
unsigned char keyval;
#define QH BIT0
#define SHIFT BIT1
#define CLOCK BIT2
#define HC165SEL P3SEL
#define HC165DIR P3DIR
#define HC165OUT P3OUT
#define HC165IN P3IN
//74HC165与单片机的连接端口初始设置
unsigned int GetHC74165(void);
void HC74165_Init()
{
HC165SEL &= ~(QH+SHIFT+CLOCK);
HC165DIR |= SHIFT + CLOCK;
HC165DIR &= ~QH;
}
/********************************************************
*名 称:GetHC74165()
*功 能:锁存74HC165的8位输入,并逐位读出
*出 口:读取的8位状态数据,放在整型数据的低八位
********************************************************/
unsigned int GetHC74165(void)
{
unsigned char i;
unsigned int indata=0;
HC165OUT &= ~SHIFT;
_NOP();
HC165OUT |= SHIFT;
_NOP();
_NOP();
for(i=0;i<8;i++)
{
indata <<= 1;
if(HC165IN & QH) indata += 1;
HC165OUT |= CLOCK;
_NOP();
HC165OUT &= ~CLOCK;
}
return indata;
}
void main( void )
{
unsigned int key,a;
WDTCTL = WDTPW + WDTHOLD;
DCOCTL=CALDCO_16MHZ;
BCSCTL1= CALBC1_16MHZ;
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
init_IO_1602();
LCD_init_first();
init_1602();
HC74165_Init();
P3DIR = 0xff;
P3OUT = 0xff;
for(a=0;a<14;a++)
dispchar(a+1,0,tishi[a]);
while(1)
{
if((key=GetHC74165())!=0xff)
{
switch(key)
{
case 0xfe:
keyval=1;
break;
case 0xfd:
keyval=2;
break;
case 0xfb:
keyval=3;
break;
case 0xf7:
keyval=4;
break;
case 0xef:
keyval=5;
break;
case 0xdf:
keyval=6;
break;
case 0xbf:
keyval=7;
break;
case 0x7f:
keyval=8;
break;
}
dispchar(2,1,table[key]);
}
}
} |