element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Freedom development platform
  • Products
  • Dev Tools
  • Freedom development platform
  • More
  • Cancel
Freedom development platform
Blog [FRDM-K64F + KDS + PE + Mikroe Click SHT11] Tutorial: Display temperature and humidity data on hyperterminal using Bit Banging
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Freedom development platform to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: bheemarao
  • Date Created: 9 Mar 2015 1:26 PM Date Created
  • Views 836 views
  • Likes 1 like
  • Comments 1 comment
  • sht11
  • bit_banging
  • click_shield
  • frdm-k64f
Related
Recommended

[FRDM-K64F + KDS + PE + Mikroe Click SHT11] Tutorial: Display temperature and humidity data on hyperterminal using Bit Banging

bheemarao
bheemarao
9 Mar 2015

Here is a project created to interface SHT11 click module (humidity and temperature sensor) with FRDM-K64 freedom board.

Before we start this project below are the pre-requisites:


  1. KDS software tool from Freescale
  2. FRDM-K64F Freedom development board
  3. FRDM-K64F click shield / Arduino UNO click shield [MIKROE-1581] (optional)
  4. SHT1x click module [MIKROE-949]

 

 

SHT11 details:


http://www.mikroe.com/click/sht1x/


SHT11 Click is an accessory board in mikroBusTm form factor. It includes a digital humidity and temperature sensor SHT11. A unique capacitive sensor element is used to measure relative humidity while the temperature is measured by a band-gap sensor. Serial I2C interface and factory calibration, allow easy and fast system integration. Board is set to use 3.3V power supply by default.

 

image

 

Communication with the SHT11 sensor is done using I2C interface. Temperature can be represented in 12-bit or 14-bit format in operating range from -40 to +100°C with accuracy of +/- 0.5°C at room temperature. Humidity can be represented in 8-bit or 12-bit resolution with +/- 3% accuracy.

The schematic of the click board is as shown below:

 

image

 

So let me start from creating the new project:


image

 

Create a New KDS project name as shown, i have selected “SHT11-using-K64”

 

image

 

Next select the device: you have 2 options for selecting it, one is by “Boards” and other is by “Processors” i am selecting it from the “Boards” option.

 

image

 

Select the board part number FRDM-K64 and proceed further

Make the selection of processor expert in standalone as shown below:

 

image

 

Click finish.

Now click on Processor component


image

 

And make the clock configuration as shown below:

image

 

Next change the clock configuration-3 as shown below

image

 

Now we will proceed further by adding our components: as we are implementing the I2C logic using Bit Banging we need only 2 bits as SDA/SCL which can act as both input and output by software logic.


The required components are as follows:


  1. BitIO_LDD:  for SDA bit I/O
  2. BitIO_LDD:  for SCL bit I/O
  3. Wait:
  4. ConsoleIO: for hyper terminal output

 

Select component library and select BitIO_LDD component as shown below:

 

image

 

Add this component two times as we require 2 Bits for SDA and SCL interface signal

Now rename the component Bit1 IO as SDA as shown below:

image


 

Now select the pin for I/O in our case the SDA bit is connected to PTC10 of the freedom board

 

image

 

Similarly connect Bit2 component as shown below:

SCL bit is connected to PTC11

image

 

Now add the component “Wait” as shown below:

image


Next add the ConsoleIO component module from component library as shown below:

image

 

Double click on the module component to add to our project: As UART0’s RX is connected to PTB16 and UART0’s TX is connected to PTB17.

This UART port pins are required for console terminal output. Next click on ConsoleIO_Serial_LDD button as shown below:


image

We have configured UART0 as shown below:

image

 

 

We can see all the four components added with required settings as shown below:

Now generate the project by clicking the below shown button

 

image

It Builds with no errors as shown below:

image

Now we are going to add our code in “main.c” file


int main(void)

/*lint -restore Enable MISRA rule (6.3) checking. */

{

/* Write your local variable definition here */

 

/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/

PE_low_level_init();

/*** End of Processor Expert internal initialization.                    ***/

 

/* Write your code here */

/* For example: for(;;) { } */

 

unsigned int *p_value1, *p_value2;

SDA_Init(SDA_Ptr);

SCL_Init(SCL_Ptr);

 

 

while(1){

 

       Read_SHT11(&temp, &rel_hum);

       WAIT1_Waitms(250);

        printf(" Temperature value T: %f %% degC\t", temp);

        printf(" Humidity value RH: %f pct\r\n", rel_hum);

 

         }


Below are the driver functions required:


void s_transstart()

{

//Initial state

SDA_SetDir(SDA_Ptr, 0); //SDA_SetDir(SDA_Ptr, 1); //set as output// SDA_dir = 1;                            //release DATA-line

SDA_SetVal(SDA_Ptr);  // giving high to the bit// SDA_pin = 1;

SCL_ClrVal(SCL_Ptr);  // giving 0 to the bit//SCL_pin = 0;                           // SCL Low

 

WAIT1_Waitus(1);   //Delay_uS(1);

SCL_SetVal(SCL_Ptr);  // giving high to the bit // SCL_pin = 1;

WAIT1_Waitus(1);//Delay_uS(1);

 

SDA_SetDir(SDA_Ptr, 1); //set as output //SDA_dir = 0;   // define SDA as output

SDA_ClrVal(SDA_Ptr);   //SDA_pin = 0;                           // SDA low

WAIT1_Waitus(1); //Delay_uS(1);

 

SCL_ClrVal(SCL_Ptr); //SCL_pin = 0;

WAIT1_Waitus(3);      // Delay_uS(3);

SCL_SetVal(SCL_Ptr);   //SCL_pin = 1;

 

WAIT1_Waitus(1); //Delay_uS(1);

SDA_SetDir(SDA_Ptr, 0); //SDA_dir = 1;

WAIT1_Waitus(1);    //Delay_uS(1);

SCL_ClrVal(SCL_Ptr); //SCL_pin = 0;

}

 

 

unsigned char s_read_byte(unsigned char ack)

{

unsigned char i=0x80;

unsigned char val=0;

 

//Initial state

SDA_SetDir(SDA_Ptr, 1);               //release DATA-line

SDA_SetVal(SDA_Ptr);

SCL_ClrVal(SCL_Ptr);                // SCL Low

 

while(i)                //shift bit for masking

{

SCL_SetVal(SCL_Ptr); //clk for SENSI-BUS

SDA_SetDir(SDA_Ptr, 0); //input mode  added

WAIT1_Waitus(1);     // Delay_uS(1);

if (SDA_GetVal(SDA_Ptr) == 1)

{

val=(val | i);      //read bit

}

SCL_ClrVal(SCL_Ptr);

WAIT1_Waitus(1); //Delay_uS(1);

i=(i>>1);

}

 

SDA_SetDir(SDA_Ptr, 1);

 

if (ack)

{

//in case of "ack==1" pull down DATA-Line

SDA_ClrVal(SDA_Ptr);

}

else

{

SDA_SetVal(SDA_Ptr);

}

 

SCL_SetVal(SCL_Ptr); //clk #9 for ack

WAIT1_Waitus(3);   //Delay_uS(3);

SCL_ClrVal(SCL_Ptr);

WAIT1_Waitus(1);   // Delay_uS(1);

 

SDA_SetDir(SDA_Ptr, 1);                           //release DATA-line

SDA_SetVal(SDA_Ptr);

return (val);

}

 

 

unsigned char s_write_byte(unsigned char value)

{

unsigned char i=0x80;

unsigned char error=0;

 

SDA_SetDir(SDA_Ptr, 1);

 

while(i)

{ //shift bit for masking

if (i & value)

{

SDA_SetVal(SDA_Ptr);    //masking value with i , write to SENSI-BUS

}

else

{

SDA_ClrVal(SDA_Ptr);

}

 

SCL_SetVal(SCL_Ptr);  //clk for SENSI-BUS

WAIT1_Waitus(3); //Delay_uS(3);

SCL_ClrVal(SCL_Ptr);

WAIT1_Waitus(3); //Delay_uS(3);

i=(i>>1);

}

 

SDA_SetDir(SDA_Ptr, 1);                           //release DATA-line

SDA_SetVal(SDA_Ptr);

 

SCL_SetVal(SCL_Ptr);                           //clk #9 for ack

WAIT1_Waitus(3);         //Delay_uS(3);

SDA_SetDir(SDA_Ptr, 0); //added input mode

 

if (SDA_GetVal(SDA_Ptr) == 1) error = 1; //check ack (DATA will be pulled down by SHT11)

WAIT1_Waitus(1);         //Delay_uS(1);

SCL_ClrVal(SCL_Ptr);

 

return(error); //error=1 in case of no acknowledge

}

 

unsigned char s_measure(unsigned int *p_value, unsigned char mode)

{

unsigned char i=0;

unsigned char msb,lsb;

unsigned char checksum;

 

*p_value=0;

s_transstart(); //transmission start

 

if(mode)

{

mode = MEASURE_HUMI;

}

else

{

mode = MEASURE_TEMP;

}

 

if (s_write_byte(mode)) return(1);

// normal delays: temp i=70, humi i=20

 

SDA_SetDir(SDA_Ptr, 0);  // SDA_dir = 1;

 

while(i<240)

{

WAIT1_Waitms(1);

WAIT1_Waitms(1);

WAIT1_Waitms(1);

if (SDA_GetVal(SDA_Ptr) == 0)

{

i=0;

break;

}

i++;

}

 

// or timeout

if(i) return(2);

 

msb=s_read_byte(ACK); //read the first byte (MSB)

lsb=s_read_byte(ACK); //read the second byte (LSB)

checksum=s_read_byte(noACK);                //read checksum (8-bit)

 

*p_value=(msb<<8)|(lsb);

 

return(0);

}

 

float calc_sth11_temp(unsigned int t)

{

float t_out;

t_out =  t*0.01 - 40;

return t_out;

}

 

float calc_sth11_humi(unsigned int h, int t)

{

float rh_lin;                      // rh_lin:  Humidity linear

float rh_true;                   // rh_true: Temperature compensated humidity

float t_C;                 // t_C   : Temperature [°C]

 

t_C=t*0.01 - 40;                    //calc. temperature from ticks to [°C]

rh_lin=C3*h*h + C2*h + C1;             //calc. humidity from ticks to [%RH]

rh_true=(t_C-25)*(T1+T2*h)+rh_lin; //calc. temperature compensated humidity

 

// now calc. Temperature compensated humidity [%RH]

// the correct formula is:

// rh_true=(t/10-25)*(0.01+0.00008*(sensor_val))+rh;

// sensor_val ~= rh*30

// we use:

// rh_true=(t/10-25) * 1/8;

 

if(rh_true>100)rh_true=100;               //cut if the value is outside of

if(rh_true<0.1)rh_true=0.1;               //the physical possible range

 

return rh_true;

}

 

void Read_SHT11(float *fT, float *fRH)

{

unsigned int t;

unsigned int h;

 

float value=0;

ucSens_Error = 0;

 

ucSens_Error = s_measure(&t, 0);

*fT = calc_sth11_temp(t);

ucSens_Error = s_measure(&h, 1);

*fRH = calc_sth11_humi(h, t);

 

 

}

 

char s_read_statusreg(unsigned char *p_value)

{

unsigned char checksum = 0;

 

s_transstart();                             //transmission start

if(s_write_byte(STATUS_REG_R)) return 1;    //send command to sensor

*p_value=s_read_byte(ACK);                  //read status register (8-bit)

checksum=s_read_byte(noACK);                //read checksum (8-bit)

 

return 0;

}

 

 

char s_write_statusreg(unsigned char value)

{

s_transstart();                             //transmission start

if(s_write_byte(STATUS_REG_W)) return 1;    //send command to sensor

if(s_write_byte(value)) return 1;           //send value of status register

 

return 0;

}

 

void s_connectionreset()

{

unsigned char i;

 

//Initial state

SDA_SetDir(SDA_Ptr, 1);                            //release DATA-line

SDA_SetVal(SDA_Ptr);

SCL_ClrVal(SCL_Ptr);                           // SCL Low

 

for(i=0; i<9; i++) //9 SCK cycles

{

SCL_SetVal(SCL_Ptr);

WAIT1_Waitus(3);

SCL_ClrVal(SCL_Ptr);

WAIT1_Waitus(3);

}

 

s_transstart(); //transmission start

}

 

 

unsigned char s_softreset(void)

{

s_connectionreset();                         //reset communication

                                               //send RESET-command to sensor

return (s_write_byte(RESET));                //return=1 in case of no response form the sensor

  }

 

 

Explanation of code:


The below are the function/driver required for this code which is implemented using Bit Banging method:

 

  • void s_transstart()
  • unsigned char s_read_byte(unsigned char ack)
  • unsigned char s_write_byte(unsigned char value)
  • unsigned char s_measure(unsigned int *p_value, unsigned char mode)
  • float calc_sth11_temp(unsigned int t)
  • float calc_sth11_humi(unsigned int h, int t)
  • void Read_SHT11(float *fT, float *fRH)
  • char s_read_statusreg(unsigned char *p_value)
  • char s_write_statusreg(unsigned char value)
  • void s_connectionreset()
  • unsigned char s_softreset(void)

 

 

Inside main we are calling the function Read_SHT11() which in turn returns the temperature and humidity values

 

The function “calc_sth11_temp()” will generate the required I2C signals by Bit Banging method i.e it simulates the signal without using the internal resource.

and gives you the temperature value.

 

The function ”calc_sth11_humi()” will generate the necessary I2C signal (Bit Banging) and give the humidity value.

 

 

For more details on timing signal refer to the SHT11 datasheet:

http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_SHT1x_Datasheet_V5.pdf

 

Now it’s time to build the project, click on the hammer button as shown

image

You can see the build is finished successfully with no errors

image


Now it is ready to debug/execute

Now connect the K64 board to OPEN SDA port to your computer through USB cable. The hardware setup is as shown below, the click board is connected via click shield

 

image

Click on Debugger configuration as shown below:

 

image

 

Now click on Apply and Debug

You can see below Debug window screen

 

image

 

Make sure you have open the corresponding hyperterminal window from your ‘Computer Management’ Device manager window:

In my case it is connected to COM41 port

 

image

 

Now click on green ‘Resume’ button

image

Open COM41 port from any of the hyper terminal application (putty in my case)


image

 

The output seen in the terminal is as shown below:

image

 

I have enclosed the project folder for quick reference.


Happy SHT11 click interfacing... image image image

Attachments:
SHT11-using-K64F.rar
SHT11-using-K64F.srec.zip
  • Sign in to reply
  • DAB
    DAB over 10 years ago

    Nice detailed post.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube