Digital Temperature/Humidity/Barometric pressure

 

History

 

    This project is one of many others I had made to control my house with rules, web interface, logger and many more. This project could take several measurements: Temperature/Humidity/Barometric pressure measurements.  All measurements are taken digitally and have their own dedicated A/D converter in each sensor.  It's a REAL digital environment sensor WITHOUT any calibration.

 

    The humidity and temperature are sense by a very good component made by Sensirion SHT75 and the impressive barometer is done my Intersema MS5534. Those two components talk their own language which is similar to other Serial synchronous protocol (or SPI).

 

Features

puce Digital reading of temperature
puce Digital reading of humidity
puce Digital reading of barometric pressure
puce NO CALIBRATION
puce RS485 interface
puce Network firmware upgradeable

 

Pictures

Click to enlarge

The famous SHT75 who give you directly digital output of the humidity and temperature.

His friend the barometric sensor MS5534

Mounting all together

Sources codes & Schematics

-Schematic Temp/Hum/Baro sensor in PDF format

//*****************************************************************************
// NetBarometer
// Version 10.0 Nov 2005
//
// 10.0 -> New code for 9bit protocol
//
// Sylvain Bissonnette
//*****************************************************************************
//
//                        R E T U R N   S T A C K   6 4
//                               X T A L  16 MHZ
//                          BootLoader of 512 word
//
//*****************************************************************************
//
//                               F U S E   B I T
//
//( )7      ( )6     (X)BL12 (X)BL11  ( )BL02   ( )BL01    ( )Lock2   ( )Lock1
//( )RSTDIS ( )WDON  (X)SPIEN(X)CKOPT (X)EESAVE (X)BOOTSZ1 ( )BOOTSZ0 (X)BOOTRST
//(X)BODLEV (X)BODEN ( )SUT1 ( )SUT0  ( )CKSEL3 ( )CKSEL2  ( )CKSEL1  ( )CKSEL0
//
//*****************************************************************************
//                              P I N   U S A G E
//
// PB0 -> n/c
// PB1 -> MCLK
// PB2 -> n/c
// PB3 -> n/c
// PB4 -> HUMIDITY_DATA
// PB5 -> HUMIDITY_SCK
// PB6 -> XTAL
// PB7 -> XTAL
//
// PC0 -> DIP SW0
// PC1 -> DIP SW1
// PC2 -> DIP SW2
// PC3 -> DIP SW3
// PC4 -> DIP SW4
// PC5 -> DIP SW5
// PC6 -> RESET
//
// PD0 -> RS485 RX
// PD1 -> RS485 TX
// PD2 -> RS485 TXE
// PD3 -> LED 
// PD4 -> n/c
// PD5 -> BAROMETER_DIN
// PD6 -> BAROMETER_DOUT
// PD7 -> BAROMETER_SCLK
//
// ADC6-> n/c
// ADC7-> n/c
//
//*****************************************************************************
//                      T I M E R   U S A G E
//
// Timer 0 is use by taskmanager
// Timer 1 is use by Barometer for 32khz
//
// NetWork Function
//-----------------------------------------------------------------------------
// Command
// 0xff           -> Get 16 bit software version
// 0xf5,0x55,0xaa -> Reset MCU
// 0xf0,0x55,L,V  -> Write eeprom at L value V
// 0xf1,0x55,L   -> Read eeprom at L
// 0x20               -> Get 16 bit Temperature * 10
// 0x21               -> Get 16 bit Humidity * 10
// 0x22               -> Get 16 bit Pressure * 10
//-----------------------------------------------------------------------------
// EEprom
// 00    -> Not for use
// 01@xx -> Free
//*****************************************************************************
// return 44330.7692 * (1 - pow(pressure / 101.325, 0.190255));   // Meters
// return 174530.2383 * (1 - pow(pressure / 101.325, 0.190255));  // Feet
//
//*****************************************************************************
//                         I N C L U D E
//*****************************************************************************
#include <iom8v.h>
#include <macros.h>
#include <stdlib.h>
#include <eeprom.h>
#include "TaskManager.h"
#include <shortnametype.h>

//*****************************************************************************
//                         D E F I N E
//*****************************************************************************
#define VERSION                   101
#define DEVICE                  80
#define TRUE                     1
#define FALSE                        0
#define XTAL                         16000000
#define ACK                        1
#define noACK                      0

#define BAROMETER_DDR               DDRD
#define BAROMETER_PIN               PIND
#define BAROMETER_PORT            PORTD
#define BAROMETER_DOUT            0x40
#define BAROMETER_DIN               0x20
#define BAROMETER_SCLK            0x80
#define BAROMETER_CLK               0x02
#define BAROMETER32K_DDR        DDRB
#define BAROMETER32K_PIN        PINB
#define BAROMETER32K_PORT       PORTB
#define BAROMETER32K_CLK        0x02

#define HUMIDITY_DDR             DDRB
#define HUMIDITY_PIN             PINB
#define HUMIDITY_PORT               PORTB
#define HUMIDITY_DATA               0x10
#define HUMIDITY_SCK             0x20
                                        //adr    command r/w
#define HUMIDITY_STATUS_REG_W    0x06    //000     0011    0
#define HUMIDITY_STATUS_REG_R    0x07    //000     0011    1
#define HUMIDITY_MEASURE_TEMP    0x03    //000     0001    1
#define HUMIDITY_MEASURE_HUMI    0x05    //000     0010    1
#define HUMIDITY_RESET          0x1e    //000      1111    0

#define LED_DDR                       DDRD
#define LED_PIN                       PIND
#define LED_PORT                   PORTD
#define LED                          0x08

#define NET_ADDRESS_DDR         DDRC
#define NET_ADDRESS_PIN         PINC
#define NET_ADDRESS_PORT        PORTC

#define NET_DDR                       DDRD
#define NET_PIN                       PIND
#define NET_PORT                   PORTD
#define NET_RX                     0x01
#define NET_TX                     0x02
#define NET_TXE                       0x04

#define NET_UBRRH                   UBRRH
#define NET_UBRRL                  UBRRL    
#define NET_UCSRA                   UCSRA
#define NET_UCSRB                   UCSRB
#define NET_UCSRC                   UCSRC
#define NET_UDR                       UDR

#define NET_RXBUFFER             10
#define NET_TXBUFFER             10

#define NET_GET_ADDRESS           (NET_ADDRESS_PIN & 0x3f) + 64;

#define NET_SPEED                  19200

#define BROADCASTDEVTYPE          0xf1
#define BROADCAST                0xff

//*****************************************************************************
//                      P R O T O T Y P E
//*****************************************************************************
void main(void);
void _StackOverflowed(char c);

void LEDInit(void);
void LEDFlash(void);

void HumidityInit(void);
void HumidityDelay(void);
void HumidityWriteByte(ushort value);
ushort HumidityReadByte(ushort ack);
void HumidityTransStart(void);
void HumidityConnectionReset(void);
void HumiditySoftReset(void);
ushort HumidityReadStatus(void);
void HumidityWriteStatus(ushort value);
uint HumidityMeasure(ushort mode);
void HumidityCalc(float *p_humidity ,float *p_temperature);
void HumidityGet(float *p_humidity ,float *p_temperature);
void HumidityUpdate(void);

void BarometerInit(void);
void BarometerUpdate(void);
void BarometerCalcPT5534( float *pressure,
                                float *temperature,
                                uint d1_arg,
                                uint d2_arg);
uint BarometerConvertWtoC5534(ushort ix,
                                               uint W1,
                                               uint W2,
                                               uint W3,
                                               uint W4);
uint BarometerGetD1(void);
uint BarometerGetD2(void);
uint BarometerGetW(ushort index);
void BarometerReset(void);
uint BarometerGet16(void);
void BarometerSendLsbFirst(char pattern, char nbr_clock);
ushort BarometerWaitOnDoutFall(void);
void BarometerSetSCLK(ushort state);
ushort BarometerGetSCLK(void);
void BarometerSetDIN(ushort state);
ushort BarometerGetDIN(void);
ushort BarometerGetDOUT(void);
void   BarometerWaitOnePulse(void);

void NetInit(void);
void NetRxChar(void);
void NetAnalyseData(void);
void NetWrite(int Value);
void NetTxByte(void);
void NetTxFinish(void);
void NetGetAddress(void);

//*****************************************************************************
//                G L O B A L   V A R I A B L E
//*****************************************************************************
ushort NetAddress;
ushort NetRxData[NET_RXBUFFER];
ushort NetTxData[NET_TXBUFFER];
ushort *NetTxPtr;
short NetTxQte;
short NetBroadCast = FALSE;

ushort NetNextEventTime;
void (*NetNextFunction)(void);

ushort BaroError = FALSE;
ushort HumiError = FALSE;
uint fc[6];

uint Temp,Hum,Pres;

float Pressure,BaroTemp,Temperature,Humidity;

//*****************************************************************************
//                         M A I N
//*****************************************************************************
void main()
{
  WDR();
  WDTCR = 0x0f;         // Enable WatchDog at 2.2 sec

  TaskInit();
  NetInit();
  BarometerInit();
  HumidityInit();
  LEDInit();
  SEI();

  TaskRegister(LEDFlash,T500MS,TRUE);

  while (1)
  {
    WDR();
    _StackCheck();
    NetGetAddress();
    BarometerUpdate();
    HumidityUpdate();
  }
}

/******************************************************************************

Name:           void _StackOverflowed(char c)

Description:   This function is automaticaly called if
              the stack crash, PD7 will be set.


Input:           none

Output:          PD7

Misc:
******************************************************************************/
void _StackOverflowed(char c)
{
  CLI();
  while(1);
}

/******************************************************************************

Name:           void LEDInit(void)

Description:   Init device for LED

Input:           none

Output:          none

Misc:

******************************************************************************/
void LEDInit(void)
{
  LED_DDR |= LED;       // Pin as output
}

/******************************************************************************

Name:           void LEDFlash(void)

Description:   This function make the LED Flashing

Input:           none

Output:          none

Misc:
******************************************************************************/
void LEDFlash(void)
{
  CLI();
  LED_PORT ^= LED;
  SEI();
}

//*****************************************************************************
//     H U M I D I T Y  & T E M P    F U N C T I O N
//*****************************************************************************
/******************************************************************************

Name:           void HumidityDelay(void)

Description:   Delay for SCLK

Input:           none

Output:          none


Misc:
******************************************************************************/
void HumidityDelay(void)
{
  int i;

  for (i=0;i<(XTAL/400000);i++) WDR();
}

/******************************************************************************

Name:           void HumidityInit(void)

Description:   Initialise port and constant used by
              the Humidity & Temp hardware


Input:           none

Output:          none

Misc:
******************************************************************************/
void HumidityInit(void)
{
  HUMIDITY_DDR |= HUMIDITY_DATA + HUMIDITY_SCK;
  HUMIDITY_PORT |= HUMIDITY_DATA + HUMIDITY_SCK;
  HumiditySoftReset();
}

/******************************************************************************

Name:           ushort HumidityWriteByte(ushort value)

Description:   writes a byte on the Sensibus and
              checks the acknowledge

Input:           ushort value -> byte to write

Output:          none


Misc:           If the device don't ACK set HumiError to TRUE;
******************************************************************************/
void HumidityWriteByte(ushort value)
{
  ushort i;

  HUMIDITY_DDR  |= HUMIDITY_DATA;       //DATA-line in output

  for (i=0x80;i>0;i/=2)                 //shift bit for masking
  {
    HumidityDelay();
    if (i & value) HUMIDITY_PORT |= HUMIDITY_DATA;
    else HUMIDITY_PORT &= ~HUMIDITY_DATA;
    HumidityDelay();
    HUMIDITY_PORT |= HUMIDITY_SCK;
    HumidityDelay();
    HUMIDITY_PORT &= ~HUMIDITY_SCK;
  }

  HUMIDITY_PORT |= HUMIDITY_DATA;       //release DATA-line
  HUMIDITY_DDR  &= ~HUMIDITY_DATA;      //DATA-line in input
  HumidityDelay();
  HUMIDITY_PORT |= HUMIDITY_SCK;       //clk #9 for ack
  HumidityDelay();
  if (HUMIDITY_PIN & HUMIDITY_DATA) HumiError = TRUE;
  HUMIDITY_PORT &= ~HUMIDITY_SCK;
  HumidityDelay();
}

/******************************************************************************

Name:           ushort HumidityReadByte(ushort ack)

Description:   reads a byte form the Sensibus and gives
              an acknowledge in case of "ack=1"

Input:           ushort ack -> ack status

Output:          ushort value readed

Misc:
******************************************************************************/
ushort HumidityReadByte(ushort ack)
{
  ushort i,val=0;

  HUMIDITY_PORT |= HUMIDITY_DATA;       //release DATA-line
  HUMIDITY_DDR  &= ~HUMIDITY_DATA;      //DATA-line in input

  for (i=0x80;i>0;i/=2)                 //shift bit for masking
  {
    HumidityDelay();
    HUMIDITY_PORT |= HUMIDITY_SCK;      //clk for SENSI-BUS
    HumidityDelay();
    if (HUMIDITY_PIN & HUMIDITY_DATA) val=(val | i);  //read bit
    HUMIDITY_PORT &= ~HUMIDITY_SCK;
  }

  if (ack == 1)
  {
    HUMIDITY_DDR  |= HUMIDITY_DATA;     //DATA-line in output
    HUMIDITY_PORT &= ~HUMIDITY_DATA;    //"ack==1" pull down DATA-Line
    HumidityDelay();
    HUMIDITY_PORT |= HUMIDITY_SCK;
    HumidityDelay();
    HUMIDITY_PORT &= ~HUMIDITY_SCK;
  }
  else
  {
    HumidityDelay();
    HUMIDITY_PORT |= HUMIDITY_SCK;
    HumidityDelay();
    HUMIDITY_PORT &= ~HUMIDITY_SCK;
  }
  return val;
}

/******************************************************************************

Name:           ushort HumidityTransStart(void)

Description:   Generates a transmission start

Input:           none

Output:          none

Misc:
              _____         ________
DATA:              |_______|
                  ___     ___
SCK :         ___|   |___|   |______
******************************************************************************/
void HumidityTransStart(void)
{
  HUMIDITY_DDR  |= HUMIDITY_DATA;       //DATA-line in output

  HUMIDITY_PORT |= HUMIDITY_DATA;       //DATA=1
  HUMIDITY_PORT &= ~HUMIDITY_SCK;       //SCK=0
  HumidityDelay();
  HUMIDITY_PORT |= HUMIDITY_SCK;        //SCK=1
  HumidityDelay();
  HUMIDITY_PORT &= ~HUMIDITY_DATA;      //DATA=0
  HumidityDelay();
  HUMIDITY_PORT &= ~HUMIDITY_SCK;       //SCK=0
  HumidityDelay();
  HUMIDITY_PORT |= HUMIDITY_SCK;           //SCK=1
  HumidityDelay();
  HUMIDITY_PORT |= HUMIDITY_DATA;          //DATA=1
  HumidityDelay();
  HUMIDITY_PORT &= ~HUMIDITY_SCK;          //SCK=0
  HumidityDelay();
}

/******************************************************************************

Name:           void HumidityConnectionReset(void)

Description:   communication Reset: DATA-line=1 and at
              least 9 SCK cycles followed by transstart

Input:           none

Output:          none

Misc:
              _____________________________________________________         ________
DATA:                                                              |_______|
                 _    _    _    _    _    _    _    _    _        ___     ___
SCK :         __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
******************************************************************************/
void HumidityConnectionReset(void)
{
  ushort i;

  HUMIDITY_DDR  |= HUMIDITY_DATA;           //DATA-line in output
  HUMIDITY_PORT |= HUMIDITY_DATA;           //DATA=1
  HUMIDITY_PORT &= ~HUMIDITY_SCK;           //SCK=0

  for (i=0;i<9;i++)                         //9 SCK cycles
  {
    HumidityDelay();
    HUMIDITY_PORT |= HUMIDITY_SCK;           //SCK=1
    HumidityDelay();
    HUMIDITY_PORT &= ~HUMIDITY_SCK;          //SCK=0
  }
  HumidityTransStart();                     //transmission start
  HumiError = FALSE;
}

/******************************************************************************

Name:           void HumiditySoftReset(void)

Description:   Resets the sensor by a softReset

Input:           none

Output:          none

Misc:

******************************************************************************/
void HumiditySoftReset(void)
{
  int i;
  HumidityConnectionReset();           //Reset communication
  HumidityWriteByte(HUMIDITY_RESET);   //send Reset-command to sensor
  for (i=0;i<100;i++) HumidityDelay(); // 11ms delay
}

/******************************************************************************

Name:           ushort HumidityReadStatus(void)

Description:   reads the status register with checksum (8-bit)

Input:           none

Output:          ushort status byte

Misc:

******************************************************************************/
ushort HumidityReadStatus(void)
{
  ushort value;

  HumidityTransStart();                     //transmission start
  HumidityWriteByte(HUMIDITY_STATUS_REG_R); //send command to sensor
  value = HumidityReadByte(ACK);            //read status register (8-bit)
  HumidityReadByte(noACK);                //dummy read checksum (8-bit)
  return value;
}

/******************************************************************************

Name:           void HumidityWriteStatus(ushort value)

Description:   writes the status register with checksum (8-bit)

Input:           ushort value to write in the status register

Output:          none

Misc:

******************************************************************************/
void HumidityWriteStatus(ushort value)
{
  HumidityTransStart();                       //transmission start
  HumidityWriteByte(HUMIDITY_STATUS_REG_W);   //send command to sensor
  HumidityWriteByte(value);                     //send value of status register
}

/******************************************************************************

Name:           uint HumidityMeasure(ushort mode)

Description:   makes a measurement (humidity/temperature)

Input:           mode -> HUMI or TEMP

Output:          uint Value mesured

Misc:

******************************************************************************/
uint HumidityMeasure(ushort mode)
{
  ushort msb,lsb;
  uint i,j;

  HumidityTransStart();                     //transmission start
  switch (mode)                                     //send command to sensor
  {
    case HUMIDITY_MEASURE_TEMP : HumidityWriteByte(HUMIDITY_MEASURE_TEMP);
    break;
    case HUMIDITY_MEASURE_HUMI : HumidityWriteByte(HUMIDITY_MEASURE_HUMI);
    break;
    default     : break;
  }

  HUMIDITY_PORT |= HUMIDITY_DATA;           //release DATA-line
  HUMIDITY_DDR  &= ~HUMIDITY_DATA;          //DATA-line in input

  for (i=0;i<=65530;i++)
  {
    for (j=0;j<10;j++) WDR();
    if((HUMIDITY_PIN & HUMIDITY_DATA) == 0) break; //wait until sensor
  }                                                    //has finished the measure
  if(i > 65520)
  {
    HumiError=1;                            // or timeout (~2 sec.) is reached
    return 0xffff;
  }

  msb  =HumidityReadByte(ACK);              //read the first byte (MSB)
  lsb=HumidityReadByte(ACK);                //read the second byte (LSB)
  HumidityReadByte(noACK);             //dummy read checksum
  return (msb<<8)+lsb;
}

/******************************************************************************

Name:           void HumidityCalc(float *p_humidity ,float *p_temperature)

Description:   calculates temperature [°C] and humidity [%RH]

Input:           humi [Ticks] (12 bit)
              temp [Ticks] (14 bit)

Output:          humi [%RH]
              temp [°C]

Misc:

******************************************************************************/
void HumidityCalc(float *p_humidity ,float *p_temperature)
{
  float C1=-4.0;                      // for 12 Bit
  float C2=+0.0405;                   // for 12 Bit
  float C3=-0.0000028;                // for 12 Bit
  float T1=+0.01;                     // for 14 Bit @ 3V
  float T2=+0.00008;                  // for 14 Bit @ 3V

  float rh=*p_humidity;               // rh:      Humidity [Ticks] 12 Bit
  float t=*p_temperature;             // t:       Temperature [Ticks] 14 Bit
  float rh_lin;                       // rh_lin:  Humidity linear
  float rh_true;                      // rh_true: Temperature compensated humidity
  float t_C;                          // t_C   :  Temperature [°C]

  t_C=t*0.01 - 39.6;                  //calc. temperature from ticks to [°C]
  rh_lin=C3*rh*rh + C2*rh + C1;       //calc. humidity from ticks to [%RH]
  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temp compensated humidity [%RH]
  if(rh_true>100)rh_true=100;         //cut if the value is outside of
  if(rh_true<0.1)rh_true=0.1;         //the physical possible range

  *p_temperature=t_C;                 //return temperature [°C]
  *p_humidity=rh_true;                //return humidity[%RH]
}

/******************************************************************************

Name:           void HumidityGet(float *p_humidity ,float *p_temperature)

Description:   measure humidity [ticks](12 bit) and temperature [ticks](14 bit)

Input:           humi [%RH]
              temp [°C]

Output:          humi [%RH]
              temp [°C]

Misc:

******************************************************************************/
void HumidityGet(float *p_humidity ,float *p_temperature)
{
  uint humi_val,temp_val;

  humi_val = HumidityMeasure(HUMIDITY_MEASURE_HUMI);  //measure humidity
  temp_val = HumidityMeasure(HUMIDITY_MEASURE_TEMP);  //measure temperature

  if(HumiError == TRUE) HumidityConnectionReset();    //in case of an error: Reset
  else
  {
    *p_humidity=(float)humi_val;                      //converts integer to float
    *p_temperature=(float)temp_val;                   //converts integer to float
    HumidityCalc(p_humidity,p_temperature);           //calculate humidity, temperature
  }
}

/******************************************************************************

Name:       void HumidityUpdate(void)

Description:

Input:

Output:
Misc:

******************************************************************************/
void HumidityUpdate(void)
{
  if (HumiError == TRUE) HumidityInit();
  HumidityGet(&Humidity,&Temperature);
  Hum = Humidity * 10;
  Temp = Temperature * 10;
}

//*****************************************************************************
//          B A R O M E T E R   F U N C T I O N
//*****************************************************************************
/******************************************************************************

Name:           void BarometerInit(void)

Description:   Initialise port and constant used by
              the barometer hardware


Input:           none

Output:          none

Misc:
******************************************************************************/
void BarometerInit(void)
{
  uint w[4];
  ushort i;

  // Timer 1
  TCCR1A = 0x40;
  TCCR1B = 0x09;         // Timer 1 prescaler at /1
  OCR1A  = 0xfa;         // for 32khz at 16Mhz

  BAROMETER_DDR |= BAROMETER_DIN + BAROMETER_SCLK;
  BAROMETER_DDR &= ~BAROMETER_DOUT;

  BAROMETER32K_DDR |= BAROMETER32K_CLK;

  BaroError = FALSE;

  BarometerReset();

  for (i=0; i<4; i++)
  {
    w[i] = BarometerGetW(i);
  }

  for (i=0; i<6; i++)
  {
    fc[i] = BarometerConvertWtoC5534(i, w[0], w[1], w[2], w[3]);
  }
}

/******************************************************************************

Name:           void BarometerUpdate(void)

Description:  Update the variable (float)Pres with the current
              humidity


Input:           none

Output:          Global variable Pres

Misc:
******************************************************************************/
void BarometerUpdate(void)
{
  uint d1,d2;

  if (BaroError == TRUE) BarometerInit();
  d1 = BarometerGetD1();
  d2 = BarometerGetD2();
  BarometerCalcPT5534(&Pressure,&BaroTemp,d1, d2);
  Pressure = (Pressure - 1013.25) * 10;
  Pres = (int)Pressure;
}

/******************************************************************************

Name:           void BarometerBarometerCalcPT5534(float *pressure,
                                                float *temperature,
                                                uint d1_arg,
                                                uint d2_arg)


Description:   Calculate the pressure & temparature with
              the given d1_arg and d2_ard


Input:           float *pressure -> Pointer to pressure value to store
              float *temperature -> Pointer to temp value to store
              uint d1_arg -> value of d1
              uint d2_arg -> value of d2

Output:        none

Misc:
******************************************************************************/
void BarometerCalcPT5534( float *pressure,
                          float *temperature,
                          uint d1_arg,
                          uint d2_arg)
{
  float fd1, fd2, x,dt, off, sens;

  fd1 = (float) d1_arg;
  fd2 = (float) d2_arg;

  dt   =   fd2 - ((8.0 * fc[4]) + 20224.0);
  off  =   fc[1] * 4.0 + (((fc[3]-512.0)*dt)/4096.0);
  sens =   24576.0 +  fc[0] + ((fc[2]*dt)/1024.0);
  x    =   (( sens * (fd1- 7168.0)) / 16384.0) - off;
  *pressure = 250.0 + x / 32;
  *temperature =  (200 +((dt*(fc[5]+50.0))/1024.0))/10;
}

/******************************************************************************

Name:           uint BarometerConvertWtoC5534(ushort ix,
                                                    uint W1,
                                                    uint W2,
                                                    uint W3,
                                                    uint W4)

Description:   Convert W value to Constant used by BarometerCalcPT5534
              to give a result


Input:           ushort ix -> Constant to find (0 to 5)
              uint W1  -> Value #1 from 16bit rom
              uint W2  -> Value #2 from 16bit rom
              uint W3  -> Value #3 from 16bit rom
              uint W4  -> Value #4 from 16bit rom

Output:          none

Misc:
******************************************************************************/
uint BarometerConvertWtoC5534(ushort ix,
                                      uint W1,
                                      uint W2,
                                      uint W3,
                                      uint W4)
{
  uint c;
  uint x, y;

  c = 0;
  switch (ix)
  {
    case 0:
    c =  (W1 >> 1) & 0x7FFF;
    break;
    case 1:
    x = (W3 << 6) & 0x0FC0;
    y =  W4       & 0x003F;
    c = x | y;
    break;
    case 2:
    c = (W4 >> 6) & 0x03FF;
    break;
    case 3:
    c = (W3 >> 6) & 0x03FF;
    break;
    case 4:
    x = (W1 << 10)& 0x0400;
    y = (W2 >> 6 )& 0x03FF;
    c = x | y;
    break;
    case 5:
    c =  W2       & 0x003F;
    break;
  }
  return(c);
}

/******************************************************************************

Name:           uint BarometerGetD1(void)

Description:   Start a pressure "D1" convertion and return
              the result


Input:           none

Output:          int -> Pressure value (RAW data)

Misc:
******************************************************************************/
uint BarometerGetD1(void)
{
  uint d1;

  BarometerSendLsbFirst(0x2F, 8);
  BarometerSendLsbFirst(0x00, 2);

  if (BarometerGetDOUT()==FALSE) BaroError = 1;   // line should be at 1 now
  BarometerSendLsbFirst(0x00, 2);

  if (!BaroError) BaroError = BarometerWaitOnDoutFall();
  if (!BaroError) d1 = BarometerGet16();
  else d1 = 0;
  return(d1);
}

/******************************************************************************

Name:           uint BarometerGetD2(void)

Description:   Start a temperature "D2" convertion and return
the result


Input:           none

Output:          int -> Temp value (RAW data)

Misc:
******************************************************************************/
uint BarometerGetD2(void)
{
  uint d2;

  BarometerSendLsbFirst(0x4F, 8);
  BarometerSendLsbFirst(0x00, 3);                 // Note the difference
                                                  // with BarometerGetD1

  if (BarometerGetDOUT()==FALSE) BaroError = 1;   // line should be at 1 now
  BarometerSendLsbFirst(0x00, 1);

  if (!BaroError) BaroError = BarometerWaitOnDoutFall();
  if (!BaroError) d2 = BarometerGet16();
  else d2 = 0;
  return(d2);
}

/******************************************************************************

Name:           uint BarometerGetW(ushort index)

Description:   Get the Rom constant

Input:           uchar -> rom x to get (0 to 3)

Output:          int -> Rom value

Misc:
******************************************************************************/
uint BarometerGetW(ushort index)
{
  uint data;

  data = 0;
  switch (index)
  {
    case 0:
    BarometerSendLsbFirst((char) 0x57, (char) 8);
    BarometerSendLsbFirst((char) 0x01, (char) 5);
    data = BarometerGet16();
    break;

    case 1:
    BarometerSendLsbFirst((char) 0xD7, (char) 8);
    BarometerSendLsbFirst((char) 0x00, (char) 5);
    data = BarometerGet16();
    break;

    case 2:
    BarometerSendLsbFirst((char) 0x37, (char) 8);
    BarometerSendLsbFirst((char) 0x01, (char) 5);
    data = BarometerGet16();
    break;

    case 3:
    BarometerSendLsbFirst((char) 0xB7, (char) 8);
    BarometerSendLsbFirst((char) 0x00, (char) 5);
    data = BarometerGet16();
    break;
  }
  BarometerSendLsbFirst(0x00, 1);  // to be compliant with the data sheet
  return(data);
}

/******************************************************************************

Name:           void BarometerReset(void)

Description:   Send a Reset commend to the barometer

Input:           none

Output:          none

Misc:
******************************************************************************/
void BarometerReset(void)
{
  BarometerSendLsbFirst(0x55, 8);
  BarometerSendLsbFirst(0x55, 8);
  BarometerSendLsbFirst(0x00, 5);
}

/******************************************************************************

Name:           uint BarometerGet16(void)

Description:   Get a 16 bit value from the barometer

Input:           none

Output:          uint -> readed value

Misc:
******************************************************************************/
uint BarometerGet16(void)
{
  char i;
  uint v;

  v = 0;
  BarometerSetSCLK(FALSE);
  BarometerWaitOnePulse();

  for (i=0; i<16; i++)
  {
    BarometerSetSCLK(TRUE);
  &n