SPI 通讯时序 在模拟中学习

有图,在CU里有图
SPI波形要按bit倒过来看。如果从低位开始发送。

/

///

///

/

//

//-----------------------函数声明,变量定义--------------------------------------------------------
#include
#include
sbit SCK=P1^0; // 将p1.0口模拟时钟输出
sbit MOSI=P1^1; // 将p1.1口模拟主机输出
sbit MISO=P1^2; // 将p1.1口模拟主机输入
sbit SS1=P1^3; // 将p1.1口模拟片选
#define delayNOP(); {nop();nop();nop();nop();};
//--------------------------------------------------------------------------------------------------
// 函数名称: SPISendByte
// 入口参数: ch
// 函数功能: 发送一个字节
//--------------------------------------------------------------------------------------------------
void SPISendByte(uint8_t ch)
{
uint8_t idata n=8; // 向SDA上发送一位数据字节,共八位
SCK = 1 ; //时钟置高
SS1 = 0 ; //选择从机

while(n–)
{
delayNOP();
SCK = 0 ; //时钟置低
if((ch&0x80) == 0x80){ // 若要发送的数据{BANNED}最佳高位为1则发送位1
SDA_MOSI = 1; // 传送位1
}
else{
SDA_MOSI = 0; // 否则传送位0
}
delayNOP();
ch = ch<<1; // 数据左移一位
SCK = 1 ; //时钟置高
}
}
//--------------------------------------------------------------------------------------------------
// 函数名称: SPIreceiveByte
// 返回接收的数据
// 函数功能: 接收一字节子程序
//--------------------------------------------------------------------------------------------------
uint8_t SPIreceiveByte()
{
uint8_t idata n=8; // 从MISO线上读取一上数据字节,共八位
uint8_t tdata;
SCK = 1; //时钟为高
SS1 = 0; //选择从机
while(n–)
{
delayNOP();
SCK = 0; //时钟为低
delayNOP();
tdata = tdata<<1; // 左移一位,或_crol_(temp,1)
if(SDA_MISO == 1)
tdata = tdata|0x01; // 若接收到的位为1,则数据的{BANNED}最佳后一位置1
else
tdata = tdata&0xfe; // 否则数据的{BANNED}最佳后一位置0
SCK=1;
}
return(tdata);
}

///

static void SPI_Start(u8 ch)
{
//初始化
SPI_SCL_L();
SPI_SDA_H();
SPI_delay();
SPI_CS_L();
SPI_CSII_L();
SPI_delay();
}

//IIC停止位
static void SPI_Stop(u8 ch)
{
SPI_SCL_L();
SPI_SDA_H();
SPI_CS_H();
SPI_CSII_H(); //eason
SPI_delay();
SPI_SCL_H();
SPI_delay();
}

//Send Byte(LSB)
static void SPI_SendByte(u8 cData, u8 cLen)
{
u8 i=cLen;

while(i–)
{
if(cData & 0x01)
SPI_SDA_H();
else
SPI_SDA_L();

cData >>= 1;

SPI_delay();
SPI_SCL_H();
SPI_delay();
SPI_SCL_L();
SPI_delay();
}
}

//Receive Byte(LSB)
static u8 SPI_ReceiveByte(u8 cLen)
{
u8 k=0,i=cLen;
u8 tmp = 0;
k=8-cLen;
SPI_SDA_H();
SPI_delay();
while(i–)
{
tmp >>= 1;
SPI_SCL_H();
SPI_delay();
if(SPI_SDA_IN())
{
tmp |= 0x80;
}
SPI_SCL_L();
SPI_delay();
}
for(;k>0;k–){
tmp>>=1;
}
//
return tmp;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xx-xx-xxx-xxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值