在計(jì)算機(jī)科學(xué)中,子程序(英語:Subroutine, procedure, function, routine, method, subprogram, callable unit),是一個大型程序中的某部份代碼,由一個或多個語句塊組成。它負(fù)責(zé)完成某項(xiàng)特定任務(wù),而且相較于其他代碼,具備相對的獨(dú)立性。接下來我們就來看看關(guān)于FM25L256的操作子程序。
#include\;
//FM25L256數(shù)據(jù)讀取子程序;
//參數(shù):*pDestination要讀入數(shù)據(jù)的;
//參數(shù)條件:uiSourceAddress+(;voidReadFM25L256(unsigne;unsignedcharucHighAddres;EnableFM25256;//CS變低SPDR;
SPDR=0;//為了讀取數(shù)據(jù),需要寫入數(shù)據(jù),發(fā);wh
#include \
//FM25L256數(shù)據(jù)讀取子程序
//參數(shù): *pDestination要讀入數(shù)據(jù)的主機(jī)內(nèi)存地址指針; uiSourceAddress要讀取的數(shù)據(jù)在FM25L256中的地址(整形); uiNum數(shù)據(jù)個數(shù)(整形)
//參數(shù)條件: uiSourceAddress+(uiNum-1)不能大于器件的最高地址; uiNum必須》0;
void ReadFM25L256(unsigned char *pDestination, unsigned int uiSourceAddress, unsigned int uiNum) {
unsigned char ucHighAddress = uiSourceAddress 》》 8; //高位地址 SPCR = (1《
EnableFM25256; //CS變低 SPDR = 0x03; //寫入讀命令字 while((SPSR&(1《 0;uiNum--) {
SPDR = 0; //為了讀取數(shù)據(jù),需要寫入數(shù)據(jù),發(fā)送SCK,故寫入0
while((SPSR&(1《
pDestination++; }
DisableFM25256; //CS變高 }
//FM25L256數(shù)據(jù)讀取并發(fā)送至串口子程序
//參數(shù): uiSourceAddress要讀取的數(shù)據(jù)在FM25L256中的地址(整形); uiNum數(shù)據(jù)個數(shù)(整形) //參數(shù)條件: uiSourceAddress+(uiNum-1)不能大于器件的最高地址; uiNum必須》0; //返回值:所發(fā)送數(shù)據(jù)的檢驗(yàn)和
unsigned char ReadFM25L256SendToUsart(unsigned int uiSourceAddress, unsigned int uiNum) {
unsigned char ucHighAddress = uiSourceAddress 》》 8; //高位地址 unsigned char ucTemp;
EnableFM25256; //CS變低 SPCR = (1《
while((SPSR&(1《 0;uiNum--) {
SPDR = 0; //為了讀取數(shù)據(jù),需要寫入數(shù)據(jù),發(fā)送SCK,故寫入0
while((SPSR&(1《
DisableFM25256; //CS變高 return (1); }
//FM25L256數(shù)據(jù)寫入子程序
//參數(shù): *pSource要寫入數(shù)據(jù)的主機(jī)內(nèi)存地址指針; uiDestinationAddress要寫入的數(shù)據(jù)在FM25L256中的地址(整形); uiNum數(shù)據(jù)個數(shù)(整形)
void WriteFM25L256(unsigned char *pSource, unsigned int uiDestinationAddress, unsigned int uiNum) {
unsigned char ucHighAddress = uiDestinationAddress 》》 8; //高位地址 EnableFM25256; //CS變低 SPCR = (1《
while((SPSR&(1《
DisableFM25256; //CS變高 _delay_us(50);
EnableFM25256; //CS變低 SPDR = 0x02; //寫入寫命令字
while((SPSR&(1《
SPDR = ucHighAddress; //寫入高位地址CI-FM25H20-DG
while((SPSR&(1《
SPDR = (unsigned char) (uiDestinationAddress & 0x00FF); //寫入低位地址 while((SPSR&(1《
for(;uiNum 》 0;uiNum--) {
SPDR = *pSource; //發(fā)送要寫入的數(shù)據(jù)
while((SPSR&(1《
pSource++; }
DisableFM25256; //CS變高 }
//FM25L256自檢子程序
//返回值:0:自檢成功,1:自檢失敗 unsigned char FM25L256SelfTest(void) {
unsigned int uiTemp;
uiTemp = ReadInt(FM25L256_CHECK_ADDRESS);
if (uiTemp == 0xA55A) //判斷是否存儲器是否能讀 return (0); else
{ //存儲器不能讀或未進(jìn)行出廠設(shè)置 SetDefault(); //出廠設(shè)置,函數(shù)定義在System.c WriteInt (0xA55A,F(xiàn)M25L256_CHECK_ADDRESS); //設(shè)置已出廠設(shè)置標(biāo)志 uiTemp = ReadInt(FM25L256_CHECK_ADDRESS);
if (uiTemp == 0xA55A) //若讀取正確返回0 return (0); else
return (1); //若仍讀取錯誤則返回1 } } /*
//測試FM25L256子程序 void VerifyFM25L256(void) {
unsigned int uiCheckAddress = 0;
unsigned int uiMemoryNum = ReadInt(MEMORY_ADDRESS); unsigned int uiOldData; unsigned char ucError = 0; unsigned char ucSendChkSum; unsigned char i;
union tagSTOREINT siUsartDataInt; //整型共用體,將整形分解為字節(jié)型存取,在DataStore.h中定義
for (;uiCheckAddress 《 uiMemoryNum;uiCheckAddress += 2) {
uiOldData = ReadInt(uiCheckAddress); //保存原有數(shù)據(jù) WriteInt (0xA55A,uiCheckAddress); //寫入測試字 // __delay_cycles(10);
if (ReadInt(uiCheckAddress) != 0xA55A) {
WriteInt (uiOldData,uiCheckAddress); //寫回原有數(shù)據(jù)
SendChar(COMM_FRAME_START); //發(fā)送反饋幀頭
ucSendChkSum = COMM_FRAME_START; //計(jì)算反饋幀的校驗(yàn)和 SendChar(VERIFY_FRAM_COMMAND); //發(fā)送命令字
ucSendChkSum += VERIFY_FRAM_COMMAND; //計(jì)算反饋幀的校驗(yàn)和 siUsartDataInt.uiStoreInt = uiMemoryNum; //發(fā)送地址總長 for (i = 0;i 《= 1;i++) {
SendChar(siUsartDataInt.ucStoreBuff[i]);
ucSendChkSum += siUsartDataInt.ucStoreBuff[i]; }
siUsartDataInt.uiStoreInt = uiCheckAddress; //發(fā)送測試地址 for (i = 0;i 《= 1;i++) {
SendChar(siUsartDataInt.ucStoreBuff[i]);
ucSendChkSum += siUsartDataInt.ucStoreBuff[i]; }
SendChar(FINISHED_SEND);
ucSendChkSum += FINISHED_SEND; //發(fā)送結(jié)束上傳標(biāo)志 SendChar(ucSendChkSum); //發(fā)送檢驗(yàn)和 SendChar(COMM_FRAME_END); //發(fā)送幀尾 ucError = 1; //置錯誤標(biāo)志 break; //退出循環(huán) }
else if ((((uiCheckAddress + 2) % 1024) == 0) && ((uiCheckAddress + 2) 《 uiMemoryNum)) {
WriteInt (uiOldData,uiCheckAddress); //寫回原有數(shù)據(jù)
SendChar(COMM_FRAME_START); //發(fā)送反饋幀頭
ucSendChkSum = COMM_FRAME_START; //計(jì)算反饋幀的校驗(yàn)和 SendChar(VERIFY_FRAM_COMMAND); //發(fā)送命令字
ucSendChkSum += VERIFY_FRAM_COMMAND; //計(jì)算反饋幀的校驗(yàn)和 siUsartDataInt.uiStoreInt = uiMemoryNum; //發(fā)送地址總長 for (i = 0;i 《= 1;i++) {
SendChar(siUsartDataInt.ucStoreBuff[i]);
ucSendChkSum += siUsartDataInt.ucStoreBuff[i]; }
siUsartDataInt.uiStoreInt = uiCheckAddress + 2; //發(fā)送測試完成地址
for (i = 0;i 《= 1;i++) {
SendChar(siUsartDataInt.ucStoreBuff[i]);
ucSendChkSum += siUsartDataInt.ucStoreBuff[i]; }
SendChar(GO_ON_SEND);
ucSendChkSum += GO_ON_SEND; //發(fā)送結(jié)束上傳標(biāo)志 SendChar(ucSendChkSum); //發(fā)送檢驗(yàn)和 SendChar(COMM_FRAME_END); //發(fā)送幀尾 // __watchdog_reset(); //復(fù)位看門狗 }
WriteInt (uiOldData,uiCheckAddress); //寫回原有數(shù)據(jù) }
if (ucError == 0) {
SendChar(COMM_FRAME_START); //發(fā)送反饋幀頭
ucSendChkSum = COMM_FRAME_START; //計(jì)算反饋幀的校驗(yàn)和 SendChar(VERIFY_FRAM_COMMAND); //發(fā)送命令字
ucSendChkSum += VERIFY_FRAM_COMMAND; //計(jì)算反饋幀的校驗(yàn)和 siUsartDataInt.uiStoreInt = uiMemoryNum; //發(fā)送地址總長 for (i = 0;i 《= 1;i++) {
SendChar(siUsartDataInt.ucStoreBuff[i]);
ucSendChkSum += siUsartDataInt.ucStoreBuff[i]; }
siUsartDataInt.uiStoreInt = uiCheckAddress; //發(fā)送測試完成地址 for (i = 0;i 《= 1;i++) {
SendChar(siUsartDataInt.ucStoreBuff[i]);
ucSendChkSum += siUsartDataInt.ucStoreBuff[i]; }
SendChar(FINISHED_SEND);
ucSendChkSum += FINISHED_SEND; //發(fā)送結(jié)束上傳標(biāo)志 SendChar(ucSendChkSum); //發(fā)送檢驗(yàn)和 SendChar(COMM_FRAME_END); //發(fā)送幀尾 } } */
評論
查看更多