SPI interface simulation by 51 mcu for GM8142
/***********************************************************
Name: SPI_Sim.c
Descibe: SPI interface simulation by 8051 series mcu, only
for GM8141 GM8142 application
Version: 1.0
Contents: Include send and receive data from SPI interface
www.fosvos.com
***********************************************************/
#i nclude <reg51.h>
#define HIGH 1
#define LOW 0
#define FALSE 0
#define TRUE 1
#define unsigned char BYTE
#define unsigned int WORD
#define DELAY_TIME 5 //User can modify the name
extern sbit SCLK,DIN,DOUT; //User can modify the name
/***********************************************************
: Delay
Describe: Time delay
Input: Byte formated time
Output: void
***********************************************************/
void Delay(BYTE Delay_Time)
{
while(--Delay_Time){;}
}
/***********************************************************
: SPI_Initial
Describe: Initialize the SPI interface
Input: void
Output: void
***********************************************************/
void SPI_Initial(void)
{
SCLK = LOW;
DIN = LOW;
DOUT = LOW;
Delay(DELAY_TIME);
}
/***********************************************************
: SPI_Transmit
Describe: Send and receive 16-bit data from the SPI interface
Input: 16-bit data to be send
Output: 16-bit data of read, user analyse the validity
***********************************************************/
WORD SPI_Transmit(WORD wSendData)
{
BYTE i;
WORD wReadData;
wReadData = 0;
for(i = 0; i < 16; i++)
{
if(wSendData & 0x8000)
DIN = 1;
else
DIN = 0;
wReadData <<= 1; //Shift first, then fill the bit
SCLK = HIGH;
wSendData <<= 1;
wReadData |= DOUT; //Receive the 16-bit data
Delay(DELAY_TIME);
SCLK = LOW;
Delay(DELAY_TIME); //Can be comment by acture use
}
return(wReadData);
}
Name: SPI_Sim.c
Descibe: SPI interface simulation by 8051 series mcu, only
for GM8141 GM8142 application
Version: 1.0
Contents: Include send and receive data from SPI interface
www.fosvos.com
***********************************************************/
#i nclude <reg51.h>
#define HIGH 1
#define LOW 0
#define FALSE 0
#define TRUE 1
#define unsigned char BYTE
#define unsigned int WORD
#define DELAY_TIME 5 //User can modify the name
extern sbit SCLK,DIN,DOUT; //User can modify the name
/***********************************************************
: Delay
Describe: Time delay
Input: Byte formated time
Output: void
***********************************************************/
void Delay(BYTE Delay_Time)
{
while(--Delay_Time){;}
}
/***********************************************************
: SPI_Initial
Describe: Initialize the SPI interface
Input: void
Output: void
***********************************************************/
void SPI_Initial(void)
{
SCLK = LOW;
DIN = LOW;
DOUT = LOW;
Delay(DELAY_TIME);
}
/***********************************************************
: SPI_Transmit
Describe: Send and receive 16-bit data from the SPI interface
Input: 16-bit data to be send
Output: 16-bit data of read, user analyse the validity
***********************************************************/
WORD SPI_Transmit(WORD wSendData)
{
BYTE i;
WORD wReadData;
wReadData = 0;
for(i = 0; i < 16; i++)
{
if(wSendData & 0x8000)
DIN = 1;
else
DIN = 0;
wReadData <<= 1; //Shift first, then fill the bit
SCLK = HIGH;
wSendData <<= 1;
wReadData |= DOUT; //Receive the 16-bit data
Delay(DELAY_TIME);
SCLK = LOW;
Delay(DELAY_TIME); //Can be comment by acture use
}
return(wReadData);
}
本文介绍了一种使用8051系列单片机模拟SPI接口的方法,适用于GM8141和GM8142应用。文章提供了初始化SPI接口、发送接收16位数据的具体代码实现。

被折叠的 条评论
为什么被折叠?



