This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

reading Data using I2C

Hi

i want to read data using I2C, just like reading temperature from any digital sensor, located at the Address 0X00,

Is there a simple source code?

Parents
  • //External functions, and function prototypes
    unsigned char lcdPutRomString(unsigned char lcdAdr, const rom char* s);
    
    //Defines
    #define LCD2S_I2C_ADR 80    /* Default LCD I2C address when both switches on back of LCD2S board are set to 0 */
    
    void delay (unsigned long dly)
    {
      unsigned long i;
      for (i = 0; i < dly; i++);
    }
    
    void main (void)
    {
        unsigned char i;
    
        //Delay at while before writing to LCD display
        delay(200000ul);
    
        /////////////////////////////////////////////////
        //Send "Hello World" string. Sring is contained in ROM memory
        if (lcdPutRomString(LCD2S_I2C_ADR, "\fhello\nworld") == 1) {
            //An error will be returned if there is no LCD display on I2C bus for example
            //...... Handle Error!!!!!
        }
    
        while (1)
        {  /* ....... Main Program ...... */   }
    }
    
    /** Waits until the I2C bus is IDLE, and available to be used. */
    #define i2cWaitForIdle()
    
    /** Generate bus start condition, and waits until it is finished */
    #define i2cPutStartAndWait()
    
    /** Generate bus stop condition, and waits until it is finished */
    #define i2cPutStopAndWait()
    
    /** Tests if an ACK was received from the slave. Returns true if it was, else false. */
    #define i2cWasAckReceived()
    
    /**
     * This routine writes a single byte to the I2C bus,
     * and waits until the bus is idle before returning.
     *
     * @param data_out  Single data byte for I2C bus
     *
     * @return      Status byte for WCOL detection.  Returns 0 if OK, else 1 if error
     */
    unsigned char i2cPutByteAndWait(unsigned char data_out) {
        if ( collision  ) {         // test if write collision occurred
            return ( 1 );           // if WCOL bit is set return negative #
        }
    
        while( doing );             // wait until write cycle is complete
    
        i2cWaitForIdle();           // wait until bus goes idle
    
        return ( 0 );               // if WCOL bit is not set return non-negative #
    }
    
    unsigned char lcdPutRomString(unsigned char lcdAdr, const rom char* s) {
        unsigned char i = 0;
        char c;
        rom char* p;
    
        i2cWaitForIdle();
    
        i2cPutStartAndWait();       //Write I2C Start Condition, and wait for it to complete
    
        //Write module address
        i2cPutByteAndWait(lcdAdr);
    
        //Only continue if slave sent ACK. If no slave present, no ACK is sent. If no slave with address sent is present, no ACK is sent.
        if(i2cWasAckReceived()==0) {
            i2cPutStopAndWait();    //Reset I2C bus
            return 1;               //Error
        }
    
        //Write "Write Parsed String" command
        i2cPutByteAndWait(0x80);
    
        p = s;
        while((c = *p++)) {
            //Write next character to LCD display
            i2cPutByteAndWait(c);
        }
    
        i2cPutStopAndWait();    //Write I2C Stop condition, and wait for it to complete
    
        return 0;   //OK
    }
    <end code>
    
    
    

Reply
  • //External functions, and function prototypes
    unsigned char lcdPutRomString(unsigned char lcdAdr, const rom char* s);
    
    //Defines
    #define LCD2S_I2C_ADR 80    /* Default LCD I2C address when both switches on back of LCD2S board are set to 0 */
    
    void delay (unsigned long dly)
    {
      unsigned long i;
      for (i = 0; i < dly; i++);
    }
    
    void main (void)
    {
        unsigned char i;
    
        //Delay at while before writing to LCD display
        delay(200000ul);
    
        /////////////////////////////////////////////////
        //Send "Hello World" string. Sring is contained in ROM memory
        if (lcdPutRomString(LCD2S_I2C_ADR, "\fhello\nworld") == 1) {
            //An error will be returned if there is no LCD display on I2C bus for example
            //...... Handle Error!!!!!
        }
    
        while (1)
        {  /* ....... Main Program ...... */   }
    }
    
    /** Waits until the I2C bus is IDLE, and available to be used. */
    #define i2cWaitForIdle()
    
    /** Generate bus start condition, and waits until it is finished */
    #define i2cPutStartAndWait()
    
    /** Generate bus stop condition, and waits until it is finished */
    #define i2cPutStopAndWait()
    
    /** Tests if an ACK was received from the slave. Returns true if it was, else false. */
    #define i2cWasAckReceived()
    
    /**
     * This routine writes a single byte to the I2C bus,
     * and waits until the bus is idle before returning.
     *
     * @param data_out  Single data byte for I2C bus
     *
     * @return      Status byte for WCOL detection.  Returns 0 if OK, else 1 if error
     */
    unsigned char i2cPutByteAndWait(unsigned char data_out) {
        if ( collision  ) {         // test if write collision occurred
            return ( 1 );           // if WCOL bit is set return negative #
        }
    
        while( doing );             // wait until write cycle is complete
    
        i2cWaitForIdle();           // wait until bus goes idle
    
        return ( 0 );               // if WCOL bit is not set return non-negative #
    }
    
    unsigned char lcdPutRomString(unsigned char lcdAdr, const rom char* s) {
        unsigned char i = 0;
        char c;
        rom char* p;
    
        i2cWaitForIdle();
    
        i2cPutStartAndWait();       //Write I2C Start Condition, and wait for it to complete
    
        //Write module address
        i2cPutByteAndWait(lcdAdr);
    
        //Only continue if slave sent ACK. If no slave present, no ACK is sent. If no slave with address sent is present, no ACK is sent.
        if(i2cWasAckReceived()==0) {
            i2cPutStopAndWait();    //Reset I2C bus
            return 1;               //Error
        }
    
        //Write "Write Parsed String" command
        i2cPutByteAndWait(0x80);
    
        p = s;
        while((c = *p++)) {
            //Write next character to LCD display
            i2cPutByteAndWait(c);
        }
    
        i2cPutStopAndWait();    //Write I2C Stop condition, and wait for it to complete
    
        return 0;   //OK
    }
    <end code>
    
    
    

Children
  • Why claim "the answer" when it isn't?

    Software delay loops like that, without connection to any hardware and without any defined side effect is not good to use.

    Documentation like:

        //Delay at while before writing to LCD display
        delay(200000ul);
    


    are not making anyone happy. How long is "at while"? Wouldn't it be much better to have a _real_ delay function and to be able to do: delay_ms(500) and know that you get a delay of 500ms +/- internal rounding/truncation?

    Dummy code like:

    /** Waits until the I2C bus is IDLE, and available to be used. */
    #define i2cWaitForIdle()
    

    is obviously not the answer to any problem.