极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17284|回复: 9

arduino+LCD1206+DHT11+直流电机

[复制链接]
发表于 2016-4-15 17:16:52 | 显示全部楼层 |阅读模式
我用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); }   // 不驱动直流电机
}


回复

使用道具 举报

 楼主| 发表于 2016-4-15 17:22:58 | 显示全部楼层
为什么还会限制文件格式大小,我的大小在范围之内啊!难道要我以压缩包的形式?
回复 支持 反对

使用道具 举报

发表于 2016-4-15 17:30:28 | 显示全部楼层
电路连接图呢?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-15 18:27:57 | 显示全部楼层
实物接线图以及出现乱码情况。


乱码只出现在显示的字符上及之后。


不驱动电机的情况下是非常正常的。


电机是用场效应管做开关的。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2016-4-16 16:33:36 | 显示全部楼层
电机加上消火花电容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-17 11:19:09 | 显示全部楼层
cjnt007 发表于 2016-4-16 16:33
电机加上消火花电容

我这里加了一个0.1UF的电容,测试的效果看上去还不错。加了这个电容之后是不是可以不用场效应管了?因为我查了一下,场效应管IRF450的正常工作状态是G和S之间的电压在3~4V之间。
回复 支持 反对

使用道具 举报

发表于 2016-4-17 12:35:12 | 显示全部楼层
不用场管怎么控制电机运转?
加电容是为了消除电机电刷产生的高频干扰,跟场管是两码事吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-18 19:01:46 | 显示全部楼层
cjnt007 发表于 2016-4-17 12:35
不用场管怎么控制电机运转?
加电容是为了消除电机电刷产生的高频干扰,跟场管是两码事吧

现在出现是情况是场效应管无法正常工作,因为它两端的电压差达不到。所以在考虑要不要换继电器来做。
回复 支持 反对

使用道具 举报

发表于 2016-4-19 09:45:48 | 显示全部楼层

这样接电容对场管导通是否有影响?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-19 23:33:28 | 显示全部楼层
cjnt007 发表于 2016-4-19 09:45
这样接电容对场管导通是否有影响?

我看不懂你怎么接的,不清楚会不会影响。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 23:36 , Processed in 0.079395 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表