stm32 普通gpio读写FM25CL64 多个数据
时间: 2023-09-09 07:14:27 浏览: 170
首先,你需要将 FM25CL64 连接到 STM32 的 GPIO 引脚上,并配置 STM32 的 GPIO 为输出或输入模式。然后,你需要编写代码来读写 FM25CL64。以下是一个简单的示例代码:
```c
#include "stm32f4xx.h"
#include "fm25cl64.h"
#define FM25CL64_CS_PIN GPIO_Pin_0
#define FM25CL64_CS_PORT GPIOA
void fm25cl64_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = FM25CL64_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FM25CL64_CS_PORT, &GPIO_InitStructure);
}
void fm25cl64_write(uint16_t addr, uint8_t *data, uint16_t len)
{
GPIO_ResetBits(FM25CL64_CS_PORT, FM25CL64_CS_PIN);
// 发送写入命令
SPI_SendData(SPI1, FM25CL64_WRITE_CMD);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
// 发送地址
SPI_SendData(SPI1, addr >> 8);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
SPI_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
// 发送数据
for (int i = 0; i < len; i++) {
SPI_SendData(SPI1, data[i]);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
GPIO_SetBits(FM25CL64_CS_PORT, FM25CL64_CS_PIN);
}
void fm25cl64_read(uint16_t addr, uint8_t *data, uint16_t len)
{
GPIO_ResetBits(FM25CL64_CS_PORT, FM25CL64_CS_PIN);
// 发送读取命令
SPI_SendData(SPI1, FM25CL64_READ_CMD);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
// 发送地址
SPI_SendData(SPI1, addr >> 8);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
SPI_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
// 读取数据
for (int i = 0; i < len; i++) {
SPI_SendData(SPI1, 0x00);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
data[i] = SPI_I2S_ReceiveData(SPI1);
}
GPIO_SetBits(FM25CL64_CS_PORT, FM25CL64_CS_PIN);
}
```
在上面的代码中,`fm25cl64_init()` 函数用于初始化 FM25CL64,将 CS 引脚配置为输出模式,`fm25cl64_write()` 函数用于向 FM25CL64 写入数据,`fm25cl64_read()` 函数用于从 FM25CL64 读取数据。你可以根据自己的需求修改代码。
阅读全文