Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
sensor_dht11_dht22/main.c

===== Description =====

This demo shows how to communicate with the DHT11 and DHT22 1-Wire interface chip, using the nz_sensDht.h and nz_sensDht.c driver. It uses non-blocking (asynchronous) functions, meaning all processing is done in background interrupts and fibers.

By default it is configured for DHT22 sensors. This is configured via the SENS_DHT_TYPE define in the projdefs.h file for DHT11 or DHT22 sensor types.

It also flashes the system LED. To add DHT22 support to a project, the following must be done:

This demo can be used with any SBC66 Netcruzer board with an USB Port (not USB Host). It uses USB Commands and Debug Messages sent and received via the Netcruzer USB Terminal app to communicate with board.

Additionally this demo also implements debugging via the USB port and the Netcruzer USB Terminal App. During the program various debug messages are written, which will be displayed on the "Netcruzer USB Terminal" (running on a PC connected to the target board's USB port). This demo also monitors the USB port for Debug messages send to us.
- If "hi" is received, it replies with "Hello"
Use the "Netcruzer USB Terminal" App to send and receive USB Debug messages. It can be downloaded here: https://netcruzer.com/usbterminal/

===== Required Hardware =====

The project requires a SBC66 Netcruzer board with an USB Port (not USB Host), like the SBC66ZL for example. Additionally a DHT11 or DHT22 Sensor has to be connected to any of the SBC66 port pins.

===== Building Project =====

This project is located in the "src/demos/sensors/sensor_dht11_dht22" folder of the Netcruzer Download. To compile for Netcruzer Board, open this project in MPLAB X, and select the "Project Configuration" for desired board. For example "SBC66ZL_R1" for the SBC66ZL Revision 1 board. For details click here

===== Programming Board =====

After compiling (build), the board can be programmed via the USB Bootloader or a PIC Programmer. USB Programming is simplified when using the SBC board together with a Prototype Board.

===== File History =====

2014-02-10, David H. (DH):

#define THIS_IS_MAIN_FILE //Uniquely identifies this as the file with the main application entry function main()
// Includes /////////////////////////////////////
#include "HardwareProfile.h" //Required for all Netcruzer projects
#include "nz_sensDht.h"
#include "nz_helpersCx.h"
#include "nz_interrupt.h"
#include "nzos_fiber.h"
//Add debugging, and define debugging level. Is a DEBUG_LEVEL_xx define. Set to DEBUG_LEVEL_OFF to disable.
#define MY_DEBUG_LEVEL DEBUG_LEVEL_INFO
#include "nz_debug.h"
// Defines //////////////////////////////////////
// Variables ////////////////////////////////////
WORD tmrFlashLed; //Timer for flashing system LED
FIBER_TCB fbrTcbDHT22; //DHT22 Fiber TCB
void __attribute__((interrupt,no_auto_psv)) _CNInterrupt(void)
{
WORD utick15bit_8us;
intOnChangeClearIF(); //Clear "interrupt on change" IF (interrupt flag)
//If "Interrupt on Change" is configured for inputs other than DHT Sensors, a check will be
//required here to ensure a DHT sensor caused this interrupt! Not required for this example.
//NZ_INT_DIS_PUSH(); //Disable interrupts - not required if this ISR has a high priority!
utick15bit_8us = tick16Get_8us_noDisi(); //Get 8us time
//The sensDht_isr_MACRO() Macro must be called in interrupt. Will defer processing to Fiber
sensDht_isr_MACRO(utick15bit_8us);
//NZ_INT_EN_POP(); //Enable interrupt
//Schedule the DHT22 fiber (calls the sensDht_fbrTask() function) to run in lower interrupt priority.
nzFbrSchedule(&fbrTcbDHT22);
}
int main(void) {
BYTE i;
tmrFlashLed = tick16Get(); //Timer for flashing system LED
//Default initialization. All ports inputs. All analog features disabled. Tick 1ms
//Create the DHT Sensor fiber! This will be executed in a lower interrupt priority than the main
//DHT Sensor "interrupt on change" interrupt. The fiber will call the sensDht_fbrTask() function.
nzFbrCreate(2, TRUE, sensDht_fbrTask, &fbrTcbDHT22);
debugPutString("\nThis is a test debug message from sensor_dht11_dht22");
//Set System LED port as outputs. Setting a DIR_xxx bit to 1 set's corresponding port to input, and 0 as output
DIR_SYSLED = OUTPUT_PIN;
//DHT22 Initialization. First sensor is given with sensDht_init(). Use sensDht_addSensor() to add additional sensors.
sensDht_init(7, 3000, IOPORT_ID_NA, 0); //Initialize DHT22, using port 7 (old port name X7), startup delay 3 seconds
sensDht_addSensor(1, 8); //Add sensor on IO Port 8 (old port name Y8) (sensor index 1)
//Configure the "interrupt on change" for pin that has a DHT Sensor - Ports 7 & 8 (old port names X7 & X8)
//IMPORTANT!!! Use equal or higher priority than is used for System Tick = 4 by default.
#if (nzINT_PRIORITY_TICK >= 6)
#error "Ensure Tick priority is smaller or equal to Interrupt On Change priority"
#endif
intOnChangeConfig(CHANGE_INT_ON|CHANGE_INT_PRI_6); //Enable Interrupt on change at Int Priority level 6 (high)
intOnChangeClearIF(); //Clear "interrupt on change" IF (interrupt flag)
intOnChangeEnablePort(7); //Enable "interrupt on change" for each sensor
intOnChangeEnablePort(8); //Enable "interrupt on change" for each sensor
delay_ms(120); //Wait for DHT22 to settle
//Main loop
while(1)
{
nzSysTaskDefault(); //Main netcruzer task, call in main loop.
sensDht_task(); //DHT22 Task
//Check if new data available for any of the sensors
for(i=0; i<SENS_DHT_NUMBER_OF_SENSORS; i++) {
if (sensDht_hasNewTemperature(i)) {
sprintf(debugTempBuf, "\nT%d: %d.%d",
i,
sensDht_getTemperatureInteger(i),
sensDht_getTemperatureDecimal(i));
debugPutString(debugTempBuf);
}
if (sensDht_hasNewHumidity(i)) {
sprintf(debugTempBuf, "\nRH%d: %d.%d",
i,
sensDht_getHumidityInteger(i),
sensDht_getHumidityDecimal(i));
debugPutString(debugTempBuf);
}
}
if (tick16TestTmr(tmrFlashLed)) {
tick16UpdateTmrMS(tmrFlashLed, 500); //Update timer to expire in 500ms again
LAT_SYSLED = !LAT_SYSLED; //Toggle System LED
//Enable this code if SENS_DHT_AUTO_READ_PERIOD is 0. No auto reading!
//if (sensDht_isIdle() == TRUE) {
// sensDht_startRead();
//}
}
}//end while
}
#if defined(HAS_NZ_DEBUGGING)
void debugService(void) {
//Check if a debug message was received
if (cbufHasWholePacket(CIRBUF_RX_DEBUG)) {
//'hi' - Reply with "Hello" on a new line
if (cbufPacketStrcmp(CIRBUF_RX_DEBUG, "hi") == 0) {
debugPutString("\nHello");
}
//Add custom debug message processing
else if (cbufPacketStrcmp(CIRBUF_RX_DEBUG, "...") == 0) {
debugPutString("\nReceived unknown debug message");
//..... Do something
}
//Remove received packet. If it was not processed above, it is lost!
cbufRemovePacket(CIRBUF_RX_DEBUG);
}
}
#endif