Hallo,
Ik ben bezig met een robotje die de temperatuur meet en de vochtigheid.
Dit doe ik met de SHT71 sensor.
Als pic gebruik ik de 18f4431
programmeren die ik via picbasic
Samen moeten ze communiceren via I2c.
Ik heb al op de site van www.oshonsoft.com gekeken maar daar hebben ze alleen maar een voorbeeld om data naar eeprom te schrijven en te lezen.
Het lukt mij nog steeds niet om de temperatuur op mijn lcd te krijgen.
(LCD werkt met tex en andere variabelen)
er komt gewoon altijd 255 als uitgelezen waarde
DEVICE18f431
Dim data As Byte
Dim addr As Word
I2CRead sda, scl, %00000011, addr, data,
Lcdcmdout LcdLine1Home
Lcdout "addr", #addr
Lcdcmdout LcdLine2Home
Lcdout "data", #data
weet er iemand wat ik verkeerd doe?
[Bericht gewijzigd door Henry S. op (1%)]
Ik hoop dat je meer code hebt... en is 'data' niet beschermd?
/move digitaal en denk de volgende keer om een echte titel.
[Bericht gewijzigd door Henry S. op (22%)]
volgens mij werkt de sht71 met een aan i2c verwant protocol maar verschilt het iets in de startsequence oid. Kijk eerst of jouw i2c driver hiervoor aangepast is.
Heb ondertussen nog wat getest.
Ik kreeg op de data en klock lijn een signaal.
Maar kon hier verder niks meedoen.
Heb nog wat opgezocht en het is inderdaad geen I2C systeem.
Ik heb ondertussen ook code gevonden voor de sensor uit te lezen maar het lukt mij niet om het om te vormen naar de compiler van oshonsoft.
De standaard dingen zijn geen probleem.
maar dingen zoals:
shiftout DataPin,clk,1,[SHTCommand\8] ' Send command byte weet ik niet hoe ik dit moet vertalen.
Mijn vraag is nu niet vertaal gans dit programma maar wel hoe vertaal je die shiftout en shiftin?
het ganse programa is :
'------------------------- Port Assignments --------------------------------
' PORTA.0 LCD Data Line - Bit 4
' PORTA.1 LCD Data Line - Bit 5
' PORTA.2 LCD Data Line - Bit 6
' PORTA.3 LCD Data Line - Bit 7
' PORTA.4 LCD Data Line - Register Select
' PORTA.5 Unused
' PORTB.0 SHT71 - Serial Clock Line
' PORTB.1 Timed Occupied Override Switch
' PORTB.2 Serial Data Input/Output
' PORTB.3 LCD Data Line - Enable
' PORTB.4 Unused
' PORTB.5 SHT71 - Data Line
' PORTB.6 Unused
' PORTB.7 Unused
'************************ 16F88 Configuration Fuses ****************************
'@ device PIC16F88, XT_osc, wdt_on, wrt_off, lvp_off, pwrt_on, protect_off
'@ __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB3 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _PWRTE_ON & _WDT_ON & _XT_OSC
' Code Protection - _CP_OFF, CP_ON
' CCP1 Pin Selection - _CCP1_RB0, _CCP1_RB3
' In-Circuit Debugger Mode - _DEBUG_OFF, _DEBUG_ON
' Flash Program Memory Write Enable - _WRT_PROTECT_OFF, _WRT_PROTECT_256, _WRT_PROTECT_2048, _WRT_PROTECT_ALL
' Data EE Memory Code Protection - _CPD_OFF, _CPD_ON
' Low-Voltage Programming Enable - _LVP_OFF, _LVP_ON
' Brown-Out Reset - _BODEN_OFF, _BODEN_ON
' RA5/MCLR/Vpp Pin Function - _MCLR_OFF, _MCLR_ON
' Power-Up Timer - _PWRTE_OFF, _PWRTE_ON
' Watchdog Timer - _WDT_OFF, _WDT_ON
' Oscillator Selection - _EXTRC_CLKOUT, _EXTRC_IO, _INTRC_CLKOUT, _INTRC_IO, _EXTCLK, _HS_OSC, _XT_OSC, _LP_OSC,
'
'@ __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_ON
' Oscillator Switchover - _IESO_OFF, _IESO_ON
' Fail-Safe Clock Monitor - _FCMEN_OFF, _FCMEN_ON
'*******************************************************************************
'--------------------------- Setup DEFINE's ----------------------------------
DEFINE OSC 4
DEFINE ADC_BITS 10 ' Set resolution of conversion
DEFINE ADC_CLOCK 8 ' Set clock source (x/FOSC or FRC)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time (in uS)
'------------------------ Port Initialization -------------------------------
CCP1CON = %00000000 ' Disable CCP Module
SSPCON.5 = 0 ' Disable SSP Module
TXSTA.5 = 0 ' Disable AUSART Tx
RCSTA.7 = 0 ' Disable Serial Port
' ANSEL = %01000000 ' Set analog ports to digital mode except RB7/AN6
CMCON = 7 ' Turn off comparator
INTCON.7 = 0
OPTION_REG.6 = 0
ADCON0 = 0
ADCON1 = 7
TRISA = %100000
TRISB = %11110101
'----------------------------- Debug Settings ----------------------------------
Define LCD_DREG PORTA
Define LCD_DBIT 0
Define LCD_RSREG PORTA
Define LCD_RSBIT 4
Define LCD_EREG PORTB
Define LCD_EBIT 3
cmd con 254 ' Control byte
clr con 1 ' Clear the display
line1 con 128 ' Move cursor to home position on line 1
line2 con 192 ' Move cursor to home position on line 2
line3 con 148 ' Move cursor to home position on line 3
line4 con 212 ' Move cursor to home position on line 4
' ----------------------------- Port Aliases ---------------------------------
Clk var portb.1
DataPin var portb.5
'---------------------- Miscellaneous Variables ------------------------------
i var byte ' Loop counter
w var word ' Variable for calculations
x var word ' Variable for calculations
CRC var byte ' Calculated CRC value
CRCCalc var byte ' Calculated CRC value
SHTCommand var byte ' Sensor Command
RawData var word ' Raw data from sensor
ReadTemp con %00000011 ' Read temperature command
ReadHumidity con %00000101 ' Read humitity command
DataBit var bit ' Bit shift variable
DataByte var byte ' Byte to perform CRC calculation on
dummy var word ' Dummy variable for DIV32 function
SignC var byte ' +/- Sign for celcius temperature
SignF var byte ' +/- Sign for fahrenheit temp.
TempC var word ' Temperature in ºC
TempF var word ' Temperature in ºF
TempK var word ' Temperature in Kelvin
RHLinear var word ' Relative Humidity (RH) Linear
RHTempComp var word ' Relative Humidity (RH) Temp. Comp.
TempCRCError var bit ' Temperature Sensor CRC Error Flag
RHCRCError var bit ' Humidity Sensor CRC Error Flag
clear
lcdout cmd,clr
'------------------ Read SHT71 Temperature / Humidity Sensor -------------------
ReadSHT71:
SHTCommand = ReadTemp
gosub Initialize
gosub readsensor
gosub GetSHTCRC
gosub ConvertToCelcius
gosub ConvertToFahrenheit
gosub ConvertToKelvin
gosub CheckCRC
SHTCommand = ReadHumidity
gosub Initialize
gosub readsensor
gosub GetSHTCRC
gosub ConvertToRH
gosub CheckCRC
gosub Display
goto ReadSHT71
'--------------------- Calculate Relative Humidity -----------------------------
ConvertToRH:
' 12 Bit RHLinear = -4 + 0.0405 * DataOutput -0.0000028 * DataOutput^2
w = 405 * RawData
w = div32 100
x = RawData * RawData
x = div32 1000
x = x * 28
x = div32 100
RHLinear = w - x - 400
'---------- Calculate Relative Humidity with Temperature Compensation ----------
w = (8 * RawData + 1000) / 10
if SignC = 160 then
if tempC > 2500 then
x = (tempC - 2500) * w
x = div32 100
RHTempComp = RHLinear + x / 100
else
x = (2500 - tempc) * w
x = div32 100
RHTempComp = RHLinear - x / 100
endif
else
x = (2500 + tempC) * w
x = div32 10000
RHTempComp = RHLinear - x
endif
sleep 1
return
'---------------------------- Initialize the Sensor ----------------------------
Initialize:
high DataPin
low clk
for i = 1 to 10
high clk
pause 1
low clk
pause 1
next i
gosub TransferStart
return
'---------------------------- Get Data from Sensor -----------------------------
ReadSensor:
gosub TransferStart
gosub WaitSensor
shiftout DataPin,clk,1,[SHTCommand\8] ' Send command byte
input DataPin ' Wait for acknowledge
low clk
while DataPin = 1
wend
pulsout clk,10 ' Send acknowledge
while DataPin = 0
wend
while DataPin = 1 ' Wait for conversion to complete
wend
low clk
shiftin DataPin,clk,0,[RawData.byte1\8] ' Get the first byte, 8 bits
low DataPin
pulsout clk,10 ' Send acknowledge
shiftin DataPin,clk,0,[RawData.byte0\8] ' Get the second byte, 8 bits
low DataPin
pulsout clk,10 ' Send acknowledge
shiftin DataPin,clk,0,[crc\8] ' Get third byte, 8 bits, CRC
high DataPin
pulsout clk,10 ' Send acknowledge
input DataPin ' End of Transmission
input clk
return
'---------------------------- Start Transfer -----------------------------------
TransferStart:
high clk
pause 1
low DataPin
pause 1
low clk
pause 1
high clk
pause 1
high DataPin
pause 1
low clk
return
'---------------------------- Wait for Sensor ----------------------------------
WaitSensor:
RawData = 4096
Loop:
RawData = RawData - 1
if DataPin && RawData.bit11 then Loop
return
'--------- Calculate the CRC from Command Byte and Received Bytes --------------
GetSHTCRC:
crccalc = 0
databyte = SHTCommand
gosub CalcSHTCRC
databyte = RawData.byte1
gosub CalcSHTCRC
databyte = RawData.Byte0
gosub CalcSHTCRC
CRC = CRC REV 8
return
'--------------------- CRC Bit Calcuation Method -------------------------------
CalcSHTCRC:
for i = 0 to 7
databit = crccalc.7 ^ databyte.7
databyte = databyte << 1
if databit = 0 then ShiftSHT
crccalc = crccalc ^ $18
ShiftSHT:
crccalc = crccalc << 1
crccalc.0 = databit
next i
return
'--------------------- CRC Bit Calcuation Method -------------------------------
CheckCRC:
if SHTCommand = %00000011 then
if CRC <> CRCCalc then
TempCRCError = 1
else
TempCRCError = 0
endif
else
if CRC <> CRCCalc then
RHCRCError = 1
else
RHCRCError = 0
endif
endif
return
'-------------------------- Convert to Kelvin ----------------------------------
ConvertToCelcius:
' 14 Bit Temperature = -40.00 + 0.01 * DataOutout
if RawData => 4000 then
tempC = RawData - 4000
SignC = 160 ' Just leave the place holder blank
else
tempC = 4000 - RawData
SignC = "-"
endif
return
'------------------------- Convert to Fahrenheit -------------------------------
ConvertToFahrenheit:
if SignC = 160 then
SignF = 160
dummy = TempC * 18
TempF = div32 10
TempF = TempF + 3200
else
TempF = (tempc + 5000) * 900
TempF = DIV32 500
if TempC => 1778 and SignC = "-" then
TempF = TempF - 12200
SignF = "-"
else
TempF = 12200 - TempF
SignF = 160
endif
endif
return
'-------------------------- Convert to Kelvin ----------------------------------
ConvertToKelvin:
if signC = "-" then
tempk = 27315 - tempc
else
tempk = tempc + 27315
endif
return
'------------------------- Display Information ---------------------------------
Display:
gosub CustomCharacters
lcdout cmd,line1,signc,dec2 TEMPc/100,".",dec1 (tempc//100)/10,223,"C"
lcdout cmd,line2,dec2 RHTempComp/100,".",dec1 (RHTempComp//100)/10,"%RH"
return
'------------------------------ Custom Characters ------------------------------
CustomCharacters:
lcdout cmd,$40,$08,$14,$08,$07,$04,$06,$04,$04 ' ºF
lcdout cmd,$48,$08,$14,$08,$03,$04,$04,$04,$03 ' ºC
lcdout cmd,$50,$0C,$0A,$0C,$0A,$00,$0A,$0E,$0A ' RH
return
END