各位大神:
我想读取W24X40的ID信息,然后在串口显示,但是串口显示全是零,请指教一下我程序哪里出错啦?
读三个ID的代码是下面的
u8 SPI_FLASH_SendByte(u8 BYTE)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, BYTE);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
u32 SPI_FLASH_ReadDeviceID(void)
{
u8 tmp0;
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
SPI_FLASH_SendByte(0xAB);
SPI_FLASH_SendByte(0xFF);
SPI_FLASH_SendByte(0xFF);
SPI_FLASH_SendByte(0xFF);
tmp0 = SPI_FLASH_SendByte(0xFF);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
return tmp0;
}
u32 SPI_FLASH_ReadID(void)
{
u32 tmp1 = 0,tmp2 = 0,tmp3 = 0,tmp4 = 0;
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
SPI_FLASH_SendByte(0x9F);
tmp1 = SPI_FLASH_SendByte(0xFF);
tmp1&=0x0FF;
tmp2 = SPI_FLASH_SendByte(0xFF);
tmp2&=0x0FF;
tmp3 = SPI_FLASH_SendByte(0xFF);
tmp3&=0x0FF;
GPIO_SetBits(GPIOA, GPIO_Pin_4);
tmp4 = (tmp1<<16)|(tmp2<<8)|tmp3;
return tmp4;
}
u16 SPI_FLASH_ReadID1(void)
{
u16 tmp = 0, tmp1 = 0, tmp2 = 0;
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
SPI_FLASH_SendByte(0x90);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
SPI_FLASH_SendByte(0x00);
tmp1 = SPI_FLASH_SendByte(0xff);
tmp2 = SPI_FLASH_SendByte(0xff);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
tmp = (tmp1<<8)|tmp2;
return tmp;
}
下面是输出到串口部分的代码:
while(1)
{
tp1 = SPI_FLASH_ReadDeviceID();
tp2 = SPI_FLASH_ReadID();
tp3 = SPI_FLASH_ReadID1();
printf("
ID已经读出来!
");
GPIO_SetBits(GPIOA, GPIO_Pin_2);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
printf("
ID1=:0x%4x
", tp1);
printf("
ID2=:0x%4x
", tp2);
printf("
ID3=:0x%2x
", tp3);
//while(1);
ZQ_Delay(1000);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
GPIO_SetBits(GPIOA, GPIO_Pin_1);
tp1 = SPI_FLASH_ReadDeviceID();
tp2 = SPI_FLASH_ReadID();
tp3 = SPI_FLASH_ReadID1();
printf("
ID 又读出来
");
printf("
ID1=:0x%4x
", tp1);
printf("
ID2=:0x%4x
", tp2);
printf("
ID3=:0x%2x
", tp3);
ZQ_Delay(1000);
}
在线求指教啊?
串口的输出结果是这样:
usart1 开启成功
SPI 开启成功
ID已经读出来!
ID1=:0x 0
ID2=:0x 0
ID3=:0x 0
ID 又读出来
ID1=:0x 0
ID2=:0x 0
ID3=:0x 0
ID已经读出来!
ID1=:0x 0
ID2=:0x3ffff
ID3=:0x 0
ID 又读出来
ID1=:0x 0
ID2=:0x 0
ID3=:0x 0
ID已经读出来!
ID1=:0x 0
ID2=:0x 0
ID3=:0x 0
ID 又读出来
ID1=:0x 0
ID2=:0x 0
ID3=:0x 0
ID已经读出来!
ID1=:0x 0
ID2=:0x 0
ID3=:0x 0
有一次读出来的值还不一样!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>