当然,我们会用就算数,只是需要掌握下面6个命令:
时序图就是SPI的规则:
接下来看读写命令字:
全都是鸡肠,要怪就怪清朝无能。。。
注意读和写的忙碌标志在最后一位,判断的时候,
只要大于0 ,就是忙!
接着看写使能:
记得,在写之前要先擦除!
数据读取命令字:03
8.2.8 Read Data (03h)
The Read Data instruction allows one or more data bytes to be sequentially read from the memory.
The instruction is initiated by driving the /CS pin low and then shifting the instruction code "03h"l followed by a 24-bit address (A23-A0) into the DI pin. The code and address bits are latched on the rising edge of the CLK pin. After the address is received, the data byte of the addressed memoryl location will be shifted out on the DO pin at the falling edge of CLK with most significant bit (MSB) first.
The address is automatically incremented to the next higher address after each byte of data is shifted out allowing for a continuous stream of data. This means that the entire memory can be accessed with a single instruction as long as the clock continues. The instruction is completed by driving /CS high.
这里有个坑,本质上SPI是移位寄存器,所以主机要先发送2 个时钟脉冲,才能完成读取
8.2.14 Page Program (02h)
The Page Program instruction allows from one byte to 256 bytes (a page) of data to be programmed at previously erased (FFh) memory locations. A Write Enable instruction must be executed before the device will accept the Page Program Instruction (Status Register bit WEL -1). The instruction is initiated by driving the /CS pin low then shifting the instruction code "02h"followed by a 24-bit address
(A23-A0) and at least one data byte, into the DI pin. The /CS pin must be held low for the entire length of the instruction while data is being sent to the device.
If an entire 256 byte page is to be programmed, the last address byte (the 8 least significant address bits) should be set to 0. If the last address byte is not zero, and the number of clocks exceeds the remaining page length, the addressing will wrap to the beginning of the page. In some cases, less than 256 bytes (a partial page) can be programmed without having any effect on other bytes within the same page. One condition to perform a partial page program is that the number of clocks cannot exceed the remaining page length. If more than 256 bytes are sent to the device the addressing will wrap to the beginning of the page and overwrite previously sent data.
As with the write and erase instructions, the /CS pin must be driven high after the eighth bit of the last byte has been latched. If this is not done the Page Program instruction will not be executed. After /CS is driven high, the self-timed Page Program instruction will commence for a time duration of tpp (See AC Characteristics). While the Page Program cycle is in progress, the Read Status Register instruction may still be accessed for checking the status of the BUSY bit. The BUSY bit is a 1 during the Page Program cycle and becomes a 0 when the cycle is finished and the device is ready to accept other instructions again. After the Page Program cycle has finished the Write Enable Latch (WEL) bit in the Status Register is cleared to 0. The Page Program instruction will not be executed if the addressed page is protected by the Block Protect (BP2, BP1, and BPO) bits (see Status Register Memory Protection table).
大致意思:
Page Program(02h)是SPI Flash操作中用于写入数据到页面的命令,其操作流程如下:操作步骤写加载(02h):发送命令02h,后跟页地址(12位)和待写入数据。数据长度为256 bytes (a page)。 1写使能(06h):发送命令06h,使能写入操作。 12写执行(10h):发送命令10h,开始写入数据到指定页面。 1状态检测:通过FSR.7轮询确认操作完成。 写入前需确保芯片处于IDLE状态,并正确配置时钟极性和相位模式。 不同厂商的扇区大小和写入时间可能不同(如Winbond扇区为1Mbit,Spansion为256Kbit)。
3 代码实现
1.ID检测函数
2.忙碌判断函数
3.写入使能函数
4扇区擦除函数
5数据读取函数
6.数据写入函数
这个,一般都没有冲哥的水平,老老实实跟着冲哥写吧!
看看烧录后运行结果:
按K2后:
按K1后:
不知道什么原因,ISP死机不动了,拔掉USB线,重新启动STC-ISP: