このページはEtoJ逐語翻訳フィルタによって翻訳生成されました。

翻訳前ページへ


転換 of program to 除去する 前進するd 命令(する)s - ar3ne1humDHT11two HOME  > >  ARDUINO MAIN PAGE > >  HINTS AND HOW-TOs > >  NuELEC DATALOG  t.o.c.     Delicious Bookmark this on Delicious   Recommend to StumbleUpon

Nuelectronics DHT11 reading code:

転換 to use simpler coding


At July 2010, the デモ code for reading a DHT11 which was 供給(する)d by nuelectronics.com used some 前進するd techniques. It made it hard for me to see what was going on. I also would structure a program 異なって than the デモ code was structured, and so I 始める,決める about trying to create a functionally 同等(の) program.

Now: I hadn't worked with DHT11's 以前. There may have been 推論する/理由s the デモ was done the way it was. However, I can tell you that my code does work, at least some of the time. その上に, I don't really see any 推論する/理由 it wouldn't work. I don't think I'm riding a wave of "luck". And my code 許すs you to attach the sensor to any 解放する/自由な 数字表示式の I/O line.

Why read the 残り/休憩(する) of this? If you are an Arduino programmer, you may find it 利益/興味ing. There's not a lot here for other readers.

The re-令状

I started by moving things around, leaving the actual code used pretty much as before.

I 手配中の,お尋ね者 the code for initializing the ソフトウェア in a 選び出す/独身 機能(する)/行事. (The 金物類/武器類 doesn't need any initializing.) And I 手配中の,お尋ね者 a 選び出す/独身 call to 原因(となる) the sensor to 報告(する)/憶測 the 現在の 条件s.

Once those two 仕事s were in 機能(する)/行事s, then half the work of making a library for them was done. And 使用者s of my code could just "削減(する) and paste" the 詳細(に述べる)s without needing to look at them. 使用者s just had to know what to call.

I've used 全世界の variables to 持つ/拘留する things passed 支援する from the 機能(する)/行事s. Nothing needs to be passed to the 機能(する)/行事s, although the pin the sensor is on is 示すd by the value of a 全世界の "#define". These are "bad" techniques, and better answers 存在する.... but doing things this way makes the code accessible to novices.

The 全世界の variables are: array dht_dat[0-4]. (存在するd before, as dht11_dat[0-5], but not as 全世界の, and element 5 was spare) and bGlobalErr (0 is returned if no error seen).

いつかs, while in one of the "黒人/ボイコット box" parts of the code, an error will be (悪事,秘密などを)発見するd. Rather than 報告(する)/憶測ing it there and then, I have elected to 始める,決める an error 旗. What 報告(する)/憶測s are 生成するd when errors occur can then be decided by someone using the ソフトウェア, without them having to make any changes in the "黒人/ボイコット box" 地域s of the code. I hope you can see that working in the code in the "宙返り飛行()" 機能(する)/行事. (What's there is NOT in the "黒人/ボイコット box" 地域... but the call to readDHT() takes the program off to the "黒人/ボイコット box", from whence it should 敏速に return.

Re-codings

This essay is going to get あらましの here... sorry. I just don't 推定する/予想する many people to read this, and even より小数の to try any of my shareware. The essay will still not be 価値(がある) いっそう少なく than you are 支払う/賃金ing me for it!

There will be a number of 封鎖するs which will start with the old code, and the code that 取って代わるd it. In some instances, there will be comments to go with the "old" and "new".

Variable 指名するs: These may... 変化させる! The デモ used "DHT11" as part of 指名するs. The code also 作品 for DHT22 sensors, I believe, so I dropped the "11"... but it may still be 現在の in some places.

Direct Port 接近: The 半導体素子 at the heart of the Arduino has three "ports", A, B and C. When we use a 数字表示式の I/O line, we are using one or another of these ports. While we stick with the lovely high level things like pinMode(12,INPUT), we don't need to 関心 ourselves with these ports, nor with how to interact with them. The デモ code made 広範囲にわたる use of direct port 接近.

There's a lot about direct port 接近 in the Arduino 言及/関連 pages, online.

The に引き続いて are usually used in 関係 with 接近ing the underlying ports. All of the words written all in caps are "known" to the Arduino 開発 環境.

Each 登録(する) can 持つ/拘留する a number. It is best to think of that number in binary form. Let's, for instance, consider that DDRB is 持つ/拘留するing what we would usually call "six". In binary, that would be 00000110. (The port has a particular width, which I'm not looking up just now. If it is not 8 bits wide, just 追加する some 無s to the left of the number I wrote out.) Each bit 支配(する)/統制するs the direction of data though a 選び出す/独身 数字表示式の pin. You can look up the mapping if you need it, but if DDRB held 00000110, then most of the pins would be 生産(高)s (or inputs... I'm not looking up what "1" says, either), and two would be for passing data the other way.

When we use pinMode(), we are setting or (疑いを)晴らすing (making 1, or making 無) one of the bits in a data direction 登録(する). Using pinMode() is a LOT easier than messing about with three DDR's, and finding the 権利 position for a given pin! But the pin's 機能(する)/行事s were 始める,決める in the デモ code using direct 巧みな操作 of the DDRs.

Now that you know all(!) about DDRs, what about the other two? The values you make the bits of the port 生産(高) 登録(する)s 決定する the 明言する/公表する of the associated pins... if they are 現在/一般に configured as 生産(高)s.

When a pin is configured (by the value in the DDR) for input, then the corresponding bit of the PIN- Port INput- 登録(する) will be 1 or 0 based on the voltage the 半導体素子 is "seeing" on the corresponding input line.

THAT's what you need to know about the ports and their 登録(する)s.

Bit Bashing

Besides knowing about PORTC and DDRC, you need to know something about bit bashing.

Abandon all knowledge of the meaning of "AND" and "OR".

In the 残り/休憩(する) of this essay, we are going to be talking about bitwise 論理(学)の ANDing and ORing.

For much of this, think in binary 条件.

Just for a moment go 支援する to "normal" numbers. The に引き続いて should 持つ/拘留する no 恐れるs....

2
2 ADD
=====
4

I'm just 明言する/公表するing a fact: If you 追加する two and two, you get four.

Here's another fact. If you do a bitwise OR between the binary numbers 00011 and 10001, you get 10011. Written as follows, the 支配する should be 平易な enough to understand....

00011
10001 bitwise OR
=====
10011

The bitwise AND 作品 like this....

00011
10001 bitwise AND
=====
00001

Who CARES??

I hope you do! 令状ing this is taking time!

Bitwise ORs and bitwise ANDs are very useful when 取引,協定ing with 登録(する)s. You can't usually 直接/まっすぐに turn just one bit on or off, or 決定する the 明言する/公表する of just one bit. But, if you are 利益/興味d, say, in the middle bit, you CAN do the に引き続いて. In each 事例/患者, the number on the 最高の,を越す is "無作為の", and the number on the second line is the 権利 mask to use if you want to know something about the middle bit, or make it a particular value....

To find out if the middle bit of the number on the 最高の,を越す is a one or a 無, 関わりなく what else is 現在の....

00011                  00111
00100 bitwise AND      00100
=====                  =====
00000 (無)           00100 (not 無)

To MAKE the middle bit a "1", leaving the others as they were....

00011
00100 bitwise OR
=====
00111 (無)

To MAKE the middle bit a "0", leaving the others as they were....

00111
11011 bitwise AND
=====
00011 (無)

There are 類似の tricks you do for other needs of this sort. Many of the above arise in the "tricky" code I 変えるd the program for the 湿度 sensor from.

Simple 転換

If you do x=_BV(4), then you will put 16 in x. The "_" isn't special... it is just part of the 指名する of the "_BV" 機能(する)/行事. _BV(0) returns 1, _BV(1) returns 2,_BV(2) returns 4, and so on. You start with 1, and 二塁打 it as many times as called for. This always results in a binary number consisting of a 選び出す/独身 "1" and lots of 無s.

Examples

At last we are ready to look at some of the code from the デモ, and what 取って代わるd it:

DDRC |= _BV(DHT11_PIN);//初めの
pinMode(dht_dpin,OUTPUT);//交替/補充

What was in DHT11_PIN and in dht_dpin was not やめる the same. If the former was 0, the latter would have had to be 14 to have the new 命令(する) do what the former did. This was because the 言及/関連 base changed. In the old code, we were talking about which bit of PORTC we 手配中の,お尋ね者 to use. The Arduino analog pins, 0-5, are on PORTC, bits 0-5 それぞれ. The numbering of the 数字表示式の pins, and pinMode(), are more clever. We can use 0-19, and the system takes care of finding the 権利 bit in the 権利 port.

DDRC |=_BV(DHT11_PIN), when DHT11_PIN is 持つ/拘留するing 0 is 同等(の) to...

Take what's in DDRC
Do a bitwise OR between that and 00001
Put the result in DDRC

The Arduino 言及/関連 has more on ports and on bit bashing.

The (警察などへの)密告,告訴(状) on _BV() (機の)カム from UrbanHonking.

More Bit Bashing / Port 巧みな操作

Now that I've taken you by the 手渡す through a few, I will not explain the 残り/休憩(する) in as much 詳細(に述べる). Other instances of 取って代わる code are as follows:

=====

PORTC &= ~_BV(DHT11_PIN);//初めの
digitalWrite(dht_dpin,LOW);//交替/補充

The tilde (~) is the bitwise NOT 操作者. The ampersand (&) says "bitwise AND". (Don't 混乱させる &- bitwise AND with && 論理(学)の AND... a different 概念). If _BV(DH11_PIN) was 000001, then the bitwise NOT of that is 111110. The 表現 above, in that 事例/患者, boils 負かす/撃墜する to...

Take what's in PORTC
Do a bitwise AND between that and 111110
Put the result in PORTC

=====

DDRC &= ~_BV(DHT11_PIN);//初めの
pinMode(dht_dpin,INPUT);//交替/補充

=====

dht11_in = PINC & _BV(DHT11_PIN);//初めの
dht_in=digitalRead(dht_pin);//交替/補充

Here we are using a bitwise AND of a number with just one "1" and the contents of one of the input 登録(する)s. It tells us the 明言する/公表する of one input, if that pin is 現在/一般に configured for input.

=====

while(!(PINC & _BV(DHT11_PIN)));//初めの
while(digitalRead(dht_dpin)==LOW);//交替/補充

=====

if(PINC & _BV(DHT11_PIN))//初めの
if(digitalRead(dht_dpin)==HIGH)//交替/補充

=====

while((PINC & _BV(DHT11_PIN)));//初めの
while(digitalRead(dht_dpin)==HIGH);//交替/補充

Building binary numbers, one bit at a time

The に引き続いて....

if(PINC & _BV(DHT11_PIN))//If pin still high
	result |=(1<<(7-i));

... bit of code builds, or 蓄積するs, a value in a variable. The 支配する of that "if" 条項 is going to be, using T for "true" and F for "誤った", "TTFT" if the 残り/休憩(する) of the code is working its way though the binary number 1101, which in our ordinary language is a way of 令状ing 13.

As the 死刑執行 of the 宙返り飛行 that code is in 進歩s, "result" will 持つ/拘留する:

 0
 1
 3
 6
12
13

Use Google to 研究 "binary to decimal 転換" if you want to know more.

The "<<" 操作者 is covered in the 延長するd Arduino 言及/関連 documentation, under "bitwise 操作者s".

It's been nice "talking"

So there you have it! You may have learned about some things you didn't 以前 know. You may have a better idea of how the デモ code from nuelectronics for the DHT11 and 類似の 湿度 sensors worked, and the 関係 between that and my code for reading the sensors.

Most of my tutorials are more carefully (手先の)技術d. If you find any worthy, please click the StumbleUpon link at the 最高の,を越す of the page. If you want to reward my (かなりの) 成果/努力, and you are using Windows, please collect some of my freeware, and spread it around.





See Also: The Arduino programming course from Sheepdog Guides:

その上の to the Arduino ideas the page you are reading now will take you to, I have 地位,任命するd a 一連の essays which try to help you become a better Arduino programmer and engineer... but, for the best result, you will have to buckle 負かす/撃墜する and work your way through them in sequence. The "How To's" here can be 接近d in whatever order you like.


Feel 解放する/自由な to use this (警察などへの)密告,告訴(状) in programming courses, etc, but a credit of the source would be 高く評価する/(相場などが)上がるd. If you 簡単に copy the pages to other web pages you will do your readers a disservice: Your copies won't stay 現在の. Far better to link to these pages, and then your readers see up-to-date 見解/翻訳/版s. For those who care- thank you- I have 地位,任命するd a page with more (警察などへの)密告,告訴(状) on what copyright 権利放棄s I 延長する, and suggestions for those who wish to put this 構成要素 on CDs, etc.





編集(者)の Philosophy

See the discussion 近づく the 底(に届く) of the "最高の,を越す level" page covering the 本体,大部分/ばら積みの of my Arduino 出資/貢献s. There is (警察などへの)密告,告訴(状) there, too, about things like "May I copy your 構成要素?", and the system of とじ込み/提出する 指名するs I am trying to work to.


   Search this 場所/位置 or the web        力/強力にするd by FreeFind
 
  場所/位置 search Web search
場所/位置 地図/計画する    What's New    Search

The search engine is not intelligent. It 単に 捜し出すs the words you 明示する. It will not do anything sensible with "What does the 'could not 収集する' error mean?" It will just return 言及/関連s to pages with "what", "does", "could", "not".... etc.
In 新規加入 to the (警察などへの)密告,告訴(状) about the nuelectronics data 保護物,者 of which this page is part, I have other 場所/位置s with 構成要素 you might find useful.....

Tutorials about the 解放する/自由な database which is part of the 解放する/自由な Open Office.
Sequenced 始める,決める of tutorials on Pascal programming and electronics interfacing.
Some pages for programmers.
Using the 平行の port of a Windows computer.

If you visit 1&1's 場所/位置 from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service... although I was いっそう少なく than pleased the other day to have what I was doing interrupted by a telephone call from their sales team, trying to get me to 延長する my 関与. Sigh. Hardly a rare event, but I'd thought 1&1 were a bit classier that some of the people who have my telephone number.



広告 from page's editor: Yes.. I do enjoy 収集するing these things for you... hope they are helpful. However.. this doesn't 支払う/賃金 my 法案s!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and 循環させる it for me? Links on your page to this page would also be 高く評価する/(相場などが)上がるd!

Click here to visit editor's Sheepdog ソフトウェア (tm) freeware, shareware pages.


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

Click here to visit the homepage of my biggest 場所/位置.

Click here to visit the homepage of Sheepdogsoftware.co.uk. 陳謝s if the "?Frmar3ne1humDHT11two" I 追加するd to that link 原因(となる)s your browser problems. Please let me know, if so?

Click here to visit editor's pages about using computers in Sensing and 支配(する)/統制する, e.g. 天候 logging.



To email this page's editor, Tom Boyd.... Editor's email 演説(する)/住所. Suggestions welcomed!


Valid HTML 4.01 Transitional Page 実験(する)d for 同意/服従 with INDUSTRY (not MS-only) 基準s, using the 解放する/自由な, 公然と accessible validator at validator.w3.org


Why does this page 原因(となる) a script to run? Because of the Google パネル盤s, and the code for the search button. Why do I について言及する the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....