uint16_t srf08GetDistance(uint8_t echo) { // valid range is [0, 16] if(echo > 16) echo = 16; i2cStart(); i2cTransmit(currentAddress); i2cTransmit(2 + echo * 2); // select the correct distance register i2cStart(); i2cTransmit(currentAddress + 1); uint16_t distance = i2cReceive(true) << 8; // get the high byte distance |= i2cReceive(false); // get the low byte i2cStop(); return distance; }
uint8_t srf08GetVersion() { i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x00); // select software version register i2cStart(); // restart to indicate read operation i2cTransmit(currentAddress + 1); // select current device's read address uint8_t version = i2cReceive(false); // get the value at version register i2cStop(); return version; }
uint8_t srf08GetLight() { i2cStart(); i2cTransmit(currentAddress); i2cTransmit(0x01); // select light register i2cStart(); i2cTransmit(currentAddress + 1); uint8_t light = i2cReceive(false); i2cStop(); return light; }
static portTASK_FUNCTION(readTemp, args) { while (1) { i2cAcquire(DS3232_I2C_ADDRESS); i2cSend(0x11); unsigned char tempMSB = i2cReceive(I2C_ACK); unsigned char tempLSB = i2cReceive(I2C_NACK) >> 6; i2cRelease(); printf("Temp %d.%d C\n", tempMSB, 25 * tempLSB); vTaskDelay(1000 / portTICK_PERIOD_MS); } }
uint8 i2cRegisterRead(uint8 sadd, uint8 regadd) { int d = 0; uint8 buff; buff = (regadd-1)*2; i2cSetSlaveAdd(i2cREG1, sadd); i2cSetCount(i2cREG1, 1); i2cSetMode(i2cREG1, I2C_MASTER | I2C_TRANSMITTER | I2C_START_COND); i2cSend(i2cREG1, 1,&buff); while((i2cREG1->STR & (uint32)I2C_ARDY) == 0U) {} i2cSetMode(i2cREG1, I2C_MASTER | I2C_RECEIVER | I2C_START_COND | I2C_STOP_COND); i2cREG1->MDR = i2cREG1->MDR & (uint32)0xFFFFFDFF; i2cSetCount(i2cREG1, 1); i2cReceive(i2cREG1, 1, &buff); while(d < 50) { d++; } return buff; }