提供一个STM32关于W25Q64的跨页写入的代码
时间: 2024-05-13 15:13:29 浏览: 127
以下是一个基于STM32的关于W25Q64的跨页写入的示例代码:
```
#include "stm32f4xx.h"
#include "spi.h"
#include "w25q64.h"
void W25Q64_Write_Page(uint32_t addr, uint8_t *data, uint16_t len)
{
uint16_t page_offset = addr % W25Q64_PAGE_SIZE;
uint16_t write_len = W25Q64_PAGE_SIZE - page_offset;
if (write_len > len) {
write_len = len;
}
W25Q64_Write_Enable();
SPI_CS_LOW();
SPI_Transmit(W25Q64_CMD_PAGE_PROGRAM);
SPI_Transmit(addr >> 16);
SPI_Transmit(addr >> 8);
SPI_Transmit(addr);
for (uint16_t i = 0; i < write_len; i++) {
SPI_Transmit(data[i]);
}
SPI_CS_HIGH();
len -= write_len;
data += write_len;
addr += write_len;
while (len > 0) {
write_len = W25Q64_PAGE_SIZE;
if (write_len > len) {
write_len = len;
}
W25Q64_Write_Enable();
SPI_CS_LOW();
SPI_Transmit(W25Q64_CMD_PAGE_PROGRAM);
SPI_Transmit(addr >> 16);
SPI_Transmit(addr >> 8);
SPI_Transmit(addr);
for (uint16_t i = 0; i < write_len; i++) {
SPI_Transmit(data[i]);
}
SPI_CS_HIGH();
len -= write_len;
data += write_len;
addr += write_len;
}
W25Q64_Write_Disable();
}
```
该代码首先计算出要写入的数据跨越的页数和每个页中的偏移量。然后,它使用W25Q64_Write_Enable()函数启用写入操作,使用SPI_Transmit()函数将数据写入W25Q64,使用W25Q64_Write_Disable()函数禁用写入操作。在写入操作的过程中,该代码使用SPI_CS_LOW()函数将片选信号拉低,使用SPI_CS_HIGH()函数将片选信号拉高。此外,该代码使用SPI_Transmit()函数将W25Q64的指令和地址写入SPI总线。
阅读全文