code for reading a single i2c device

hippy

Technical Support
Staff member
I am using the AXE027 cable but the PC does not show the results even removing the terminal directive.
How can I solve it ?
I would guess by putting the #TERMINAL line back in and taking out any DEBUG commands you have added.
 

Benjie

Senior Member
Hello Hippy,
I played quite a lot with your code which definitely works; thank you.
I reduced the code to essentials because the variometer project intends to detect pressure variations while real pressure values are irrelevant.
I kept all your labels and symbols but I removed all lines concerned with temperature and final mathematics.
Here the code:
Code:
#Picaxe 08M2
#Terminal 4800
#No_Data

Symbol C1 = w1  : Symbol C1.msb = b3  : Symbol C1.lsb = b2
Symbol C2 = w2  : Symbol C2.msb = b5  : Symbol C2.lsb = b4
Symbol C3 = w3  : Symbol C3.msb = b7  : Symbol C3.lsb = b6
Symbol C4 = w4  : Symbol C4.msb = b9  : Symbol C4.lsb = b8
Symbol C5 = w5  : Symbol C5.msb = b11 : Symbol C5.lsb = b10
Symbol C6 = w6  : Symbol C6.msb = b13 : Symbol C6.lsb = b12

Symbol Pl = w7  : Symbol Pl.msb = b15 : Symbol Pl.lsb = b14
Symbol Ph = w8  : Symbol Ph.msb = b17 : Symbol Ph.lsb = b16

'Symbol Tl = w9  : Symbol Tl.msb = b19 : Symbol Tl.lsb = b18
'Symbol Th = w10 : Symbol Th.msb = b21 : Symbol Th.lsb = b20

Symbol CMD_RESET   = $1E
Symbol CMD_D1_256  = $40
Symbol CMD_D1_512  = $42
Symbol CMD_D1_1024 = $44
Symbol CMD_D1_2048 = $46
Symbol CMD_D1_4096 = $48
Symbol CMD_D2_256  = $50
Symbol CMD_D2_512  = $52
Symbol CMD_D2_1024 = $54
Symbol CMD_D2_2048 = $56
Symbol CMD_D2_4096 = $58
Symbol CMD_ADC     = $00
Symbol CMD_PROM_0  = $A0
Symbol CMD_PROM_1  = $A2
Symbol CMD_PROM_2  = $A4
Symbol CMD_PROM_3  = $A6
Symbol CMD_PROM_4  = $A8
Symbol CMD_PROM_5  = $AA
Symbol CMD_PROM_6  = $AC
Symbol CMD_PROM_7  = $AE

PowerOnReset:

  HI2cSetup I2CMASTER, %11101110, I2CSLOW, I2CBYTE

  HI2cOut ( CMD_RESET )
  Pause 10

  HI2cOut ( CMD_PROM_1 ) : HI2cIn ( C1.msb, C1.lsb )
  HI2cOut ( CMD_PROM_2 ) : HI2cIn ( C2.msb, C2.lsb )
  HI2cOut ( CMD_PROM_3 ) : HI2cIn ( C3.msb, C3.lsb )
  HI2cOut ( CMD_PROM_4 ) : HI2cIn ( C4.msb, C4.lsb )
  HI2cOut ( CMD_PROM_5 ) : HI2cIn ( C5.msb, C5.lsb )
  HI2cOut ( CMD_PROM_6 ) : HI2cIn ( C6.msb, C6.lsb )
    w10 = 0
MainLoop:

  Do


    HI2cOut ( CMD_D1_4096 )
    Pause 50
    HI2cOut ( CMD_ADC )
    Pause 50
    HI2cIn ( Ph.msb, Ph.lsb, Pl.msb ) : Pl.lsb = 0
    b18=Ph.msb
    b19=Ph.lsb
    b20=Pl.msb 
    sertxd(#b18,"   ",#b19,"   ",#w9,"   ",#b20,lf,cr)

pause 80
  Loop
Some question:
What is the PI.msb? On the terminal I get many differente values not correlated with anything.
The MS5611 data sheet pag 13 shows output data as 3 bytes; I get meaningful msd and lsd which I believe are the 7-0 and 8-15 bits but what about the 23-16 bits? are them the PI.msb?
The b19 (lsb) has small variation with a 9 meters difference in altitude. The project needs a highest resolution to be worth the effort. Should the PI.msb the key to higher resolution?

Thank you for the patience.
 

hippy

Technical Support
Staff member
Code:
HI2cIn ( Ph.msb, Ph.lsb, Pl.msb ) : Pl.lsb = 0
    b18=Ph.msb
    b19=Ph.lsb
    b20=Pl.msb 
    sertxd(#b18,"   ",#b19,"   ",#w9,"   ",#b20,lf,cr)
I think you have confused yourself by including #w9. I would remove that or, more preferable, just use -
Code:
    sertxd(#ph.msb,"   ",#ph.lsb,"   ",#pl.msb,lf,cr)
Those are the three bytes returned as the pressure reading, MSB on the left to LSB on the right.

'Pl.msb' is the LSB of the pressure reading, but aligned in the 'Pl' word as its MSB ...
Code:
.-----------------.
| $12 | $34 | $56 |  Raw Reading
`-----------------'
   |     |     |
   |     |     `------.       $00
   |     `---.        |
   |         |        |        | 
  \|/       \|/      \|/      \|/
.--------.--------.--------.--------.
| Ph.msb | Ph.lsb | Pl.msb | Pl.lsb |
|--------^--------|--------^--------|
|       Ph        |       Pl        |
`-----------------^-----------------'
      $1234             $5600
The other possible option is -
Code:
                .-----------------.
                | $12 | $34 | $56 |  Raw Reading
                `-----------------'
                   |     |     |
   $00       .-----'     |     |
             |        .--'     |
    |        |        |        |
   \|/      \|/      \|/      \|/
.--------.--------.--------.--------.
| Ph.msb | Ph.lsb | Pl.msb | Pl.lsb |
|--------^--------|--------^--------|
|       Ph        |       Pl        |
`-----------------^-----------------'
      $0012             $3456
I suspect the first is more useful but possibly not. It depends on what actual data values you are receiving or are expecting to be receiving.
 
Last edited:

Benjie

Senior Member
Extremely clear and instructive! Will experiment with that.
My handicap is the lack of hex addressing familiarity; I do not have a clue on the various $xxxx.
May be too late to learn further.
Thanks a lot.
 

Benjie

Senior Member
Hippy,
in your original code, reading both temperature and pressure, you specified line 12 "symbol Pl=w7" and line 37 "symbol CMD_7 = $AE".
I understand that you intended to read both bytes of Pl (b15 and b14). However the HI2cOut (CMD_PROM_7) is not listed after line 51.
To sertxt b15 and b14 I assume that the HI2cOut (CMD_PROM_7) should be executed which has to be followed by HI2cIn (Ph.msb, Ph.lsb, Pl.msb, Pl.lsb).
In this case PI.lsb = 0 will be omitted.

Resuming, to read all pressure bytes (24 bit), the following line should be added:
Code:
HI2cOut ( CMD_PROM_7 ) : HI2cIn ( C7.msb, C7.lsb )
after line 51 and line 60 changed to:
Code:
HI2cIn ( Ph.msb, Ph.lsb, PI.msb, PI.lsb)
Is this correct?
 

Benjie

Senior Member
HI Hippy, I'm back.
the introduction of the corrections of my previous post (see the full code used), I get this srtxd output sample:terminal.jpg

The cose is:
Code:
#Picaxe 08M2
#Terminal 4800
#No_Data

Symbol C1 = w1  : Symbol C1.msb = b3  : Symbol C1.lsb = b2
Symbol C2 = w2  : Symbol C2.msb = b5  : Symbol C2.lsb = b4
Symbol C3 = w3  : Symbol C3.msb = b7  : Symbol C3.lsb = b6
Symbol C4 = w4  : Symbol C4.msb = b9  : Symbol C4.lsb = b8
Symbol C5 = w5  : Symbol C5.msb = b11 : Symbol C5.lsb = b10
Symbol C6 = w6  : Symbol C6.msb = b13 : Symbol C6.lsb = b12
Symbol C7 = w9 : Symbol C7.msb = b19 : Symbol C7.lsb = b18

Symbol Pl = w7  : Symbol Pl.msb = b15 : Symbol PI.lsb = b14
Symbol Ph = w8  : Symbol Ph.msb = b17 : Symbol Ph.lsb = b16

'Symbol Tl = w9  : Symbol Tl.msb = b19 : Symbol Tl.lsb = b18
'Symbol Th = w10 : Symbol Th.msb = b21 : Symbol Th.lsb = b20

Symbol CMD_RESET   = $1E
Symbol CMD_D1_256  = $40
Symbol CMD_D1_512  = $42
Symbol CMD_D1_1024 = $44
Symbol CMD_D1_2048 = $46
Symbol CMD_D1_4096 = $48
Symbol CMD_D2_256  = $50
Symbol CMD_D2_512  = $52
Symbol CMD_D2_1024 = $54
Symbol CMD_D2_2048 = $56
Symbol CMD_D2_4096 = $58
Symbol CMD_ADC     = $00
Symbol CMD_PROM_0  = $A0
Symbol CMD_PROM_1  = $A2
Symbol CMD_PROM_2  = $A4
Symbol CMD_PROM_3  = $A6
Symbol CMD_PROM_4  = $A8
Symbol CMD_PROM_5  = $AA
Symbol CMD_PROM_6  = $AC
Symbol CMD_PROM_7  = $AE

PowerOnReset:

  HI2cSetup I2CMASTER, %11101110, I2CSLOW, I2CBYTE

  HI2cOut ( CMD_RESET )
  Pause 10

  HI2cOut ( CMD_PROM_1 ) : HI2cIn ( C1.msb, C1.lsb )
  HI2cOut ( CMD_PROM_2 ) : HI2cIn ( C2.msb, C2.lsb )
  HI2cOut ( CMD_PROM_3 ) : HI2cIn ( C3.msb, C3.lsb )
  HI2cOut ( CMD_PROM_4 ) : HI2cIn ( C4.msb, C4.lsb )
  HI2cOut ( CMD_PROM_5 ) : HI2cIn ( C5.msb, C5.lsb )
  HI2cOut ( CMD_PROM_6 ) : HI2cIn ( C6.msb, C6.lsb )
  HI2cOut ( CMD_PROM_7 ) : HI2cIn ( C7.msb, C7.lsb )
    'w10 = 0
MainLoop:

  Do


    HI2cOut ( CMD_D1_4096 )
    Pause 10
    HI2cOut ( CMD_ADC )
    Pause 10
    HI2cIn ( Ph.msb, Ph.lsb, Pl.msb, PI.lsb)
 
    sertxd(#Ph.msb,"   ",#Ph.lsb,"   ",#Pl.msb,"   ",#PI.lsb,cr,lf)
    Pause 1000
  Loop
PI.msd and PI.lsb do not look very meaningful ! Any idea why? The sensor was not moving.
 

hippy

Technical Support
Staff member
Resuming, to read all pressure bytes (24 bit), the following line should be added:
Code:
HI2cOut ( CMD_PROM_7 ) : HI2cIn ( C7.msb, C7.lsb )
after line 51 and line 60 changed to:
Code:
HI2cIn ( Ph.msb, Ph.lsb, PI.msb, PI.lsb)
Is this correct?
No.

The PROM holds configuration constants, not the actual sensor data. CMD_PROM_7 reads the CRC for the configuration which my code doesn't use so I did not bother reading it.

That is entirely separate from reading the raw pressure data.

The pressure reading is three bytes, not four, which is why I was reading three bytes, into two words, and forcing the fourth byte to $00. Reading four byes is just putting a 'random' 63 into Pl.lsb.

So, ignoring that 63, and looking at your data; '134 236 x' and '134 235 x' seems potentially reasonable to me. Only the least significant bits are changing and that would seem likely if there is even a slight breeze around the sensor.

As to what those numbers mean as an actual pressure I have no idea. One would have to apply the equations using the configuration coefficients, do the maths.

This is the code I would use if also reading all words in the configuration -

Code:
#Picaxe 08M2
#Terminal 4800
#No_Data

Symbol C0 = w1  : Symbol C0.msb = b3  : Symbol C0.lsb = b2
Symbol C1 = w2  : Symbol C1.msb = b5  : Symbol C1.lsb = b4
Symbol C2 = w3  : Symbol C2.msb = b7  : Symbol C2.lsb = b6
Symbol C3 = w4  : Symbol C3.msb = b9  : Symbol C3.lsb = b8
Symbol C4 = w5  : Symbol C4.msb = b11 : Symbol C4.lsb = b10
Symbol C5 = w6  : Symbol C5.msb = b13 : Symbol C5.lsb = b12
Symbol C6 = w7  : Symbol C6.msb = b15 : Symbol C6.lsb = b14
Symbol C7 = w8  : Symbol C7.msb = b17 : Symbol C7.lsb = b16

Symbol Pl = w9  : Symbol Pl.msb = b19 : Symbol Pl.lsb = b18
Symbol Ph = w10 : Symbol Ph.msb = b21 : Symbol Ph.lsb = b20

Symbol Tl = w11 : Symbol Tl.msb = b23 : Symbol Tl.lsb = b22
Symbol Th = w12 : Symbol Th.msb = b25 : Symbol Th.lsb = b24

Symbol CMD_RESET   = $1E
Symbol CMD_D1_256  = $40
Symbol CMD_D1_512  = $42
Symbol CMD_D1_1024 = $44
Symbol CMD_D1_2048 = $46
Symbol CMD_D1_4096 = $48
Symbol CMD_D2_256  = $50
Symbol CMD_D2_512  = $52
Symbol CMD_D2_1024 = $54
Symbol CMD_D2_2048 = $56
Symbol CMD_D2_4096 = $58
Symbol CMD_ADC     = $00
Symbol CMD_PROM_0  = $A0
Symbol CMD_PROM_1  = $A2
Symbol CMD_PROM_2  = $A4
Symbol CMD_PROM_3  = $A6
Symbol CMD_PROM_4  = $A8
Symbol CMD_PROM_5  = $AA
Symbol CMD_PROM_6  = $AC
Symbol CMD_PROM_7  = $AE

PowerOnReset:

  HI2cSetup I2CMASTER, %11101110, I2CSLOW, I2CBYTE

  HI2cOut ( CMD_RESET )
  Pause 10

  HI2cOut ( CMD_PROM_0 ) : HI2cIn ( C0.msb, C0.lsb )
  HI2cOut ( CMD_PROM_1 ) : HI2cIn ( C1.msb, C1.lsb )
  HI2cOut ( CMD_PROM_2 ) : HI2cIn ( C2.msb, C2.lsb )
  HI2cOut ( CMD_PROM_3 ) : HI2cIn ( C3.msb, C3.lsb )
  HI2cOut ( CMD_PROM_4 ) : HI2cIn ( C4.msb, C4.lsb )
  HI2cOut ( CMD_PROM_5 ) : HI2cIn ( C5.msb, C5.lsb )
  HI2cOut ( CMD_PROM_6 ) : HI2cIn ( C6.msb, C6.lsb )
  HI2cOut ( CMD_PROM_7 ) : HI2cIn ( C7.msb, C7.lsb )

MainLoop:

  Do

    HI2cOut ( CMD_D1_4096 )
    Pause 10
    HI2cOut ( CMD_ADC )
    Pause 10
    HI2cIn ( Ph.msb, Ph.lsb, Pl.msb ) : Pl.lsb = 0
 
    sertxd(#Ph.msb,"   ",#Ph.lsb,"   ",#Pl.msb,cr,lf)

    Pause 1000
  Loop
Edited: Read all configuration, C0 as well as C7.
Edited: Corrected PI.lsb (P-eye) to be Pl.lsb (P-ell)

That should give similar numbers to what you were previously seeing, except only three sets of number.

If so; reading the pressure data would seem to be working, and putting the 16-bit MSB into Ph was probably the right guess.

The question then is what you want to do with that data, what you want to do with it now you can read it ?
 
Last edited:

Benjie

Senior Member
Thank you Hippy.
The final goal of the project is to remotely sense the pressure variations, not absolute values. The circuit, embarked on a flying model glider, can transmit to ground a tone telling if the model is going up or down.
The next step is to store the pressure reading and compare it with the next reading and use the difference, positive or negative, to generate a beep proportional in frequency to the pressure variation.
The project has been completed with an Arduino Nano but using picaxe it can be smaller and lighter.
I am not familiar with the Arduino code so I'm designing this project from scratch.
Your help is very appreciated and I hope that this work will be of interest of the forum members. That is what happened with my past projects: "Wireless remote control for multiple remote antenna tuners" and "radio-ham autotuner for magloops“ for which I still have, after several years, inquiries.
 

hippy

Technical Support
Staff member
The next step is to store the pressure reading and compare it with the next reading and use the difference, positive or negative, to generate a beep proportional in frequency to the pressure variation.
That should be relatively easy and you can probably just use the Ph word value.

The challenge is going to be in determining what Ph values you can expect, what tone you should generate depending on the difference. That's probably going to require some experimentation.
 

Benjie

Senior Member
In fact I use the Ph word which exhibits the right behavior.
For the tone generation I already have experimented with sound to generate a beep which frequency depends on increase or decrease of Ph. In case the sound is generated, I open the data input of a 433MHz transmitter and inject via a varicap the tone to the resonator so the tone will generate a FM signal instead of a AM. Such signal can be received by any cheap walkie-talkie. I am well advanced on this.
 
Top