微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 模拟电路设计 > 用于MAX7456随屏显示器SPI

用于MAX7456随屏显示器SPI

时间:04-22 来源:互联网 点击:

  1. /*************************************************************************************** spiWriteReg** Writes to an 8-bit register with the SPI port**************************************************************************************/void spiWriteReg(const unsigned char regAddr, const unsigned char regData){unsigned char SPICount;                                       // Counter used to clock out the dataunsigned char SPIData;                                        // Define a data structure for the SPI dataSPI_CS = 1;                                        		// Make sure we start with active-low CS highSPI_CK = 0;                                        		// and CK lowSPIData = regAddr;                                            // Preload the data to be sent with AddressSPI_CS = 0;                                                   // Set active-low CS low to start the SPI cycle // Although SPIData could be implemented as an "int", // resulting in one// loop, the routines run faster when two loops // are implemented with// SPIData implemented as two "char"s.for (SPICount = 0; SPICount  8; SPICount++)                  // Prepare to clock out the Address byte{if (SPIData  0x80)                                         // Check for a 1SPI_MOSI = 1;                                             // and set the MOSI line appropriatelyelseSPI_MOSI = 0;SPI_CK = 1;                                                 // Toggle the clock lineSPI_CK = 0;SPIData = 1;                                              // Rotate to get the next bit}                                                             // and loop back to send the next bit// Repeat for the Data byteSPIData = regData;                                            // Preload the data to be sent with Datafor (SPICount = 0; SPICount  8; SPICount++){if (SPIData  0x80)SPI_MOSI = 1;elseSPI_MOSI = 0;SPI_CK = 1;SPI_CK = 0;SPIData = 1;}          SPI_CS = 1;SPI_MOSI = 0;}

    读字节操作程序

    读字节操作(图2)程序如下所示,与上述程序类似。首先发送地址,然后发送时钟从MISO读回数据。

    /*************************************************************************************** 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}

    自动递增模式下的写字节操作程序

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top