野火stm32-pdf教程13-i2c讀寫eepromat24c020_W_第1頁
野火stm32-pdf教程13-i2c讀寫eepromat24c020_W_第2頁
野火stm32-pdf教程13-i2c讀寫eepromat24c020_W_第3頁
野火stm32-pdf教程13-i2c讀寫eepromat24c020_W_第4頁
野火stm32-pdf教程13-i2c讀寫eepromat24c020_W_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、 stm32 I2C ( EEPROM-AT24C02 )偠fireE-MQQ313303034 STM32 ST3.0.0偠EERPOM 偠 偠PB6-I2C1_SCLPB7-I2C1_SDAstartup/start_stm32f10x_hd.c CMSIS/core_cm3.c CMSIS/system_stm32f10x.c FWlib/stm32f10x_gpio.c FWlib/stm32f10x_rcc.c FWlib/stm3

2、2f10x_usart.c FWlib/stm32f10x_i2c.c : USER/main.cUSER/stm32f10x_it.c USER/usart1.c USER/i2c_ee.c3 / 14 STM32 I2C-EEPROM I2C -I2C()I2C I2C m3st2 I2C DMA STM32 STM32F103VET62 I2C I/OPB6- I2C1_SCLPB7-I2C1_SDAPB10-I2C2_SCLPB11-I2C2_SDA 偠I2C1 EERPOM AT24C02 SCL SDA I2C EERPOM 偠 偠-佪 Fwlib 1. stm32f10x_gpi

3、o.c2. stm32f10x_rcc.c3. stm32f10x_usart.c4. stm32f10x_i2c.c stm32f10x_conf.h 1. /* Uncomment the line below to enable peripheral header file inclusion*/2. /* #include stm32f10x_adc.h */3. /* #include stm32f10x_bkp.h */4. /* #include stm32f10x_can.h */5. /* #include stm32f10x_crc.h */6. /* #include s

4、tm32f10x_dac.h */7. /* #include stm32f10x_dbgmcu.h */8. /* #include stm32f10x_dma.h */9. /* #include stm32f10x_exti.h */10. /*#include stm32f10x_flash.h*/11. /* #include stm32f10x_fsmc.h */12. #include stm32f10x_gpio.h13. #include stm32f10x_i2c.h14. /* #include stm32f10x_iwdg.h */15. /* #include stm

5、32f10x_pwr.h */16. #include stm32f10x_rcc.h17. /* #include stm32f10x_rtc.h */18. /* #include stm32f10x_sdio.h */19. /* #include stm32f10x_spi.h */20. /* #include stm32f10x_tim.h */21. #include stm32f10x_usart.h22. /* #include stm32f10x_wwdg.h */23. /*#include misc.h*/* High level functions for NVIC

6、and SysTick (a dd-on to CMSIS functions) */ main 21.22.I2C_Test(); 23./* I2C (AT24C02) */ I2C_EE_Init();USART1_printf(USART1, rn I2C (AT24C02) rn);USART1_printf(USART1, rn ( DATE - TIME ) rn);20./* 1 */ USART1_Config();/* 72M */ SystemInit();9.7.*/8. int main(void) 9. 5.*

7、 6.* 1. /*2.* main 3.* 4.* stm32 24.while (1)25.26.27. SystemInit(); 72M USART1_Config(); /*15. #ifdef EEPROM_Block0_ADDRESS16. /* EEPROM Block0 */17. EEPROM_ADDRESS = EEPROM_Block0_ADDRESS;18. #endif19.20. #ifdef EEPROM_Block1_ADDRESS21. /* EEPROM Block1 */22. EEPROM_ADDRE

8、SS = EEPROM_Block1_ADDRESS;23. #endif24.25. #ifdef EEPROM_Block2_ADDRESS26. /* EEPROM Block2 */27. EEPROM_ADDRESS = EEPROM_Block2_ADDRESS;28. #endif29.30. #ifdef EEPROM_Block3_ADDRESS31. /* EEPROM Block3 */32. EEPROM_ADDRESS = EEPROM_Block3_ADDRESS;33. #endif14. /* i2c_ee.h EEPROM */I2C_Mode_Configu

9、();I2C_GPIO_Config();3.*/7. void I2C_EE_Init(void) 8. 9.* * 6. * * I2C_EE_Init* I2C (EEPROM) .5.I2C_EE_Init(); I2C_GPIO_Config(); I2CI/O I2C_Mode_Configu(); I2C EERPOM 0xA01. /*2.* I2C_EE_Test3.* I2C(AT24C02)4.* 5.* 6.* 7.*/8. void I2C_Test(void) 9. 10 / 14printf(0x%02X , I2c_Buf_Wr

10、itei);if(i%16 = 15)printf(nr);/ I2c_Buf_Write 乎 EERPOM I2C_EE_BufferWrite( I2c_Buf_Write, EEP_Firstpage, 256);printf(nr nr);/ EEPROM 乎 I2c_Buf_Read I2C_EE_BufferRead(I2c_Buf_Read, EEP_Firstpage, 256);/ I2c_Buf_Read for (i=0; i256; i+)if(I2c_Buf_Readi != I2c_Buf_Writei)printf(0x%02X , I2c_Buf_Readi);

11、printf(:I2C EEPROM nr);return;printf(0x%02X , I2c_Buf_Readi);if(i%16 = 15)printf(nr);printf(I2C(AT24C02)nr);printf( nr);for ( i=0; i=255; i+ ) / I2c_Buf_Writei = i;u16 i;3.44.45. I2C_Test(void) 0255 乎

12、 I2C_EE_BufferWrite()EEPROMAddr = WriteAddr % I2C_PageSize; count = I2C_PageSize - Addr;u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;12. 13. 14. 15. 16.10. */11. void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)8.* 9.* 6.*-NumByteToWrite EEPROM 7.* -pBuffer -WriteAddr

13、EEPROM 4.* 5.*3.* I2C EPROM1. /*2.* I2C_EE_BufferWrite/* 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)I2C_EE_PageWrite(pBuffer, WriteAd

14、dr, count); I2C_EE_WaitEepromStandbyState();WriteAddr += count; pBuffer += count;while(NumOfPage-)I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); I2C_EE_WaitEepromStandbyState();WriteAddr +=I2C_PageSize; pBuffer += I2C_PageSize;if(NumOfSingle != 0)I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle

15、); I2C_EE_WaitEepromStandbyState();if(NumOfSingle!=0)I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState();/* If WriteAddr is I2C_PageSize aligned*/if(Addr = 0)/* If NumByteToWrite I2C_PageSize */elsewhile(NumOfPage-)I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); I2

16、C_EE_WaitEepromStandbyState();WriteAddr +=I2C_PageSize; pBuffer += I2C_PageSize;NumOfPage =NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_PageSize;9.60.61

17、.3.84.85. AT24C02 EEPROM 義 義 88 義 I2C_EE_BufferWrite() I2C_EE_PageWrite() EEPROM I2C I2C_EE_PageWrite()stm32 I2C 1. /*2. * I2C_EE_PageWrite3. * EEPROM 4. *EEPROM 義AT24C02 義 8 5. * -pBuffer 6. *-WriteAddr EEPROM 7. *-NumByteToWrite 8.* 9

18、.* 10. * 11. */EEPROM 12. void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)13. 14.while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY); / Added by Najoua 27/08/200815.16. /* Send START condition */17. I2C_GenerateSTART(I2C1, ENABLE); 18.19. /* Test on EV5 and clear it */20. while(!I2C_Chec

19、kEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); 21.22./* Send EEPROM address for write */23.;24.I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter)25. /* Test on EV6 and clear it */26. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECT ED);27.28. /* Send the EEPROMs in

20、ternal address to write to */29. I2C_SendData(I2C1, WriteAddr); 30.31. /* Test on EV8 and clear it */32. while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED); 33.34. /* While there is data to be written */35. while(NumByteToWrite-)36.37. /* Send the current byte */38. I2C_SendData(I2C1, *

21、pBuffer); 39.40. /* Point to the next byte to be written */41. pBuffer+; 42.43./* Test on EV8 and clear it */44.while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED);45.46. /* Send STOP condition */47. I2C_GenerateSTOP(I2C1, ENABLE);48. stm32 I2C STM32 I2C I2C_CheckEvent() I2C SDA stm32 5E

22、V5 while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); I2C_EVENT_MASTER_MODE_SELECT EV5 I2C_EE_BufferWrite() I2C_EE_PageWrite() I2C_EE_WaitEepromStandbyState()* * I2C_EE_WaitEepromStandbyState* Wait for EEPROM Standby state1. /*7. do/* Send START condition */ I2C_GenerateSTART(I2C1, EN

23、ABLE);/* Read I2C1 SR1 register */SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);/* Send EEPROM address for write */I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002);/* Clear AF flag */ I2C_ClearFlag(I2C1, I2C_FLAG_AF)

24、;/* STOP condition */I2C_GenerateSTOP(I2C1, ENABLE); / Added by Najoua 27/08/2008.*/9. void I2C_EE_WaitEepromStandbyState(void)10. 11.vu16 SR1_Tmp = 0; 8.19.20.* 7.5.* 6.* EEPROM EEPROM EEPROM Standby I2C_Test() EEPROM I2C_EE_BuferRead()/* Test on EV5 and clear

25、it */while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT);/* Send EEPROM address for write */* Send START condition */ I2C_GenerateSTART(I2C1, ENABLE);/*(u8 *)0x4001080c) &=0x80;4.13./*(u8 *)0x4001080c) |=0x80;14.while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY); / Added

26、by Najoua 27/08/200810. */11. void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)12. -NumByteToWrite EEPROM 6.*7.* 8.* 9.* -pBuffer EEPROM -WriteAddr EEPROM 4.* 5.*1. /*2. * I2C_EE_BufferRead3. * EEPROM 25.;26.I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter)27. /*

27、 Test on EV6 and clear it */28. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECT ED);29.30. /* Clear EV6 by setting again the PE bit */31. I2C_Cmd(I2C1, ENABLE); 32.33. /* Send the EEPROMs internal address to write to */34. I2C_SendData(I2C1, ReadAddr); 35.36. /* Test on EV8 and

28、clear it */37. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED); 38.39. /* Send STRAT condition a second time */40. I2C_GenerateSTART(I2C1, ENABLE); 41.42. /* Test on EV5 and clear it */43. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); 44.45. /* Send EEPROM address for read */46. I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Receiver);47.48. /* Test on EV6 and clear it */49. while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論