试用MAX485组半双工RS485网
2011-10-7 13:00 上传
(12.73 KB)
需Max485两个 DIP-8的我买的0.5元一个
Arduino2、面包板2个、面包板线若干、120欧电阻2个
接线原理图
2011-10-7 13:04 上传
(20.58 KB)
RO接Arduino的pin0(RX)RE和DE一起接pin2(其实直接接5V和GND亦可,接5V为发送状态,接pin2的话相对灵活一点)DI接pin1(TX)
VCC接Arduino的5V GND接GND A接A B接B 原理图里应该能看到AB之间接根120欧电阻(应该是阻抗)
2011-10-7 13:13 上传
(277.34 KB)
2011-10-7 13:13 上传
(228.75 KB)
2011-10-7 13:13 上传
(49.41 KB)
测试用程序 (匆忙中写的 你应该比我写的更好)
ARDUINO 代码复制打印
// Master//-------------------------------int EN = 2;byte h1 = 'red';byte h2 = 'white';void setup(){pinMode(EN, OUTPUT);Serial.begin(19200);}void loop(){// send data digitalWrite(EN, HIGH);//使能发送Serial.print(h1,BYTE); delay(6000);Serial.print(h2,BYTE);delay(6000);}//----------------------// Slaves//----------------------int ledPin = 13;int EN = 2;void setup(){pinMode(ledPin, OUTPUT);pinMode(EN, OUTPUT);Serial.begin(19200);}void loop(){// receive data digitalWrite(EN, LOW);//使能接收if (Serial.available() > 0) { byte val=Serial.read(); byte val1='red'; byte val2='white'; if (val == val1) { digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); } else if(val == val2){ digitalWrite(ledPin, HIGH); delay(5000); digitalWrite(ledPin, LOW); }}}
// Master
//-------------------------------
int EN = 2;
byte h1 = 'red';
byte h2 = 'white';
void setup()
{
pinMode(EN, OUTPUT);
Serial.begin(19200);
}
void loop()
{
// send data
digitalWrite(EN, HIGH);//使能发送
Serial.print(h1,BYTE);
delay(6000);
Serial.print(h2,BYTE);
delay(6000);
}
//----------------------
// Slaves
//----------------------
int ledPin = 13;
int EN = 2;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(EN, OUTPUT);
Serial.begin(19200);
}
void loop()
{
// receive data
digitalWrite(EN, LOW);//使能接收
if (Serial.available() > 0) {
byte val=Serial.read();
byte val1='red';
byte val2='white';
if (val == val1) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
else if(val == val2){
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
}
}
当接收到red指令时13号灯闪3下 接到white时亮5秒!(之前用红白色的led试的,发现没有自带的13号灯方便)
via - 极客工坊