51單片機做的溫濕度測量儀
該1602液晶的D0~D7數(shù)據(jù)口連接單片機的P17~P10口,所以程序里的指令和數(shù)據(jù)都是逆向輸入的。
程序和電路圖下載:http://www.51hei.com/f/溫濕度傳感器+1602原理圖.rar
#include#include #include #define uchar unsigned char #define uint unsigned int #define noACK 0 #define ACK 1#define STATUS_REG_W 0x06#define STATUS_REG_R 0x07#define MEASURE_TEMP 0x03 #define MEASURE_HUMI 0x05 #define RESET 0x1esbit SCK =P1^0;sbit DATA=P1^1;sbit led1=P1^7; sbit rs=P2^3; //H數(shù)據(jù),L命令sbit en=P2^5; //高脈沖使能,使能脈寬最小150nssbit rw=P2^4;uchar code table_temp[]=" TEMP: ";uchar code table_humi[]=" HUMI: ";uchar code table_c[]=" C ";uchar code table_p[]=" % ";uchar error=0; void delayms(uint z) { uint i,j; for(i=z;i>0;i--) for( j=110;j>0;j--); } //啟動時序 void transstart() { SCK=1;_nop_(); DATA=0;_nop_(); SCK=0;_nop_();_nop_();_nop_(); SCK=1;_nop_(); DATA=1;_nop_(); SCK=0; } //寫入命令子函數(shù) uchar write_byte(uchar value) { uchar i; for(i=0x80;i>0;i=i/2) { if(i&value) DATA=1; else DATA=0; SCK=1; _nop_();_nop_();_nop_(); SCK=0; } DATA=1; SCK=1; error=DATA; SCK=0; return error; }
評論