Show Posts
|
|
Pages: 1 [2] 3 4 ... 10
|
|
16
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: February 16, 2012, 05:51:01 pm
|
The master asks for data void Read_Data() { Wire.beginTransmission(2); Wire.write('H'); Wire.endTransmission();
delay(200);
Wire.beginTransmission(2); Wire.requestFrom(2,127); Wire.endTransmission(); for ( int i=0; i<128; i++ ) Serial.write(Wire.read()); Serial.println(""); }
and on the slave Wire.begin(I2C_Address); Wire.onRequest(requestEvent); Wire.onReceive(ReceiveCommand);
void ReceiveCommand(int howMany) { command = Wire.read(); }
void loop() { if (command == 'H') { Capture_OS4000(); command = 'N'; } }
void Capture_OS4000() { //Serial.println("mpika capture"); // debug //Serial.flush(); while ( Serial.read () != '$' ); //Serial.flush(); while( Serial.available() < 127 ); OS4000Data[0] = '$'; for(Position=1; Position<128; Position++){ buf = Serial.read(); if ( buf == '*' ) EOL = Position + 2; if ( Position <= EOL ) OS4000Data[Position] = buf; else OS4000Data[Position] = B0; } }
void requestEvent() { //Wire.write (OS4000Data, 127); Wire.write ("AbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789000987654321"); }
but i am receiving AbcdefghijklmnopqrstuvwxyzABCDEFÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ thanks
|
|
|
|
|
22
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: July 29, 2011, 06:26:16 pm
|
#include <Wire.h>
#define I2C_Address 2 #define UartBaudRate 57600
int Position;
byte OS4000Data [128];
char command;
void setup() { Serial.begin(UartBaudRate); Wire.begin(I2C_Address); Wire.onRequest(requestEvent); Wire.onReceive(ReceiveCommand); }
void loop() { if(command == 'C') { Capture_OS4000(); command = 'N'; } }
void ReceiveCommand(int howMany) { while(!Wire.available()); command = Wire.receive(); }
void requestEvent() { Wire.send ("kffkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkdd"); //Wire.send (OS4000Data, 127); }
void Capture_OS4000() { Serial.flush(); while ( Serial.read () != '$' ); Serial.flush(); while( Serial.available() < 127 ); OS4000Data[Position] = Serial.read(); }
#include <Wire.h> #define requestdelay 25
void setup(){ Wire.begin(); Serial.begin(9600); }
void loop(){
Wire.beginTransmission(2); Wire.send('C'); Wire.endTransmission(); delay(200);
Wire.beginTransmission(2); Wire.requestFrom(2,40); for ( int i=0; i<40; i++ ) Serial.print(Wire.receive()); Serial.println(); }
not working with the above code i am reading kffkkkkkkkkkkkkkkkkkkkkkkkkkkkkk and not kffkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkdd
|
|
|
|
|
26
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: June 20, 2011, 04:22:11 am
|
this seems to work Wire.beginTransmission(2); Wire.send('P'); Wire.endTransmission(); delay(requestdelay); Wire.requestFrom(2,2); if ( Wire.available() > 1 ) { pitchMSB = Wire.receive(); pitchLSB = Wire.receive(); pitch = ( ( pitchMSB<<8 ) | ( pitchLSB ) ); Serial.print("P: "); Serial.print(pitch); Serial.print(" "); } with requestdelay at 25ms thanks a lot for you replies -------------------------------------------------------------- i am not sure how to use atof and atoi this is my uart input: $OHPR,346.2,1.0,1.8,30.6,0.0000,168.01,-163.19,-39.50,-5.94,1.011,0.019,0.033,1.010,0.00,-0.01,-0.01,48145S0*59
$OHPR,346.2,1.0,1.8,30.6,0.0000,168.00,-163.18,-39.50,-5.95,1.011,0.018,0.033,1.010,0.00,-0.01,-0.01,48146S0*5A
$OHPR,346.2,1.0,1.8,30.6,0.0000,168.02,-163.20,-39.50,-5.98,1.011,0.019,0.033,1.010,0.00,-0.01,-0.01,48147S0*5E $OHPR,heading,pitch,roll,temp,depth, ..... .......,gyroX*
|
|
|
|
|
27
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: June 19, 2011, 06:29:27 pm
|
first if all thanks for you replies the second code is just to test the first code the i2c readout it will be done by an FPGA -------- thats why i dont have the #define SLAVE_ADDRESS 2 as i have on the first one #define I2C_Address 2 and everything is on a loop ------------------------------ Now 200ms i think its enought it is working and on 100ms I also have tested at 500ms and i have the same issues when you send the C "Capture-Convert" command 1- the programm listens the Uart and seeking for the '$' 2- after he finds it - fills in the Uart buffer 3- resets to 0 the heading 4- updates the heading 5- resets to 0 the pitch 6 - udates the pitch 7- resets to 0 the roll 8 - udates the roll 9- resets to 0 the temp 10 - udates the temp then if you just send again the C command it will do the same or if you send another command it has to reply back with some data i might have to use that Wire.endTransmission(); delay(requestdelay); Wire.requestFrom(2,2); as you suggest me because the programm might not have the time to do that : void ReceiveCommand(int howMany) { while(!Wire.available()); command = Wire.receive(); } tanks again the reader does not receive the two bytes ( thats why it freezes with while command ) and it is not receiving two zero bytes
|
|
|
|
|
28
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: June 19, 2011, 04:47:16 am
|
#include <Wire.h> #define requestdelay 10
int heading; byte headingMSB; byte headingLSB;
int pitch; byte pitchMSB; byte pitchLSB;
int roll; byte rollMSB; byte rollLSB;
int temp; byte tempMSB; byte tempLSB;
void setup(){ Wire.begin(); Serial.begin(9600); }
void loop(){
Wire.beginTransmission(2); Wire.send('C'); Wire.endTransmission(); delay(200);
Wire.beginTransmission(2); Wire.send('H'); Wire.endTransmission(); Wire.requestFrom(2,2); delay(requestdelay); if ( Wire.available() > 1 ) { headingMSB = Wire.receive(); headingLSB = Wire.receive(); heading = ( ( headingMSB<<8 ) | ( headingLSB ) ); Serial.print("H: "); Serial.print(heading); Serial.print(" "); }
Wire.beginTransmission(2); Wire.send('P'); Wire.endTransmission(); Wire.requestFrom(2,2); delay(requestdelay); if ( Wire.available() > 1 ) { pitchMSB = Wire.receive(); pitchLSB = Wire.receive(); pitch = ( ( pitchMSB<<8 ) | ( pitchLSB ) ); Serial.print("P: "); Serial.print(pitch); Serial.print(" "); }
Wire.beginTransmission(2); Wire.send('R'); Wire.endTransmission(); Wire.requestFrom(2,2); delay(requestdelay); if ( Wire.available() > 1 ) { rollMSB = Wire.receive(); rollLSB = Wire.receive(); roll = ( ( rollMSB<<8 ) | ( rollLSB ) ); Serial.print("R: "); Serial.print(roll); Serial.print(" "); }
Wire.beginTransmission(2); Wire.send('T'); Wire.endTransmission(); Wire.requestFrom(2,2); delay(requestdelay); if ( Wire.available() > 1 ) { tempMSB = Wire.receive(); tempLSB = Wire.receive(); temp = ( ( tempMSB<<8 ) | ( tempLSB ) ); Serial.print("T: "); Serial.print(temp); Serial.print(" "); }
Serial.println(); } My output: H: 3478 P: 17 R: 17 T: 305 H: 3478 P: 17 R: 17 T: 304 H: 3478 P: 17 R: 17 T: 303 H: 3478 P: 17 R: 17 T: 303 H: 3478 P: 17 R: 17 T: 305 H: 3478 P: 17 R: 17 T: 306 H: 3478 P: 17 R: 17 T: 307 H: 3478 P: 17 R: 17 T: 307 H: 3478 P: 17 R: 17 T: 306 H: 0 P: 17 R: 17 T: 304 H: 3478 P: 17 R: 17 T: 304 H: 3478 P: 17 R: 17 T: 303 H: 3478 P: 17 R: 17 T: 303 H: 3478 P: 17 R: 17 T: 305 H: 3478 P: 17 R: 17 T: 306 H: 3478 P: 17 R: 17 T: 307 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 0 R: 17 T: 309 H: 3478 P: 17 R: 17 T: 309 H: 3478 P: 17 R: 17 T: 309 H: 3478 P: 17 R: 17 T: 309 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 17 R: 17 T: 308 H: 3478 P: 17 R: 17 T: 306 H: 3478 P: 17 R: 17 T: 305 it is not periodic and not always on the same output (heading,pitch,roll,temp) its 0 because the receiver never gets two bytes *this is checked becase if i use while ( Wire.available() < 2 ){} it frezzes thanks a lot
|
|
|
|
|
29
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: June 19, 2011, 04:43:09 am
|
#include <Wire.h>
#define I2C_Address 2 #define UartBaudRate 57600
int Position; int NextData;
boolean SentenceState = false; char OS4000Data[128];
char command;
int Heading; int Heading100; int Heading10; int Heading1; int Heading01;
int Pitch; int Pitch100; int Pitch10; int Pitch1; int Pitch01;
int Roll; int Roll100; int Roll10; int Roll1; int Roll01;
int Temp; int Temp100; int Temp10; int Temp1; int Temp01;
int Depth;
int MagneticVector;
int MagneticX;
int MagneticY;
int MagneticZ;
int AccelarationVector;
int AccelarationX;
int AccelarationY;
int AccelarationZ;
int GyroX;
int GyroY;
void setup() { Serial.begin(UartBaudRate); Wire.begin(I2C_Address); Wire.onRequest(requestEvent); Wire.onReceive(ReceiveCommand); }
void loop() { if(command == 'C') { Capture_OS4000(); Check_Sentence(); if ( SentenceState == true ) Extract_Info(); command = 'N'; } }
void ReceiveCommand(int howMany) { while(!Wire.available()); command = Wire.receive(); }
void requestEvent() { switch (command) { case 'A': WireSendAll(); break; case 'H': WireSendInt(Heading); break; case 'P': WireSendInt(Pitch); break; case 'R': WireSendInt(Roll); break; case 'T': WireSendInt(Temp); break; case 'D': WireSendInt(Depth); break; case 'M': WireSendInt(MagneticVector); break; case 'X': WireSendInt(MagneticX); break; case 'Y': WireSendInt(MagneticY); break; case 'Z': WireSendInt(MagneticZ); break; case 'a': WireSendInt(AccelarationVector); break; case 'x': WireSendInt(AccelarationX); break; case 'y': WireSendInt(AccelarationY); break; case 'z': WireSendInt(AccelarationZ); break; case 'G': WireSendInt(GyroX); break; case 'g': WireSendInt(GyroY); break; default: break; } command = 'N'; }
void Capture_OS4000() { Serial.flush(); while ( Serial.read () != '$' ); Serial.flush(); while( Serial.available() < 127 ); for ( Position = 0; Position < 126; Position++ ){ OS4000Data[Position] = char( Serial.read()); } }
void Check_Sentence() { if ( ( OS4000Data[0] == 'O' ) && ( OS4000Data[1] == 'H' ) && ( OS4000Data[2] == 'P' ) && ( OS4000Data[3] == 'R' ) && ( OS4000Data[4] == ',' ) )SentenceState = true; else SentenceState = false; }
void Extract_Info() { Heading100 = 0; Heading10 = 0; Heading1 = 0; Heading01 = 0; if ( OS4000Data[8] == '.' ){ Heading100 = OS4000Data[5] - '0'; Heading10 = OS4000Data[6] - '0'; Heading1 = OS4000Data[7] - '0'; Heading01 = OS4000Data[9] - '0'; NextData = 10; } else if ( OS4000Data[7] == '.' ){ Heading10 = OS4000Data[5] - '0'; Heading1 = OS4000Data[6] - '0'; Heading01 = OS4000Data[8] - '0'; NextData = 9; } else if ( OS4000Data[6] == '.' ){ Heading1 = OS4000Data[5] - '0'; Heading01 = OS4000Data[7] - '0'; NextData = 8; } Heading = 1000*Heading100 + 100*Heading10 + 10*Heading1 + Heading01;
Pitch100 = 0; Pitch10 = 0; Pitch1 = 0; Pitch01 = 0; if ( OS4000Data[NextData+1] == '-' ){ if ( OS4000Data[NextData+5] == '.' ){ Pitch100 = OS4000Data[NextData+2] - '0'; Pitch10 = OS4000Data[NextData+3] - '0'; Pitch1 = OS4000Data[NextData+4] - '0'; Pitch01 = OS4000Data[NextData+6] - '0'; NextData += 7; } else if ( OS4000Data[NextData+4] == '.' ){ Pitch10 = OS4000Data[NextData+2] - '0'; Pitch1 = OS4000Data[NextData+3] - '0'; Pitch01 = OS4000Data[NextData+5] - '0'; NextData += 6; } else if ( OS4000Data[NextData+3] == '.' ){ Pitch1 = OS4000Data[NextData+2] - '0'; Pitch01 = OS4000Data[NextData+4] - '0'; NextData += 5; } Pitch = - ( 1000*Pitch100 + 100*Pitch10 + 10*Pitch1 + Pitch01 ); } else { if ( OS4000Data[NextData+4] == '.' ){ Pitch100 = OS4000Data[NextData+1] - '0'; Pitch10 = OS4000Data[NextData+2] - '0'; Pitch1 = OS4000Data[NextData+3] - '0'; Pitch01 = OS4000Data[NextData+5] - '0'; NextData += 6; } else if ( OS4000Data[NextData+3] == '.' ){ Pitch10 = OS4000Data[NextData+1] - '0'; Pitch1 = OS4000Data[NextData+2] - '0'; Pitch01 = OS4000Data[NextData+4] - '0'; NextData += 5; } else if ( OS4000Data[NextData+2] == '.' ){ Pitch1 = OS4000Data[NextData+1] - '0'; Pitch01 = OS4000Data[NextData+3] - '0'; NextData += 4; } Pitch = ( 1000*Pitch100 + 100*Pitch10 + 10*Pitch1 + Pitch01 ); }
Roll100 = 0; Roll10 = 0; Roll1 = 0; Roll01 = 0; if ( OS4000Data[NextData+1] == '-' ){ if ( OS4000Data[NextData+5] == '.' ){ Roll100 = OS4000Data[NextData+2] - '0'; Roll10 = OS4000Data[NextData+3] - '0'; Roll1 = OS4000Data[NextData+4] - '0'; Roll01 = OS4000Data[NextData+6] - '0'; NextData += 7; } else if ( OS4000Data[NextData+4] == '.' ){ Roll10 = OS4000Data[NextData+2] - '0'; Roll1 = OS4000Data[NextData+3] - '0'; Roll01 = OS4000Data[NextData+5] - '0'; NextData += 6; } else if ( OS4000Data[NextData+3] == '.' ){ Roll1 = OS4000Data[NextData+2] - '0'; Roll01 = OS4000Data[NextData+4] - '0'; NextData += 5; } Roll = - ( 1000*Roll100 + 100*Roll10 + 10*Roll1 + Roll01 ); } else { if ( OS4000Data[NextData+4] == '.' ){ Roll100 = OS4000Data[NextData+1] - '0'; Roll10 = OS4000Data[NextData+2] - '0'; Roll1 = OS4000Data[NextData+3] - '0'; Roll01 = OS4000Data[NextData+5] - '0'; NextData += 6; } else if ( OS4000Data[NextData+3] == '.' ){ Roll10 = OS4000Data[NextData+1] - '0'; Roll1 = OS4000Data[NextData+2] - '0'; Roll01 = OS4000Data[NextData+4] - '0'; NextData += 5; } else if ( OS4000Data[NextData+2] == '.' ){ Roll1 = OS4000Data[NextData+1] - '0'; Roll01 = OS4000Data[NextData+3] - '0'; NextData += 4; } Roll = ( 1000*Roll100 + 100*Roll10 + 10*Roll1 + Roll01 ); }
Temp100 = 0; Temp10 = 0; Temp1 = 0; Temp01 = 0; if ( OS4000Data[NextData+1] == '-' ){ if ( OS4000Data[NextData+5] == '.' ){ Temp100 = OS4000Data[NextData+2] - '0'; Temp10 = OS4000Data[NextData+3] - '0'; Temp1 = OS4000Data[NextData+4] - '0'; Temp01 = OS4000Data[NextData+6] - '0'; NextData += 7; } else if ( OS4000Data[NextData+4] == '.' ){ Temp10 = OS4000Data[NextData+2] - '0'; Temp1 = OS4000Data[NextData+3] - '0'; Temp01 = OS4000Data[NextData+5] - '0'; NextData += 6; } else if ( OS4000Data[NextData+3] == '.' ){ Temp1 = OS4000Data[NextData+2] - '0'; Temp01 = OS4000Data[NextData+4] - '0'; NextData += 5; } Temp = - ( 1000*Temp100 + 100*Temp10 + 10*Temp1 + Temp01 ); } else { if ( OS4000Data[NextData+4] == '.' ){ Temp100 = OS4000Data[NextData+1] - '0'; Temp10 = OS4000Data[NextData+2] - '0'; Temp1 = OS4000Data[NextData+3] - '0'; Temp01 = OS4000Data[NextData+5] - '0'; NextData += 6; } else if ( OS4000Data[NextData+3] == '.' ){ Temp10 = OS4000Data[NextData+1] - '0'; Temp1 = OS4000Data[NextData+2] - '0'; Temp01 = OS4000Data[NextData+4] - '0'; NextData += 5; } else if ( OS4000Data[NextData+2] == '.' ){ Temp1 = OS4000Data[NextData+1] - '0'; Temp01 = OS4000Data[NextData+3] - '0'; NextData += 4; } Temp = ( 1000*Temp100 + 100*Temp10 + 10*Temp1 + Temp01 ); } }
void WireSendInt (int val) { byte buf [2]; buf [0] = val >> 8; buf [1] = val & 0xFF; Wire.send (buf, 2); }
void WireSendAll () { byte buf [8]; buf [0] = Heading >> 8; }
the Extract_Info() is totaly corect it is always updates corectly the Heading PItch Roll Temp i have check it with uart output also i have tried void requestEvent() { switch (command) { case 'A': WireSendAll(); break; case 'H': WireSendInt(100); break; case 'P': WireSendInt(-214); break; case 'R': WireSendInt(5); break; case 'T': WireSendInt(-98); break;
|
|
|
|
|
30
|
Using Arduino / Networking, Protocols, and Devices / Re: I2C read problems
|
on: June 18, 2011, 05:29:50 am
|
|
i am not using it it since the time you told me not to
then my output is 0 H: 0 P: 17 R: 17 T: 304
so i wanted to know if i get two 0 bytes
or none byte
if it freeze it means that is not receiving 2 bytes
------------
but why its not receiving them?
|
|
|
|
|