嵌入式-软件模拟SPI通信

使用软件模拟SPI通信,扩展SPI资源

/********************************************************************************
*
*
* 设计软件模拟SPI通信的接口
* author:jindouliu2024@163.com 
* date:2025.4.10
* 
*
* Copyright (c)  2024-2025   jindouliu2024@163.com   All right Reserved
* *********************************************************************************/

SPI.C

#include "stm32f10x.h"                  // Device header
void myspi_ss(uint8_t bitvalue)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)bitvalue);
}
void myspi_clk(uint8_t bitvalue)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)bitvalue);
}
void myspi_mosi(uint8_t bitvalue)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_7,(BitAction)bitvalue);
}
uint8_t myspi_miso(void)
{
	return GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6);
}
void myspi_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;					
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;				
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	myspi_ss(1);
	myspi_clk(0);
	
}

void myspi_start()
{
	myspi_ss(0);
}
void myspi_stop()
{
	myspi_ss(1);
}

uint8_t myspi_swapbyte(uint8_t sendbyte)
{
	uint8_t i,receivebyte=0x00;
		for(i=0;i<8;i++)
		{
			myspi_mosi(sendbyte&(0x80>>i));
			myspi_clk(1);
			if(myspi_miso()==1){receivebyte=receivebyte|(0x80>>i);}
			myspi_clk(0);
		}
		return receivebyte;

}

	

SPI.h

#ifndef __MYSPI_H
#define __MYSPI_H
void myspi_Init();
void myspi_start();
void myspi_stop();
uint8_t myspi_swapbyte(uint8_t sendbyte);
#endif

#include "stm32f10x.h"                  // Device header
void myspi_ss(uint8_t bitvalue)
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)bitvalue);
}
//void myspi_clk(uint8_t bitvalue)
//{
//	GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)bitvalue);
//}
//void myspi_mosi(uint8_t bitvalue)
//{
//	GPIO_WriteBit(GPIOA,GPIO_Pin_7,(BitAction)bitvalue);
//}
//uint8_t myspi_miso(void)
//{
//	return GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6);
//}
void myspi_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;					
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;		
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;				
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
//	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;				
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	SPI_InitTypeDef SPI_InitStruct;
	SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStruct.SPI_CRCPolynomial = 7;
	SPI_Init(SPI1,&SPI_InitStruct);
	SPI_Cmd(SPI1,ENABLE);
	
	myspi_ss(1);
	//myspi_clk(0);
	
}

void myspi_start()
{
	myspi_ss(0);
}
void myspi_stop()
{
	myspi_ss(1);
}

uint8_t myspi_swapbyte(uint8_t sendbyte)
{
//	uint8_t i,receivebyte=0x00;
//		for(i=0;i<8;i++)
//		{
//			myspi_mosi(sendbyte&(0x80>>i));
//			myspi_clk(1);
//			if(myspi_miso()==1){receivebyte=receivebyte|(0x80>>i);}
//			myspi_clk(0);
//		}
//		return receivebyte;
	while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)!=SET);
	SPI_I2S_SendData(SPI1,sendbyte);
	while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)!=SET);
	return SPI_I2S_ReceiveData(SPI1);
}

	
posted @ 2025-04-10 20:37  LRadian  阅读(81)  评论(0)    收藏  举报