您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 薪酬管理 > dht11在stc15程序
上面为项目文件结构,使用STC15F104E单片机在6MHz时钟频率调试成功,因为dht11对软件延时精度要求比较高,以上程序的软件延时是通过逻辑分析仪得出,不能移植与其他单片机,只能用于stc12c/stc11使用6MHz晶振或者其它晶振使用分频产生6MHz主时钟,或者使用stc15f10X系列无晶振单片机把主时钟调整为6MHz.源程序1…………………………dht11.c#includeSTC12.h#include5110.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharsbitio=P3^4;//DHT11数据端uchardata_byte;ucharHH,HL,TH,TL;voiddelay(ucharz)//10ms@6Mhz{unsignedchari,j,k;for(k=0;kz;k++){_nop_();_nop_();i=59;j=89;do{while(--j);}while(--i);}}voiddelay1()//延时5us@6Mhz{_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}voidDelay2()//@6.000MHz200us{unsignedchari,j;_nop_();_nop_();i=2;j=38;do{while(--j);}while(--i);}voidstart()//开始信号{io=1;delay(5);io=0;delay(2);io=1;}ucharreceive_byte(){uchari,count;for(i=0;i8;){while(!io)count=0;while(io){count++;delay1();}if((count13)&&(count17)){data_byte=1;data_byte|=0x01;++i;}if((count3)&&(count7)){data_byte=1;++i;}}returndata_byte;}voidreceive(){ucharT_H,T_L,H_H,H_L,check,num_check;start();//开始信号while(io);//读取DHT11响应信号{Delay2();H_H=receive_byte();H_L=receive_byte();T_H=receive_byte();T_L=receive_byte();check=receive_byte();io=1;num_check=H_H+H_L+T_H+T_L;if(num_check==check)//校验数据{HH=H_H;HL=H_L;TH=T_H;TL=T_L;check=num_check;}}}main(){ucharg;delay(50);for(g=0;g10;g++)//输出延时波形用于调整软件延时{io=!io;delay1();}LCD_init();//初始化LCD模块LCD_clear();//清屏幕HH=0;TH=0;HL=0;TL=0;LCD_write_hanzi(0,0,0);//初始化屏幕LCD_write_hanzi(2,0,1);LCD_write_hanzi(0,2,2);LCD_write_hanzi(2,2,3);while(1){receive();LCD5110ShowNum(4,0,TH);//显示温度LCD5110ShowNum(4,2,HH);//显示数据delay(255);HH=0;TH=0;HL=0;TL=0;}}源程序2…………………………5110-51.c#includeSTC12.h#include5110.h#includeziku.hsbitsce=P3^0;//片选sbitres=P3^5;//复位,0复位sbitdc=P3^1;//1写数据,0写指令sbitsdin=P3^2;//数据sbitsclk=P3^3;//时钟//sbitio=P3^4;//DHT11数据端/*--------------------------------------------LCD_write_byte:使用SPI接口写数据到LCD输入参数:dt:写入的数据;command:写数据/命令选择;编写日期:20130918----------------------------------------------*/voidLCD_write_byte(unsignedchardt,unsignedcharcommand){unsignedchari;sce=0;dc=command;for(i=0;i8;i++){if(dt&0x80)sdin=1;elsesdin=0;dt=dt1;sclk=0;sclk=1;}dc=1;sce=1;sdin=1;}/*---------------------------------------LCD_init:3310LCD初始化编写日期:20080918-----------------------------------------*/voidLCD_init(void){res=0;delay_1ms();res=1;LCD_write_byte(0x21,0);//初始化Lcd,功能设定使用扩充指令LCD_write_byte(0xC0,0);//设定液晶偏置电压LCD_write_byte(0x06,0);LCD_write_byte(0x13,0);LCD_write_byte(0x20,0);//使用基本指令LCD_write_byte(0x0C,0);//设定显示模式,正常显示}/*-------------------------------------------LCD_set_XY:设置LCD坐标函数输入参数:X:0-83Y:0-5编写日期:20130918---------------------------------------------*/voidLCD_set_XY(unsignedcharX,unsignedcharY){LCD_write_byte(0x40|Y,0);//columnLCD_write_byte(0x80|X,0);//row}/*------------------------------------------LCD_clear:LCD清屏函数编写日期:20130918--------------------------------------------*/voidLCD_clear(void){unsignedchart;unsignedchark;LCD_set_XY(0,0);for(t=0;t6;t++){for(k=0;k84;k++){LCD_write_byte(0x00,1);}}}/*---------------------------------------------LCD_write_shu:显示8(宽)*16(高)点阵列数字字母符号等半角类输入参数:c:显示的字符;编写日期:20130918-----------------------------------------------*/voidLCD_write_shu(unsignedcharrow,unsignedcharpage,unsignedcharc)//row:列page:页dd:字符{unsignedchari;LCD_set_XY(row*8,page);//列,页for(i=0;i8;i++){LCD_write_byte(shuzi[c*16+i],1);}LCD_set_XY(row*8,page+1);//列,页for(i=8;i16;i++){LCD_write_byte(shuzi[c*16+i],1);}}/*---------------------------------------------LCD_write_hanzi:显示16(宽)*16(高)点阵列汉字等半角类输入参数:c:显示的字符;编写日期:20130918-----------------------------------------------*/voidLCD_write_hanzi(unsignedcharrow,unsignedcharpage,unsignedcharc)//row:列page:页dd:字符{unsignedchari;LCD_set_XY(row*8,page);//列,页for(i=0;i16;i++){LCD_write_byte(hanzi[c*32+i],1);}LCD_set_XY(row*8,page+1);//列,页for(i=16;i32;i++){LCD_write_byte(hanzi[c*32+i],1);}}/********************************************************************************************************************************************@f_name:voidLCD5110ShowNum(u8X,u16Y,u32num)@brief:显示一个数字@param:x,y:起点坐标num:数值(0~2^32);@return:None*********************************************************************************************************************************************///m^n函数unsignedintmypow(unsignedcharm,unsignedcharn){unsignedintresult=1;while(n--)result*=m;returnresult;}voidLCD5110ShowNum(unsignedcharX,unsignedcharY,unsignedintnum){unsignedintres;unsignedchart=0,t1=0;res=num;if(!num)LCD_write_shu(X,Y,0);//显示0while(res)//得到数字长度{res/=10;t++;}t1=t;while(t)//显示数字{res=mypow(10,t-1);LCD_write_shu(X+(t1-t),Y,(num/res)%10);//数字字符第一个在'0'之前t--;}}头文件1……………………………5110.h#ifndef__5110_H__#define__5110_H__voidLCD_init(void);//初始化voidLCD_clear(void);//清屏voidLCD_write_shu(unsignedcharrow,unsignedcharpage,unsignedcharc);//显示英文与数字(x,y)voidLCD_write_hanzi(unsignedcharrow,unsignedcharpage,unsignedcharc);//显示中文x:0,2,4,6voidLCD5110ShowNum(unsignedcharX,unsignedcharY,unsignedintnum);//显示数量y:0,2,4#endif头文件2……………………………………ZIKU.h#ifndef__ziku_H__#define__ziku_H__unsignedcharcodeshuzi[]={/*--文字:0--*//*--宋体12;此字体下对应的点阵
本文标题:dht11在stc15程序
链接地址:https://www.777doc.com/doc-3233215 .html