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
}
/*
函数: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:命令字
*/ //6
void 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~7
dp=0:小数点不亮 //7
dp=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
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !