|
|
我用UNO板做主控,通过温湿度传感器检测,用1602显示,检测数据与预设值对比并控制直流电机的驱动。在驱动的过程中遇到这样一个问题。在正确检测并显示数据后,一旦驱动直流电机,LCD就会出现乱码,起初以为是供电不足引起的,在给直流电机外加电源之后还是会出现该问题。在实在无法找到解决方案的情况下。跪求论坛大神能够指点迷津!!
如下是代码、实物接线、现象。
/* LiquidCrystal Library - DHT11
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 10
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor: ends to +5V and ground,wiper to LCD VO pin (pin 3)
*/
// include the library code:
#include <LiquidCrystal.h>
byte smiley[8] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
};
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 10, 5, 4, 3, 2);
int temp;//温度
int humi;//湿度
int tol;//校对码
int j;
unsigned int loopCnt;
int chr[40] = {0};//创建数字数组,用来存放40个bit
unsigned long time;
#define pin 11 //DHT11 连接arduino11引脚
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("Temp Humi"); // Print a message to the LCD.
lcd.createChar(0, smiley);
}
void loop()
{
pinMode(6, OUTPUT);
bgn: delay(2000);
pinMode(pin,OUTPUT);//设置11号接口模式为:输出
digitalWrite(pin,LOW);//输出低电平20ms(>18ms)
delay(20);
digitalWrite(pin,HIGH);//输出高电平40μs
delayMicroseconds(40);
digitalWrite(pin,LOW);
pinMode(pin,INPUT);//设置11号接口模式:输入
//高电平响应信号
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=1; }
else{ chr=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];
//输出:温度、湿度、校对码
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(temp);
lcd.setCursor(2, 1);
lcd.write(byte(0));
lcd.setCursor(3, 1);
lcd.print("C");
lcd.setCursor(10, 1); // set the cursor to column 10, line 1
lcd.print(humi);
lcd.setCursor(12, 1);
lcd.print("%");
Serial.print("temp:");
Serial.print(temp);
Serial.print(" Celsius, ");
Serial.print("humi:");
Serial.print(humi);
Serial.println("%");
if (temp>20)
{digitalWrite(6, HIGH);} // 驱动直流电机
else
{digitalWrite(6, LOW); } // 不驱动直流电机
}
|
|