用CCS写的复杂版在这里
https://bbs.eeworld.com.cn/thread-374278-1-6.html
最近有在玩Arduino,就觉得这样的编程思想很不错,完全无需知道底层代码和寄存器的实现方式,只要用封装好的函数就可以实现相应的功能,开发者只要专注在逻辑和应用上就可以了,方便许多。于是回过头来玩一下Launchpad,这次用Energia,做一个之前做过的USB-I2C工具,代码量小很多,调试错误量也小很多,方便不少,我就把代码贴在这里,大家玩玩。。呵呵
- #include // Need the Wire library for I2C communication
- unsigned char dataBuff[130]; // The data buffer
- // The effective data area is 128 bytes.
- unsigned char oneByte = 0; // For holding the byte during each Wire.read() action
- unsigned int dataBuffIdx = 0; // Data position during read and write
- unsigned char inputDataComplete = 0; // Indicate the input is finished
- // Whenever a CR or LF is received
- // or input data length excceed the effective data length
- unsigned int availableBytes = 0; // Number of bytes in Serial RX buffer for read
- unsigned char slaveAddr = 0; // The 7-bit slave address
- unsigned char i2cRW = 0; // The I2C RW bit
- // 0x00 for master write
- // 0x01 for master read
- unsigned char trDataLen = 0; // The length of data to be transfered
- // either to I2C device or serial console
- unsigned char CRLF[2] = {0x0D, 0x0A}; // The CRLF
- void setup() {
- Serial.begin(9600); // Start serial dialog @9600 BR
- Wire.begin(); // Prepare I2C peripherals
- }
- void loop() {
- if (inputDataComplete == 1) { // Data receive finished
- // Go on processing data
- if (dataBuffIdx > 2) { // At lease 3 bytes in the data buffer
- i2cRW = dataBuff[dataBuffIdx - 1]; // The last byte received indicates I2C RW action
- slaveAddr = dataBuff[dataBuffIdx - 2]; // The I2C slave address is the byte before the RW byte
- if (i2cRW == 0x00) { // Master write
- trDataLen = dataBuffIdx - 2; // The actual data length
- // Excluding address byte and RW byte
- // The I2C master write action
- Wire.beginTransmission(slaveAddr);
- Wire.write(dataBuff, trDataLen);
- Wire.endTransmission();
- // --
- }
- if (i2cRW == 0x01) { // Master read
- trDataLen = dataBuff[0]; // The 1st byte is the number of bytes to read
- dataBuffIdx = 0; // Clear the buffer index for read counting
- // The I2C master read action
- Wire.requestFrom(slaveAddr, trDataLen);
- while(Wire.available()) {
- dataBuff[dataBuffIdx] = Wire.read();
- dataBuffIdx++;
- }
- // --
- Serial.write(dataBuff, (unsigned char)dataBuffIdx);
- // Write the data to serial console
- // Enforce the data length to be 128 bytes at max
- Serial.write(CRLF, 2); // Print CRLF indicating data ending
- }
-
- // Reset data count and action status
- dataBuffIdx = 0;
- inputDataComplete = 0;
- // Clear the serial buffer
- Serial.flush();
- }
- } else { // Read incoming data if available
- availableBytes = Serial.available(); // Get number of bytes available
- // for reading in serial buffer
- while (availableBytes && !inputDataComplete) { // Read data when there are some bytes available
- // And the input is not finished
- if (dataBuffIdx > 129) { // We will ignore the data
- // when the buffer limit is reached
- inputDataComplete = 1; // And also make the input is finished
- continue; // Skip all following codes
- }
- oneByte = Serial.read(); // Read 1 byte from the serial buffer
- if (oneByte == 0x0D || oneByte == 0x0A) { // Either CR(0x0D) or LF(0x0A) indicates the end of input
- inputDataComplete = 1; // Make the input is finished
- } else {
- dataBuff[dataBuffIdx] = oneByte; // Copy the byte to the data buffer
- availableBytes--;
- dataBuffIdx++;
- }
- }
- }
- }
复制代码