新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Freescale 9S12 系列單片機(jī)應(yīng)用筆記(EETS4K模塊) 1

Freescale 9S12 系列單片機(jī)應(yīng)用筆記(EETS4K模塊) 1

作者: 時間:2016-11-20 來源:網(wǎng)絡(luò) 收藏

EETS4K模塊應(yīng)用筆記(1)

9S12系列單片機(jī)的通常包含4KB的EEPROM。Freescale將EEPROM模塊稱之為EETS4K。實(shí)際上,這里所謂的EEPROM其實(shí)是FLASH,只不過Freescale特意將這里Flash的sector做的很小(4Bytes),使得用戶用起來像是在用EEPROM。

本文引用地址:http://m.butianyuan.cn/article/201611/318821.htm

EEPROM是直接映射到9S12單片機(jī)的地址空間的,如果程序中只是讀取EEPROM中的內(nèi)容,而不涉及到對EEPROM中數(shù)據(jù)的修改。那就不需要特殊的編程。就像讀取RAM數(shù)據(jù)那樣直接訪問就可以了。

只有當(dāng)需要在程序中更新EEPROM中內(nèi)容時,才需要學(xué)習(xí)下面的內(nèi)容。

初始化EETS4K

在向EETS4K寫入數(shù)據(jù)或擦除數(shù)據(jù)前要先配置EETS4K的時鐘。EETS4K的時鐘頻率必須在150KHz——200KHz之間,為此需要配置ECLKDIV寄存器。

ECLKDIV寄存器(EEPROMClockDividerRegister)

圖1ECLKDIV寄存器

PRDIV8是預(yù)分頻位:當(dāng)PRDIV8=1時輸入時鐘被預(yù)分頻為1/8。

EDIV5——EDIV8為分頻除數(shù)寄存器,最多可以產(chǎn)生1/64的分頻比。簡單的計(jì)算可知,當(dāng)輸入時鐘大于12.8MHz時需要將PRDIV8置位。

經(jīng)過PRDIV8和EDIV兩級分頻最多可將時鐘頻率分為1/512。

擦除和寫入和讀取

這里不詳細(xì)介紹每一個寄存器的用法。只對需要注意的地方加以說明。

EETS4K模塊的最小擦除單位是4Bytes,EETS4K模塊提供了兩條相關(guān)命令,一條是擦除一個sector,也就是4字節(jié),并且要求是字節(jié)對其的雙字。另一條命令擦除全部EEPROM空間。

每次編程(寫入)單位為兩個字節(jié)。并且這兩個字節(jié)要是對其字。

當(dāng)EETS4K模塊正在進(jìn)行擦除或編程操作時是不能同時讀取EEPROM中內(nèi)容的。

有了這些介紹就夠了。下面給出一個具體的例子。


  1. /*EETS4K.h*/
  2. #ifndefNVM_H
  3. #defineNVM_H
  4. /*
  5. *CONSTANTS
  6. */
  7. #defineNVM_NO_ERR(1)
  8. #defineNVM_ODD_ACCESS_ERR(-1)
  9. #defineNVM_ACCESS_ERR(-2)
  10. #defineNVM_PROTECTION_ERR(-3)
  11. /*
  12. *FUNCTIONPROTOTYPES
  13. */
  14. voidEEPROM_Init(unsignedlongsysclk);
  15. charEEPROM_Write_Word(unsignedintaddress,unsignedintdata);
  16. charEEPROM_Erase_Sector(unsignedintaddress);
  17. charEEPROM_Erase_All(void);
  18. unsignedintEEPROM_Read_Word(unsignedintaddress);
  19. #endif/*Endoffile*/
  1. /*EETS4K.C*/
  2. #include/*commondefinesandmacros*/
  3. #include"derivative.h"/*derivative-specificdefinitions*/
  4. #include"eets4k.h"
  5. /**@briefThisfunctioninitializestheNonVolatileEEPROMcontrolregisters
  6. *andmustbecalledbeforeattemptingtowriteoreraseanEEPROMsector.
  7. *
  8. *@parasysclktheCPUclockfrequency(SYSCLK)drivenbytheonboardoscillatororthePLLifenabled.
  9. */
  10. voidEEPROM_Init(unsignedlongsysclk)
  11. {
  12. unsignedchareclk_val;
  13. if(sysclk>=12000){/*IftheSYSCLKis>12MHz,thensetFDIV8bit*/
  14. eclk_val=(sysclk/(8*200))-1;/*Computethecorrectdividervalue*/
  15. ECLKDIV|=ECLKDIV_PRDIV8_MASK|eclk_val;/*WritetheECLKDIVregisterwiththecorrectsettings*/
  16. }else{
  17. eclk_val=(sysclk/200)-1;/*Computethecorrectdividervalue*/
  18. ECLKDIV|=eclk_val;/*WritetheECLKDIVregisterwiththecorrectsettings*/
  19. }
  20. ESTAT|=(ESTAT_PVIOL_MASK|ESTAT_ACCERR_MASK);/*Clearanyerrorflags*/
  21. }
  22. /**@briefThisfunctionwritesa16-bitwordtoEEPROM
  23. *@paramaddress,thedestinationEEPROMaddresstowritethedata
  24. *@paramdata,thedatatowritetoargumentaddress.
  25. *@return
  26. *NVM_NO_ERR-EEPROMWriteSuccess
  27. *NVM_ODD_ACCESS_ERR-EEPROMWriteError,Addressnotonanevenaddressboundry
  28. *NVM_ACCESS_ERR-EEPROMWriteError,AccessViolation
  29. *NVM_PROTECTION_ERR-EEPROMWriteError,Attemptedtowriteaprotectedsector
  30. */
  31. charEEPROM_Write_Word(unsignedintaddress,unsignedintdata)
  32. {
  33. while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
  34. ;
  35. }
  36. ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
  37. if(address&0x0001){
  38. return(NVM_ODD_ACCESS_ERR);/*AddressisNOTalignedonanevenboundry?*/
  39. }
  40. (*(unsignedint*)address)=data;/*Writethedatatothespecifiedaddress*/
  41. ECMD=ECMD_CMDB5_MASK;/*StoreprogrammingcommandinFCMD*/
  42. ESTAT_CBEIF=1;/*Executethecommand*/
  43. if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
  44. return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
  45. }
  46. if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
  47. return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
  48. }
  49. return(NVM_NO_ERR);/*ReturnNoError*/
  50. }
  51. /**@briefThisfunctionerasesa4-bytesectorofEEPROM
  52. *@paramaddress,thestartofthe4-bytesectortoaddress
  53. *@return
  54. *NVM_NO_ERR-EEPROMWriteSuccess
  55. *NVM_ODD_ACCESS_ERR-EEPROMWriteError,Addressnotonanevenaddressboundry
  56. *NVM_ACCESS_ERR-EEPROMWriteError,AccessViolation
  57. *NVM_PROTECTION_ERR-EEPROMWriteError,Attemptedtowriteaprotectedsector
  58. */
  59. charEEPROM_Erase_Sector(unsignedintaddress)
  60. {
  61. while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
  62. ;
  63. }
  64. ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
  65. if(address&0x0001){
  66. return(NVM_ODD_ACCESS_ERR);/*AddressisNOTalignedonanevenboundry?*/
  67. }
  68. (*(unsignedint*)address)=0xFFFF;/*Writethedatatothespecifiedaddress*/
  69. ECMD=ECMD_CMDB6_MASK;/*StoreprogrammingcommandinFCMD*/
  70. ESTAT_CBEIF=1;/*Executethecommand*/
  71. if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
  72. return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
  73. }
  74. if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
  75. return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
  76. }
  77. return(NVM_NO_ERR);/*ReturnNoError*/
  78. }
  79. charEEPROM_Erase_All(void)
  80. {
  81. while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
  82. ;
  83. }
  84. ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
  85. (*(unsignedint*)0x0400)=0xFFFF;/*Writethedatatothespecifiedaddress*/
  86. ECMD=0x41;/*StoreprogrammingcommandinFCMD*/
  87. ESTAT_CBEIF=1;/*Executethecommand*/
  88. if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
  89. return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
  90. }
  91. if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
  92. return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
  93. }
  94. return(NVM_NO_ERR);/*ReturnNoError*/
  95. }
  96. /**@briefThisfunctionreadsa16-bitwordfromthespecifiedaddressinEEPROM
  97. *@paramaddress,thestartofthe16-bitdatatoread
  98. *@returnThe16-bitwordstoredinlocationaddress
  99. */
  100. unsignedintEEPROM_Read_Word(unsignedintaddress)
  101. {
  102. unsignedintdata;
  103. while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
  104. ;
  105. }
  106. data=(*(unsignedint*)address);/*Readthedataatlocationaddress*/
  107. return(data);/*Returnthedata*/
  108. }



  1. #include/*commondefinesandmacros*/
  2. #include"derivative.h"/*derivative-specificdefinitions*/
  3. #include"sci.h"
  4. #include"eets4k.h"
  5. voidmain(void)
  6. {
  7. unsignedintdata;
  8. EEPROM_Init(16384);
  9. EnableInterrupts;
  10. EEPROM_Erase_All();
  11. EEPROM_Write_Word(0x400,1234);
  12. data=EEPROM_Read_Word(0x400);
  13. for(;;)
  14. {
  15. _FEED_COP();/*feedsthedog*/
  16. }/*loopforever*/
  17. }



評論


技術(shù)專區(qū)

關(guān)閉