用于MAX7456随屏显示器SPI
时间:04-22
来源:互联网
点击:
/*************************************************************************************** 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}
自动递增模式下的写字节操作程序
模拟电路 模拟芯片 德州仪器 放大器 ADI 模拟电子 相关文章:
- 12位串行A/D转换器MAX187的应用(10-06)
- AGC中频放大器设计(下)(10-07)
- 低功耗、3V工作电压、精度0.05% 的A/D变换器(10-09)
- PIC16C5X单片机睡眠状态的键唤醒方法(11-16)
- 用简化方法对高可用性系统中的电源进行数字化管理(10-02)
- 利用GM6801实现智能快速充电器设计(11-20)