[ell-i-developers] ELL-i-Arduino Delay functions and interrupt disable

  • From: Félix Halcartegaray V. <felix@xxxxxxxx>
  • To: ell-i-developers@xxxxxxxxxxxxx
  • Date: Thu, 20 Feb 2014 22:26:20 +0200

Hello,

I was testing the temperature and humidity sensor DHT22:
https://github.com/Ell-i/Hackathon/wiki/Components%20DHT22%20Humidity%20sensor

and in the current version of the Arduino ELL-i environment it is not
working. I tested the same sensor and code with an Arduino Mega and
works fine, so is not the sensor nor the Arduino code.

Checking the code of the DHT library for Arduino, I find out two things:
1) The temperature sensor sends data on a very critical timing, doing
highs and lows in a few microseconds (I checked with an oscilloscope)
and the code needs to capture all the transitions to calculate the
values correctly, so it uses the function delayMicroseconds() from
Arduino to read fast but not continuously, and this function is not be
tested in our environment according to Pekka. Can someone test if this
function is doing what it should? Probably the problem is here.
Another delay that was mentioned somewhere in Arduino documentation (
http://playground.arduino.cc/Main/AVR ) is to use an Assembler
version:
__asm__("nop\n\t");
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");  \\ gang them up like this

I don´t know if that works in our environment, if it does, please tell
me how many nanoseconds each nop should take. They say that one cycle
at 16 Mhz is 62.5 nanoseconds. I don´t know what is the frequency of
our processor at the moment, so it would be interesting to know.
Anyways, still is not a very good solution as it totally depends on
the speed of the processor, and that is probably not a good idea but
still is useful to test the sensor. The delayMicroseconds function is
better as it should be already adapted for the correct processor. Any
other ideas to do a microsecond delay would be great, at least to have
the sensor working.

2) The code also calls cli() and sei() that are AVR calls for Atmel to
suspend and resume interrupts, although is not important at the moment
if you are only reading temperature as it works fine. I commented them
out and the code still works fine and reads temperature and humidity
(testing on Arduino Mega). Anyways, I changed the function call to
noInterrupts() and interrupts() that is a more portable version
(defined directly as cli and sei in arduino.h for AVR) and that also
compiles in our ELL-i environment, though I don't know if it does
disable and enable interrupts as it should. If someone can confirm
this, I would appreciate it.

Thanks,
Felix.

Other related posts: