/**********************************************************/
//MSP430高档试验开发组件 – ZLG7289键盘显现组件示例程序
//时钟设置:
////ACLK=N/A,MCLK=SMCLK=default(DCO~800k)
//硬件衔接:
//// MSP430 Broad ZLG7289 Broad
//// —————— ——————
//// | VCC | ——> | VCC |
//// | P65 | ——> | CS |
//// | P64 | ——> | CLK |
//// | P63 | <-----> | DAT |
//// | P24 | <------ | KEY(INT) |
//// | GND | ——> | GND |
//// —————— ——————
//当时演示程序功用描绘:
////当有按键按下时,430进入中止读取键值,显现在LED数码管上
/*********************************************************/
#include <msp430x14x.h>
#define Z7289_CSBIT5
#define Z7289_CLKBIT4
#define Z7289_DATBIT3
#defineZ7289_KEYBIT4
#define Z7289_DIRP6DIR
#define Z7289_OUTP6OUT
#define Z7289_INP6IN
#define Z7279_INT_DIRP2DIR
#define Z7279_INT_IEP2IE
#define Z7279_INT_IESP2IES
#define Z7279_INT_IFGP2IFG//界说MSP430端口
#define Z7289_CS_LOZ7289_OUT &= ~Z7289_CS
#define Z7289_CS_HIZ7289_OUT |= Z7289_CS
#define Z7289_CLK_LO Z7289_OUT &= ~Z7289_CLK
#define Z7289_CLK_HIZ7289_OUT |= Z7289_CLK
#define Z7289_DAT_LO Z7289_OUT &= ~Z7289_DAT
#define Z7289_DAT_HIZ7289_OUT |= Z7289_DAT
#define Z7289_RESETZ7289_Send1(0xA4)
#define Z7289_TESTZ7289_Send1(0xBF)
#define Z7289_LMOVZ7289_Send1(0xA1)
#define Z7289_RMOVZ7289_Send1(0xA0)
#define Z7289_RLMOVZ7289_Send1(0xA3)
#define Z7289_RRMOVZ7289_Send1(0xA2)//界说ZLG7289功用
const unsigned char DelayConst = 0;//体系延时
const unsigned char Code[3]={//显现方法
0x80,0xC8,0x90
};
const unsigned char Keys[16]={//按键编码
0x01,0x18,0x19,0x1A,0x10,0x11,0x12,0x08,0x09,0x0A,0x1B,0x13,0x0B,0x03,0x00,0x02
};
void Z7289_Delayt(unsigned int dtime);
void Z7289_Send1(unsigned char dat);
void Z7289_Send2(unsigned char add,unsigned char dat);
void Z7289_Show(unsigned char add,unsigned char coding,unsigned char d);
void Z7289_Init(void);
void Z7279_Init(void);
void OSC_Init(void);
unsigned char Z7289_ReadKey(void);
unsigned char readkey;
int j=8;
unsigned char key_array[8];
void main(void) {
WDTCTL = WDTPW + WDTHOLD; //中止体系软件看门狗
OSC_Init();
Z7289_Init();
Z7279_Init();
_BIS_SR(GIE);
}
//中止服务程序
////读取键值并显现
#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR (void) {
readkey = Z7289_ReadKey();
key_array[j] = readkey;
Z7289_Show(j,1,readkey);
Z7279_INT_IFG = 0;
j–;
if(j<0)
j=8;
}
//
void OSC_Init(void){
BCSCTL1 &= ~XT2OFF; //XT2 = HF-XTAL
do {
IFG1 &= ~OFIFG; //铲除OSCFault标志位
for (int i=0xFF;i>0;i–); //延时等候晶振
} while ((IFG1 & OFIFG)); //OSCFault标志位一直有用
BCSCTL2 |= SELM_2; //MCLK = XT2
}
//初始化程序
////设定端口和中止,初始化ZLG7289
void Z7289_Init(void) {
Z7289_DIR |= (Z7289_CS + Z7289_CLK + Z7289_DAT);
Z7289_CS_HI;
Z7289_CLK_LO;
Z7289_RESET;
}
//初始化7279
void Z7279_Init(void){
Z7279_INT_DIR &= ~Z7289_KEY;
Z7279_INT_IE |= Z7289_KEY;
Z7279_INT_IES |= Z7289_KEY;
}
//向ZLG7289发送一个字节(指令)
void Z7289_Send1(unsigned char dat) {
unsigned char itemp;
Z7289_CS_LO;
Z7289_Delayt(1);
for(itemp=0;itemp<8;itemp++) {
if((dat<
else
Z7289_DAT_LO;
Z7289_Delayt(1);
Z7289_CLK_HI;
Z7289_Delayt(1);
Z7289_CLK_LO;
}
Z7289_CS_HI;
}
//向ZLG7289发送两个字节(指令+数据)
void Z7289_Send2(unsigned char add,unsigned char dat) {
unsigned char itemp;
Z7289_CS_LO;
Z7289_Delayt(1);
for(itemp=0;itemp<8;itemp++) {
if((add<
else
Z7289_DAT_LO;
Z7289_Delayt(1);
Z7289_CLK_HI;
Z7289_Delayt(1);
Z7289_CLK_LO;
}
Z7289_Delayt(1);
for(itemp=0;itemp<8;itemp++) {
if((dat<
else
Z7289_DAT_LO;
Z7289_Delayt(1);
Z7289_CLK_HI;
Z7289_Delayt(1);
Z7289_CLK_LO;
}
Z7289_CS_HI;
}
//依照编码方法显现字符
void Z7289_Show(unsigned char add,unsigned char coding,unsigned char d) {
Z7289_Send2(Code[coding]+add,d);
}
//向ZLG7289发送指令后,读取按键键值,并查表回来成果
unsigned char Z7289_ReadKey(void) {
unsigned char itemp;
unsigned char dat;
dat = 0x15;
Z7289_CS_LO;
Z7289_Delayt(1);
for(itemp=0;itemp<8;itemp++) {//向7289发送指令
if((dat<
else
Z7289_DAT_LO;
Z7289_Delayt(1);
Z7289_CLK_HI;
Z7289_Delayt(1);
Z7289_CLK_LO;
}
Z7289_DIR &= ~Z7289_DAT;
Z7289_Delayt(1);
for(itemp=0;itemp<8;itemp++) {//依据输进口的电平凹凸来确认dat的值
Z7289_CLK_HI;
Z7289_Delayt(1);
Z7289_CLK_LO;
dat<<=1;
if((Z7289_IN&Z7289_DAT) == Z7289_DAT)
dat |= BIT0;
else
dat &= ~BIT0;
}
Z7289_DIR |= Z7289_DAT;
Z7289_CS_HI;
for(itemp=0;itemp<16;itemp++)
if(dat==Keys[itemp])//查表回来键值,表能够从IAR中调查得到
break;
return itemp;
}
void Z7289_Delayt(unsigned int dtime) {
unsigned int itemp,jtemp;
for(itemp=0;itemp