首页 > 商业

STM32模拟SPI的程序

来源:个人图书馆-myallmy 发表时间:2023-07-31 12:32:34
分享到


(资料图片仅供参考)

最近完成的一个项目因为SPI管脚被别的资源占用了,只能通过模拟SPI对存储器进行操作。最后测试发现模拟SPI的速度也很快,和硬件SPI相比感觉不到太大的差异。现将模拟SPI的程序分享如下:

管脚定义,大家可以根据需要自行修改,任何一个GPIO都可以模拟SPI

#define  SPI_WP_PIN          GPIO_Pin_6#define  SPI_WP_PORT         GPIOC#define  SPI_CS_PIN          GPIO_Pin_15#define  SPI_CS_PORT         GPIOA#define  SPI_CLK_PIN         GPIO_Pin_3#define  SPI_CLK_PORT        GPIOB#define  SPI_MOSI_PIN        GPIO_Pin_12#define  SPI_MOSI_PORT       GPIOC#define  SPI_MISO_PIN        GPIO_Pin_4#define  SPI_MISO_PORT       GPIOB#define  SPI_BASE SPI3#define  SPI_FLASH_CS_LOW()    GPIO_ResetBits(SPI_CS_PORT, SPI_CS_PIN)  /* Select SPI MEM1: ChipSelect pin low  */#define  SPI_FLASH_CS_HIGH()   GPIO_SetBits(SPI_CS_PORT, SPI_CS_PIN)    /* Deselect SPI MEM1: ChipSelect pin high */#define  SPI_FLASH_WP_LOW()    GPIO_ResetBits(SPI_WP_PORT, SPI_WP_PIN)  //PC4#define  SPI_FLASH_WP_HIGH()   GPIO_SetBits(SPI_WP_PORT, SPI_WP_PIN)    //PC4

模拟SPI MOSI MOSI CLK

//模拟MOSIvoid SPI_MOSI(unsigned char Status){if(Status)GPIO_WriteBit(SPI_MOSI_PORT,SPI_MOSI_PIN,Bit_SET);elseGPIO_WriteBit(SPI_MOSI_PORT,SPI_MOSI_PIN,Bit_RESET);}//模拟CLKvoid SPI_CLK(unsigned char Status){if(Status)GPIO_WriteBit(SPI_CLK_PORT,SPI_CLK_PIN,Bit_SET);elseGPIO_WriteBit(SPI_CLK_PORT,SPI_CLK_PIN,Bit_RESET);}//模拟MISOunsigned char SPI_MISO(void){if(GPIO_ReadInputDataBit(SPI_MISO_PORT,SPI_MISO_PIN))return 1;else return 0;}

SPI初始化

void gd25qxxInit(void){GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN; //PA5:SCK,PA7:MOSIGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStructure);SPI_MOSI(1);GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN; //PA5:SCK,PA7:MOSIGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出GPIO_Init(SPI_CLK_PORT, &GPIO_InitStructure);SPI_CLK(1);GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(SPI_MISO_PORT, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN;                             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(SPI_CS_PORT, &GPIO_InitStructure);SPI_FLASH_CS_HIGH();GPIO_InitStructure.GPIO_Pin = SPI_WP_PIN;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(SPI_WP_PORT, &GPIO_InitStructure);SPI_FLASH_WP_HIGH();}

SPI数据输入输出

uint8_t gd25qxxSendByte(uint8_t byte){unsigned char i,Result = 0;;for(i=0;i<8;i++){if(byte & 0x80)SPI_MOSI(1);else SPI_MOSI(0);byte <<= 1;SPI_CLK(0);SPI_CLK(1);Result <<= 1;if(SPI_MISO()) Result |= 0x01;}SPI_CLK(0);return(Result);}

示例:

uint32_t gd25qxxReadID(void){  uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;  /* Select the FLASH: Chip Select low */  SPI_FLASH_CS_LOW();  /* Send "RDID " instruction */  gd25qxxSendByte(0x9F);  /* Read a byte from the FLASH */  Temp0 = gd25qxxSendByte(DUMMY_BYTE);  /* Read a byte from the FLASH */  Temp1 = gd25qxxSendByte(DUMMY_BYTE);  /* Read a byte from the FLASH */  Temp2 = gd25qxxSendByte(DUMMY_BYTE);  /* Deselect the FLASH: Chip Select high */  SPI_FLASH_CS_HIGH();  Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;  return Temp;}

关键词:

保安全、拿资格、抠细节 冬运中心全面进入北京冬奥会临战状态

2021-12-16

人民财评:稳步朝着实现共同富裕的目标迈进

2021-12-16

靠双手让日子变得更美好

2021-12-16

第一观察·瞬间 | 中俄元首视频会晤:相约北京见,携手向未来

2021-12-16

世卫组织:全球累计新冠确诊病例达270791973例

2021-12-16

他们为何奔向先行示范区——2021深圳全球招商大会透视

2021-12-16

Copyright   2015-2022 华中木材网 版权所有  备案号:京ICP备12018864号-26   联系邮箱:2 913 236 @qq.com