Here is a project created to interface SHT11 click module (humidity and temperature sensor) with FRDM-KL46Z freedom board.
Before we start this project below are the pre-requisites:
- KDS 2.0.0 software tool from Freescale
(http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=KDS_IDE)
- FRDM-KL46Z Freedom development board
- FRDM-KL46Z click shield / Arduino UNO click shield [MIKROE-1581] (optional)
(http://www.mikroe.com/click/frdm-kl46z-shield/)
- SHT1x click module [MIKROE-949]
SHT11 details:
[http://www.mikroe.com/click/sht1x/]
SHT11 Click is an accessory board in mikroBus 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.
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:
FRDM-KL46Z click shield details:
http://www.mikroe.com/click/frdm-kl46z-shield/
FRDM-KL46Z click shield is an extension for Freescale's FRDM-KL46Z development platform. It's a simple shield with two mikroBUS host sockets that allow you to interface more than 100 different types of click boards with the Kinetis L series KL4x MCU aboard FRDM-KL46Z. Quickly add functionalities like GSM, GPS, WiFi, ZigBee, Bluetooth, or thunder detection, proximity and color sensing and so on. The package includes two 1x8 headers, and single 1x10, and 1x6 headers.
mikroBUS is specially designed pinout standard with SPI, I2C, Analog, UART, Interrupt, PWM, Reset and Power supply pins.
FRDM-KL46Z click shield allows you to use click add-on boards from MikroElektronika on Freescale’s FRDM-KL46Z development platform.
Key features
- Two mikroBUS host connectors for attaching click boards.
- Oscillator probe
Key Benefits
- Shield is designed to stay within dimensions of FRDM-KL46Z for smooter integration.
- Brings click board connectivity to Freescale’s Kinetis L Series KL4x
So let me start from creating the new project:
Create a New KDS project name as shown, i have selected “KL46-sht1x”
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.
Select the board part number FRDM-KL46Z and proceed further
Make the selection of processor expert in standalone as shown below:
Click finish and proceed further.
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:
- BitIO_LDD: for SDA bit I/O
- BitIO_LDD: for SCL bit I/O
- Wait:
- ConsoleIO: for hyper terminal output
Select component library and select BitIO_LDD component as shown below:
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:
Now select the pin for I/O in our case the SDA bit is connected to PTC2 of the freedom board
Similarly connect Bit2 component as shown below:
SCL is connected to PTC1
Now add the component “Wait” as shown below:
Next add the ConsoleIO component module from component library as shown below:
Double click on the module component to add to our project:
As UART0’s RX is connected to PTA1 and UART0’s TX is connected to PTA2.
This UART port pins are required for console terminal output.
Next click on ConsoleIO_Serial_LDD button as shown below:
We have configured UART0 as shown below:
We can see all the four components added with required settings as shown below:
Now generate the project by clicking the below shown button
It Builds with no errors as shown below:
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(;;) { } */ unsignedint *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:
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 get the humidity value.
For more details on timing signal refer to the SHT11 datasheet:
Now it’s time to build the project, click on the hammer button as shown
You can see the build is finished successfully with no errors
Now it is ready to debug/execute
Now connect the KL46Z 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
Click on Debugger configuration as shown below:
Then select the settings for the current project
Now click on Apply and Debug
You can see below Debug window screen
Make sure you have open the corresponding hyperterminal window from your ‘Computer Management’ Device manager window:
In my case it is connected to COM24 port
Now click on green ‘Resume’ button
Open COM24 port from any of the hyper terminal application (putty in my case)
The output seen in the terminal is as shown below:
I have enclosed the project folder and SREC executable for quick reference.