I2C slave device support

This is not an example of good code, or good use of registers, or anything good for that matter!
This is purely a "working demonstration" of being able to communicate with an I2C device - in this case a BMP-180 temperature and pressure sensor - to read the calibration constants, raw temperature sensor value and calculate a compensated temperature output with the new I2C-capable WC8 firmware.
 
This was really just a "proof code" and no more, but presented to hopefully get others on the way with their own I2C projects.
 

Code:
START    
        set ram1 0xb4   ; read AC6
        callsub get
        set ram8 ram1

        set ram1 0xb2   ; read AC5
        callsub get
        set ram7 ram1

        set ram1 0xbc   ; read MC
        callsub get
        tstgt ram1 32767        ; correct sign
        sub ram1 65536 ram1     ; if > 32768, make negative
        set ram6 ram1

        set ram1 0xbe   ; read MD
        callsub get
        set ram5 ram1


        set op1 1     ; trigger DSO on rising edge to see the conversion setup
        i2cwrite 1 0 0xee       ; set up for temperature conversion
        i2cwrite 0 0 0xf4
        i2cwrite 0 1 0x2e
        delay 10                ; brief pause for conversion to complete
 
        set op1 0      ; trigger DSO on falling edge to see reply
        set ram1 0xf6           ; read back uncompensated temperature
        callsub get

        sub ram1 ram8 ram1      ; X1 = (UT-AC6) * AC5/2^15
        mul ram1 ram7 ram1
        div ram1 32768 ram4

        mul ram6 2048 ram1      ; X2 = MC * 2^11 / (X1 + MD)
        add ram4 ram5 ram2
        div ram1 ram2 ram3

        add ram4 ram3 ram1      ; T*10 = (X1 + X2 + 8) / 2^4
        add ram1 8 ram1
        div ram1 16 var8

        delay 100   
        END    

get:
        i2cwrite 1 0 0xee
        i2cwrite 0 0 ram1
        i2cwrite 1 0 0xef
        i2cread 0 0 ram11
        i2cread 1 1 ram10
        ret
 
Hi Ross,
 
You got the test firmware working for I2CREAD and I2CWRITE?
 
I2CWRITE 1 0 0xEE 
That 0xEE is the I2C address of the sensor.  Later on
I2CWRITE 1 0 0xEF
0xEF is also address to the same sensor, but with READ bit on for that sensor.
Once that sensor received that, it will send out the data field specified in the first I2CWRITE command.
 
Each time when sending address to sensor, make send_start bit 1, that is telling sensor either start communication or re-start communication.
Only in the last read or write, send_end bit set to 1.
 
I2C support is implemented using top two spare pins on the 8 pin header.  There are four already have pull up resistors,  the one near 5 pin header is the CLK, next is DATA.  You will also need 5V and ground, that you can find from the 5 pin header.
 
Anyone interested in testing this I2C firmware and having WC8 board with bootloader, please contact us directly.
 
With I2C support, it may be better to use I2C real time clock support instead of using 1-wire real time clock.  The I2C realtime clock module is cheap on eBay #221427344970
 
Yes, Wayne... it's working fine. Sensor is currently right at the board but I will try with a length of cable later today.
I also finished coding the pressure reading and compensation code last night. It's working too, although the sensor seems to drift badly.
It seems to be hyper-sensitive to temperature variations or something. It's drifting several millibars/hPa over a timescale of a minute or so, even with maximum oversampling.
I don't think this is anything to do with the I2C code though, especially as the temperature display itself is quite stable.
 
Okay,  Thanks for your confirmation.  The pull-up resistor R16 and R17 are 10K.  If cable is longer, you may change that to 5K, if the communication ever had problem.  It will be interesting to see how long the cable will still run fine.
 
I2C EEPROM can easily store some configuration parameters on external EEPROM chip, like 24AA128. 
I2C LCD display mostly uses PCF8574T chip, Both those chip has four bit product ID and 3 bit device ID.
For example, PCF8574T product ID is 7, and device ID can be 2-0xE, even number.
 
I2C address has lowest bit being used for R/W purpose. When WebControl write to that device, bit 0 in the address byte is 0.  When read from the device, the address byte bit zero is 1.
 
There are a lot of I2C I/O expander module sold on eBay as LCD I2C controller, like item #161290241324
It is based on PCF8574T chip.  That can add more IO pins to WC8 through PLC program.  Its read and write commands are very simple to use.
 
WC8 firmware provided all the critical timing, so that user only need to hook up correct wires and access chip through PLC logic to read or write.
 
Back
Top