为了节约板子的IO口这里的显示使用的四线驱动显示接法,显示使用的LiquidCrystal库,库的安装可以直接从
IDE 中的项目选项下的 include Library中选择manage library 中搜索LCD,安装必要的库文件。也可以在github中下载zip包来安装,URL:http://https://github.com/adafruit/Adafruit_LiquidCrystal
代码:
#include <LiquidCrystal.h>// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);int temp;//温度
int humi;//湿度
int tol;//校对码
int j;
unsigned int loopCnt;
int chr[40] = {0};//创建数字数组,用来存放40个bit
unsigned long time;
#define pin 8void setup() {// set up the LCD's number of columns and rows:lcd.begin(16, 2);// Print a message to the LCD.//lcd.print("hello, world!");Serial.begin(9600);}void loop() {bgn:delay(2000);
//设置2号接口模式为:输出
//输出低电平20ms(>18ms)
//输出高电平40μspinMode(pin,OUTPUT);digitalWrite(pin,LOW);delay(20);digitalWrite(pin,HIGH);delayMicroseconds(40);digitalWrite(pin,LOW);
//设置2号接口模式:输入pinMode(pin,INPUT);//高电平响应信号loopCnt=10000;while(digitalRead(pin) != HIGH){if(loopCnt-- == 0){
//如果长时间不返回高电平,输出个提示,重头开始。Serial.println("HIGH");goto bgn;}}//低电平响应信号loopCnt=30000;while(digitalRead(pin) != LOW){if(loopCnt-- == 0){
//如果长时间不返回低电平,输出个提示,重头开始。Serial.println("LOW");goto bgn;}}
//开始读取bit1-40的数值 for(int i=0;i<40;i++){while(digitalRead(pin) == LOW){}
//当出现高电平时,记下时间“time”time = micros();while(digitalRead(pin) == HIGH){}
//当出现低电平,记下时间,再减去刚才储存的time
//得出的值若大于50μs,则为‘1’,否则为‘0’
//并储存到数组里去if (micros() - time >50){chr[i]=1;}else{chr[i]=0;}}//湿度,8位的bit,转换为数值
humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];//温度,8位的bit,转换为数值
temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];//校对码,8位的bit,转换为数值
tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
//输出:温度、湿度、校对码if(tol >= humi + temp){Serial.print("temp:");Serial.println(temp);Serial.print("humi:");Serial.println(humi);Serial.print("tol:");Serial.println(tol);
}else{Serial.println("error!");//校对码,我这里没用上-//理论上,湿度+温度=校对码//如果数值不相等,说明读取的数据有错。
}lcd.clear();lcd.setCursor(0,0);lcd.print("Tem(");lcd.print((char)223);//显示o符号lcd.print("C):");lcd.setCursor(8,0); lcd.print(temp);lcd.setCursor(0,1);lcd.print("Hum(");lcd.print((char)37);//显示%符号lcd.print("):");lcd.setCursor(7,1);lcd.print(humi);
}