SPI interface simulation by 51 mcu for GM8142

本文介绍了一种使用8051系列单片机模拟SPI接口的方法,适用于GM8141和GM8142应用。文章提供了初始化SPI接口、发送接收16位数据的具体代码实现。

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);
}
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值