byte comdata[3]={0}; //定义数组数据,存放串口接收数据
float temperature;
float humidity;
int dustPin=0;
int dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void test_do_data(void)
{
if(comdata[0] == 0x55) //0x55和0xAA均为判断是否为有效命令
{
if(comdata[1] == 0xAA)
{
switch(comdata[2])
{
case temperature_Command:
dht11.read(DHT11PIN);
Serial.print(temperature,2);
break;
case humidity_Command:
dht11.read(DHT11PIN);
Serial.print(humidity,2);
break;
case dust_Command:
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);
Serial.println(dustVal);
break;
}
}
}
}
Arduino的程序:
#include <dht11.h>
#define temperature_Command 0x10 //采集命令字
#define humidity_Command 0x20 //A1采集命令字
#define dust_Command 0x30 //D1采集命令字
/*-----( Declare objects )-----*/
dht11 dht11;
/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2
byte comdata[3]={0}; //定义数组数据,存放串口接收数据
float temperature;
float humidity;
int dustPin=0;
int dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void receive_data(void); //接受串口数据
void test_do_data(void); //测试串口数据是否正确,并更新数据
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(ledPower,OUTPUT);
pinMode(4, OUTPUT);
analogReference(INTERNAL);
}
void loop()
{
while (Serial.available() > 0) //不断检测串口是否有数据
{
receive_data(); //接受串口数据
test_do_data(); //测试数据是否正确并更新标志位
}
}
void receive_data(void)
{
int i ;
for(i=0;i<3;i++)
{
comdata[i] =Serial.read();
//延时一会,让串口缓存准备好下一个字节,不延时可能会导致数据丢失,
delay(2);
}
}
void test_do_data(void)
{
if(comdata[0] == 0x55) //0x55和0xAA均为判断是否为有效命令
{
if(comdata[1] == 0xAA)
{
switch(comdata[2])
{
case temperature_Command:
dht11.read(DHT11PIN);
Serial.print(temperature,2);
break;
case humidity_Command:
dht11.read(DHT11PIN);
Serial.print(humidity,2);
break;
case dust_Command:
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);
Serial.println(dustVal);
break;
}
}
}
}
你的ardunio程序也没问题?怎么判断的。
用串口助手之类的调试过吗?
这种硬件调试,你帖个代码屁用都没有啊。别人没硬件根本帮不上忙的。
一周热门 更多>