|
大家好
我用arduino來傳輸LED的數位訊號但是速度到1K就上不去了
arduino得極限不只是這樣吧
電路方面我單獨測試過可到Mega bps的速度
可以看看arduino程式有什麼問題嗎?
#define analogInPin 0 //類比輸入訊號腳位
int sensorValue = 0;
//發射端:
void setup(){
Serial.begin(57600);
pinMode(11, OUTPUT);
}
void loop(){
int t;
t=1000;
char C;
if(Serial.available()>0) {
Serial.print("TX=");
C = Serial.read(); //讀取電腦經由串列埠傳送的資料
digitalWrite(11,HIGH); delayMicroseconds(t); //開始輸出
//Serial.print(1);
Serial.println(C); //將收到的資料再回傳給電腦
//Serial.println(c,HEX);
// Serial.println(c,BIN);
Serial.print((C>>7)&0x01,BIN);
digitalWrite(11,(C>>7)&0x01); delayMicroseconds(t);
Serial.print((C>>6)&0x01,BIN);
digitalWrite(11,(C>>6)&0x01);delayMicroseconds(t);
Serial.print((C>>5)&0x01,BIN);
digitalWrite(11,(C>>5)&0x01);delayMicroseconds(t);
Serial.print((C>>4)&0x01,BIN);
digitalWrite(11,(C>>4)&0x01);delayMicroseconds(t);
Serial.print((C>>3)&0x01,BIN);
digitalWrite(11,(C>>3)&0x01);delayMicroseconds(t);
Serial.print((C>>2)&0x01,BIN);
digitalWrite(11,(C>>2)&0x01);delayMicroseconds(t);
Serial.print((C>>1)&0x01,BIN);
digitalWrite(11,(C>>1)&0x01);delayMicroseconds(t);
Serial.println((C)&0x01,BIN);
digitalWrite(11,(C)&0x01);delayMicroseconds(t);
digitalWrite(11, LOW); delayMicroseconds(t);
}
//接收端:
//讀取接腳的類比訊號數值
sensorValue = analogRead(analogInPin);
int a,b,c,d,e,f,g,h,i;
char j;
// sensorValue = sensorValue/1023*5;
if ( sensorValue > 800){
delayMicroseconds(t);
delayMicroseconds(1);
// delayMicroseconds(2);
a=b=c=d=e=f=g=h=i=0;
//01
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);a=128;}
else{Serial.print(0); delayMicroseconds(t);a=0;}
// Serial.println(sensorValue);
//02
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);b=64;}
else{Serial.print(0); delayMicroseconds(t);b=0;}
// Serial.println(sensorValue);
//03
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);c=32;}
else{Serial.print(0); delayMicroseconds(t);c=0;}
// Serial.println(sensorValue);
//04
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);d=16;}
else{Serial.print(0); delayMicroseconds(t);d=0;}
// Serial.println(sensorValue);
//05
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);e=8;}
else{Serial.print(0); delayMicroseconds(t);e=0;}
// Serial.println(sensorValue);
//06
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);f=4;}
else{Serial.print(0); delayMicroseconds(t);f=0;}
// Serial.println(sensorValue);
//07
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.print(1); delayMicroseconds(t);g=2;}
else{Serial.print(0); delayMicroseconds(t);g=0;}
// Serial.println(sensorValue);
//08
sensorValue = analogRead(analogInPin);
if (sensorValue > 800){Serial.println(1); delayMicroseconds(t);h=1;}
else{Serial.println(0); delayMicroseconds(t);h=0;}
// Serial.println(sensorValue);
i=a+b+c+d+e+f+g+h;
j=i;
Serial.print("RX=");
Serial.println(j);
// Serial.println("-------------");
}
} |
|