我知道有很多這方面的例子,但是他們都沒有工作。 PPTR是一個指向這種類型將32位長分成4個字節
typedef struct
{
TIMESTAMP_TYPE oTimeStamp;
ASSERT_ID_TYPE ucAssertID;
Int16 iData1;
Int16 iData2;
UInt16 uiChecksum;
}
LOG_ENTRY_TYPE;
,我出列,我的日誌時,有一個我想關店到EEEPROM的臨時日誌。
oTimestamp的類型是
typedef struct
{
UInt32 ulSecond;
UInt16 usMilliSecond;
UInt16 usPowerCycleCount;
}
TIMESTAMP_TYPE;
所有其他的訪問,甚至可用於毫秒寫入工作,但我不能細分爲4個字節秒時間戳值。
這是我曾嘗試(很明顯,只有一個版本的註釋去掉了):
UChar tmpByteHigh;
UChar tmpByteLow;
//Attempt1
tmpByteHigh = (pPtr->oTimeStamp.ulSecond >> 24) & 0x000000FF;
SPIwrite(tmpByteHigh);
tmpByteLow = (pPtr->oTimeStamp.ulSecond >> 16) & 0x000000FF;
SPIwrite(tmpByteLow);
tmpByteHigh = (pPtr->oTimeStamp.ulSecond >> 8) & 0x000000FF;
SPIwrite(tmpByteHigh);
tmpByteLow = (pPtr->oTimeStamp.ulSecond) & 0x000000FF;
SPIwrite(tmpByteLow);
//Attempt 2
tmpByteHigh = (pPtr->oTimeStamp.ulSecond & 0xFF000000UL) >> 24;
SPIwrite(tmpByteHigh);
tmpByteLow = (pPtr->oTimeStamp.ulSecond & 0x00FF0000UL) >> 16;
SPIwrite(tmpByteLow);
tmpByteHigh = (pPtr->oTimeStamp.ulSecond & 0x0000FF00UL) >> 8;
SPIwrite(tmpByteHigh);
tmpByteLow = (pPtr->oTimeStamp.ulSecond) & 0x000000FFUL;
SPIwrite(tmpByteLow);
//Attempt 3
//get msw from 32 bit value and write the 2 msB from it
tmpWord = (pPtr->oTimeStamp.ulSecond >> 16) & 0x0000FFFF;
tmpByteHigh = (tmpWord >> 8) & 0x00FF;
SPIwrite(tmpByteHigh);
tmpByteLow = tmpWord & 0x00FF;
SPIwrite(tmpByteLow);
//get lsw from 32 bit value and write the 2 lsB from it
tmpWord = pPtr->oTimeStamp.ulSecond & 0x0000FFFF;
tmpByteHigh = (tmpWord >> 8) & 0x00FF;
SPIwrite(tmpByteHigh);
tmpByteLow = tmpWord & 0x00FF;
SPIwrite(tmpByteLow);
//Attempt 4
UChar* myPointer = (UChar*)&pPtr->oTimeStamp.ulSecond;
UChar myArray[4];
myArray[0]=myPointer[0];
myArray[1]=myPointer[1];
myArray[2]=myPointer[2];
myArray[3]=myPointer[3];
SPIwrite(myArray[0]);
SPIwrite(myArray[1]);
SPIwrite(myArray[2]);
SPIwrite(myArray[3]);
每一次我得到0×00 0×00 0×00 0x80的送過來SPI。有什麼想法嗎?對我來說簡單我不是一個優秀的程序員。
...什麼是預期輸出? – user35443
想法?是。你的'pPtr-> oTimeStamp.ulSecond'中有錯誤的數據。或者'SPIwrite'中的錯誤。因爲轉換代碼很好。 –
也許'pPtr-> oTimeStamp.ulSecond'確實等於'0x80000000'?你用4種不同的方式證明了這一點。似乎是一個非常有力的證據 – anatolyg