MIKROBASIC WITH SHT11 PROGRAMMING
MIKROBASIC WITH SHT11 PROGRAMMING
EVERYBODY USER
WHAT IS MIKROBASIC LANGUAGE SHT11 TEMPERATURE-HUMIDTY PROGRAM.
THANKS :
WHAT IS MIKROBASIC LANGUAGE SHT11 TEMPERATURE-HUMIDTY PROGRAM.
THANKS :
I'll bite. This is untested, better give it a good once over to be sure it's right. I have had two SHT11's sitting on my bench for the last 6 months, someday I'll get around to trying it. It does compile in 2.004, that's about all I can tell you.
Code: Select all
program SHT11Demo
'SHT11 test program
'data on PORTB.0
'clock on PORTB.1
'proc=16f819
dim wReading as word
sub procedure sht11(dim commandbyte as byte)
dim tmp as byte
trisb.0=0 'data input
trisb.1=1 'clock output
'start seq
portb.0=1 'data high
portb.1=1 'clock high
portb.0=0 'data low
portb.1=0 'clock low
portb.1=1 'clock high
portb.0=1 'data high
'send command byte
for tmp=7 to 0 step -1
portb.1=0 'clock low
if commandbyte.tmp=1 then
portb.0=1
else
portb.0=0
end if
portb.1=1 'clock high
next tmp
portb.1=0 'clock low
'check for ack
trisb.0=1 'data input
portb.1=1 'clock high
if portb.0=1 then 'ack not received
'error code
exit
end if
'wait for data ready (data=0)
portb.1=0 'clock low
for tmp=1 to 255
if portb.0=0 then
break
end if
next tmp
if portb.0=1 then
'data not ready after spec delay
'error code here
exit
end if
'data is ready, read in upper 8 bits
wReading=0
for tmp=7 to 0 step -1
wReading=(wReading<<1)
portb.1=1 'clock high
if portb.0=1 then
wReading.0=1
else
wReading.0=0
end if
portb.1=0 'clock low
next tmp
'ack msb
trisb.0=0 'data output
portb.0=0 'data low
portb.1=1 'toggle clock / ack
portb.1=0 '
trisb.0=1 'data input
'get lsb
for tmp=7 to 0 step -1
wReading=(wReading<<1)
portb.1=1 'clock high
if portb.0=1 then
wReading.0=1
else
wReading.0=0
end if
portb.1=0 'clock low
next tmp
end sub
main:
sht11(0x03) 'temperature - result in wReading
sht11(0x05) 'humidity - result in wReading
end.
I have tested the SHT11 demo code and have found a few minor errors.
The trisb command for the clock needs to be set for output.
The for next loops fail to work in 2.004 with a negative step.
I added the code to read the crc from the SHT11 as it will not make continuous readings without it.
I have connected the SHT11 to PORTA as it allowed me to display the results on a LCD.
The reading for humidity is an approximation.
The trisb command for the clock needs to be set for output.
The for next loops fail to work in 2.004 with a negative step.
I added the code to read the crc from the SHT11 as it will not make continuous readings without it.
I have connected the SHT11 to PORTA as it allowed me to display the results on a LCD.
The reading for humidity is an approximation.
Code: Select all
program SHT11G
'SHT11 test program
'data on porta.0
'clock on porta.1
'proc=16F818
dim wReading as word
dim crc as byte
dim txt as string[8]
sub procedure sht11(dim commandbyte as byte)
dim tmp as byte
dim tmp1 as byte
trisa.0=0 'data output
trisa.1=0 'clock output
'start seq
porta.0=1 'data high
porta.1=1 'clock high
porta.0=0 'data low
porta.1=0 'clock low
porta.1=1 'clock high
porta.0=1 'data high
'send command byte
tmp1=7
for tmp=0 to 7
porta.1=0 'clock low
if commandbyte.tmp1=1 then
porta.0=1
else
porta.0=0
end if
porta.1=1 'clock high
tmp1=tmp1-1
next tmp
porta.1=0 'clock low
'check for ack
trisa.0=1 'data input
porta.1=1 'clock high
if porta.0=1 then 'ack not received
'error code
exit
end if
'wait for data ready (data=0)
porta.1=0 'clock low
for tmp=1 to 255
if porta.0=0 then
break
end if
next tmp
if porta.0=1 then
'data not ready after spec delay
'error code here
exit
end if
'data is ready, read in upper 8 bits
wReading=0
for tmp=0 to 7
wReading=(wReading<<1)
porta.1=1 'clock high
if porta.0=1 then
wReading.0=1
else
wReading.0=0
end if
porta.1=0 'clock low
next tmp
'ack msb
trisa.0=0 'data output
porta.0=0 'data low
porta.1=1 'toggle clock / ack
porta.1=0 '
trisa.0=1 'data input
'get lsb
for tmp=0 to 7
wReading=(wReading<<1)
porta.1=1 'clock high
if porta.0=1 then
wReading.0=1
else
wReading.0=0
end if
porta.1=0 'clock low
next tmp
'ack lsb
trisa.0=0 'data output
porta.0=0 'data low
porta.1=1 'toggle clock / ack
porta.1=0 '
trisa.0=1 'data input
'get crc - not used
for tmp=0 to 7
crc=(crc<<1)
porta.1=1 'clock high
if porta.0=1 then
crc.0=1
else
crc.0=0
end if
porta.1=0 'clock low
next tmp
'ack crc
trisa.0=0 'data output
porta.0=0 'data low
porta.1=1 'toggle clock / ack
porta.1=0 '
trisa.0=1 'data input
end sub
main:
OSCCON.4=0
OSCCON.5=1
OSCCON.6=1
ADCON1=6
Lcd_Init(PORTB)
Lcd_cmd( LCD_CURSOR_OFF)
main1:
sht11(0x03) 'temperature - result in wReading
WordToStr(wReading, txt)
Lcd_Out(2, 1, txt)
wReading=wReading-4000
WordToStr(wReading, txt)
Lcd_Chr(1,1,txt[0])
Lcd_Chr_cp(txt[1])
Lcd_Chr_cp(txt[2])
Lcd_Chr_cp(".")
Lcd_Chr_cp(txt[3])
Lcd_Chr_cp(txt[4])
Lcd_Chr_cp("C")
delay_ms(1000)
sht11(0x05) 'humidity - result in wReading
WordToStr(wReading, txt)
Lcd_Out(2, 8, txt)
wReading=wReading div 32
WordToStr(wReading, txt)
Lcd_Out(1, 8, txt)
delay_ms(4000)
goto main1
end.
Re: important
This program in fact has bug. Sometimes it will display error values when I tested it. PM me your e-mail if you want a working and reliable version of the code.cdunkel wrote:Hello,
i tried this code with MBasic 2.2 and it works great. Now, i installed MBasic 4.03 and the code would not run.
Can anybody help me to solve this problem?
Thank you!
By the way, which country are you from?
HCWong
PIC Rules!!
Re: important
For the sake of others on this forum, it would be great that you post the problem and solution for it.hcwong wrote:This program in fact has bug. Sometimes it will display error values when I tested it. PM me your e-mail if you want a working and reliable version of the code.
I have had another look at the code I posted and recompiled it under MBasic 4.03 and found it did not work.
I don't know why but I needed to add TRISB=0 before Lcd_Init(PORTB) to get the LCD to initialise.
Also I have added a 1ms delay in ' wait for data ready'
'wait for data ready (data=0)
porta.1=0 'clock low
for tmp=1 to 255
if porta.0=0 then
break
end if
' ******* Add 1 ms delay ******
delay_ms(1)
next tmp
if porta.0=1 then
'data not ready after spec delay
'error code here
exit
end if
The code works with a PIC16F628 if you remove the OSCCON
and ADCON1 instructions and add CMCON=7
The program uses the internal RC oscillator running at 4Mhz.
Trevor
I don't know why but I needed to add TRISB=0 before Lcd_Init(PORTB) to get the LCD to initialise.
Also I have added a 1ms delay in ' wait for data ready'
'wait for data ready (data=0)
porta.1=0 'clock low
for tmp=1 to 255
if porta.0=0 then
break
end if
' ******* Add 1 ms delay ******
delay_ms(1)
next tmp
if porta.0=1 then
'data not ready after spec delay
'error code here
exit
end if
The code works with a PIC16F628 if you remove the OSCCON
and ADCON1 instructions and add CMCON=7
The program uses the internal RC oscillator running at 4Mhz.
Trevor
I have a library in MikroPascal language.
You can translate in MikroBasic if you want !!!
For P18 : http://www.microelemente.ro/MikroPascal/Sensirion.zip
For P16 : http://www.microelemente.ro/MikroPascal ... on_P16.zip
You can translate in MikroBasic if you want !!!
For P18 : http://www.microelemente.ro/MikroPascal/Sensirion.zip
For P16 : http://www.microelemente.ro/MikroPascal ... on_P16.zip
Best regards, Florin Andrei Medrea.
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
Sorry I don't have time !!!
But ...
http://www.mikroe.com/forum/viewtopic.p ... hlight=sht
http://www.mikroe.com/forum/viewtopic.p ... hlight=sht
But ...
http://www.mikroe.com/forum/viewtopic.p ... hlight=sht
http://www.mikroe.com/forum/viewtopic.p ... hlight=sht
Best regards, Florin Andrei Medrea.
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
Hello!
I have managed to use hansolo's code and now i am able to display data.
The main problem is that i cannot read data, everything seems to be connected fine.
With the sensor connected to PORT C and a pullup resistor on the data pin i get this http://img156.imageshack.us/img156/7031/dsc00577rd3.jpg
When i remove the pullup resistor i get this http://img231.imageshack.us/img231/3641/dsc00578gq5.jpg
There is no change when i remove the sensor, i also removed the current limit resistors (220Ohm). Power supply is 5V (from the easypic5) and every connection seems to be fine.
Anyone has a clue?
Thnx :)
I have managed to use hansolo's code and now i am able to display data.
The main problem is that i cannot read data, everything seems to be connected fine.
With the sensor connected to PORT C and a pullup resistor on the data pin i get this http://img156.imageshack.us/img156/7031/dsc00577rd3.jpg
When i remove the pullup resistor i get this http://img231.imageshack.us/img231/3641/dsc00578gq5.jpg
There is no change when i remove the sensor, i also removed the current limit resistors (220Ohm). Power supply is 5V (from the easypic5) and every connection seems to be fine.
Anyone has a clue?
Thnx :)