用于MAX7456隨屏顯示器SPI
讀字節(jié)操作程序
讀字節(jié)操作(圖2)程序如下所示,與上述程序類似。首先發(fā)送地址,然后發(fā)送時(shí)鐘從MISO讀回?cái)?shù)據(jù)。
/*************************************************************************************** spiReadReg** Reads an 8-bit register with the SPI port.* Data is returned. **************************************************************************************/unsigned char spiReadReg (const unsigned char regAddr){unsigned char SPICount; // Counter used to clock out the dataunsigned char SPIData; SPI_CS = 1; // Make sure we start with active-low CS highSPI_CK = 0; // and CK lowSPIData = regAddr; // Preload the data to be sent with Address and DataSPI_CS = 0; // Set active-low CS low to start the SPI cyclefor (SPICount = 0; SPICount 8; SPICount++) // Prepare to clock out the Address and Data{if (SPIData 0x80)SPI_MOSI = 1;elseSPI_MOSI = 0;SPI_CK = 1;SPI_CK = 0;SPIData = 1;} // and loop back to send the next bitSPI_MOSI = 0; // Reset the MOSI data lineSPIData = 0;for (SPICount = 0; SPICount 8; SPICount++) // Prepare to clock in the data to be read{SPIData =1; // Rotate the dataSPI_CK = 1; // Raise the clock to clock the data out of the MAX7456SPIData += SPI_MISO; // Read the data bitSPI_CK = 0; // Drop the clock ready for the next bit} // and loop backSPI_CS = 1; // Raise CSreturn ((unsigned char)SPIData); // Finally return the read data}
自動(dòng)遞增模式下的寫字節(jié)操作程序
自動(dòng)遞增模式下的寫字節(jié)操作(圖3)程序如下所示,與和上述單字節(jié)寫程序類似。首先發(fā)送地址,然后發(fā)送時(shí)鐘從MISO讀回?cái)?shù)據(jù)。
/*************************************************************************************** spiWriteRegAutoIncr** Writes to an 8-bit register with the SPI port using the MAX7456's autoincrement mode**************************************************************************************/void spiWriteRegAutoIncr(const unsigned char regData){unsigned char SPICount; // Counter used to clock out the dataunsigned char SPIData; // Define a data structure for the SPI data.SPI_CS = 1; // Make sure we start with active-low CS highSPI_CK = 0; // and CK lowSPIData = regData; // Preload the data to be sent with Address and DataSPI_CS = 0; // Set active-low CS low to start the SPI cyclefor (SPICount = 0; SPICount 8; SPICount++) // Prepare to clock out the Address and Data{if (SPIData 0x80)SPI_MOSI = 1;elseSPI_MOSI = 0;SPI_CK = 1;SPI_CK = 0;SPIData = 1;} // and loop back to send the next bit SPI_MOSI = 0; // Reset the MOSI data line}
自動(dòng)遞增模式下寫顯示存儲(chǔ)器的程序
自動(dòng)遞增模式下寫顯示存儲(chǔ)器的程序如下,程序使用稱為 "data"的全局變量數(shù)組。定義如下:
extern volatile unsigned char data[DATA_BUF_LENGTH];DATA_BUF_LENGTH = 968
調(diào)用程序時(shí),data[]包含顯示存儲(chǔ)器內(nèi)容,格式如下:
data[0] = ignored (contains a command byte used by the EV kit GUI software)data[1] = character byte 1data[2] = attribute byte 1data[3] = character byte 2data[4] = attribute byte 2etc.
自動(dòng)遞增模式通過寫0xFF結(jié)束,所以該模式下不能向顯示寄存器寫0xFF。如果需要寫OxFF,可以采用單字節(jié)寫指令。
/*************************************************************************************** spiWriteCM** Writes to the Display Memory (960 bytes) from "data" extern.* 960 = 16 rows × 30 columns × 2 planes {char vs. attr} screen-position-indexed memory**************************************************************************************/void spiWriteCM() // On entry: global data[1..960]// contains char+attr bytes // (optionally terminated by 0xFF data)// First, write data[1,3,5,...] Character plane;// MAX7456 WriteReg(0x05,0x41)// "Character Memory Address High";// 0x02:Attribute bytes; // 0x01:character memory address msb{volatile unsigned int Index = 0x0001; // Index for lookup into// data[1..960] spiWriteReg(DM_ADDRH_WRITE,0x00); // initialise the Display Memory high-bytespiWriteReg(DM_ADDRL_WRITE,0x00); // and the low-bytespiWriteReg(DM_MODE_WRITE ,0x41); // MAX7456 WriteReg(0x04,0x41) "Display Memory Mode";// 0x40:Perform 8-bit operation; 0x01:AutoIncrementDo // Loop to write the character data{if (data[Index] == 0xFF) { // Check for the break characterbreak; } // and finish if foundspiWriteRegAutoIncr(data[Index]); // Write the characterIndex += 2; // Increment the index to the next character, // skipping over the attribute } while(Index 0x03C1); // 0x03C1 = 961// and loop back to send the next characterspiWriteRegAutoIncr(0xFF); // Write the "escape character" to end AutoIncrement
評(píng)論