完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
DHT11.c
DHT11.c如下: #include #define uint unsigned int #define uchar unsigned char #define wenshi P0_6 uchar ucharFLAG, uchartemp; uchar shidu_shi, shidu_ge, wendu_shi, wendu_ge = 4; uchar ucharT_data_H, ucharT_data_L, ucharRH_data_H, ucharRH_data_L, ucharcheckdata; uchar ucharT_data_H_temp, ucharT_data_L_temp, ucharRH_data_H_temp, ucharRH_data_L_temp, ucharcheckdata_temp; uchar ucharcomdata; uchar temp[2] = {0, 0}; uchar temp1[5] = "temp="; uchar humidity[2] = {0, 0}; uchar humidity1[9] = "humidity="; void Delay_us() { /* 1us延时 */ asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); asm ( "nop" ); } void Delay_10us() { /* 10us延时 */ Delay_us(); Delay_us(); Delay_us(); Delay_us(); Delay_us(); Delay_us(); Delay_us(); Delay_us(); Delay_us(); Delay_us(); } void Delay_ms ( uint Time ) { /* 延时n毫秒 */ unsigned char i; while ( Time-- ) { for ( i = 0; i < 100; i++ ) { Delay_10us(); } } } void COM ( void ) { uchar i; for ( i = 0; i < 8; i++ ) { ucharFLAG = 2; while ( ( !wenshi ) && ucharFLAG++ ); Delay_10us(); Delay_10us(); Delay_10us(); uchartemp = 0; if ( wenshi ) { uchartemp = 1; } ucharFLAG = 2; while ( ( wenshi ) && ucharFLAG++ ); if ( ucharFLAG == 1 ) { break; } ucharcomdata <<= 1; ucharcomdata |= uchartemp; } } void DHT11 ( void ) { /* 温湿传感器启动 */ wenshi = 0; Delay_ms ( 19 ); /* > 18ms */ wenshi = 1; P0DIR &= ~0x40; /* 重新配置IO口方向 */ Delay_10us(); Delay_10us(); Delay_10us(); Delay_10us(); if ( !wenshi ) { ucharFLAG = 2; while ( ( !wenshi ) && ucharFLAG++ ); ucharFLAG = 2; while ( ( wenshi ) && ucharFLAG++ ); COM(); ucharRH_data_H_temp = ucharcomdata; COM(); ucharRH_data_L_temp = ucharcomdata; COM(); ucharT_data_H_temp = ucharcomdata; COM(); ucharT_data_L_temp = ucharcomdata; COM(); ucharcheckdata_temp = ucharcomdata; wenshi = 1; uchartemp = ( ucharT_data_H_temp + ucharT_data_L_temp + ucharRH_data_H_temp + ucharRH_data_L_temp ); if ( uchartemp == ucharcheckdata_temp ) { ucharRH_data_H = ucharRH_data_H_temp; ucharRH_data_L = ucharRH_data_L_temp; ucharT_data_H = ucharT_data_H_temp; ucharT_data_L = ucharT_data_L_temp; ucharcheckdata = ucharcheckdata_temp; } wendu_shi = ucharT_data_H / 10; wendu_ge = ucharT_data_H % 10; shidu_shi = ucharRH_data_H / 10; shidu_ge = ucharRH_data_H % 10; } else { /* 没有成功读取,返回0 */ wendu_shi = 0; wendu_ge = 0; shidu_shi = 0; shidu_ge = 0; } } DHT11.h DHT11.h如下: #ifndef __DHT11_H__ #define __DHT11_H__ #define uchar unsigned char extern void Delay_ms ( unsigned int xms ); /* 延时函数 */ extern void COM ( void ); /* 温湿写入 */ extern void DHT11 ( void ); /* 温湿传感启动 */ extern uchar temp[2]; extern uchar temp1[5]; extern uchar humidity[2]; extern uchar humidity1[9]; extern uchar shidu_shi, shidu_ge, wendu_shi, wendu_ge; #endif UART.c UART.c如下: #include #define uchar unsigned char #define uint unsigned int void InitUart(); /* 初始化串口 */ void Uart_Send_String ( unsigned char *Data, int len ); void InitUart() { CLKCONCMD &= ~0x40; /* 设置系统时钟源为32MHZ晶振 */ while ( CLKCONSTA & 0x40 ); /* 等待晶振稳定 */ CLKCONCMD &= ~0x47; /* 设置系统主时钟频率为32MHZ */ PERCFG = 0x00; /* 位置1 P0口 */ P0SEL = 0x3c; /* P0_2、P0_3、P0_4、P0_5用作串口,第二功能 */ P2DIR &= ~0XC0; /* P0优先作为UART0,优先级 */ U0CSR |= 0x80; /* UART方式 */ U0GCR |= 11; /* U0GCR与U0BAUD配合 */ U0BAUD |= 216; /* 波特率设为115200 */ UTX0IF = 0; /* UART0的TX中断标志初始置位0 */ } void Uart_Send_String ( uchar *Data, int len ) { int j; for ( j = 0; j < len; j++ ) { U0DBUF = *Data++; while ( UTX0IF == 0 ); UTX0IF = 0; } } UART.h UART.h如下: #ifndef __UART_H__ #define __UART_H__ extern void InitUart(); extern void Uart_Send_String ( unsigned char *Data, int len ); #endif main.c main.c如下: #include #include #include "UART.h" #include "DHT11.h" void main ( void ) { Delay_ms ( 1000 ); /* 让设备稳定 */ InitUart(); /* 串口初始化 */ while ( 1 ) { DHT11(); /* 获取温湿度 */ P0DIR |= 0x40; /* IO口需要重新配置 */ /*----- 温湿度的ASCII码转换 -----*/ temp[0] = wendu_shi + 0x30; temp[1] = wendu_ge + 0x30; humidity[0] = shidu_shi + 0x30; humidity[1] = shidu_ge + 0x30; /*------- 信息通过串口打印 --------*/ Uart_Send_String ( temp1, 5 ); Uart_Send_String ( temp, 2 ); Uart_Send_String ( "n", 1 ); Uart_Send_String ( humidity1, 9 ); Uart_Send_String ( humidity, 2 ); Uart_Send_String ( "n", 1 ); Delay_ms ( 2000 ); /* 延时,使周期性2S读取1次 */ } } |
|
|
|
只有小组成员才能发言,加入小组>>
3320 浏览 9 评论
3000 浏览 16 评论
3496 浏览 1 评论
9069 浏览 16 评论
4089 浏览 18 评论
1190浏览 3评论
612浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
603浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2339浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1899浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-27 12:14 , Processed in 1.944043 second(s), Total 78, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (威廉希尔官方网站 图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号