zlg7289 c程序#include"msp430x14x.h"#include"IO.h"#define DELAY_1US _NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP()void Delay(unsigned int t) //延时 t us{ unsigned int i; for (i=0;i<t;i++) DELAY_1US;}/*函数:ZLG7289_SPI_Write()功能:向SPI 总线写入1 个字节的数据参数:dat:要写入的数据*/void ZLG7289_SPI_Write(char dat) //5{ unsigned char t = 8; ZLG7289_OperateIO &=~ZLG7289_CLK; //clk=0 do { if(dat&0x80) ZLG7289_OperateIO |=ZLG7289_DAT; else ZLG7289_OperateIO &=~ZLG7289_DAT; ZLG7289_OperateIO |=ZLG7289_CLK; //clk=1 dat <<= 1; Delay(4); ZLG7289_OperateIO &=~ZLG7289_CLK; //clk=0 Delay(4); }while ( --t != 0 );}/*函数:ZLG7289_SPI_Read()功能:从SPI 总线读取1 个字节的数据返回:读取到的数据*/unsigned char ZLG7289_SPI_Read(){ unsigned char dat=0x00; unsigned char t = 8; ZLG7289_OperateIO &=~ZLG7289_CLK; //clk=0 //ZLG7289_DAT = 1; //读取数据之前DIO 引脚要置1 以切换到输入状态 do { ZLG7289_OperateIO |=ZLG7289_CLK; //clk=1 Delay(4); dat <<= 1; if ((ZLG7289_OperateIO_IN&ZLG7289_DAT)) dat++; ZLG7289_OperateIO &=~ZLG7289_CLK; //clk=0 Delay(4); }while ( --t != 0 ); return dat;}/*函数:ZLG7289_cmd()功能:执行ZLG7289 纯指令参数:cmd:命令字*/ //6void ZLG7289_cmd(char cmd){ ZLG7289_OperateIO_DIR |=ZLG7289_DAT; //output ZLG7289_OperateIO &=~ZLG7289_CS; //CS=0 Delay(24); ZLG7289_SPI_Write(cmd); ZLG7289_OperateIO |=ZLG7289_CS; //CS=1 ZLG7289_OperateIO_DIR &=~ZLG7289_DAT; //input}/*函数:ZLG7289_cmd_dat()功能:执行ZLG7289 带数据指令参数:cmd:命令字dat:数据*/void ZLG7289_cmd_dat(char cmd, char dat){ ZLG7289_OperateIO_DIR |=ZLG7289_DAT; //output ZLG7289_OperateIO &=~ZLG7289_CS; //CS=0 Delay(24); ZLG7289_SPI_Write(cmd); Delay(14); ZLG7289_SPI_Write(dat); ZLG7289_OperateIO |=ZLG7289_CS; //CS=1 ZLG7289_OperateIO_DIR &=~ZLG7289_DAT; //input}/*函数:ZLG7289_Download()功能:下载数据参数:mod=0:下载数据且按方式0 译码mod=1:下载数据且按方式1 译码mod=2:下载数据但不译码x:数码管编号(横坐标),取值0~7dp=0:小数点不亮 //7dp=1:小数点亮dat:要显示的数据说明:译码方式的具体含义请参考数据手册*/void ZLG7289_Download(unsigned char mod, char x,char dp, char dat) { char ModDat[3] = {0x80,0xC8,0x90}; char d1; char d2; if ( mod > 2 ) mod = 2; d1 = ModDat[mod]; x = x & 0x07; d1 = d1 | x; d2 = dat & 0x7F; if ( dp ) d2 =d2 | 0x80; ZLG7289_cmd_dat(d1,d2);}/*功能:执行ZLG7289 键盘命令返回:返回读到的按键值:0~63如果返回0xFF 则表示没有键按下*/unsigned char ZLG7289_Key(){ unsigned char key; ZLG7289_OperateIO_DIR |=ZLG7289_DAT; //output ZLG7289_OperateIO &=~ZLG7289_CS; //CS=0 Delay(24); ZLG7289_SPI_Write(0x15); ZLG7289_OperateIO_DIR &=~ZLG7289_DAT; //input Delay(14); key = ZLG7289_SPI_Read(); ZLG7289_OperateIO |=ZLG7289_CS; //CS=0 return key;}void ZLG7289_initial(void){ ZLG7289_OperateIO_DIR |= ZLG7289_CS+ZLG7289_CLK+ZLG7289_KEY; ZLG7289_OperateIO |= ZLG7289_CS+ZLG7289_KEY; ZLG7289_OperateIO &=~ ZLG7289_CLK; ZLG7289_OperateIO_DIR &=~ ZLG7289_KEY; //INPUT ZLG7289_OperateIE |= ZLG7289_KEY; //enable interrupt ZLG7289_OperateIES |= ZLG7289_KEY; //failing}