在线时间428 小时
UID3006061
注册时间2015-3-23
NXP金币76
TA的每日心情 | 开心 2025-1-21 08:52 |
---|
签到天数: 861 天 连续签到: 1 天 [LV.10]以坛为家III
金牌会员
 
- 积分
- 5867
- 最后登录
- 2025-1-22
|
试着在mbed,用DHT11测试了一下温湿度,可能是lpc824的针太细了,插得不是很牢,显示的误差也特别大,附上代码
- #include "mbed.h"
- Serial pc(USBTX, USBRX);
- DigitalOut myled(LED1);
- //DigitalIn mybutton(USER_BUTTON); // Activate button
- DigitalInOut data_pin(A0); // Activate digital in/out (pio0_6)
- Timer tmr; //initialize timer
- uint64_t adat; // 64 bit variable for temporary data
- int i;
- void dht_read(void) {
- data_pin.output(); // Set A0 as output
- // Initialize measurement > 18 ms low
- data_pin = 0;
- wait_ms(20);
- // After high and release the pin switch input mode
- data_pin = 1;
- data_pin.input();
- // Wait until the end of 80 us low
- while(!data_pin.read()) {}
- // Wait until end of 80 us high
- while(data_pin.read()) {}
- // 40 bit, 40 read out cycle
- for(i=0; i<40; i++) {
- adat = adat << 1; // Shift for new number
- tmr.stop(); // Stop timer if runs
- tmr.reset(); // Reset timer
- // Wait until pin
- while(!data_pin.read()) {}
- tmr.start();
- while(data_pin.read()) {}
- // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us)
- if(tmr.read_us() > 40) {
- // bit is 1
- adat++;
- }
- }
- }
- int main() {
-
-
-
- pc.printf("Read the DHT11 temperature and humidity sensor!\r\n"); //Welcome message
- while(1) {
-
- adat = 0;
- myled = !myled; // LED is ON
- dht_read(); // Call the function
- // Send result through UART result
- pc.printf("Humidity: ");
- pc.printf("%d", (adat & 0x000000ff00000000) >> 32); // Humidity
- pc.printf("\n\r"); // Send a new line and carriage return.
- pc.printf("Temperature: ");
- pc.printf("%d", (adat & 0x0000000000ff0000) >> 16 ); // Temperature
- pc.printf("\n\r");
- pc.printf("Checksum: ");
- pc.printf("%d", adat & 0x00000000000000ff); // Checksum.
- pc.printf("\n\r");
- //wait(2); // Wait 2 sec till continue.
-
-
- }
- }
复制代码
|
-
编译后下载,依然不能超过18k
-
串口返回的信息,跳动比较大,接触不良
|