void LedPowerUp(void) { volatile BYTE bTemp; #ifdef SUPPORT_E1H_PANEL BYTE speed; #endif Printf("\nLED Powerup"); #ifdef SUPPORT_E1H_PANEL WriteI2CByte(0x58, 0x01, 0x05); #endif //WaitVBlank(1); //Printf("\nI2CID_ISL97671A INDEX:1 DATA:0x5"); WriteI2CByte(I2CID_ISL97671A, 0x01, 0x05); bTemp = ReadI2CByte(I2CID_ISL97671A, 0x01); if ((bTemp & 0x05) != 0x05) { Printf(" ID:58 Idx:1 W:0x05 R:%bx", bTemp); WriteI2CByte(I2CID_ISL97671A, 0x01, 0x05); bTemp = ReadI2CByte(I2CID_ISL97671A, 0x01); if ((bTemp & 0x05) != 0x05) { Printf(" W:0x05 R:%bx", bTemp); } } }
/// <summary> /// read the interrupt status for the pins on the selected port. /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> byte ABElectronics_IOPi::ReadInterruptStatus(byte port) { switch (port) { case 0: return ReadI2CByte(INTFA); case 1: return ReadI2CByte(INTFB); default: // default break; } }
/// <summary> /// read the value from the selected port at the time of the last interrupt trigger. /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> byte ABElectronics_IOPi::ReadInterruptCapture(byte port) { switch (port) { case 0: return ReadI2CByte(INTCAPA); case 1: return ReadI2CByte(INTCAPB); default: // default break; } }
/// <summary> /// read all pins on the selected port. /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> /// <returns>returns number between 0 and 255 or 0x00 and 0xFF</returns> byte ABElectronics_IOPi::ReadPort(byte port) { switch (port) { case 0: portaval = ReadI2CByte(GPIOA); return portaval; case 1: portbval = ReadI2CByte(GPIOB); return portbval; default: // default break; } }
/** * set FP_Bias */ void FP_BiasOnOff(BYTE fOn) { Printf("\nFP_Bias %s",fOn ? "On" : "Off"); //WriteTW88Page(PAGE0_GENERAL); //WriteTW88(REG084, 0x01); //disable if(fOn) { WriteI2CByte( I2CID_SX1504, 1, 0 ); // output enable WriteI2CByte( I2CID_SX1504, 0, ReadI2CByte(I2CID_SX1504, 0) & 0xFD ); // FPBIAS enable. } else { WriteI2CByte( I2CID_SX1504, 1, 0 ); // output enable WriteI2CByte( I2CID_SX1504, 0, ReadI2CByte(I2CID_SX1504, 0) | 0x02 ); // FPBIAS disable } }
/** * set FP_PWC */ void FP_PWC_OnOff(BYTE fOn) { Printf("\nFP_PWC %s",fOn ? "On" : "Off"); I2C_delay_base = 3; //assume 108/1.5. WriteTW88Page(PAGE0_GENERAL); WriteTW88(REG084, 0x01); //disable if(fOn) { WriteI2CByte( I2CID_SX1504, 1, 0 ); // output enable WriteI2CByte( I2CID_SX1504, 0, ReadI2CByte(I2CID_SX1504, 0) & 0xFE ); // FPPWC enable } else { WriteI2CByte( I2CID_SX1504, 1, 0 ); // output enable WriteI2CByte( I2CID_SX1504, 0, ReadI2CByte(I2CID_SX1504, 0) | 0x01 ); // FPPWC disable } }
int main(int argc, char* argv[]) { BYTE Buttons; printf("Example2: ByteIO\n"); #ifdef OnOSX signal(SIGINT, Quit); // Trap Control+C function Quit on Mac #endif if (!InitializeForByteIO()) { printf("\nFT232R cable found\n"); // Configure Buttons to be input WriteI2CByte(0xFF, ConfigurationRegister, ButtonsAddress); // Setup Buttons(InputPortRegister) as the default register WriteI2CByte(0xAA, InputPortRegister, ButtonsAddress); // Configure Lights to be output WriteI2CByte(0, ConfigurationRegister, LightsAddress); WriteI2CByte(0, ConfigurationRegister, DigitLSB); for (int i=0; i<100; i++) { Buttons = ReadI2CByte(ButtonsAddress); printf("\rButtons = %2.2X", Buttons); // BYTE Answer = (Buttons & 0xF) * ((Buttons>>4) & 0xF); WriteI2CByte(Buttons, OutputPortRegister, LightsAddress); WriteI2CByte(SevenSegmentLookup[i&0xf], OutputPortRegister, DigitLSB); Idle(1000); } } Quit(); }
/* * Will attempt to read the MPU6050 PWR_MGMT_1 register and either enable or disable the sleep mode bit * by writing the bit back to the register. Will return true upon a successful write and false otherwise. */ unsigned char EnableSleepMode(unsigned char en) { byte = 0; // read the register to get the current value if (ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, &byte)) { if ((en) && !(byte & 0x40)) byte |= 0x40; else if (!(en) && (byte & 0x40)) byte &= ~0x40; else return 1; } else return 0; // write back the altered register value if (WriteI2CBytes(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, &byte, 1)) { return 1; } return 0; }
/* * Will attempt to burst read the ACCEL_OUT registers * Returns true if the read was a success and false otherwise */ unsigned char GetAccelVals() { for (accelDataNdx = 0; accelDataNdx < 6; accelDataNdx++) { if (ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_XOUT_H + accelDataNdx, &byte)) { accelData[accelDataNdx] = byte; } else { return 0; } } curAccelXVal = accelData[0]; curAccelXVal = (curAccelXVal << 8) & 0xFF00; curAccelXVal |= accelData[1]; curAccelYVal = accelData[2]; curAccelYVal = (curAccelYVal << 8) & 0xFF00; curAccelYVal |= accelData[3]; curAccelZVal = accelData[4]; curAccelZVal = (curAccelZVal << 8) & 0xFF00; curAccelZVal |= accelData[5]; return 1; }
unsigned char GetGyroVals() { for (gyroDataNdx = 0; gyroDataNdx < 6; gyroDataNdx++) { if (ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_XOUT_H + gyroDataNdx, &byte)) { gyroData[gyroDataNdx] = byte; } else { return 0; } } curGyroXVal = gyroData[0]; curGyroXVal = (curGyroXVal << 8) & 0xFF00; curGyroXVal |= gyroData[1]; curGyroYVal = gyroData[2]; curGyroYVal = (curGyroYVal << 8) & 0xFF00; curGyroYVal |= gyroData[3]; curGyroZVal = gyroData[4]; curGyroZVal = (curGyroZVal << 8) & 0xFF00; curGyroZVal |= gyroData[5]; return 1; }
/** * set GPIO for FP */ void FP_GpioDefault(void) { WriteTW88Page(PAGE0_GPIO); //IR uses PORT1_4(GPIO40). To disable TCPOLN output, we need to enable it as GPIO and uses it as input port with INT11. WriteTW88(REG084, 0x01); //GPIO 4x Enable - GPIO40 enable WriteTW88(REG08C, 0x00); //GPIO 4x direction - GPIO40 input WriteTW88(REG094, 0x00); //GPIO 4x output data - GPIO40 outdata as 0. if(access) { //turn off FPPWC & FPBias. make default // 0x40 R0 R1 is related with FP_PWC_OnOff WriteI2CByte( I2CID_SX1504, 1, 0/*3*/ ); //RegDir: input WriteI2CByte( I2CID_SX1504, 0, 0xFF/*3*/ ); //RegData: FPBias OFF. FPPWC disable. //WriteI2CByte( I2CID_SX1504, 1, 0xFF/*3*/ ); //RegDir: input Printf("\nI2CID_SX1504 0:%02bx 1:%bx",ReadI2CByte(I2CID_SX1504, 0), ReadI2CByte(I2CID_SX1504, 1)); } }
/// <summary> /// read the value of an individual pin. /// </summary> /// <param name="pin">1 - 16</param> /// <returns>0 = logic level low, 1 = logic level high</returns> bool ABElectronics_IOPi::ReadPin(byte pin) { pin = (byte)(pin - 1); if (pin >= 0 && pin < 8) { portaval = ReadI2CByte(GPIOA); return CheckBit(portaval, pin); } else if (pin >= 8 && pin < 16) { portbval = ReadI2CByte(GPIOB); return CheckBit(portbval, (byte)(pin - 8)); } else { // default } }
/* * WIll attemp to read the MPU6050_RA_Y_SELF_TEST register and acquire the factory trim * value for the y gyro and accel. Will return true if the read is successful */ unsigned char GyroAccelZSelfTest() { if (ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_Z_SELF_TEST, &byte)) { gyroZFT = byte & 0x1F; // mask the lower 5 bits accelZFT = byte & 0xE0; return 1; } return 0; }
/* * Will attempt to write to the MPU6050_RA_INT_ENABLE register and enable whichever interrupt * is passed in through the argument inter. Will return true if the interrupt was enabled. */ unsigned char EnableMPUInterrupt(unsigned char inter) { if (ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_INT_ENABLE, &byte)) { byte |= inter; if (WriteI2CBytes(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_INT_ENABLE, &byte, 1)) { return 1; } } return 0; }
/// <summary> /// Set the PWM frequency in hertz /// </summary> /// <param name="freq"></param> /// <example>servopi.SetPWMFreqency(60);</example> void ABElectronics_ServoPWMPi::SetPWMFreqency(int freq) { double scaleval = 25000000.0; // 25MHz scaleval /= 4096.0; // 12-bit scaleval /= freq; scaleval -= 1.0; double prescale = floor(scaleval + 0.5); byte oldmode = ReadI2CByte(MODE1); byte newmode = (byte)((oldmode & 0x7F) | 0x10); WriteI2CByte(MODE1, newmode); WriteI2CByte(PRE_SCALE, (byte)(floor(prescale))); WriteI2CByte(MODE1, oldmode); WriteI2CByte(MODE1, (byte)(oldmode | 0x80)); }
/* * Will attempt to read the MPU6050 WHO_AM_I register which contains the MPU6050's slave address. * Returns true if the read succeeded with the correct return value and false otherwise. */ unsigned char TestI2CConnection() { byte = 0; if (ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_WHO_AM_I, &byte)) { if (byte == 0x68) { return 1; } } return 0; }
/* * Will attempt to read the MPU6050_RA_INT_STATUS register and will return its value. */ unsigned char GetMPUIntStatus() { ReadI2CByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_INT_STATUS, &byte); return byte; }