S8 I2C1_WRITE_String(U8 DevAdd, U8 RegAdd, U8 *RegValue, U8 len) { char ComRes; DevAdd = (DevAdd<<1)|M_I2C_Write; StartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(DevAdd); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(RegAdd); // Write the chip ID register loaction for read IdleI2C1(); // Send the start bit while(I2C1STATbits.ACKSTAT); //wait for acknowledgement while(len--) { ComRes=MasterWriteI2C1(*RegValue); // Write the chip ID register loaction for read IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement RegValue++; } StopI2C1(); // Stop the I2C communication IdleI2C1(); // Wait to complete return ComRes; }
S8 I2C1_READ_String(U8 DevAdd, U8 RegAdd, U8 *RegValue, U8 len) { int DataWait = len*100; char ComRes,DevAddr; DevAddr = (DevAdd<<1)|M_I2C_Write; StartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(DevAddr); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(RegAdd); // Write the chip ID register loaction for read IdleI2C1(); // Send the start bit while(I2C1STATbits.ACKSTAT); //wait for acknowledgement RestartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement DevAddr = (DevAdd<<1)|M_I2C_Read; MasterWriteI2C1(DevAddr); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement MastergetsI2C1(len, RegValue, DataWait); // Read the Register value IdleI2C1(); // Wait to complete NotAckI2C1(); // Not Acknowledge I2C IdleI2C1(); // Wait to complete StopI2C1(); // Stop I2C communication IdleI2C1(); // Wait to complete; return ComRes; }
uint8_t I2C_WriteToReg(uint8_t I2CAddress, uint8_t deviceRegister, uint8_t data) { StartI2C1(); while(I2CCONbits.SEN); IFS1bits.MI2C1IF = 0; MasterWriteI2C1(I2CAddress << 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(deviceRegister); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(data); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag StopI2C1(); // Write stop sequence. while(I2CCONbits.PEN); IdleI2C1(); return(1); }
uint8_t I2C_ReadFromReg(uint8_t I2CAddress, uint8_t deviceRegister) { uint8_t data = 0; StartI2C1(); while(I2CCONbits.SEN); MasterWriteI2C1(I2CAddress << 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(deviceRegister); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag RestartI2C1(); // Second start. while(I2CCONbits.RSEN); MasterWriteI2C1((I2CAddress << 1 )+ 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag data = MasterReadI2C1(); NotAckI2C1(); // Read stop sequence. while(I2C1CONbits.ACKEN == 1); StopI2C1(); while(I2CCONbits.PEN); IdleI2C1(); return(data); }
void I2C_WriteToReg(uint8_t address, uint8_t deviceRegister, uint8_t data) { // Assert the start condition StartI2C1(); while (I2C1CONbits.SEN); IFS1bits.MI2C1IF = 0; // Send the 7-bit I2C device address MasterWriteI2C1(address << 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the register address MasterWriteI2C1(deviceRegister); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the data MasterWriteI2C1(data); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Assert the stop condition StopI2C1(); while (I2C1CONbits.PEN); // Sit idle on the bus IdleI2C1(); }
/** * @brief Write to device using generic i2c protocol * @param[in] slave_addr - slave address * @param[in] reg_addr - register address * @param[in] length - number of bytes to read * @param[in] *data - pointer to where register data is to be transfered * @return 0 if sucessfull, 1 otherwise */ int i2c_read(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char *data) { BYTE i=2; StartI2C1(); //Send the Start Bit IdleI2C1(); if (MasterWriteI2C1(((slave_addr<<1)|(0x00)))) return 1; IdleI2C1(); if (MasterWriteI2C1(reg_addr)) return 1; IdleI2C1(); StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete if (MasterWriteI2C1(((slave_addr<<1)|(0x01)))) return 1; IdleI2C1(); I2CReceiverEnable ( I2C1, TRUE); for(i=0;i<length;i++) { data[i] = MasterReadI2C1(); if(i<(length-1)) { I2CAcknowledgeByte(MPU_I2C, TRUE); IdleI2C1(); } else { I2CAcknowledgeByte(MPU_I2C, FALSE); IdleI2C1(); } } StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete return 0; }
void LDByteWriteI2C(unsigned char SlaveAddress, unsigned char reg, unsigned char data){ StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1(SlaveAddress); //transmit write command IdleI2C1(); //Wait to complete MasterWriteI2C1(reg); IdleI2C1(); MasterWriteI2C1(data); IdleI2C1(); StopI2C1(); IdleI2C1(); }
void I2C_ReadFromReg_Burst(uint8_t address, uint8_t deviceRegister, uint8_t* data, uint8_t burstNum) { // Assert the start condition StartI2C1(); while (I2C1CONbits.SEN); // Send the 7-bit device address MasterWriteI2C1(address << 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the register address MasterWriteI2C1(deviceRegister); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Start a new I2C transaction RestartI2C1(); while (I2C1CONbits.RSEN); // Send the second address MasterWriteI2C1((address << 1) + 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Read the data data[0] = MasterReadI2C1(); if (burstNum > 1) { uint8_t i; for (i = 1; i < burstNum; i++) { AckI2C1(); while (I2C1CONbits.ACKEN == 1); data[i] = MasterReadI2C1(); } } // No need to ack reception of this data NotAckI2C1(); while (I2C1CONbits.ACKEN == 1); // Stop the I2C transaction StopI2C1(); while (I2C1CONbits.PEN); // Go idle on the bus IdleI2C1(); }
void ClearAll(void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(Clear); IdleI2C1(); StopI2C1(); IdleI2C1(); }
void FirstLine(void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(0x80); IdleI2C1(); StopI2C1(); IdleI2C1(); }
void SecondLine(void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(Line2); IdleI2C1(); StopI2C1(); IdleI2C1(); }
/*************************************************** * SendData(int data, unsigned int address) * * * * Sends a byte of data (DATA) over the I2C line * * to I2C address ADDRESS * * * * Returns: nothing * ***************************************************/ void SendData (int data, unsigned int address){ StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1((address << 1)); //Sends the slave address over the I2C line. This must happen first so the //proper slave is selected to receive data. IdleI2C1(); //Wait to complete MasterWriteI2C1(data); //Sends data byte over I2C line IdleI2C1(); //Wait to complete StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete } //end function
void Write23X08_17(unsigned char reg, unsigned char data, unsigned char gAddrPins) { StartI2C1(); // Wait till start sequence is completed while (I2C1CONbits.SEN); MasterWriteI2C1(gControlByte | WrtCmd | gAddrPins); WaitForACK(); MasterWriteI2C1(reg); WaitForACK(); MasterWriteI2C1(data); WaitForACK(); StopI2C1(); // Wait till stop sequence is completed while (I2C1CONbits.PEN); }
void LDByteReadI2C(unsigned char SlaveAddress, unsigned char reg, unsigned char *data, int num){ StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1(SlaveAddress); //transmit write command IdleI2C1(); //Wait to complete MasterWriteI2C1(reg); IdleI2C1(); StopI2C1(); IdleI2C1(); StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1(SlaveAddress|0x01); //transmit read command IdleI2C1(); //Wait to complete MastergetsI2C1(num, data, 30); StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete }
/* * Function reads I2C device from slaveAdd * inputs: slaveAdd * registerAdd * returns: int recByte -> received byte from slave device * -1 -> reading error */ unsigned char I2C1ReadByte1(UINT8 slaveAdd, UINT8 registerAdd) { unsigned char recByte = 0; UINT8 address; StartI2C1(); IdleI2C1(); //Wait to complete address = (slaveAdd<<1) & 0xFE; //write MasterWriteI2C1(address); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return 0; } //Send register register location MasterWriteI2C1(registerAdd); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return 0; } RestartI2C1(); //Send start condition again IdleI2C1(); address = (slaveAdd<<1) | 0x01; //Read MasterWriteI2C1(address); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return 0; } recByte = MasterReadI2C1(); //NotAckI2C1(); IdleI2C1(); StopI2C1(); IdleI2C1(); return recByte; }
unsigned int portableMasterWriteI2C(I2cBusConnection* i2cBusConnection, unsigned char data) { I2cBus* i2cBus = i2cBusConnection->i2cBus; if (i2cBus == NULL) { return MasterWriteI2C1(data); } else { I2C_MODULE i2cModule = getI2C_MODULE(i2cBus->port); return I2CSendByte(i2cModule, data); } }
int RcvData(unsigned int address) { StartI2C1(); //Send line start condition IdleI2C1(); //Wait to complete MasterWriteI2C1((address << 1) | 1); //Write out slave address OR 1 (read command) IdleI2C1(); //Wait to complete int rcv = MasterReadI2C1(); //Read in a value StopI2C1(); //Send line stop condition IdleI2C1(); //Wait co complete return rcv; //Return read value }
void i2cSendByte(unsigned char channel, unsigned char byte) { if (channel == 1) { MasterWriteI2C1(byte); while(I2C1STATbits.TRSTAT); while(I2C1STATbits.ACKSTAT); } else { MasterWriteI2C2(byte); while(I2C2STATbits.TRSTAT); while(I2C2STATbits.ACKSTAT); } }
unsigned char Read23X08_17(unsigned char reg, unsigned char gAddrPins) { unsigned char num; StartI2C1(); // Wait till start sequence is completed while (I2C1CONbits.SEN); MasterWriteI2C1(gControlByte | WrtCmd | gAddrPins); WaitForACK(); MasterWriteI2C1(reg); WaitForACK(); RestartI2C1(); IdleI2C1(); MasterWriteI2C1(gControlByte | RdCmd | gAddrPins); WaitForACK(); num = MasterReadI2C1(); NotAckI2C1(); StopI2C1(); IdleI2C1(); return (num); }
void Show(unsigned char *text) { // Assert a start condition StartI2C1(); while(_SEN); // Write the address of the slave MasterWriteI2C1(Slave); while(I2C1STATbits.TBF); //Wait till address is transmitted while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle IdleI2C1(); MasterWriteI2C1(Datasend); IdleI2C1(); while(*text) { MasterWriteI2C1(*text); IdleI2C1(); ++text; } StopI2C1(); delay(2); }
/** * @brief Write to device using generic i2c protocol * @param[in] slave_addr - slave address * @param[in] reg_addr - register address * @param[in] length - number of bytes to write * @param[in] *data - pointer for data to write * @return 0 if sucessfull, 1 otherwise */ int i2c_write(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char const *data) { BYTE i; StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete if (MasterWriteI2C1(((slave_addr<<1)|(0x00)))) return 1; IdleI2C1(); if (MasterWriteI2C1(reg_addr)) return 1; IdleI2C1(); for(i=0;i<length;i++){ if (MasterWriteI2C1(data[i])) return 1; } StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete return 0; }
void InitDisplay() { unsigned int config1 = I2C_ON | I2C_7BIT_ADD; //unsigned int config2 = 15; // Works with no pll in use unsigned int config2 = 24; delay(100); // THIS IS REQUIRED TO ALLOW THE VOLTAGE TO STABILIZE BEFORE INIT-ING THE DISPLAY ConfigIntI2C1(MI2C_INT_OFF); //Disable I2C interrupt CloseI2C1(); OpenI2C1( config1, config2); IdleI2C1(); // Assert a start condition StartI2C1(); while(_SEN); // Write the address of the slave MasterWriteI2C1(Slave); while(I2C1STATbits.TBF); //Wait till address is transmitted while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(0x38); IdleI2C1(); delay(10); MasterWriteI2C1(0x39); IdleI2C1(); delay(10); MasterWriteI2C1(0x14); IdleI2C1(); MasterWriteI2C1(0x25); IdleI2C1(); MasterWriteI2C1(0x5E); IdleI2C1(); MasterWriteI2C1(0x6D); IdleI2C1(); MasterWriteI2C1(0x0C); IdleI2C1(); MasterWriteI2C1(0x01); IdleI2C1(); MasterWriteI2C1(0x06); IdleI2C1(); delay(10); StopI2C1(); IdleI2C1(); CGRAM(); //define CGRAM StartI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(0x39); IdleI2C1(); MasterWriteI2C1(0x01); //go back Home IdleI2C1(); StopI2C1(); delay(10); }
void CGRAM (void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(0x38); //go to instructino table 0 IdleI2C1(); MasterWriteI2C1(0x40); //Set CGRAM address to 0x00 IdleI2C1(); StopI2C1(); delay(10); StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Datasend); IdleI2C1(); MasterWriteI2C1(0x00); //write to first CGRAM address IdleI2C1(); MasterWriteI2C1(0x1E); IdleI2C1(); MasterWriteI2C1(0x18); IdleI2C1(); MasterWriteI2C1(0x14); IdleI2C1(); MasterWriteI2C1(0x12); IdleI2C1(); MasterWriteI2C1(0x01); IdleI2C1(); MasterWriteI2C1(0x00); IdleI2C1(); MasterWriteI2C1(0x00); //8 bytes per character IdleI2C1(); //continue writing to remaining CGRAM if desired StopI2C1(); IdleI2C1(); }