I'm trying to read temperature form DHT22 using Raspbian patched with xenomai real time system. I'm following steps of datasheet page 4 using gpio4. . the circuit is correctly wired(I can read with existing codes). but I get false result (temperature about 80000).
my question is, since xenomai is hard real time, did it's sleep
function take really the given amount of time???.
the max latency is about 30us (tested in parallel with dd
command which take 80% of processor. (/usr/xenomai/bin/latency
program)
or did the datasheet not specify exactly the communication (signal) steps???
#define TASK_PRIO 99 /* Highest RT priority */
#define TASK_MODE 0 /* No flags */
#define TASK_STKSZ 0 /* Stack size (use default one) */
#define SIGNALS (0x1|0x4) /* Signals to send */
...
void read(void *cookie) {
readDHT();
}
... main
int err;
mlockall(MCL_CURRENT | MCL_FUTURE);
err = rt_task_create(&task_desc, "MakoudaTask",
TASK_STKSZ,
TASK_PRIO,
TASK_MODE);
if (!err)
rt_task_start(&task_desc, &read, NULL);
if (err) {
printf(" cant reate task\n");
return 0;
}
readDHT22.C
uint64_t readDHT(int pin) {
// Set GPIO pin to output
GPIO_DIR_OUTPUT(pin); // set pin output
GPIO_CLEAR_PIN(pin); // Set low
rt_task_sleep(5000000); // DHT22 needs min 1-10 ms to signal a startup
GPIO_SET_PIN(pin); // Take pin high
rt_task_sleep(40000); //sleep 40 us
/*------------------sensor response --------------------*/
GPIO_DIR_INPUT(pin); // set pin input
rt_task_sleep(80000); //sensor pull low 80 us
rt_task_sleep(100000); // sensor pull up 80 us
// while(GPIO_READ_PIN(pin)); // wait till sensor pull down (ready to transmit data)
/*--------------------- begin transmit data ------------*/
uint64_t measure = 0; // store the 40 bits
uint8_t i;
// start to read 40 bits
for (i = 0; i < 40; i++) {
while(GPIO_READ_PIN(pin) == 0) // wait till sensor pull up (ready to transmit bit)
rt_task_sleep(1); //sleep 1 ns
rt_task_sleep(40000); //wait 40us if still high then bit = 1 else bit = 0 because output=1 still 70 us
if (GPIO_READ_PIN(pin))
{
measure = measure | (1 << i);
while(GPIO_READ_PIN(pin)) //wait sensor pull down
rt_task_sleep(1000);
}
}
return measure;
}
rt_task_sleep
really sleep exactly this amount of time (more accurate then native sleep??) since I followed the signal steps in datasheet and got false results and I read that xenomai is hard real time – Makhlouf GHARBI Sep 30 '14 at 12:48