/// <summary> /// Set the PWM output on a single channel /// </summary> /// <param name="channel">1 to 16</param> /// <param name="on">Value between 0 and 4096</param> /// <param name="off">Value between 0 and 4096</param> /// <example>servopi.SetPWM(1,512,1024);</example> void ABElectronics_ServoPWMPi::SetPWM(byte channel, short on, short off) { channel = (byte)(channel - 1); WriteI2CByte((byte)(LED0_ON_L + (4 * channel)), (byte)(on & 0xFF)); WriteI2CByte((byte)(LED0_ON_H + (4 * channel)), (byte)(on >> 8)); WriteI2CByte((byte)(LED0_OFF_L + (4 * channel)), (byte)(off & 0xFF)); WriteI2CByte((byte)(LED0_OFF_H + (4 * channel)), (byte)(off >> 8)); }
/// <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)); }
/// <summary> /// Sets the type of interrupt for each pin on the selected port. 1 = interrupt is fired when the pin matches the default value. 0 = the interrupt is fired on state change /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> /// <param name="value">number between 0 and 255 or 0x00 and 0xFF</param> void ABElectronics_IOPi::SetInterruptType(byte port, byte value) { switch (port) { case 0: WriteI2CByte(INTCONA, value); break; case 1: WriteI2CByte(INTCONB, value); break; 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 } }
/// <summary> /// These bits set the compare value for pins configured for interrupt-on-change on the selected port. If the associated pin level is the opposite from the register bit, an interrupt occurs. /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> /// <param name="value">number between 0 and 255 or 0x00 and 0xFF</param> void ABElectronics_IOPi::SetInterruptDefaults(byte port, byte value) { switch (port) { case 0: WriteI2CByte(DEFVALA, value); break; case 1: WriteI2CByte(DEFVALB, value); break; default: // default break; } }
/** * 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 } }
/// <summary> /// This sets the polarity of the INT output pins /// </summary> /// <param name="value">1 = Active - high. 0 = Active - low.</param> void ABElectronics_IOPi::SetInterruptPolarity(byte value) { switch (value) { case 0: config = UpdateByte(config, 1, false); WriteI2CByte(IOCON, config); break; case 1: config = UpdateByte(config, 1, true); WriteI2CByte(IOCON, config); break; default: // default break; } }
/// <summary> /// Sets the mirror status of the interrupt pins. /// </summary> /// <param name="value">0 = The INT pins are not mirrored. INTA is associated with PortA and INTB is associated with PortB. 1 = The INT pins are internally connected</param> void ABElectronics_IOPi::MirrorInterrupts(byte value) { switch (value) { case 0: config = UpdateByte(config, 6, false); WriteI2CByte(IOCON, config); break; case 1: config = UpdateByte(config, 6, true); WriteI2CByte(IOCON, config); break; default: // default break; } }
/// <summary> /// set the internal 100K pull-up resistors for the selected IO port /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> /// <param name="value">number between 0 and 255 or 0x00 and 0xFF</param> void ABElectronics_IOPi::SetPortPullups(byte port, byte value) { switch (port) { case 0: porta_pullup = value; WriteI2CByte(GPPUA, value); break; case 1: portb_pullup = value; WriteI2CByte(GPPUB, value); break; default: // default break; } }
void ABElectronics_IOPi::InvertPort(byte port, byte polarity) { switch (port) { case 0: WriteI2CByte(IPOLA, polarity); porta_polarity = polarity; break; case 1: WriteI2CByte(IPOLB, polarity); portb_polarity = polarity; break; default: // default break; } }
/// <summary> /// Enable interrupts for the pins on the selected port. /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> /// <param name="value">number between 0 and 255 or 0x00 and 0xFF</param> void ABElectronics_IOPi::SetInterruptOnPort(byte port, byte value) { switch (port) { case 0: WriteI2CByte(GPINTENA, value); intA = value; break; case 1: WriteI2CByte(GPINTENB, value); intB = value; break; default: // default break; } }
/// <summary> /// write to all pins on the selected port. /// </summary> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> /// <param name="value">number between 0 and 255 or 0x00 and 0xFF</param> void ABElectronics_IOPi::WritePort(byte port, byte value) { switch (port) { case 0: WriteI2CByte(GPIOA, value); portaval = value; break; case 1: WriteI2CByte(GPIOB, value); portbval = value; break; default: // default break; } }
/// <summary> /// write to an individual pin /// </summary> /// <param name="pin">1 - 16</param> /// <param name="value">0 = logic low, 1 = logic high</param> void ABElectronics_IOPi::WritePin(byte pin, bool value) { pin = (byte)(pin - 1); if (pin >= 0 && pin < 8) { portaval = UpdateByte(portaval, pin, value); WriteI2CByte(GPIOA, portaval); } else if (pin >= 8 && pin < 16) { portbval = UpdateByte(portbval, (byte)(pin - 8), value); WriteI2CByte(GPIOB, portbval); } else { // default } }
/** * 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)); } }
void ABElectronics_IOPi::SetPinDirection(byte pin, bool direction) { pin = (byte)(pin - 1); if (pin >= 0 && pin < 8) { port_a_dir = UpdateByte(port_a_dir, pin, direction); WriteI2CByte(IODIRA, port_a_dir); } else if (pin >= 8 && pin < 16) { port_b_dir = UpdateByte(port_b_dir, (byte)(pin - 8), direction); WriteI2CByte(IODIRB, port_b_dir); } else { // catch all for invalid pin } }
/// <summary> /// Set the direction for an IO port. You can control the direction of all 8 pins on a port by sending a single byte value. /// Each bit in the byte represents one pin so for example 0x0A would set pins 2 and 4 to inputs and all other pins to outputs. /// </summary> /// <param name="direction">Direction for all pins on the port. 1 = input, 0 = output</param> /// <param name="port">0 = pins 1 to 8, 1 = pins 9 to 16</param> void ABElectronics_IOPi::SetPortDirection(byte port, byte direction) { switch (port) { case 0: WriteI2CByte(IODIRA, direction); port_a_dir = direction; break; case 1: WriteI2CByte(IODIRB, direction); port_b_dir = direction; break; default: // default break; } }
/// <summary> /// invert the polarity of the selected pin. /// </summary> /// <param name="pin">1 to 16</param> /// <param name="polarity">False = same logic state of the input pin, True = inverted logic state of the input pin</param> void ABElectronics_IOPi::InvertPin(byte pin, bool polarity) { pin = (byte)(pin - 1); if (pin >= 0 && pin < 8) { porta_polarity = UpdateByte(portaval, pin, polarity); WriteI2CByte(IPOLA, porta_polarity); } else if (pin >= 8 && pin < 16) { portb_polarity = UpdateByte(portbval, (byte)(pin - 8), polarity); WriteI2CByte(IPOLB, portb_polarity); } else { // default } }
/// <summary> /// Enable interrupts for the selected pin. /// </summary> /// <param name="pin">1 to 16</param> /// <param name="value">0 = interrupt disabled, 1 = interrupt enabled</param> void ABElectronics_IOPi::SetInterruptOnPin(byte pin, bool value) { pin = (byte)(pin - 1); if (pin >= 0 && pin < 8) { intA = UpdateByte(intA, pin, value); WriteI2CByte(GPINTENA, intA); } else if (pin >= 8 && pin < 16) { intB = UpdateByte(intB, (byte)(pin - 8), value); WriteI2CByte(GPINTENB, intB); } else { // default } }
/// <summary> /// Set the internal 100K pull-up resistors for an individual pin /// </summary> /// <param name="pin">1 to 16</param> /// <param name="value">true = enabled, false = disabled</param> void ABElectronics_IOPi::SetPinPullup(byte pin, bool value) { pin = (byte)(pin - 1); if (pin >= 0 && pin < 8) { porta_pullup = UpdateByte(porta_pullup, pin, value); WriteI2CByte(GPPUA, porta_pullup); } else if (pin >= 8 && pin < 16) { portb_pullup = UpdateByte(portb_pullup, (byte)(pin - 8), value); WriteI2CByte(GPPUB, portb_pullup); } else { // default } }
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(); }
void I2CDeviceInitialize(BYTE *RegSet, BYTE delay) { int cnt=0; BYTE addr, index, val; WORD w_page=0; // BYTE speed; addr = *RegSet; #ifdef DEBUG_TW88 dPrintf("\nI2C address : %02bx", addr); #endif cnt = *(RegSet+1); //ignore cnt RegSet+=2; // if(addr) // speed = SetI2CSpeed(I2C_SPEED_SLOW); while (( RegSet[0] != 0xFF ) || ( RegSet[1]!= 0xFF )) { // 0xff, 0xff is end of data index = *RegSet; val = *(RegSet+1); if ( addr == 0 ) { if(index==0xFF) { w_page=val << 8; } else { WriteTW88(w_page+index, val); } } else WriteI2CByte(addr, index, val); if(delay) delay1ms(delay); #ifdef DEBUG_TW88 dPrintf("\n addr=%02x index=%03x val=%02x", (WORD)addr, w_page | index, (WORD)val ); #endif RegSet+=2; } // if(addr) // SetI2CSpeed(speed); }
void InitAudio(int frequency) { int mode; switch(frequency) { case 8000: mode=0x00; break; case 11025: mode=0x10; break; case 12000: mode=0x20; break; case 16000: mode=0x40; break; case 22050: mode=0x50; break; case 24000: mode=0x60; break; case 32000: mode=0x80; break; default: case 44100: mode=0x90; break; case 48000: mode=0xa0; break; case 96000: mode=0xe0; break; } /* WriteI2CByte(0x4b,0x7A,0x20); // VAUX3_DEV_GRP WriteI2CByte(0x4b,0x7D,0x03); // VAUX3_DEDICATED WriteI2CByte(0x4b,0x8e,0xe0); // VPLL2_DEV_GRP WriteI2CByte(0x4b,0x91,0x05); // VPLL2_DEDICATED WriteI2CByte(0x49,0x01,0x93); // codec_MODE WriteI2CByte(0x49,0x02,0xc3); // OPTION WriteI2CByte(0x49,0x03,0x00); // ? WriteI2CByte(0x49,0x04,0x00); // MICBIAS_CTL WriteI2CByte(0x49,0x05,0x00); // ANAMICL WriteI2CByte(0x49,0x06,0x00); // ANAMICR WriteI2CByte(0x49,0x07,0x00); // AVADC_CTL WriteI2CByte(0x49,0x08,0x00); // ADCMICSEL WriteI2CByte(0x49,0x09,0x00); // DIGMIXING WriteI2CByte(0x49,0x0a,0x00); // ATXL1PGA WriteI2CByte(0x49,0x0b,0x00); // ATXR1PGA WriteI2CByte(0x49,0x0c,0x00); // AVTXL2PGA WriteI2CByte(0x49,0x0d,0x00); // AVTXR2PGA WriteI2CByte(0x49,0x0e,0x01); // AUDIO_IF WriteI2CByte(0x49,0x0f,0x00); // VOICE_IF WriteI2CByte(0x49,0x10,0x00); // ARXR1PGA WriteI2CByte(0x49,0x11,0x00); // ARXL1PGA WriteI2CByte(0x49,0x12,0x6c); // ARXR2PGA WriteI2CByte(0x49,0x13,0x6c); // ARXL2PGA WriteI2CByte(0x49,0x14,0x00); // VRXPGA WriteI2CByte(0x49,0x15,0x00); // VSTPGA WriteI2CByte(0x49,0x16,0x00); // VRX2ARXPGA WriteI2CByte(0x49,0x17,0x0c); // AVDAC_CTL WriteI2CByte(0x49,0x18,0x00); // WriteI2CByte(0x49,0x19,0x00); // WriteI2CByte(0x49,0x1a,0x00); // WriteI2CByte(0x49,0x1b,0x2b); // ARXL2_APGA_CTL WriteI2CByte(0x49,0x1c,0x2b); // ARXR2_APGA_CTL WriteI2CByte(0x49,0x1d,0x00); // WriteI2CByte(0x49,0x1e,0x00); // WriteI2CByte(0x49,0x1f,0x00); // WriteI2CByte(0x49,0x20,0x00); // WriteI2CByte(0x49,0x21,0x00); // WriteI2CByte(0x49,0x22,0x24); // HS_SEL WriteI2CByte(0x49,0x23,0x0a); // HS_GAIN_SET WriteI2CByte(0x49,0x24,0x42); // HS_POPN_SET WriteI2CByte(0x49,0x25,0x00); // WriteI2CByte(0x49,0x26,0x00); // WriteI2CByte(0x49,0x27,0x00); // WriteI2CByte(0x49,0x28,0x00); // WriteI2CByte(0x49,0x29,0x00); // WriteI2CByte(0x49,0x2a,0x00); // WriteI2CByte(0x49,0x2b,0x00); // WriteI2CByte(0x49,0x2c,0x00); // WriteI2CByte(0x49,0x2d,0x00); // WriteI2CByte(0x49,0x2e,0x00); // WriteI2CByte(0x49,0x2f,0x00); // WriteI2CByte(0x49,0x30,0x00); // WriteI2CByte(0x49,0x31,0x00); // WriteI2CByte(0x49,0x32,0x00); // WriteI2CByte(0x49,0x33,0x00); // WriteI2CByte(0x49,0x34,0x00); // WriteI2CByte(0x49,0x35,0x00); // WriteI2CByte(0x49,0x36,0x00); // WriteI2CByte(0x49,0x37,0x00); // WriteI2CByte(0x49,0x38,0x00); // WriteI2CByte(0x49,0x39,0x00); // WriteI2CByte(0x49,0x3a,0x15); // APLL_CTL WriteI2CByte(0x49,0x3b,0x00); // WriteI2CByte(0x49,0x3c,0x00); // WriteI2CByte(0x49,0x3d,0x00); // WriteI2CByte(0x49,0x3e,0x00); // WriteI2CByte(0x49,0x3f,0x00); // WriteI2CByte(0x49,0x40,0x00); // WriteI2CByte(0x49,0x41,0x00); // WriteI2CByte(0x49,0x42,0x00); // WriteI2CByte(0x49,0x43,0x00); // WriteI2CByte(0x49,0x44,0x00); // WriteI2CByte(0x49,0x45,0x00); // WriteI2CByte(0x49,0x46,0x00); // WriteI2CByte(0x49,0x47,0x00); // WriteI2CByte(0x49,0x48,0x00); // WriteI2CByte(0x49,0x49,0x00); // */ WriteI2CByte(0x4b,0x44,0xc0); // PROTECT_KEY WriteI2CByte(0x4b,0x44,0x0c); // PROTECT_KEY WriteI2CByte(0x4b,0x3b,0x1a); // CFG_BOOT WriteI2CByte(0x4b,0x44,0x00); // PROTECT_KEY WriteI2CByte(0x49,0x02,0xc3); // OPTION // WriteI2CByte(0x49,0x04,0x04); // MICBIAS_CTL WriteI2CByte(0x49,0x05,0x14); // ANAMICL WriteI2CByte(0x49,0x06,0x14); // ANAMICR WriteI2CByte(0x49,0x07,0x0a); // AVADC_CTL WriteI2CByte(0x49,0x0a,0x00); // ATXL1PGA WriteI2CByte(0x49,0x0b,0x00); // ATXR1PGA WriteI2CByte(0x49,0x0e,0x01); // AUDIO_IF - 16 bit word length, 16 bit data, codec mode WriteI2CByte(0x49,0x48,0x00); // ANAMIC_GAIN WriteI2CByte(0x49,0x12,0x3f); // ARXR2PGA WriteI2CByte(0x49,0x13,0x3f); // ARXL2PGA WriteI2CByte(0x49,0x17,0x0c); // AVDAC_CTL WriteI2CByte(0x49,0x1b,0x33); // ARXL2_APGA_CTL WriteI2CByte(0x49,0x1c,0x33); // ARXR2_APGA_CTL // WriteI2CByte(0x49,0x1b,0x2d); // ARXL2_APGA_CTL // WriteI2CByte(0x49,0x1c,0x2d); // ARXR2_APGA_CTL // WriteI2CByte(0x49,0x3e,0x20); // MISC_SET_1 WriteI2CByte(0x49,0x22,0x24); // HS_SEL WriteI2CByte(0x49,0x23,0x0a); // HS_GAIN_SET // WriteI2CByte(0x49,0x24,0x42); // HS_POPN_SET WriteI2CByte(0x49,0x3a,0x16); // APLL_CTL - set PLL input frequency to 26 MHz (?) WriteI2CByte(0x49,0x01,mode|0x03); // codec_MODE WriteI2CByte(0x49,0x24,0x00|0x14); // HS_POPN_SET WriteI2CByte(0x49,0x24,0x00|0x14|0x40); // HS_POPN_SET WriteI2CByte(0x49,0x24,0x00|0x14|0x40|0x02); // HS_POPN_SET WriteI2CByte(0x49,0x01,mode|0x01); // codec_MODE WriteI2CByte(0x49,0x01,mode|0x03); // codec_MODE MCBSP2_SPCR2_REG=0x00000200; // place transmitter in reset MCBSP2_SPCR1_REG=0x00002000; // place receiver in reset, sign-extend input values MCBSP2_SYSCONFIG_REG=0x00000208; //MCBSP2_MCR2_REG=0; //MCBSP2_MCR1_REG=0; MCBSP2_THRSH2_REG=0x0000002; // Always read and write samples in chunks of at least 2 MCBSP2_THRSH1_REG=0x0000002; MCBSP2_XCCR_REG=0x00001008; MCBSP2_RCCR_REG=0x00000808; MCBSP2_RCR2_REG=0x00008041; // dual phase, 16 bit data, 1 bit delay MCBSP2_RCR1_REG=0x00000040; // 16 bit data in second phase MCBSP2_XCR2_REG=0x00008041; // dual phase, 16 bit data, 1 bit delay MCBSP2_XCR1_REG=0x00000040; // 16 bit data in second phase MCBSP2_SRGR2_REG=0x00004000; // use falling edge of clock and frame sync MCBSP2_SRGR1_REG=0x00000000; MCBSP2_PCR_REG=0x00000083; // 81? MCBSP2_UNDOCUMENTED1_REG=0x00000023; // MCBSP2_DXR_REG=0x000056f3; MCBSP2_SPCR2_REG=0x00000201; // take transmitter out of reset MCBSP2_SPCR1_REG=0x00002001; // take receiver out of reset, sign-extend input values }
void ABElectronics_ServoPWMPi::begin() { WIRE.begin(); // Reset PWM Chip WriteI2CByte(MODE1, 0x00); }