/*
* Project name:
DHT22 click (Demonstration of mikroBUS DHT22 click board)
* Copyright:
(c) Mikroelektronika, 2015.
* Revision History:
20140225:
- initial release (FJ);
* Description:
This code demonstrates how to use DHT22 click with EasyMX Pro v7 board.
DHT22 sensor measures temperature and relative humidity.
* Test configuration:
MCU: STM32F107VC
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/CD00171190.pdf
Dev. Board: EasyMx PRO v7 for STM32
http://www.mikroe.com/eng/products/view/852/easymx-pro-v7-for-stm32/
Oscillator: HS-PLL 72.0000 MHz
http://www.mikroe.com/store/components/displays/
SW: mikroC PRO for ARM
http://www.mikroe.com/mikroc/arm/
* NOTES:
- Place DHT22 click board at the mikroBUS socket 1 on the EasyMx Pro v7 board.
- Put EasyTFT in its socket, turn on backlight using the SW11.3
*/
#include <resources.h>
// TFT module connections
unsigned int TFT_DataPort at GPIOE_ODR;
sbit TFT_RST at GPIOE_ODR.B8;
sbit TFT_RS at GPIOE_ODR.B12;
sbit TFT_CS at GPIOE_ODR.B15;
sbit TFT_RD at GPIOE_ODR.B10;
sbit TFT_WR at GPIOE_ODR.B11;
sbit TFT_BLED at GPIOE_ODR.B9;
// End TFT module connections
// AM2302 module connections
sbit AM2302_Bus_Out at GPIOD_ODR.B13;
sbit AM2302_Bus_In at GPIOD_IDR.B13;
// END AM2302 module connections
char AM2302_Data[5] = {0, 0, 0, 0, 0};
unsigned humidity = 0, temperature = 0;
//Get Sensor values
char AM2302_Read(unsigned *humidity, unsigned *temperature) {
char i = 0, j = 1;
char timeout = 0;
char sensor_byte;
GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_13); //Set GPIOD pin 13 as digital input
GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_13); //Set GPIOD pin 13 as digital output
AM2302_Bus_Out = 1; //Set GPIOD pin 13 HIGH for 100 milliseconds
Delay_ms(100); //Delay 100ms
AM2302_Bus_Out = 0; //Host the start signal down time min: 0.8ms, typ: 1ms, max: 20ms
Delay_ms(2); //Delay 2ms
AM2302_Bus_Out = 1; //Set GPIOD pin 13 HIGH
GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_13); //Set GPIOD pin 13 as digital input
// Bus master has released time min: 20us, typ: 30us, max: 200us
timeout = 200;
while (AM2302_Bus_In) {
Delay_us(1);
if (!timeout--) {
return 1;
} //ERROR: Sensor not responding
}
// AM2302 response signal min: 75us, typ: 80us, max: 85us
while (!AM2302_Bus_In) { //response to low time
Delay_us(1);
}
while (AM2302_Bus_In) { //response to high time
Delay_us(1);
}
/*
* time in us: min typ max
* signal 0 high time: 22 26 30 (bit=0)
* signal 1 high time: 68 70 75 (bit=1)
* signal 0,1 down time: 48 50 55
*/
i = 0; //get 5 byte
for (i = 0; i < 5; i++) {
j = 1;
for (j = 1; j <= 8; j++) { //get 8 bits from sensor
while (!AM2302_Bus_In) { //signal "0", "1" low time
Delay_us(1);
}
Delay_us(30);
sensor_byte <<= 1; //add new lower byte
if (AM2302_Bus_In) { //if sda high after 30us => bit=1 else bit=0
sensor_byte |= 1;
delay_us(45);
while (AM2302_Bus_In) {
Delay_us(1);
}
}
}
AM2302_Data[i] = sensor_byte;
}
*humidity = (AM2302_Data[0] << 8) + AM2302_Data[1];
*temperature = (AM2302_Data[2] << 8) + AM2302_Data[3];
return 0;
}
void Clear_TFT_humidity() {
TFT_Set_Font(&Tahoma15x16_Bold, CL_BLACK, FO_HORIZONTAL);
TFT_Set_Pen(CL_WHITE, 10);
TFT_Set_Brush(1, CL_WHITE, 0, 0, 0, 0);
TFT_Rectangle(170,80,230,95);
TFT_Rectangle(40,160,320,180);
}
void Clear_TFT_temperature() {
TFT_Set_Font(&Tahoma15x16_Bold, CL_BLACK, FO_HORIZONTAL);
TFT_Set_Pen(CL_WHITE, 10);
TFT_Set_Brush(1, CL_WHITE, 0, 0, 0, 0);
TFT_Rectangle(170,100,230,115);
TFT_Rectangle(40,160,320,180);
}
void Clear_TFT_Error() {
TFT_Set_Font(&Tahoma15x16_Bold, CL_RED, FO_HORIZONTAL);
TFT_Set_Pen(CL_WHITE, 10);
TFT_Set_Brush(1, CL_WHITE, 0, 0, 0, 0);
TFT_Rectangle(40,160,320,180);
TFT_Rectangle(170,100,230,115);
TFT_Rectangle(170,80,230,95);
}
// Process and display current value to TFT
void processValue(unsigned humidity, unsigned temperature) {
char txt[15];
float temp;
temp = humidity / 10.0;
sprintf(txt, "%2.1f", temp); // Format Relative_humidity_val and store it to txt
Clear_TFT_humidity();
TFT_Write_Text(txt,170,80);
TFT_Write_Char(0x25,200,80);
temp = temperature / 10.0;
sprintf(txt, "%2.1f ºC", temp); // Format UV_val and store it to txt
Clear_TFT_temperature();
TFT_Write_Text(txt,170,100);
}
void Display_Init(){
TFT_Init_ILI9341_8bit(320, 240);
TFT_BLED = 1;
}
void DrawFrame() {
TFT_Fill_Screen(CL_WHITE);
TFT_Set_Pen(CL_BLACK, 1);
TFT_Line(20, 220, 300, 220);
TFT_LIne(20, 46, 300, 46);
TFT_Set_Font(&Tahoma15x16_Bold, CL_RED, FO_HORIZONTAL);
TFT_Write_Text("DHT22 Click Board Demo", 25, 14);
TFT_Set_Font(&Tahoma15x16_Bold, CL_BLACK, FO_HORIZONTAL);
TFT_Write_Text("EasyMx PRO v7", 19, 223);
TFT_Set_Font(&Tahoma15x16_Bold, CL_RED, FO_HORIZONTAL);
TFT_Write_Text("www.mikroe.com", 200, 223);
TFT_Set_Font(&Tahoma15x16_Bold, CL_BLACK, FO_HORIZONTAL);
TFT_Write_Text("Relative Humidity:", 40, 80);
TFT_Write_Text("Temperature:", 40, 100);
}
void main() {
char k = 0, t;
Display_Init();
DrawFrame();
while (1) {
if (AM2302_Read(&humidity, &temperature) == 0) // Display AM2302_Read sensor values via TFT
processValue(humidity, temperature); // Display AM2302_Read sensor values via TFT
else {
Clear_TFT_Error();
TFT_Write_Text("ERROR: Sensor not responding", 40, 160);
}
Delay_ms(2000); //Delay 2000ms
}
}