Freescale 9S12 系列單片機(jī)應(yīng)用筆記(EETS4K模塊) 1
EETS4K模塊應(yīng)用筆記(1)
9S12系列單片機(jī)的通常包含4KB的EEPROM。Freescale將EEPROM模塊稱之為EETS4K。實(shí)際上,這里所謂的EEPROM其實(shí)是FLASH,只不過Freescale特意將這里Flash的sector做的很?。?Bytes),使得用戶用起來像是在用EEPROM。
本文引用地址:http://www.ex-cimer.com/article/201611/318821.htmEEPROM是直接映射到9S12單片機(jī)的地址空間的,如果程序中只是讀取EEPROM中的內(nèi)容,而不涉及到對(duì)EEPROM中數(shù)據(jù)的修改。那就不需要特殊的編程。就像讀取RAM數(shù)據(jù)那樣直接訪問就可以了。
只有當(dāng)需要在程序中更新EEPROM中內(nèi)容時(shí),才需要學(xué)習(xí)下面的內(nèi)容。
初始化EETS4K
在向EETS4K寫入數(shù)據(jù)或擦除數(shù)據(jù)前要先配置EETS4K的時(shí)鐘。EETS4K的時(shí)鐘頻率必須在150KHz——200KHz之間,為此需要配置ECLKDIV寄存器。
ECLKDIV寄存器(EEPROMClockDividerRegister)
圖1ECLKDIV寄存器
PRDIV8是預(yù)分頻位:當(dāng)PRDIV8=1時(shí)輸入時(shí)鐘被預(yù)分頻為1/8。
EDIV5——EDIV8為分頻除數(shù)寄存器,最多可以產(chǎn)生1/64的分頻比。簡(jiǎn)單的計(jì)算可知,當(dāng)輸入時(shí)鐘大于12.8MHz時(shí)需要將PRDIV8置位。
經(jīng)過PRDIV8和EDIV兩級(jí)分頻最多可將時(shí)鐘頻率分為1/512。
擦除和寫入和讀取
這里不詳細(xì)介紹每一個(gè)寄存器的用法。只對(duì)需要注意的地方加以說明。
EETS4K模塊的最小擦除單位是4Bytes,EETS4K模塊提供了兩條相關(guān)命令,一條是擦除一個(gè)sector,也就是4字節(jié),并且要求是字節(jié)對(duì)其的雙字。另一條命令擦除全部EEPROM空間。
每次編程(寫入)單位為兩個(gè)字節(jié)。并且這兩個(gè)字節(jié)要是對(duì)其字。
當(dāng)EETS4K模塊正在進(jìn)行擦除或編程操作時(shí)是不能同時(shí)讀取EEPROM中內(nèi)容的。
有了這些介紹就夠了。下面給出一個(gè)具體的例子。
- /*EETS4K.h*/
- #ifndefNVM_H
- #defineNVM_H
- /*
- *CONSTANTS
- */
- #defineNVM_NO_ERR(1)
- #defineNVM_ODD_ACCESS_ERR(-1)
- #defineNVM_ACCESS_ERR(-2)
- #defineNVM_PROTECTION_ERR(-3)
- /*
- *FUNCTIONPROTOTYPES
- */
- voidEEPROM_Init(unsignedlongsysclk);
- charEEPROM_Write_Word(unsignedintaddress,unsignedintdata);
- charEEPROM_Erase_Sector(unsignedintaddress);
- charEEPROM_Erase_All(void);
- unsignedintEEPROM_Read_Word(unsignedintaddress);
- #endif/*Endoffile*/
- /*EETS4K.C*/
- #include
/*commondefinesandmacros*/ - #include"derivative.h"/*derivative-specificdefinitions*/
- #include"eets4k.h"
- /**@briefThisfunctioninitializestheNonVolatileEEPROMcontrolregisters
- *andmustbecalledbeforeattemptingtowriteoreraseanEEPROMsector.
- *
- *@parasysclktheCPUclockfrequency(SYSCLK)drivenbytheonboardoscillatororthePLLifenabled.
- */
- voidEEPROM_Init(unsignedlongsysclk)
- {
- unsignedchareclk_val;
- if(sysclk>=12000){/*IftheSYSCLKis>12MHz,thensetFDIV8bit*/
- eclk_val=(sysclk/(8*200))-1;/*Computethecorrectdividervalue*/
- ECLKDIV|=ECLKDIV_PRDIV8_MASK|eclk_val;/*WritetheECLKDIVregisterwiththecorrectsettings*/
- }else{
- eclk_val=(sysclk/200)-1;/*Computethecorrectdividervalue*/
- ECLKDIV|=eclk_val;/*WritetheECLKDIVregisterwiththecorrectsettings*/
- }
- ESTAT|=(ESTAT_PVIOL_MASK|ESTAT_ACCERR_MASK);/*Clearanyerrorflags*/
- }
- /**@briefThisfunctionwritesa16-bitwordtoEEPROM
- *@paramaddress,thedestinationEEPROMaddresstowritethedata
- *@paramdata,thedatatowritetoargumentaddress.
- *@return
- *NVM_NO_ERR-EEPROMWriteSuccess
- *NVM_ODD_ACCESS_ERR-EEPROMWriteError,Addressnotonanevenaddressboundry
- *NVM_ACCESS_ERR-EEPROMWriteError,AccessViolation
- *NVM_PROTECTION_ERR-EEPROMWriteError,Attemptedtowriteaprotectedsector
- */
- charEEPROM_Write_Word(unsignedintaddress,unsignedintdata)
- {
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
- if(address&0x0001){
- return(NVM_ODD_ACCESS_ERR);/*AddressisNOTalignedonanevenboundry?*/
- }
- (*(unsignedint*)address)=data;/*Writethedatatothespecifiedaddress*/
- ECMD=ECMD_CMDB5_MASK;/*StoreprogrammingcommandinFCMD*/
- ESTAT_CBEIF=1;/*Executethecommand*/
- if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
- return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
- }
- if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
- return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
- }
- return(NVM_NO_ERR);/*ReturnNoError*/
- }
- /**@briefThisfunctionerasesa4-bytesectorofEEPROM
- *@paramaddress,thestartofthe4-bytesectortoaddress
- *@return
- *NVM_NO_ERR-EEPROMWriteSuccess
- *NVM_ODD_ACCESS_ERR-EEPROMWriteError,Addressnotonanevenaddressboundry
- *NVM_ACCESS_ERR-EEPROMWriteError,AccessViolation
- *NVM_PROTECTION_ERR-EEPROMWriteError,Attemptedtowriteaprotectedsector
- */
- charEEPROM_Erase_Sector(unsignedintaddress)
- {
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
- if(address&0x0001){
- return(NVM_ODD_ACCESS_ERR);/*AddressisNOTalignedonanevenboundry?*/
- }
- (*(unsignedint*)address)=0xFFFF;/*Writethedatatothespecifiedaddress*/
- ECMD=ECMD_CMDB6_MASK;/*StoreprogrammingcommandinFCMD*/
- ESTAT_CBEIF=1;/*Executethecommand*/
- if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
- return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
- }
- if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
- return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
- }
- return(NVM_NO_ERR);/*ReturnNoError*/
- }
- charEEPROM_Erase_All(void)
- {
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- ESTAT=(ESTAT_ACCERR_MASK|ESTAT_PVIOL_MASK);/*Clearexistingerrorflags*/
- (*(unsignedint*)0x0400)=0xFFFF;/*Writethedatatothespecifiedaddress*/
- ECMD=0x41;/*StoreprogrammingcommandinFCMD*/
- ESTAT_CBEIF=1;/*Executethecommand*/
- if(ESTAT_ACCERR){/*Checkiftherehasbeenanaccesserror*/
- return(NVM_ACCESS_ERR);/*ReturnanAccessErrorcode*/
- }
- if(ESTAT_PVIOL){/*Checkiftherehasbeenaprotectionerror*/
- return(NVM_PROTECTION_ERR);/*ReturnaProtectionErrorcode*/
- }
- return(NVM_NO_ERR);/*ReturnNoError*/
- }
- /**@briefThisfunctionreadsa16-bitwordfromthespecifiedaddressinEEPROM
- *@paramaddress,thestartofthe16-bitdatatoread
- *@returnThe16-bitwordstoredinlocationaddress
- */
- unsignedintEEPROM_Read_Word(unsignedintaddress)
- {
- unsignedintdata;
- while(!ESTAT_CBEIF){/*WaitforEEPROMaccesscontrollertobecomeready*/
- ;
- }
- data=(*(unsignedint*)address);/*Readthedataatlocationaddress*/
- return(data);/*Returnthedata*/
- }
- #include
/*commondefinesandmacros*/ - #include"derivative.h"/*derivative-specificdefinitions*/
- #include"sci.h"
- #include"eets4k.h"
- voidmain(void)
- {
- unsignedintdata;
- EEPROM_Init(16384);
- EnableInterrupts;
- EEPROM_Erase_All();
- EEPROM_Write_Word(0x400,1234);
- data=EEPROM_Read_Word(0x400);
- for(;;)
- {
- _FEED_COP();/*feedsthedog*/
- }/*loopforever*/
- }
評(píng)論