Using the SHT11 with HC908GP32

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using the SHT11 with HC908GP32

3,249 Views
damitsi
Contributor I

Hello to everyone, I'm a student and i'm working on a project. I started to learn C language two months ago so i don't have enough experience with this.I want to use the SHT11 sensor .
 I'm using the microcontroller HC908GP32 and ICC08 from ImageCraft as a C-compiler and ICS08GPGTZ(Win IDE development environment) to input the < .S19> files to the device.
The development board which I'm using is non a commercial board.
 Relative to the programming part I considered to put a led inside the code to help me find the error, when the led is on  it means that the code runs ok until led. I believe that the error must be in the function <<char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)>> or in the function <<char s_write_statusreg(unsigned char *p_value)>>
 The purpose of the project is to check the temp and humi. If temp get over the 40 degrees of celsius a funcooler must be start and something similar with  the value of humidity. I will attach the codes below first the prototype one from sensirion's web site and second the incorrect one.
I would be very grateful on any advice with this. 


Regards Jim.

Labels (1)
0 Kudos
Reply
6 Replies

842 Views
bigmac
Specialist III
Hello Jim,
 
I can see a couple of obvious problems with your code -
 
Firstly the data_read() function does not return a value - I assume it should do so, and the following code would return a value.
 
unsigned char data_read (void)
{
  DDRE = 0x00;
   return (PTE & 0x01);
}
 
There is also a problem with the following function -
 
char s_read_byte(unsigned char ack)
{
  unsigned char i,val=0;
 
  data1();                         // release DATA line
  for (i = 0x80; i > 0; i /= 2) {  // shift bit for masking
     sck1();                       // clk for SENSIBUS
     input();
     if (PTE)
        val = (val|i); //read bit
     sck0();
  }
  if ((ack & 0x01) == 1)
     data0(); 
// in case of "ack==1" pull down DATA Line
  sck1();     // clk #9 for ack
  delayus(6); // pulswith approx. 5 us
  sck0();
  data1();    //release DATA line
  return val;
}
You are actually testing the state of all bits of PTE - I don't think this what you want.  To test only the bit used for the serial data, the line should read -
 
  if (PTE & 0x01)
 
I will also make some other comments about the code, although these will not affect the present operation.  In many of your functions you require to set or clear a single bit associated with a port or its DDR.  However, you are actually affecting all eight bits within the port.  Unless specifically a function to initialise all bits within a port, generally a function should only alter those bits that it uses.
 
Consider if you decided to use another bit of the same port for a different purpose - you would have to find all instances where that bit was affected in the existing code.  This would involve a lot of modification, and would be prone to error, or perhaps a missed occurrance.
 
The approach I might usually take is to initialise all bits within a port, its associated DDR, and perhaps the other registers associated with that port.  This code would reside in an initialisation function that is called only once whenever the program executes (for example your init() function).  This can easily be edited as circumstances change.  The various functions called would then set or clear only the bits they were concerned with, and not affect any other bit.
 
The following macros for example -
#define DATA_0  (DDRE |= 0x01)  // Set DDRE0 bit
#define DATA_1  (DDRE &= 0xFE)  // Clear DDRE0 bit
#define SCLK_0  (PTA &= 0x7F)   // Clear PTA7 bit
#define SCLK_1  (PTA |= 0x80)   // Set PTA7 bit
 
Regards,
Mac
0 Kudos
Reply

842 Views
bigmac
Specialist III
Hello again,
 
I have just noticed another problem.  In addition to the data_read() function returning a value, the s_write_byte() function will also need to be modified to use the return value, similarly to the original sample code from Sensiron.  I notice you have omitted the local variable error - this would need to be reinstated.  The final part of the function would then be as follows -
 
  data1();             // release DATA line
  sck1();              // clk #9 for ACK
  error = data_read(); // check ACK (DATA pulled down by SHT11)
  sck0();                             
  return error         // Return 1 if no ACK
Within the s_read_byte() function, I also notice that use of the input() function is unnecessary, since the DATA line had previously been released.
 
You could also replace the modification I suggested in the previous post
   if (PTE & 0x01) ...
 
with a variation that will operate in an equivalent manner
   if (data_read()) ...
 
It makes more sense to re-use the function you have already created.
 
Regards,
Mac
 
0 Kudos
Reply

842 Views
damitsi
Contributor I
Dear Mac , I don't know how to thank you. It seems it's working ok, after your advices the led turns on  and it takes 8 seconds.
So know i tryed to put a check of 40 degrees, i thought a function but it's not working correct, the led turn's on without warm up the sensor.
I put the function
 
void heat(void)
{ short t_C;
 if (t_C > 40) led();
 
to the main() after the < calc_sth11(&humi_val.s,&temp_val.s);>
 
So i would like to thank you again for spending your time with me.
 
Regards,
 Jim
0 Kudos
Reply

842 Views
bigmac
Specialist III
Hello Jim,
 
After the function calc_sth11() has completed, the temperature value will reside in the variable temp_val.s (for a temperature of 40C, this should actually contain the value 400, since the temperature is expressed in 0.1C increments).
 
The new heat() function will require to use a parameter so that the temperature value may be passed to it, and not a local variable as you currently have.
 
void heat(short t_C)
{
  if (t_C > 400)
     led();
}
 
Regards,
Mac
 
0 Kudos
Reply

842 Views
damitsi
Contributor I
Dear Mac, i ask from you to be more specific with your previous post relative to
 
<The new heat() function will require to use a parameter so that the temperature value may be passed to it, and not a local variable as you currently have.>
 
As you can see i'm still having problem with this.
The code
0 Kudos
Reply

842 Views
bigmac
Specialist III
Hello Jim,
 
We are getting to some of the fundamental aspects of the C language.  I think you should consult a basic C tutorial, that will explain these issues much better than I can.  If you do a Google search on the topic of "C programming" or "C tutorial", you should be able to obtain plenty of appropriate material.
 
Some of the areas in which you currently seem to have difficulty -
 
The visibility of variables within a program.
Function parameters, and the passing of data to a function.
The return of data from a function.
Nested if-else statements.
 
To explain why your heat() function does not work - you do not pass the temperature value to the function.  You have actually defined a new (and unnecessary) local variable within main(), and attempt to pass its uninitialised value to the function.
 
Regards,
Mac
 
0 Kudos
Reply