极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10129|回复: 3

Arduino两块板传输数据怎么样抗干扰?

[复制链接]
发表于 2013-12-10 07:03:09 | 显示全部楼层 |阅读模式
现在有两块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);
}
回复

使用道具 举报

发表于 2013-12-10 10:59:24 | 显示全部楼层
发送数据加校验,接收成功发送下一个,不成功一直发送,心跳序号保证连续
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-12-10 12:02:33 | 显示全部楼层
chaqing 发表于 2013-12-10 10:59
发送数据加校验,接收成功发送下一个,不成功一直发送,心跳序号保证连续

请问怎么样加校验呢?
回复 支持 反对

使用道具 举报

发表于 2013-12-10 17:30:11 | 显示全部楼层
对数据的完整性,连续性判断(如果需要的话)
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 18:34 , Processed in 0.036537 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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