STM32模拟软件SPI的8种模式

头文件

soft_spi.h

#ifndef __SOFT_SPI_H
#define __SOFT_SPI_H

#ifdef __cplusplus
 extern "C" {
#endif

#include "sys.h"
#if SYSTEM_SUPPORT_OS
#include "maintask.h"
#endif

//引脚定义
#define SoftSpi_MISO_PIN 		GPIO_Pin_8
#define SoftSpi_MISO_GPIO_PORT 	GPIOB
#define SoftSpi_MOSI_PIN 		GPIO_Pin_9
#define SoftSpi_MOSI_GPIO_PORT 	GPIOB
#define SoftSpi_CLK_PIN 		GPIO_Pin_10
#define SoftSpi_CLK_GPIO_PORT 	GPIOB

//使用位带操作
#define SoftSpi_MISO			PBin(8)
#define SoftSpi_MOSI			PBout(9)
#define SoftSpi_CLK				PBout(10)

//外设时钟使能
#define SoftSpi_CLK_ENABLE()	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE)

//SPI模式选择
//0:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),MSB先行
//1:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),MSB先行
//2:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),MSB先行
//3:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),MSB先行
//4:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),LSB先行
//5:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),LSB先行
//6:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),LSB先行
//7:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),LSB先行
#define SoftSpiMode 0

void SoftSpi_Init(void);
uint8_t SoftSpi_ReadWriteByte(uint8_t dat);

#ifdef __cplusplus
}
#endif

#endif

C文件

soft_spi.c

#include "soft_spi/soft_spi.h"
#include "delay.h"

//根据具体的SPI通信速度设置延时
//#define SoftSpi_Delay() delay_us(1)
#define SoftSpi_Delay() //定义为空,不延时
/**
  * @brief  软件SPI配置
  */
void SoftSpi_Init(void)
{    	 
	GPIO_InitTypeDef  GPIO_InitStructure;

	SoftSpi_CLK_ENABLE();//使能GPIO时钟

	//输入配置
	GPIO_InitStructure.GPIO_Pin = SoftSpi_MISO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
//	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
	GPIO_Init(SoftSpi_MISO_GPIO_PORT, &GPIO_InitStructure);//初始化GPIO

	//输出配置
	GPIO_InitStructure.GPIO_Pin = SoftSpi_MOSI_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(SoftSpi_MOSI_GPIO_PORT, &GPIO_InitStructure);//初始化GPIO
	
	GPIO_InitStructure.GPIO_Pin = SoftSpi_CLK_PIN;
	GPIO_Init(SoftSpi_CLK_GPIO_PORT, &GPIO_InitStructure);//初始化GPIO
	
	SoftSpi_MOSI = 1;
#if SoftSpiMode==0||SoftSpiMode==1||SoftSpiMode==4||SoftSpiMode==5
	SoftSpi_CLK = 0;//模式0、1、4、5,空闲的时候为低电平
#else
	SoftSpi_CLK = 1;//模式2、3、6、7,空闲的时候为高电平
#endif
}

/**
  * @brief	SPI读写一个字节
  * @param	dat -> 要写的一个字节数据
  * @retval	读到的数据
  */ 
uint8_t SoftSpi_ReadWriteByte(uint8_t dat)
{
	uint8_t reveDat,i;
	reveDat = 0;
#if SoftSpiMode==0 //0:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),MSB先行
	for(i=0;i<8;i++)
	{		
		if(dat&0x80) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat*=2;
		SoftSpi_CLK = 1;
		SoftSpi_Delay();
		reveDat*=2;
		if(SoftSpi_MISO) reveDat++;
		SoftSpi_CLK = 0;
		SoftSpi_Delay();
	}
#elif SoftSpiMode==1 //1:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),MSB先行
	for(i=0;i<8;i++)
	{			
		SoftSpi_CLK = 1;
		if(dat&0x80) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat*=2;
		SoftSpi_Delay();
		SoftSpi_CLK = 0;
		reveDat*=2;
		if(SoftSpi_MISO) reveDat++;
		SoftSpi_Delay();
	}
#elif SoftSpiMode==2 //2:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),MSB先行
	for(i=0;i<8;i++)
	{			
		if(dat&0x80) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat*=2;
		SoftSpi_Delay();
		SoftSpi_CLK = 0;
		reveDat*=2;
		if(SoftSpi_MISO) reveDat++;
		SoftSpi_Delay();
		SoftSpi_CLK = 1;
	}
#elif SoftSpiMode==3 //3:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),MSB先行
	for(i=0;i<8;i++)
	{			
		SoftSpi_CLK = 0;
		if(dat&0x80) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat*=2;
		SoftSpi_Delay();
		SoftSpi_CLK = 1;
		reveDat*=2;
		if(SoftSpi_MISO) reveDat++;
		SoftSpi_Delay();
	}
	#elif SoftSpiMode==4 //4:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),LSB先行
	for(i=0;i<8;i++)
	{		
		if(dat&0x01) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat/=2;
		SoftSpi_CLK = 1;
		SoftSpi_Delay();
		reveDat/=2;
		if(SoftSpi_MISO) reveDat+=8;
		SoftSpi_CLK = 0;
		SoftSpi_Delay();
	}
#elif SoftSpiMode==5 //5:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),LSB先行
	for(i=0;i<8;i++)
	{			
		SoftSpi_CLK = 1;
		if(dat&0x01) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat/=2;
		SoftSpi_Delay();
		SoftSpi_CLK = 0;
		reveDat/=2;
		if(SoftSpi_MISO) reveDat+=8;
		SoftSpi_Delay();
	}
#elif SoftSpiMode==6 //6:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),LSB先行
	for(i=0;i<8;i++)
	{			
		if(dat&0x01) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat/=2;
		SoftSpi_Delay();
		SoftSpi_CLK = 0;
		reveDat/=2;
		if(SoftSpi_MISO) reveDat+=8;
		SoftSpi_Delay();
		SoftSpi_CLK = 1;
	}
#elif SoftSpiMode==7 //7:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),LSB先行
	for(i=0;i<8;i++)
	{			
		SoftSpi_CLK = 0;
		if(dat&0x01) SoftSpi_MOSI=1;
		else SoftSpi_MOSI=0;
		dat/=2;
		SoftSpi_Delay();
		SoftSpi_CLK = 1;
		reveDat/=2;
		if(SoftSpi_MISO) reveDat+=8;
		SoftSpi_Delay();
	}
#endif
	return reveDat;
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

拾风染尘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值