用arduino自带的SPI库访问FLASH,并读取其ID号。并验证SPI的通信实验
见代码
#include <SPI.h>
const int slaveSelectPin = 10;
void setup() {
// put your setup code here, to run once:
pinMode (slaveSelectPin,OUTPUT);
while (!Serial) { delay(10); }
Serial.begin(115200);
Serial.println(">>> FLASH ID read test example: >>");
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
/*
if(!SPI.begin())
{
Serial.println("!!! SPI device initialize failure");
while(1);
}
*/
SPI.begin();
}
void loop() {
// put your main code here, to run repeatedly:
for(uint8_t i=3;i>0;i--)
FLASH_ID();
FLASH_ID_AB();
while(1);
}
void FLASH_ID(void)
{
int16_t temp = 0xAA;
Serial.println("int8_t temp = 0XCC:-->");
Serial.print(temp,HEX);Serial.println("\n");
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(0x90);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
temp = SPI.transfer(0xFF) << 8;
temp |= SPI.transfer(0xFF);
digitalWrite(slaveSelectPin,HIGH);
Serial.print(temp,HEX);Serial.println("\n");
}
void FLASH_ID_AB(void)
{
int8_t tmp = 0xCC;
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(0xAB);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
tmp = SPI.transfer(0xFF);
digitalWrite(slaveSelectPin,HIGH);
Serial.print(tmp,HEX);Serial.println("\n");
}
W25X16的ID寄存器
ID值对应表:
运行结果:
说明几点:
1)读取结果为32bit,但定义的temp变量为16bit。很奇怪
2)mega2560供电为5V,所以SPI总线电平为5V,不能连接到LTS251QV的屏排线上;看来还得回到stm32平台的SPI口。待解决
3)调试的时候8脚封装的FLASH pin7(HOLD)悬空,导致一直未读取成功。上拉到5V后成功读写