MIKROBASIC WITH SHT11 PROGRAMMING

General discussion on mikroBasic.
Author
Message
efe1409
Posts: 6
Joined: 13 Jun 2005 12:06

MIKROBASIC WITH SHT11 PROGRAMMING

#1 Post by efe1409 » 29 Jun 2005 15:39

EVERYBODY USER

WHAT IS MIKROBASIC LANGUAGE SHT11 TEMPERATURE-HUMIDTY PROGRAM.

THANKS : :(

cashsale
Posts: 104
Joined: 23 Mar 2005 01:37
Location: 39N, -108W

#2 Post by cashsale » 01 Jul 2005 04:51

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.

tjc
Posts: 3
Joined: 06 Jul 2005 19:49

#3 Post by tjc » 06 Jul 2005 22:02

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.

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.

cdunkel
Posts: 42
Joined: 13 Sep 2005 06:18
Location: Switzerland

important

#4 Post by cdunkel » 16 Apr 2006 15:15

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. :cry:
Can anybody help me to solve this problem?
Thank you!

hansolo
Posts: 344
Joined: 28 Oct 2005 07:28

Re: important

#5 Post by hansolo » 16 Apr 2006 23:23

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. :cry:
Can anybody help me to solve this problem?
Thank you!
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.

By the way, which country are you from?

HCWong
PIC Rules!!

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: important

#6 Post by zristic » 17 Apr 2006 08:53

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.
For the sake of others on this forum, it would be great that you post the problem and solution for it.

tjc
Posts: 3
Joined: 06 Jul 2005 19:49

#7 Post by tjc » 21 Apr 2006 14:35

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

kostasgr
Posts: 15
Joined: 16 Jan 2008 12:57

#8 Post by kostasgr » 16 Jan 2008 18:18

Hello!
I am bringing an old topic back to ask you if anyone has a working example or a library to read the sensor.
I also seen somewere that you can improove the reading.

Regards!

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#9 Post by yo2lio » 16 Jan 2008 18:55

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
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

kostasgr
Posts: 15
Joined: 16 Jan 2008 12:57

#10 Post by kostasgr » 16 Jan 2008 19:22

Hi yo2lio and thnk's for your great help, i've seen those libraries before but i'm not sure how to convert them to mikrobasic (noob alert). :(

Thank you

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#11 Post by yo2lio » 16 Jan 2008 19:30

Ok,

I don't have time right now, please remind me after few days ....
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

kostasgr
Posts: 15
Joined: 16 Jan 2008 12:57

#12 Post by kostasgr » 19 Jan 2008 13:07

Hello!
Any news updates for the libraries?
Thank's for your time :)

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#13 Post by yo2lio » 19 Jan 2008 14:55

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

kostasgr
Posts: 15
Joined: 16 Jan 2008 12:57

#14 Post by kostasgr » 21 Jan 2008 09:53

No prob!
I will search and read again :)

kostasgr
Posts: 15
Joined: 16 Jan 2008 12:57

#15 Post by kostasgr » 30 Jan 2008 19:51

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 :)

Post Reply

Return to “mikroBasic General”