说明:
SPI接口有一个特点,即在时钟SCK的上升沿打入数据MOSI,在下降沿读入数据MISO.
片选信号CS有正负区别.在硬件上MOSI与MISO是可以短路变为SIO可读写IO的.
故SPI可为(不包括CS)
三线(SCK,MOSI,MISO)协议,两线(SCK,SIO)协议
再者,SPI一般为双向同时高速收发数据的,方向由时钟SCK的跳变沿决定。
根据以上所述,模拟SPI读写模块编制成为一体化模块是必要的。
而且调用规则只需注意读数据时要写入0xff即可。非常方便好用。
例如:
res = SpiReadWrite(val);//模块写
SpiReadWrite(0xff);//模块读
对于具体器件,由于涉及到命令、地址及数据等,故一个完整的SPI读或写操作
可能需要几个模拟SPI读写一体化模块来完成。
所以一般完整的SPI读或写操作需以下函数组合完成
void SpiOpen(void); //打开片选 CS="0或CS"=1
void SpiClose(void); //关闭片选 CS="1或CS"=0
unsigned char SpiReadWrite(val); //模拟SPI读写一体化模块
void SpiWriteEnable(void); //使能写操作
void SpiWriteDisable(void); //禁止写操作
unsigned char SpiReadStatus(void); //读状态
void SpiWriteStatus(unsigned char val); //写状态
void SpiWriteWait(void); //等待写入完成
void SpiWriteByte(unsigned int addr, unsigned char val);//写一个字节
void SpiWriteWord(unsigned char addr, unsigned int val);//写一个字
unsigned char SpiReadByte(unsigned int addr); //读一个字节
unsigned int SpiReadWord(unsigned char addr); //读一个字
/*----------------------------------------------------------------------------
/*-----------------------------------------------
例:
X5045模拟SPI读写一体化模块
PTR905模拟SPI读写一体化模块
------------------------------------------------*
unsigned char SpiReadWrite(unsigned char val)
{
unsigned char i;
ACC = val;
for (i = 8; i > 0; i --)
{
CY = MISO;//取数据SO
_rlca_();//存数据ACC.0读数据ACC.7同时进行
MOSI = CY;//送数据SI
SCK = 1;//上升沿打入数据
_nop_();//延时
SCK = 0;//下降沿读入数据
}
return ACC;
}
/*-----------------------------------------------
例:
X5045模拟SPI读写一体化模块
ISD4004模拟SPI读写一体化模块
------------------------------------------------*
unsigned char SpiReadWrite(unsigned char val)
{
unsigned char i;
ACC = val;
for (i = 8; i > 0; i --)
{
SCK = 0;//下降沿读入数据
_nop_();//延时
CY = MISO;//取数据SO
_rlca_();//存数据ACC.0读数据ACC.7同时进行
MOSI = CY;//送数据SI
_nop_();//延时
SCK = 1;//上升沿打入数据
MOSI = 1;//释放总线SI
}
return ACC;
}
/*-----------------------------------------------
例:
AT93C46模拟SPI读写模块
------------------------------------------------*
sbit ACC_7 = ACC^7;
unsigned char SpiReadWrite(unsigned char val)
{
unsigned char i;
ACC = val;
for (i = 8; i > 0; i --)
{
CY = ACC_7;//读数据ACC.7
MOSI = CY;//送数据SI
SCK = 1;//上升沿打入数据
CY = MISO;//取数据SO
_rlca_();//存数据ACC.0
SCK = 0;//下降沿
}
return ACC;
}
发表于 2006/12/30 1:53:17
/*----------------------------------------------
DS1302读写一体化C51源程序
HotPower@126.com
------------------------------------------------*/
//打开DS1302
void TimeSpiOpen(void)
{
TIMECLK = 0;
TIMERST = 0;//禁止DS1302
TIMEIO = 1;//释放数据总线
TIMERST = 1;//使能DS1302
}
//关闭DS1302
void TimeSpiClose(void)
{
TIMERST = 0;//禁止DS1302
TIMEIO = 1;//释放数据总线
TIMECLK = 1;
}
//读写DS1302
unsigned char TimeSpiReadWrite(unsigned char val)
{
unsigned char i;
ACC = val;//取8位数据
for (i = 8;i > 0; i--)
{
TIMECLK = 0;//时钟下降沿输入数据(DS1302读)
_nop_();//延时
CY = TIMEIO;//接收串行数据到CY
_rrca_();//右移一位数据到CY(先存后取)
TIMEIO = CY;//发送串行数据
TIMECLK = 1;//时钟上升沿打入数据(DS1302写)
TIMEIO = 1;//释放数据总线
}
val = ACC;
return val;
}
//读DS1302字节
unsigned char TimeByteRead(unsigned char address)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite(address);//写入DS1302命令
address = TimeSpiReadWrite(0xff);//读入DS1302数据
TimeSpiClose();//关闭DS1302
return address;//返回数据
}
//读DS1302RAM字节
unsigned char TimeRamByteRead(unsigned char address)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite((address << 1) | 0xc1);//写入DS1302命令
address = TimeSpiReadWrite(0xff);//读入DS1302数据
TimeSpiClose();//关闭DS1302
return address;//返回数据
}
//写DS1302字节
void TimeByteWrite(unsigned char address, unsigned char val)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite(address);//写地址
TimeSpiReadWrite(val);//写数据
TimeSpiClose();//关闭DS1302
}
//写DS1302字节
void TimeRamByteWrite(unsigned char address, unsigned char val)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite((address << 1) | 0xc0);//写地址
TimeSpiReadWrite(val);//写数据
TimeSpiClose();//关闭DS1302
}
//使能DS1302写保护
void TimeWriteEnable(void)
{
TimeByteWrite(timewriteregall, timewriteseg);//使能DS1302写保护
}
//开放DS1302写保护
void TimeWrieDisbale(void)
{
TimeByteWrite(timewriteregall, 0);//开放DS1302写保护
}
//DS1302工作开始
void TimeWorkStart(void)
{
unsigned char val;
val = TimeByteRead(timereadseg);//读秒数据
val &= 0x7f;//打开晶振
TimeWrieDisbale();//开放DS1302写保护
TimeByteWrite(timewriteseg, val);//打开晶振
TimeByteWrite(tricklewrite, 0xa5);//打开充电二极管
TimeWriteEnable();//使能DS1302写保护
}
阅读(1774) | 评论(0) | 转发(0) |