你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

查看: 2478|回复: 14

[STM32F042 NucLeo] Mbed+DHT11温湿度

[复制链接]

6

主题

1026

回帖

7

蝴蝶豆

金牌会员

最后登录
2020-11-25
发表于 2016-12-5 12:17:27 | 显示全部楼层 |阅读模式
正好手头上有个DHT11温湿度传感器,在Mbed下初步试着用了一下,代码相当简单
  1. include "mbed.h"

  2. //------------------------------------
  3. // Hyperterminal configuration
  4. // 115200 bauds, 8-bit data, no parity
  5. //------------------------------------

  6. Serial pc(SERIAL_TX, SERIAL_RX);
  7. DigitalOut myled(LED1);


  8. DigitalInOut data_pin(A0); // Activate digital in as dht11 Pin

  9. Timer tmr; //initialize timer
  10. uint64_t adat; // 64 bit variable for temporary data
  11. int i;

  12. void dht_read(void) {
  13.     data_pin.output(); // Set A0 as output
  14.     // Initialize measurement > 18 ms low
  15.     data_pin = 0;
  16.     wait_ms(20);
  17.     // After high and release the pin switch input mode
  18.     data_pin = 1;
  19.     data_pin.input();
  20.     // Wait until the end of 80 us low
  21.     while(!data_pin.read()) {}
  22.     // Wait until end of 80 us high
  23.     while(data_pin.read()) {}
  24.     // 40 bit, 40 read out cycle
  25.     for(i=0; i<40; i++) {
  26.         adat = adat << 1; // Shift for new number
  27.         tmr.stop(); // Stop timer if runs
  28.         tmr.reset();  // Reset timer
  29.         // Wait until pin
  30.         while(!data_pin.read()) {}         
  31.         tmr.start();            
  32.         while(data_pin.read()) {}
  33.         // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us)
  34.         if(tmr.read_us() > 40) {
  35.             // bit is 1
  36.             adat++;
  37.         }
  38.     }
  39. }

  40. int main() {
  41.     pc.printf("Hello World !\r\n");
  42.     pc.baud(115200);

  43.      pc.printf("Read the DHT11 temperature and humidity sensor!\r\n"); //Welcome message
  44.     while(1) {
  45.          

  46.             adat = 0;
  47.             myled = !myled; // LED is ON/OFF
  48.             dht_read(); // Call the function
  49.             // Send result through UART result
  50.             pc.printf("Humidity: ");
  51.             pc.printf("%d", (adat  & 0x000000ff00000000) >> 32); // Humidity
  52.             pc.printf("\n\r"); // Send a new line and carriage return.
  53.              pc.printf("Temperature: ");
  54.             pc.printf("%d", (adat & 0x0000000000ff0000) >> 16 ); // Temperature
  55.             pc.printf("\n\r");
  56.             pc.printf("Checksum: ");
  57.             pc.printf("%d", adat & 0x00000000000000ff); // Checksum.
  58.             pc.printf("\n\r");
  59.             wait(2); // Wait 2 sec till continue.

  60.         
  61.     }
  62. }
复制代码


UART连接PA_2,PA_3,显示当前温度和湿度

UART连接PA_2,PA_3,显示当前温度和湿度

接线比较乱

接线比较乱

评分

参与人数 1ST金币 +10 收起 理由
zero99 + 10

查看全部评分

<
回复

使用道具 举报

8

主题

130

回帖

0

蝴蝶豆

金牌会员

最后登录
2020-3-22
发表于 2016-12-5 13:00:16 | 显示全部楼层
看贴回帖     
回复 支持 反对

使用道具 举报

10

主题

693

回帖

0

蝴蝶豆

金牌会员

最后登录
2018-6-15
发表于 2016-12-5 13:31:37 | 显示全部楼层
谢谢分享              
回复 支持 反对

使用道具 举报

3

主题

1377

回帖

0

蝴蝶豆

论坛元老

最后登录
2020-12-9
发表于 2016-12-6 09:44:15 | 显示全部楼层
谢谢分享
回复 支持 反对

使用道具 举报

6

主题

1026

回帖

7

蝴蝶豆

金牌会员

最后登录
2020-11-25
 楼主| 发表于 2016-12-6 16:17:44 | 显示全部楼层
谢谢各位支持
回复 支持 反对

使用道具 举报

1182

主题

3785

回帖

1

蝴蝶豆

论坛元老

最后登录
2020-3-17
发表于 2016-12-19 16:28:15 | 显示全部楼层
感谢分享,这个板块之前关注少了
回复 支持 反对

使用道具 举报

6

主题

1026

回帖

7

蝴蝶豆

金牌会员

最后登录
2020-11-25
 楼主| 发表于 2016-12-19 22:29:41 | 显示全部楼层
zero99 发表于 2016-12-19 16:28
感谢分享,这个板块之前关注少了

谢谢鼓励
回复 支持 反对

使用道具 举报

35

主题

1405

回帖

22

蝴蝶豆

金牌会员

最后登录
2020-12-3
发表于 2016-12-30 11:39:21 | 显示全部楼层
感谢分享
回复 支持 反对

使用道具 举报

3

主题

143

回帖

0

蝴蝶豆

中级会员

最后登录
2018-2-5
发表于 2016-12-30 15:39:30 | 显示全部楼层
学习中学习中
回复 支持 反对

使用道具 举报

2

主题

224

回帖

0

蝴蝶豆

高级会员

最后登录
2020-9-5
发表于 2017-1-2 09:00:15 | 显示全部楼层
看起来是是简单一些,底层初始化驱动基本都省去了!
回复 支持 反对

使用道具 举报

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版