PDA

View Full Version : reading si7021 humy_temp sensor



iw2fvo
- 10th May 2016, 10:48
hi to all in this forum,
Is there somebody that used the si7021 sensor with pbp please ?
Any sample program available ?
thanks
Ambrogio

alec
- 11th May 2016, 01:31
This is a program I wrote a while back to read a barometer and a humidity/temperature sensor

Good luck



'************************************************* ***************
'* Name : 18F45K223_HDU21D_test.pbp *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2015 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 7/21/2015 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'********************DEVICE CONFIGURATION**********************************
#CONFIG
CONFIG FOSC = INTIO67 ; Internal oscillator block
CONFIG PLLCFG = OFF ; Oscillator used directly
CONFIG PRICLKEN = OFF ; Primary clock can be disabled by software
CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor disabled
CONFIG IESO = OFF ; Oscillator Switchover mode disabled
CONFIG PWRTEN = OFF ; Power up timer disabled
CONFIG BOREN = SBORDIS ; Brown-out Reset enabled in hardware only (SBOREN is disabled)
CONFIG BORV = 190 ; VBOR set to 1.90 V nominal
CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect
CONFIG WDTPS = 32768 ; 1:32768
CONFIG CCP2MX = PORTC1 ; CCP2 input/output is multiplexed with RC1
CONFIG PBADEN = OFF ; PORTB<5:0> pins are configured as digital I/O on Reset
CONFIG CCP3MX = PORTB5 ; P3A/CCP3 input/output is multiplexed with RB5
CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
CONFIG T3CMX = PORTC0 ; T3CKI is on RC0
CONFIG P2BMX = PORTD2 ; P2B is on RD2
CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
CONFIG STVREN = ON ; Stack full/underflow will cause Reset
#ENDCONFIG
'*************************DEFINEs***************** **************************
DEFINE OSC 16 ' Tell PBP the expected systemclock frequency
' LCD settings
' Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 4
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 5
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 1500
' Set data delay time in us
DEFINE LCD_DATAUS 44
define ADC_BITS 10
'define ADC_SAMPLEUS 50
define I2C_HOLD 1 'allow slave to hold bus low
'************************ALIASES****************** *****************************
'click boards - addresses from easypic7 board
'click slot 1
'click slot 2
CS var portA.5
SCL var portC.3
SDA var portC.4
'************************Constants**************** ******************************
'LCD commands
command con $FE
clr con $01
home con $02
line1 con $80
line2 con $C0
trig_temp con $E3
trig_RH con $E5
HTU21D_i2c_address con $40
MPL3115A2_i2c_address con $60
control con %10100000
'************************VARIABLES**************** ******************************
adval1 Var word
adval1_dig var byte
adval1_dec var word
adval2 var word
adval2_dig var byte
adval2_dec var word
period var word
dtycycl var byte
X var byte 'variable for the counter
memory var word
Temp var word
Temp1 var long
RH var word
RH1 var long
deg_F var word
HTU21D_i2c_address1 var byte
MPL3115A2_i2c_address1 var byte

P_status var byte
out_P_MSB var byte
out_P_CSB var byte
out_P_LSB var byte
out_T var word
Pressure var long
'********************INITIALIZE REGISTERS***************************************
init:
TrisA = %00000011
TrisB = %00000000
TrisC = %00010000
TrisE = %00000001
OSCCON = %01110010
OSCCON2 = %00000100
ANSELA = %00000011 'RA0, 1 analog, others digital
ANSELB = %00000000 'all others digital
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000
ADCON1 = %00001000 'AD Vre3f+ connected to FVR BUF2
ADCON2 = %10010111 'right justified, 4Tad aquisition time, Frc clock derived from internal oscilator
VREFCON0 = %10100000 'internal voltage refernce enabled, set to 2.048 Vdc
CCP2CON = %00001100 'single outputP2A modulated

' PROGRAM CODE
pause 1500 'allow LCD to wake up
lcdout command, clr
pause 20
lcdout command, line1, "hello world"
pause 5000
lcdout command, clr
pause 20
HTU21D_i2c_address1 = HTU21D_i2c_address <<1
MPL3115A2_i2c_address1 = MPL3115A2_i2c_address<<1
Main:
i2cwrite SDA, SCL, HTU21D_i2c_address1, [trig_temp]
pause 10
i2cread SDA, SCL, HTU21D_i2c_address1, [temp]
pause 10
i2cwrite SDA, SCL, HTU21D_i2c_address1, [trig_RH]
pause 10
i2cread SDA, SCL, HTU21D_i2c_address1, [RH]
temp.1 = 0
Temp.0 = 0
RH.1 = 0
RH.0 = 0
RH1 = rh ** 12500
rh1 = RH1 - 600
temp1 = temp ** 17572
temp1 = temp1 - 4685
deg_F = ((Temp1 *9)/5) + 3200
lcdout command, line1, dec2 deg_F/100, ".", dec1 deg_F//100, " ", 223, "F ", dec2 RH1/100, ".", dec1 RH1//100, " %RH"
pause 10
i2cwrite SDA, SCL, MPL3115A2_i2c_address1, $26, [%00000011]
pause 20
i2cread sda, scl, MPL3115A2_i2c_address1, $00, [P_status, out_P_MSB, out_P_CSB, out_P_LSB, out_T]
pressure = 0
pressure.byte2 = out_P_MSB
pressure.byte1 = out_P_CSB
pressure.byte0 = out_P_LSB
pressure = pressure>>6
pressure = Pressure * 10000
pressure = pressure / 338639
'pressure = pressure/33864
'out_T = out_T>>3
lcdout command, line2, dec2 pressure/100, ".",dec2 pressure//100, " in Hg"
'lcdout command, line2, dec6 out_T, " ", 223, "C"
pause 2000
goto main

end

iw2fvo
- 12th May 2016, 12:48
thanks Alec,
Ambrogio