logo资料库

MSP430G2553驱动DHT11温湿度传感器.doc

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
////////////////////main.c ///////BY Wangzengri NKU #include "msp430g2553.h" #include"12864.h" const unsigned char tishi_W[]={"温度:"}; const unsigned char tishi_Wd[]={"°"}; const unsigned char tishi_S[]={"湿度:"}; const unsigned char tishi_Sd[]={"%"}; const unsigned char tishi_dot[]={".00"}; #define CPU #define delay_us(x) #define delay_ms(x) (1000000) (__delay_cycles((double)x*CPU/1000000.0)) (__delay_cycles((double)x*CPU/1000.0)) #define HIGH P2OUT|= BIT1; #define LOW P2OUT &=~BIT1; unsigned char temph,templ,humdh,humdl,check,cal; void show_Wendu(); void show_Shidu(); void show_tishi(); /*void DelayNus(unsigned int n) { unsigned int i; for(i = n;i > 0;i--) _NOP(); }*/ char receive(void) { unsigned char num=0,tem,cnt; for(cnt=0;cnt<8;cnt++) { tem=0; while(!(P2IN&BIT1)); delay_us(30); if((P2IN&BIT1)) { tem=1; while((P2IN&BIT1)); } num<<=1; num|=tem; } //延时 Nus //接受函数 //临时变量用于存储接受数据 //等待 14us 的低电平结束 /////////长于 30us 定义为 1 //等待一位的采集结束
return num; } void receive_init() { P2DIR|=BIT1; LOW; delay_ms(20); HIGH; delay_us(30); P2DIR &=~BIT1; if(!(P2IN&BIT1)); { //20--40 //从机发出响应信号 while(!(P2IN&BIT1));//等待响应响应结束 while((P2IN&BIT1));//开始采集 humdh=receive(); humdl=receive(); temph=receive(); templ=receive(); check=receive(); } } void main( void ) { WDTCTL=WDTPW+WDTHOLD; DCOCTL = CALDCO_1MHZ; Ini_Lcd(); show_tishi(); while(1) { receive_init(); cal=humdh+humdl+temph+templ; if(cal==check) { show_Wendu(); show_Shidu(); } } } void show_tishi() { Disp_HZ(0x80,tishi_W,3); Disp_HZ(0x86,tishi_Wd,1);
Disp_HZ(0x88,tishi_S,3); Disp_HZ(0x8e,tishi_Sd,1); } void show_Wendu() { Disp_SZ(0x83,temph); Disp_HZ(0x84,tishi_dot,2); //Disp_SZ(0x84,templ); } void show_Shidu() { Disp_SZ(0x8b,humdh); Disp_HZ(0x8c,tishi_dot,2); //Disp_SZ(0x8c,humdl); } //12864.c #include typedef unsigned int uint; typedef unsigned char uchar; #define BIT(x) (1 << (x)) const uchar numtable[]={"0123456789"}; void Send(uchar type,uchar transdata); /**********液晶控制 IO 的宏定义*************/ #define cyCS #define cySID #define cyCLK #define cyPORT #define cyDDR //P1.0,片选信号 //P1.1,串行数据 //P1.2,同步时钟 0 1 2 P1OUT P1DIR 能:延时 N 个 us 的时间 数:n--延时长度 /******************************************* 函数名称:delay_Nus 功 参 返回值 :无 ********************************************/ void delay_Nus(uint n) { uchar i; for(i = n;i > 0;i--) _NOP(); } /******************************************* 函数名称:delay_1ms 功 能:延时约 1ms 的时间
数:无 参 返回值 :无 ********************************************/ void delay_1ms(void) { uchar i; for(i = 150;i > 0;i--) _NOP(); } /******************************************* 函数名称:delay_Nms 能:延时 N 个 ms 的时间 功 参 数:无 返回值 :无 ********************************************/ void delay_Nms(uint n) { uint i = 0; for(i = n;i > 0;i--) delay_1ms(); } /******************************************* 函数名称:Ini_Lcd 能:初始化液晶模块 功 参 数:无 返回值 :无 ********************************************/ void Ini_Lcd(void) { /*DDRAM 地址归位*/ cyDDR |= BIT(cyCLK) + BIT(cySID) + BIT(cyCS); delay_Nms(100); Send(0,0x30); delay_Nus(72); Send(0,0x02); delay_Nus(72); Send(0,0x0c); delay_Nus(72); Send(0,0x01); delay_Nus(72); Send(0,0x06); delay_Nus(72); } /******************************************* 函数名称:Send //延时等待液晶完成复位 /*功能设置:一次送 8 位数据,基本指令集*/ //相应的位端口设置为输出 /*显示设定:开显示,不显示光标,不做当前显示位反白闪动*/ /*清屏,将 DDRAM 的位址计数器调整为“00H”*/ /*功能设置,点设定:显示字符/光标从左到右移位,DDRAM 地址加 1*/
功 参 能:MCU 向液晶模块发送 1 一个字节的数据 数:type--数据类型,0--控制命令,1--显示数据 transdata--发送的数据 返回值 :无 ********************************************/ void Send(uchar type,uchar transdata) { uchar firstbyte = 0xf8; uchar temp; uchar i,j = 3; if(type) firstbyte |= 0x02; cyPORT |= BIT(cyCS); cyPORT &= ~BIT(cyCLK); while(j > 0) { if(j == 3) temp = firstbyte; else if(j == 2) temp = transdata&0xf0; else temp = (transdata << 4) & 0xf0; for(i = 8;i > 0;i--) { cyPORT |= BIT(cySID); cyPORT &= ~BIT(cySID); if(temp & 0x80) else cyPORT |= BIT(cyCLK); temp <<= 1; cyPORT &= ~BIT(cyCLK); } //三个字节之间一定要有足够的延时,否则易出现时序问题 if(j == 3) delay_Nus(600); else delay_Nus(200); j--; } cyPORT &= ~BIT(cySID); cyPORT &= ~BIT(cyCS); } /******************************************* 函数名称:Clear_GDRAM 能:清除液晶 GDRAM 内部的随机数据 功 参 数:无 返回值 :无
********************************************/ void Clear_GDRAM(void) { uchar i,j,k; Send(0,0x34); i = 0x80; for(j = 0;j < 32;j++) { //打开扩展指令集 Send(0,i++); Send(0,0x80); for(k = 0;k < 16;k++) { Send(1,0x00); } } i = 0x80; for(j = 0;j < 32;j++) { Send(0,i++); Send(0,0x88); for(k = 0;k < 16;k++) { Send(1,0x00); } } Send(0,0x30); //回到基本指令集 } /******************************************* 函数名称:Disp_HZ 功 参 能:显示汉字程序 数:addr--显示位置的首地址 pt--指向显示数据的指针 num--显示数据的个数 返回值 :无 ********************************************/ void Disp_HZ(uchar addr,const uchar * pt,uchar num) { uchar i; Send(0,addr); for(i = 0;i < (num*2);i++) Send(1,*(pt++)); }
能:在整个屏幕上画一个图片 数:ptr--指向保存图片位置的指针 /******************************************* 函数名称:Draw_PM 功 参 返回值 :无 ********************************************/ void Draw_PM(const uchar *ptr) { uchar i,j,k; Send(0,0x34); i = 0x80; for(j = 0;j < 32;j++) { //打开扩展指令集 Send(0,i++); Send(0,0x80); for(k = 0;k < 16;k++) { Send(1,*ptr++); } } i = 0x80; for(j = 0;j < 32;j++) { Send(0,i++); Send(0,0x88); for(k = 0;k < 16;k++) { Send(1,*ptr++); } } Send(0,0x36); Send(0,0x30); //打开绘图显示 //回到基本指令集 } /******************************************* 函数名称:Draw_TX 功 参 能:在液晶上描绘一个 16*16 的图形 数:Yaddr--Y 地址, Xaddr--X 地址 dp--指向保存图形数据的指针 返回值 :无 ********************************************/ void Draw_TX(uchar Yaddr,uchar Xaddr,const uchar * dp) {
uchar j; uchar k = 0; Send(0,0x34); for(j = 0;j < 16;j++) { Send(0,Yaddr++); Send(0,Xaddr); Send(1,dp[k++]); Send(1,dp[k++]); } Send(0,0x36); Send(0,0x30); //使用扩展指令集,关闭绘图显示 //Y 地址 //X 地址 //送两个字节的显示数据 //打开绘图显示 //回到基本指令集模式 } /******************************************* 函数名称:Disp_SZ 功 参 能:显示一个两位数字 数:addr--显示地址 数字--显示的数字 返回值 :无 ********************************************/ void Disp_SZ(uchar addr,uchar shuzi) { uchar tmp0,tmp1; tmp0 = shuzi / 10; tmp1 = shuzi % 10; Send(0,addr); Send(1,numtable[tmp0]); Send(1,numtable[tmp1]); } //12864.h void Send(unsigned char type,unsigned char transdata); void Ini_Lcd(void); void Clear_GDRAM(void); void Disp_HZ(unsigned char addr,const unsigned char * pt,unsigned char num); void Draw_PM(const unsigned char *ptr); void Draw_TX(unsigned char Yaddr,unsigned char Xaddr,const unsigned char * dp) ; void Disp_SZ(unsigned char addr,unsigned char shuzi); void delay_Nus(unsigned int n); void delay_Nms(unsigned int n); void delay_1ms(void);
分享到:
收藏