msp430的一些常用的C語言控制程序---ZLG7289
//MSP430高級實驗開發(fā)組件 - ZLG7289鍵盤顯示組件示例程序
//時鐘設(shè)置:
////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 |
//// ------------------ ------------------
//當(dāng)前演示程序功能描述:
////當(dāng)有按鍵按下時,430進(jìn)入中斷讀取鍵值,顯示在LED數(shù)碼管上
/*********************************************************/
#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;//系統(tǒng)延時
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; //停止系統(tǒng)軟件看門狗
OSC_Init();
Z7289_Init();
Z7279_Init();
_BIS_SR(GIE);
}
//中斷服務(wù)程序
////讀取鍵值并顯示
#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標(biāo)志位
for (int i=0xFF;i>0;i--); //延時等待晶振
} while ((IFG1 & OFIFG)); //OSCFault標(biāo)志位始終有效
BCSCTL2 |= SELM_2; //MCLK = XT2
}
//初始化程序
////設(shè)定端口和中斷,初始化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發(fā)送一個字節(jié)(命令)
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發(fā)送兩個字節(jié)(命令+數(shù)據(jù))
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發(fā)送命令后,讀取按鍵鍵值,并查表返回結(jié)果
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發(fā)送命令
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++) {//根據(jù)輸入口的電平高低來確定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
評論