Arduino两块板传输数据怎么样抗干扰?
现在有两块Arduino板,想在两块板之间传输数据。现在能传输到数据。但是如果旁边有红外线干扰得时候会受到干扰。请问怎么让才能解决。还有我想如果在两块Arduino传输数据的时候如果有物理阻碍导致无法接受信息,但是阻碍消失后数据还能继续传输。这是我的Arduino代码。
bool transmitting_IR;
bool receiving_IR;
void turn_off_IR ()
{
TCCR2A = 0; // Disconnect PWM
TCCR2B = 0; // Stops the timer
OCR2A = 0;// No timer top
digitalWrite(11, LOW);// Ensure output is off
transmitting_IR = false;
}
void turn_on_IR ()
{
TCCR2A = _BV(WGM21) | _BV(COM2A0); // This mode toggles output once per timer cycle
TCCR2B = _BV(CS20);
OCR2A = 210;38 khz
transmitting_IR = true;
}
bool detect_IR()
{
return receiving_IR = ! digitalRead(12);
}
void setup ()
{
Serial.begin(9600);
pinMode (11, OUTPUT); // IR LED
pinMode (12, INPUT);// IR receiver
turn_off_IR();// My helper function for controlling the IR LED
detect_IR(); // Ensure the state is correct.
}
void loop ()
{
detect_IR();
char ch;
if(receiving_IR)
ch = readch();
ch = Serial.read();
if(ch>0)
sendch(ch);
}
unsigned long p=500;
char readch()
{
char ch = 0;
unsigned long start_time = micros();
while(micros() < start_time + p);
for(int i = 0; i < 8; i++)
{
unsigned long bit_time = micros();
while(micros() < bit_time + 2*p);
int result = detect_IR();
ch = ch | (result << i);
}
Serial.print(ch);
}
void sendch(char ch)
{
turn_on_IR();
unsigned long start_time = micros();
while(micros() < start_time + 2*p);
for(int i = 0; i < 8; i++)
{
int result = (ch >> i) & 1;
if(result == 1)
{
turn_on_IR();
unsigned long bit_time = micros();
while(micros() < bit_time + 2*p);
}
else
{
turn_off_IR();
unsigned long bit_time = micros();
while(micros() < bit_time + 2*p);
}
}
turn_off_IR();
unsigned long end_time = micros();
while(micros() < end_time + 2*p);
}
发送数据加校验,接收成功发送下一个,不成功一直发送,心跳序号保证连续 chaqing 发表于 2013-12-10 10:59 static/image/common/back.gif
发送数据加校验,接收成功发送下一个,不成功一直发送,心跳序号保证连续
请问怎么样加校验呢? 对数据的完整性,连续性判断(如果需要的话)
页:
[1]