|
回復 1# phfd2013
你的URM-04是否如下圖接到他付的這種擴充板?
=============
你的擴充板是否如下圖與arduino做結合?
=================================
是的話,這裡有sample code,(不過他好像是接六個URM-04)你一樣拿來試用看看:
//Connect the URM04 with the I/O Expansion Shield V5 (SKU:DFR0088) For Arduino
//
#include <Metro.h>
int EN = 2;
Metro TriggerMetro = Metro(120,true); //100
Metro readurmMetro = Metro(20,true);
byte cmdst[10];
int address[6]={0x11,0x12,0x13,0x14,0x15,0x16};
int URM[6];
byte delivered=0x00;
void setup()
{
pinMode(EN, OUTPUT);
Serial.begin(19200);
cmdst[0]=0x55;
cmdst[1]=0xaa;
}
int tmp=0;
boolean sent=true;
void loop()
{
if(TriggerMetro.check()==1)
{
digitalWrite(EN, HIGH);
for(int i=0;i<4;i++)
{
cmdst[2]=address;
Trigger();
delay(2);
}
readurmMetro.interval(40);//25
delivered=0x00;
digitalWrite(EN, LOW);
}
if(int(delivered)<4&&readurmMetro.check()==1)
{
getds(delivered);
readurmMetro.interval(18);//16
delivered+=0x01;
}
}
byte cmdrd[10];
void getds(byte num)
{
digitalWrite(EN, HIGH);
cmdst[2]=address[int(num)];
Readds();
digitalWrite(EN, LOW);
serial_read();
}
void Trigger()
{
cmdst[3]=0x00;
cmdst[4]=0x01;
TransCode();
}
void TransCode()
{
cmdst[5]=cmdst[0]+cmdst[1]+cmdst[2]+cmdst[3]+cmdst[4];
for(int j=0;j<6;j++)
Serial.print(cmdst[j],BYTE);
}
void Readtmp()
{
cmdst[3]=0x00;
cmdst[4]=0x03;
TransCode();
delay(1);
}
void Readds()
{
cmdst[3]=0x00;
cmdst[4]=0x02;
TransCode();
delay(1);
//delayMicroseconds(500);
}
int S_comd[10];
void serial_read()
{
if(Serial.available())
{
int sum=0;
int i=0;
//int j=0;
boolean flag=true;
while(flag)
{
cmdrd=Serial.read();
if(cmdrd==0xaa&&cmdrd[i-1]==0x55)
{
S_comd[0]=i;
//j++;
}
if(cmdrd!=0xff)
{
//Serial.print(cmdrd);
i++;
}
if(i>7)
flag=false;
}
for(int h=0;h<7;h++)
sum+=cmdrd[h];
if(byte(sum)==cmdrd[7])
{
int num=S_comd[0]+1;
if(cmdrd[num+2]==0x02&&cmdrd[num+1]==0x02)
{
switch (cmdrd[num])
{
case 0x11:
URM[0]=cmdrd[num+3]*256+cmdrd[num+4];
//Serial.print("0x11:");
//Serial.println(URM[0]);
break;
case 0x12:
URM[1]=cmdrd[num+3]*256+cmdrd[num+4];
//Serial.print("0x12:");
//Serial.println(URM[1]);
break;
case 0x13:
URM[2]=cmdrd[num+3]*256+cmdrd[num+4];
//Serial.print("0x13:");
//Serial.println(URM[2]);
break;
case 0x14:
URM[3]=cmdrd[num+3]*256+cmdrd[num+4];
//Serial.print("0x14:");
//Serial.println(URM[3]);
break;
default:
break;
}
//delivered+=0x01;
}
}
}
} |
|