微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > msp430的一些常用的C语言控制程序---ZLG7289

msp430的一些常用的C语言控制程序---ZLG7289

时间:11-11 来源:互联网 点击:
/**********************************************************/

//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((datZ7289_DAT_HI;
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((addZ7289_DAT_HI;
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((datZ7289_DAT_HI;
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((datZ7289_DAT_HI;
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;
}

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

网站地图

Top