spi协议在51单片机上的驱动实现(Drive implementation of SPI protocol on 51 single chip microcomputer)
spi协议在51单片机上的驱动实现(Drive implementation of SPI
protocol on 51 single chip microcomputer)
SPI protocol on the 51 MCU driver to achieve the author lxpkaka date 2009-2-13 0:56:00
Zero
Recommend
//----------------------- function declarations, variable definition
--------------------------------------------------------
I, nclude,
I, nclude,
Sbit SCK=P1^0; / / P1.0 analog clock output
Sbit MOSI=P1^1; / / P1.1 analog output will host
Sbit MISO=P1^2; / / P1.1 analog input host
Sbit SS1=P1^3; / / p1.3 analog chip select
#define delayNOP (); {_nop_ (); _nop_ (); _nop_ (); _nop_ ();};
//--------------------------------------------------------------------------------------------------
Function name: / / SPISendByte
Entrance parameters: / / Ch
/ / function: Send a byte
//--------------------------------------------------------------------------------------------------
Void SPISendByte (unsigned, char, CH)
{
Unsigned char IData n=8; / / to the SDA to send a byte of data,
a total of eight
SCK = 1; / / set high clock
SS1 = 0; / / select from machine
While (n--)
{
DelayNOP ();
SCK = 0; / / set low clock
If ((ch&0x80) = 0x80) / / if the data to be sent to the highest
position for 1 bit 1 is sent
{
MOSI = 1; / / transfer 1
}
Else
{
MOSI = 0; / / or transfer 0
}
DelayNOP ();
Ch = ch<<1; / / data left a
SCK = 1; / / set high clock
}
}
//---------------------------------------------------------
-----------------------------------------
Function name: / / SPIreceiveByte
/ / return data
/ / function: receive byte subroutine
//---------------------------------------------------------
-----------------------------------------
Unsigned, char, SPIreceiveByte ()
{
Unsigned char IData n=8; / / read data byte from the MISO line,
a total of eight
Unsigned char tdata;
SCK = 1; / / clock is high
SS1 = 0; / / select from machine
While (n--)
{
DelayNOP ();
SCK = 0; / / clock is low
DelayNOP ();
Tdata = tdata<<1; / / the left one, or _crol_ (temp, 1)
If (MISO = 1)
Tdata = tdata|0x01; / / if the received bit is 1, then the data of the last 1 position
Else
Tdata = tdata&0xfe; / / or the last 0 position data
SCK=1;
}
Return (Tdata);
}
//--------------------------------------------------------------------------------------------------
Function name: / / SPIsend_receiveByte
Entrance parameters: / / Ch
/ / return data
/ / function: serial input / output subroutine
//--------------------------------------------------------------------------------------------------
Unsigned char SPIsend_receiveByte (unsigned char CH)
{
Unsigned char IData n=8; / / read data byte from the MISO line, a total of eight
Unsigned char tdata;
SCK = 1; / / clock is high
SS1 = 0; / / select from machine
While (n--)
{
DelayNOP ();
SCK = 0; / / clock is low
DelayNOP ();
{
Tdata = tdata<<1; / / the left one, or _crol_ (temp, 1)
If (MISO = 1)
Tdata = tdata|0x01; / / if the received bit is 1, then the data of the last 1 position
Else
Tdata = tdata&0xfe; / / or the last 0 position data
}
{
If ((ch&0x80) = 0x80) / / if the data to be sent to the highest
position for 1 bit 1 is sent
{
MOSI = 1; / / transfer 1
}
Else
{
MOSI = 0; / / or transfer 0
}
Ch = ch<<1; / / data left a
}
SCK=1;
}
Return (Tdata);
}
本文档为【spi协议在51单片机上的驱动实现(Drive implementation of SPI protocol on 51 single chip microcomputer)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。