Microchip
直播中

杨秀英

7年用户 1390经验值
私信 关注
[问答]

怎么使用PIC24FJ256GA704在W25Q16 EEPROM上读写

嗨,我需要帮助。我正在尝试使用一个PIC24FJ256GA704在W25Q16 EEPROM上写和读。我需要为页面写3字节,确认这是写的,然后读它们。当EEPROM芯片满时,我必须保存第一扇区上的最后3个字节,并且擦除芯片的其他扇区。我用MCC用XC16编译器配置SPI和MPLAB X 3.65。我写的代码:SPI.HSPI.C:对我好一点……我是新手!谢谢你的帮助。

以上来自于百度翻译


      以下为原文

    Hi,
I need help.
I'm trying to write and read on a w25q16 eeprom using a PIC24FJ256GA704. I need to write 3 bytes for page, verify if this are written, and read them. When the eeprom chip is full, I have to save the last 3 bytes on the first sector, and the erase the other sectors of the chip. I used MCC to configure SPI and MPLAB X 3.65 with XC16 compiler.
The code I wrote:
SPI.h


#ifndef SPI_H
#define SPI_H

#ifdef __cplusplus
extern "C" {
#endif




#ifdef __cplusplus
}
#endif

#endif /* SPI_H */

#include "utils.h"
#include "SPI_eeprom.h"

void spi_send_8bit(uint8_t cmd);
void spi_send_address(uint32_t address);
void spi_write_page(uint32_t address, uint8_t data);
uint8_t spi_read(uint32_t address);



SPI.c:


#include "SPI.h"

void spi_send_8bit(uint8_t cmd)
{
cmd = flipbits(cmd);
uint8_t buff[8] = {0};
int i = 0;
for (i = 0; i < 8; i++)
{
if ((cmd & 1) > 0)
buff = 1;
else
buff = 0;
}
PCSO_LAT = set_low;
SPI1_Exchange(buff, NULL);
PCSO_LAT = set_high;
}

void spi_send_address(uint32_t address)
{
address = flip32bit(address);
uint8_t buff[32] = {0};
int i = 0;
for( i = 0; i < 24; i++)
{
if((address & 1) > 0)
buff = 1;
else
buff = 0;
PCSO_LAT = set_low;
SPI1_Exchange(buff, NULL);
PCSO_LAT = set_high;
}
}

void spi_write_page(uint32_t address, uint8_t data)
{
spi_send_8bit(CMD_WREN); //write enable
spi_send_address(address);
spi_send_8bit(data);
}
uint8_t spi_read(uint32_t address)
{
spi_send_address(address);
spi_send_8bit(CMD_READ);
return SPI1_Exchange8bit(NULL);
}



Be kind with me...I'm a newbie!
Thanks in advance for your help.

回帖(5)

薄坤坤

2019-2-21 15:10:01
你的问题是什么?代码是否如预期的那样工作?如果是这样的话,那么你需要告诉我们它在做什么是错误的。苏珊

以上来自于百度翻译


      以下为原文

    What is your question?
Does you code not work as expected? If that is the case then you need to tell us what it is doing that is wrong.
Susan
举报

俞敏东

2019-2-21 15:29:46
对不起,我忘了这个问题。代码(部分代码)不像预期的那样工作,所以我需要一些帮助,因为我不理解SPI通信是如何在这个PIC上使用这种闪存(WiNoLTW25Q16)的。谢谢您的回复!

以上来自于百度翻译


      以下为原文

    Sorry...I forgot the question. 
The code (partial code) is not working as expected, so I need some help because I didn't understood how SPI communication works on this PIC with this type of flash memory (winbond w25q16). 
Thanks for your reply! 
举报

王焕树

2019-2-21 15:46:38
苏珊问了三个问题。你回答了“1”和“2”,“3”是用来解释你所期望的和你得到的。

以上来自于百度翻译


      以下为原文

    Susan asked three questions. You answered #1 and #2.
#3 was to explain what you expected, and what you got.
 
举报

薄坤坤

2019-2-21 15:52:36
当我们等待您的答复时,这里有一些可能对您有帮助的信息。SPI是一个相当低的协议,它在主设备和从设备之间交换两个值:主设备向从属服务器发送一个值,同时从服务器向主机发送一个值。那就是全部!在硬件级别,您需要确保数据被放到总线上,并在主时钟和从机上读取相同的时钟转换。在微芯片世界中,这意味着在某些情况下获得CKP和CKE位以及SMP位。在上面,您需要在任何时间点定义值“均值”。通常是从属设备定义它需要什么信息以及它将发送什么信息。在这种情况下,你需要查看W25Q16 EEPROM数据表,以查看它期望什么值以及它将如何解释这些值。数据表显示,对于它所理解的每个命令,命令值(主将发送给从机;主机也将忽略此时从属服务器发送的任何内容)以及此后必须交换的任何值。例如,数据表的图8显示了“读”。结构。它显示了主控器发送指令,3个地址值(而忽略了从属服务器可能返回的任何内容),然后发送一个虚拟值(从属者将忽略此时间),而从属服务器从内存中返回该值。如果我是你,我会先确保SPI连接工作正常。你可以通过读取JEDEC ID或设备ID值来做这件事,因为你应该知道你将从奴隶那里得到什么。如果你不是这样的话,你需要解决这个问题,否则就没有其他的工作了,但是如果你能做到这一点,那么你就可以根据你的知识来执行你真正需要使用的操作。我不知道“fLIPTITE”函数是什么,以及为什么你要在“sEndoSpix8BIT”函数中解释“CMD”参数的位(我想你还是有一个bug),但是这对于一些非常简单的事情来说似乎是很有用的。苏珊

以上来自于百度翻译


      以下为原文

    While we wait for your reply, here is some information that might be helpful to you.
SPI is a fairly low level protocol that exchanges two values between a master and a slave device: the master sends a value to the slave at the same time as the slave sends a value back to the master. That is all it is!
At the hardware level you need to make sure that the data is being put onto the bus and being read on the same clock transitions by both the master and the slave. In the Microchip world, that means getting the CKP and CKE bits correct as well as the SMP bit in some cases.
Above that you need to define what the values "mean" at any point in time. Quite often it is the slave device that defines what information it needs and what information it will send back.
In your case, you need to look at the W25Q16 EEPROM data sheet to see what values it expects and how it will interpret those values. The data sheet shows, for each command that it understands, the command value (which the master will send to the slave; the master will also ignore whatever the slave sends back at this time), and any values that must be exchanged after that.
For example, Figure 8 of the data sheet shows a "read" instruction. It shows the master sending the instruction, 3 address values (all the while ignoring whatever the slave may be sending back) and then sending a dummy value (which the slave will ignore this time) while the slave sends back the value from memory.
All of that is the protocol that is define *above* the SPI protocol.
If I were you, I would start off by making sure that the SPI connection is working. You could do that by reading the JEDEC ID, or Device ID values as you should know what you will be getting back from the slave. If you are not then you need to sort out the problem or nothing else will work,
However if you can do that, then you can build on your knowledge to perform the operations you really need to use.
Looking at your code, you seem to be making life a bit hard for yourself. I have no idea what the 'flipbits' function does and why you are trying to interpret the bits of the 'cmd' parameter in the 'send_spi_8bits' function (and I think you have a bug there anyway) but this seems to be a lot of work for something that is really very simple.
Susan
举报

更多回帖

发帖
×
20
完善资料,
赚取积分