ATM使用說明SPIEEPROMSTM_第1頁
ATM使用說明SPIEEPROMSTM_第2頁
ATM使用說明SPIEEPROMSTM_第3頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、1. 芯片訪問規(guī)那么符合 AT25 系列原那么,可以找相關(guān)的例子,但是25M01 目前沒有2.芯片工作原那么,不考慮寫保護問題,所有操作的第一步都是要發(fā)送寫允許指令,然后重新 片選有效,發(fā)送下一步的命令。注意問題:SPI的EEPROM的數(shù)據(jù)輸出的原理是,在輸入一個字節(jié)的同時輸出一個字節(jié),沒 有輸入字節(jié),就輸出,所以,所謂的讀數(shù)據(jù),其實質(zhì)是先發(fā)送一個數(shù)據(jù),然后在接收一個數(shù) 據(jù)。操作如下:(1) 存放器讀1) 先片選,在發(fā)送寫允許指令,片選無效2) 片選,發(fā)送讀存放器指令,讀存放器,片選無效(2) 存放器寫1) 先片選,在發(fā)送寫允許指令,片選無效2) 片選,發(fā)送寫存放器指令,寫數(shù)據(jù),片選無效(3)

2、 寫數(shù)據(jù)1) 先片選,在發(fā)送寫允許指令,片選無效2) 片選,發(fā)送寫數(shù)據(jù)指令,寫數(shù)據(jù),片選無效(4) 讀數(shù)據(jù)1) 先片選,在發(fā)送寫允許指令,片選無效2) 片選,發(fā)送讀數(shù)據(jù)指令,讀數(shù)據(jù),片選無效注意:STM32的SPI需要,每次都開啟void SPI_EEPROM_Init(void)SPI_InitTypeDef SPI_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;/* Enable SPI1 and GPIO clocks */*! SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO, SPI_F

3、LASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_ AFIO, ENABLE);/*! SPI_FLASH_SPI Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);/*! Configure SPI

4、_FLASH_SPI pins: SCK */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);/*! Configure SPI_FLASH_SPI pins: MOSI */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;GPIO_Init(GPIOA, &GP

5、IO_InitStructure);/*! Configure SPI_FLASH_SPI pins: MISO */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;/GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);/ 選通 采用 軟件 模式 /*!=(uint32_t)(1024*128*2)return 0;if(addr=(uint32_t)(1024*128) addr=addr-(uint32_t)(1024*128

6、);nss=2; else nss=1;SPI_Cmd(SPI1,ENABLE);AT25_WaitReady(nss);SPI_WREN(nss);NSS_CS_ENABLE(nss);SPI_WriteByte(READ);SPI_WriteByte(uint8_t)(addr & 0x010000)16);/* A16*/SPI_WriteByte(uint8_t)(addr & 0x00FF00)8);/* A15-A8*/ SPI_WriteByte(uint8_t)(addr & 0x0000FF);/* A7-A0*/tmp = SPI_ReadByte();NSS_CS_DIS

7、ABLE( );SPI_Cmd(SPI1,DISABLE);return tmp;void AT25_WriteByte(uint8_t data, uint32_t addr)uint8_t nss=1;uint32_t tt=1024*128*2;if(addr = tt)return ;if(addr=(uint32_t)(1024*128) addr=addr-(uint32_t)(1024*128);nss=2; else nss=1;SPI_Cmd(SPI1,ENABLE);AT25_WaitReady(nss);SPI_WREN(nss);NSS_CS_ENABLE(nss);S

8、PI_WriteByte(WRITE);SPI_WriteByte(uint8_t)(addr & 0x010000)16);/* A16*/SPI_WriteByte(uint8_t)(addr & 0x00FF00)8);/* A15-A8*/SPI_WriteByte(uint8_t)(addr & 0x0000FF);/* A7-A0*/ SPI_WriteByte(data);NSS_CS_DISABLE( );SPI_Cmd(SPI1,DISABLE);/ 多字節(jié)操作/* brief將緩沖區(qū)中的數(shù)據(jù)寫到I2C EEPROM中* param* arg pBuffer:緩沖區(qū)指針* a

9、rg WriteAddr: 寫地址* arg NumByteToWrite: 寫的字節(jié)數(shù)* retval 無*/void SPI_EE_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite,uint8_t NSS)u32 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;Addr = WriteAddr % I2C_PageSize;count = I2C_PageSize - Addr;NumOfPage = NumByteToWrite / I2C_PageSize;Num

10、OfSingle = NumByteToWrite % I2C_PageSize;/* If WriteAddr is I2C_PageSize aligned */if(Addr = 0)/* If NumByteToWrite I2C_PageSize */elsewhile(NumOfPage-)SPI_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize,NSS);WriteAddr += I2C_PageSize;pBuffer += I2C_PageSize;if(NumOfSingle!=0)SPI_EE_PageWrite(pBuffer,

11、 WriteAddr, NumOfSingle,NSS);/* If WriteAddr is not I2C_PageSize aligned */else/* If NumByteToWrite I2C_PageSize */elseNumByteToWrite -= count;NumOfPage = NumByteToWrite / I2C_PageSize;NumOfSingle = NumByteToWrite % I2C_PageSize; if(count != 0)SPI_EE_PageWrite(pBuffer, WriteAddr, count,NSS);WriteAdd

12、r += count;pBuffer += count;while(NumOfPage-)SPI_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize,NSS);WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; if(NumOfSingle != 0)SPI_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle,NSS); /* brief 在EEPROM的一個寫循環(huán)中可以寫多個字節(jié),但一次寫入的字節(jié)數(shù)* 不能超過EEPROM頁的大小,AT24C02每頁有8個字節(jié)*

13、param* arg pBuffer: 緩沖區(qū)指針* arg WriteAddr: 寫地址* arg NumByteToWrite: 寫的字節(jié)數(shù)* retval 無*/ void SPI_EE_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite,uint8_t NSS) SPI_Cmd(SPI1,ENABLE); AT25_WaitReady(NSS);SPI_WREN(NSS);NSS_CS_ENABLE(NSS); SPI_WriteByte(WRITE);SPI_WriteByte(uint8_t)(WriteAddr & 0

14、x010000)16);/* A16*/SPI_WriteByte(uint8_t)(WriteAddr & 0x00FF00)8);/* A15-A8*/ SPI_WriteByte(uint8_t)(WriteAddr & 0x0000FF);/* A7-A0*/* While there is data to be written */ while(NumByteToWrite-)/* Send the current byte */SPI_WriteByte(*pBuffer);/* Point to the next byte to be written */ pBuffer+;/*

15、 Send STOP condition */ NSS_CS_DISABLE();SPI_Cmd(SPI1,DISABLE); /* brief 從EEPROM里面讀取一塊數(shù)據(jù)* param* arg pBuffer:存放從EEPROMI讀取的數(shù)據(jù)的緩沖區(qū)指針* arg WriteAddr:接收數(shù)據(jù)的 EEPROM的地址* arg NumByteToWrite:要從 EEPROM讀取的字節(jié)數(shù)* retval 無*/void SPI_EE_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead,uint8_t NSS) uint8_t ns

16、s=NSS;SPI_Cmd(SPI1,ENABLE);AT25_WaitReady(nss);SPI_WREN(NSS);NSS_CS_ENABLE(nss);SPI_WriteByte(READ);SPI_WriteByte(uint8_t)(ReadAddr & 0x010000)16);/* A16*/SPI_WriteByte(uint8_t)(ReadAddr & 0x00FF00)8);/* A15-A8*/ SPI_WriteByte(uint8_t)(ReadAddr & 0x0000FF);/* A7-A0*/* While there is data to be read

17、*/while(NumByteToRead)*pBuffer = SPI_ReadByte(); pBuffer+;NumByteToRead-;/* Enable Acknowledgement to be ready for another reception */NSS_CS_DISABLE( );SPI_Cmd(SPI1,DISABLE);/*wdz*/void SPI_EE_BufferWrite2(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)u32 temp=WriteAddr+NumByteToWrite-1;u32 number

18、=WriteAddr+NumByteToWrite-1024*128; if(temp1024*128)SPI_EE_BufferWrite(pBuffer,WriteAddr,NumByteToWrite,1);else if(WriteAddr=1024*128)SPI_EE_BufferWrite(pBuffer,WriteAddr,NumByteToWrite-number,1); SPI_EE_BufferWrite(pBuffer+NumByteToWrite-number,0,number,2);else if(WriteAddr=1024*128)SPI_EE_BufferWrite(pBuffer,WriteAddr-1024*128,NumByteToWrite,2);void SPI_EE_BufferRead2(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)u32 temp=ReadAddr+NumByteToRead-1;u32 number=Rea

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論