⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.c

📁 用c语言编写的SPI总线在单片机中的应用源代码
💻 C
字号:
#include<reg51.h>
#include<intrins.h>
sbit SCK=P1^0;//将p1口摸仪时钟输出
sbit MOSI=P1^1;//将P1.1口模拟主机输出
sbit MISO=P1^2;//将p1.2口模拟主机输入
sbit CS1=P1^3;//将P1.3模拟片选
#define delayNOP();{_nop_();_nop_();_nop_();_nop_();};
//---------------------------------------------------------
//函数名称:SPISENDBYTE
//入口参数:CH
//函数功能:发送一个字符
//-----------------------------------------------------------
void SPISendByte(unsigned char ch)
{unsigned char idata n=8;//向SDA上发送一位数据字节,共8位
SCK=1;//时钟置高
CS1=0;//选择从机
while(n--)
  {delayNOP();
   SCK=0;//时钟置低
  if((ch&0x80)==0x80)//若发送的数据等于1,则发送位1
    {MOSI=1;//传送位1
    }
  else
    {MOSI=0;//否则传送0
    }
  delayNOP();
  ch=ch<<1;//数据左移一位
  SCK=1;//时钟置高
  }
}
//-------------------------------------------------------------------
//函数名称SPIreceiveByte
//返回接收的数据
//函数功能:接受1BYTE子程序
//--------------------------------------------------------------------
unsigned char SPIreceiveByte()
{
  unsigned char idata n=8;//从MISO线上读取上一字节,共8位
  unsigned char tdata;
  SCK=1;//时钟为高
  CS1=0;//选择从机
  while(n--)
  {  delayNOP();
     SCK=0;//时钟为低
	 delayNOP();
	 tdata=tdata<<1;//左移一位,或_crol_(temp,1)
	 if(MISO==1)
	    tdata=tdata|0x01;//若接收到的位为1,则数据的最后的一位置1
	 else
        tdata=tdata&0xfe;//否则数据的最后一位置0
	 SCK=1;
  }
  return(tdata);
}
//---------------------------------------------------------------------------
//函数名称:SPIsend-receiveByte
//入口参数:CH
//返回接收的数据
//函数功能:串行输入/输出子程序
//----------------------------------------------------------------------------
//unsigned char SPIsendByte(unsigned char ch)
//{ 
 // unsigned char idata n=8;//从MISO线上读取上一数据字节,共8位
 // unsigned char tdata;
  //SCK=1;//时钟为高
  //CS1=0;//选择从机
 // while(n--)
 // {delayNOP();
   //  SCK=0;//时钟为低
	// delayNOP();
    // tdata=tdata<<1;//左移一位
	 // if(MISO==1)
	   // tdata=tdata|0x01;//若接收到的位为1,则数据的最后一位置1
	  //else
	   // tdata=tdata&0xfe;//否则数据的最后一位置0
      //if((ch&0x80)==0x80)
	  //{ MOSI=1;//传送位1
	  //}
	// else
	 // {MOSI=0;//否则传送位0
	 // }
	   // ch=ch<<1;//数据左移1位
	   // SCK=1;
 // }
//return(tdata);
//} 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -