用STM32F407的SPI来控制片外EEPROM(X25040)时,读出的数据是乱码,

2019-07-21 04:59发布

[mw_shl_code=c,true]void WRITE_X25042(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) { //sFLASH_WaitForWriteEnd(); /*!< Enable the write access to the FLASH */ sFLASH_WriteEnable(); /*!< Select the FLASH: Chip Select low */ sFLASH_CS_LOW(); /*!< Send "Write to Memory " instruction */ sFLASH_SendByte(sFLASH_CMD_WRITE); sFLASH_CS_HIGH(); for(Index=0;Index<10;Index++); sFLASH_CS_LOW(); /*!< Send WriteAddr high nibble address byte to write to */ // sFLASH_SendByte((WriteAddr & 0xFF0000) >> 16); /*!< Send WriteAddr medium nibble address byte to write to */ //sFLASH_SendByte((WriteAddr & 0x0100) >> 8); /*!< Send WriteAddr low nibble address byte to write to */ sFLASH_SendByte(WriteAddr & 0xFF); /*!< while there is data to be written on the FLASH */ while (NumByteToWrite--) { /*!< Send the current byte */ sFLASH_SendByte(*pBuffer); /*!< Point on the next byte to be written */ printf("%c",*pBuffer); pBuffer++; } /*!< Deselect the FLASH: Chip Select high */ sFLASH_CS_HIGH(); /*!< Wait the end of Flash writing */ sFLASH_WaitForWriteEnd(); } void READ_X25042(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) { /*!< Select the FLASH: Chip Select low */ sFLASH_CS_LOW(); /*!< Send "Read from Memory " instruction */ sFLASH_SendByte(sFLASH_CMD_READ); /*!< Send ReadAddr high nibble address byte to read from */ //sFLASH_SendByte((ReadAddr & 0xFF0000) >> 16); /*!< Send ReadAddr medium nibble address byte to read from */ // sFLASH_SendByte((ReadAddr& 0x0100) >> 8); /*!< Send ReadAddr low nibble address byte to read from */ sFLASH_SendByte(ReadAddr & 0xFF); while (NumByteToRead--) /*!< while there is data to be read */ { /*!< Read a byte from the FLASH */ *pBuffer = sFLASH_SendByte(sFLASH_DUMMY_BYTE); /*!< Point to the next location where the byte read will be saved */ printf("%c",*pBuffer); pBuffer++; } /*!< Deselect the FLASH: Chip Select high */ sFLASH_CS_HIGH(); } int main(void) { STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); USART_Config();//USART配置 /* Initialize the SPI FLASH driver */ sFLASH_Init(); /* Write Tx_Buffer data to SPI FLASH memory */ WRITE_X25042(Tx_Buffer, FLASH_WRITE_ADDRESS, BufferSize); printf(" "); /* Read data from SPI FLASH memory */ READ_X25042(Rx_Buffer, FLASH_READ_ADDRESS, BufferSize); printf(" "); /* Check the correctness of written dada */ TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize); /* TransferStatus1 = PASSED, if the transmitted and received data by SPI1 are the same */ /* TransferStatus1 = FAILED, if the transmitted and received data by SPI1 are different */ if(TransferStatus1 == PASSED) { STM_EVAL_LEDOn(LED1); printf("SPI_EEPROM demo is successful "); } else { STM_EVAL_LEDOn(LED2); printf("SPI_EEPROM demo is failed "); } while (1){} }[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。