Single wire or 1-wire interface is one of the typically used protocols for small inexpensive devices such as digital humidity sensor, thermometer and weather instruments. The protocol is similar to I2C but with only one wire, and specially designed for low bandwidth and low baud rates.
Communicating with 1-wire protocol is based on reading the duration of high and low of a signal. If a signal bit was high for more than a specific interval then the bit is one else it is zero.
When PSoC sends a start signal which is pull down for approx 18ms, DHT11 starts in running mode and calculate the humidity and temperature . Once it is completed, DHT11 sends a response signal of 40-bit data that include the relative humidity and temperature information to PSoC . Users can choose to collect (read) some data. Once data is collected, DHT11 will change to the lowpower consumption mode until it receives a start signal from MCU again
Creating new project in PSoC:
Open PSoC Creator 4 and go to Files-> New -> Project.
Click next, and select empty schematic and enter your own project and click Finish.
Once the project is loaded, open TopDesign.cysch and add the below list of components.
- Digital Bidirectional Pin
- UART(SCB mode)
- Bootloadable
Open the Bootloadble configuration window by double clicking the component. Open Dependencies tap and select the Bootloader HEX files and ELF files that are generated using the following project. And then click OK.
Now the Bootloader has been configured successfully. Also configure the Digital Pin and UART component as shown in the image below.
Digital Pin: Drive mode should be Resistive Pull up and name should be 'DHT
'.
UART: Baud rate should be 115200 and name should be 'UART
'.
Now allocate the pins by opening Pins in the Design wide resources under the workspace Explorer. Allocate the pins as following using the drop down list.
UART\rx : P4[0]
UART\tx : P4[1]
DHT : P0[0]
Instruction set:
Now open main.c
inside source files from the workspace and area and you will see the default text as below.
Function for Reading the DHT output:
static int temperature=99;
static int humidity=99;
int DHTread()
{
uint8 IState;
IState=CyEnterCriticalSection();
uint8 bits[5];
uint8 cnt = 7;
uint8 idx = 0;
int calc=0;
int timeout=0;
for (i=0; i< 5; i++)
bits[i] = 0;
DHT_Write(0u);
CyDelay(19);
DHT_Write(1u);
while(DHT_Read()==1)
{
timeout++;
if(timeout>500)
goto r99; //DHT error function
}
while(DHT_Read()==0)
{
timeout++;
if(timeout>500)
goto r99; //DHT error function
}
calc=timeout;
timeout=0;
while(DHT_Read()==1);
for (i=0; i<40; i++)
{
timeout=0;
while(DHT_Read()==0);
while(DHT_Read()==1)
timeout++;
//Data acquiring point
if ((timeout) > (calc/2))
bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}
humidity = bits[0];
temperature = bits[2];
CyExitCriticalSection(IState);
CyDelay(1);
return 0;
r99: //Goto label for error in DHT reading
humidity = 99;
temperature = 99;
CyExitCriticalSection(IState);
return 99;
}
Write the above function in your main.c
. Also modify the main function to as given below.
int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
char outputstring[40];
UART_Start();
for(;;)
{
DHTread();
sprintf(outputstring,"Temperature=%i Humidity= %i \r\n");
UART_UartPutString(outputstring);
CyDelay(5000); //Delay in milli seconds.
}
}
Compile the project, program PSoC using Bootloader Host and Enjoy.
Comments
Please log in or sign up to comment.