Show Posts
|
|
Pages: [1] 2 3
|
|
1
|
Topics / Science and Measurement / Re: Reading data at 10 Khz rate
|
on: January 11, 2013, 12:44:12 am
|
|
That's true but i need to shift bit by bit until i find a match so skipping a clock might not be best method as the data might be the particular preamble first bit(the preamble can be anywhere i don't where it starts so shift bit by bit 1st until match found).
|
|
|
|
|
2
|
Topics / Science and Measurement / Re: Reading data at 10 Khz rate
|
on: January 10, 2013, 11:11:58 pm
|
No it is working fine and there no necessity for me make it 8 bit but for just better understanding that if the state machine works for 4 bit then if we do slight modification on the state machine it should work for 8 bit. So i modified the state machine as well as getNibble() function but it does not read any data so i came to forum for possible solution(I am work alone so i have no other alternative).
|
|
|
|
|
3
|
Topics / Science and Measurement / Re: Reading data at 10 Khz rate
|
on: January 10, 2013, 05:54:46 am
|
Just for curiosity i tried to read byte by byte rather than nibble by nibble but it does not read the data at all. Can anybody indicate where i am doing wrong. const int CLOCK_PIN = 2; const int DATA_PIN = 4; const int PACKET_BITS = 1024; const int PREAMBLE_LEN = 3; const int DATA_LEN = ((PACKET_BITS / 8) - PREAMBLE_LEN);
enum { PRE_0, PRE_1, PRE_2, PRE_3, PRE_4, PRE_5, DATA } states;
byte preamble [] = {0xCA,0xAC,0x1F}; char hexAsciiVals[] = "0123456789ABCDEF0123456789ABCDEF"; byte state = PRE_0; byte nibble; int nibble_count = 0; byte firstval; //const char header = "CAAC1F";
void setup() { pinMode(CLOCK_PIN, INPUT); pinMode(DATA_PIN, INPUT); Serial.begin(57600); }
void loop() {
nibble = getNibble(); //nibble_count++; switch (state) { case PRE_0: // if (nibble_count > PREAMBLE_LEN + DATA_LEN) { // // we've read 32 bytes and still not found a match // // for the preamble so skip a clock pulse // while (digitalRead(CLOCK_PIN) == LOW); // while (digitalRead(CLOCK_PIN) == HIGH); // nibble_count = 0; // } else // { // firstval = 12; // Serial.write(hexAsciiVals[firstval]); state = (nibble == preamble[0]) ? PRE_1 : PRE_0; // Serial.write(hexAsciiVals[nibble]); // } break; case PRE_1: // Serial.write (hexAsciiVals[firstval]); // Serial.print(hexAsciiVals[12]); // Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[1]) ? PRE_2 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break; case PRE_2: // Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[2]) ? DATA : PRE_0; //Serial.write (hexAsciiVals[nibble]); break;
// case PRE_3: // // Serial.print(hexAsciiVals[nibble]); // state = (nibble == preamble[3]) ? PRE_4 : PRE_0; // // Serial.write (hexAsciiVals[nibble]); // break; // // case PRE_4: // // Serial.print(hexAsciiVals[nibble]); // state = (nibble == preamble[4]) ? PRE_5 : PRE_0; // // Serial.write (hexAsciiVals[nibble]); // break; // // case PRE_5: // // Serial.print(hexAsciiVals[nibble]); // // Serial.print(202, HEX); // // Serial.print(172, HEX); // // Serial.println(); // state = (nibble == preamble[5]) ? DATA : PRE_0; // Serial.print("$"); // // nibble_count = 0; // break; case DATA: // Serial.print("CAAC1F"); Serial.print(nibble, HEX); if (nibble_count == DATA_LEN) { // all done, start again state = PRE_0; nibble_count = 0; // Serial.print("\n"); // Serial.print(hexAsciiVals[12]); } nibble_count++; //Serial.println(); break; } // delay(10); }
byte getNibble() {
byte val = 0; for (byte bit_count = 0; bit_count < 8; bit_count++) { while (digitalRead(CLOCK_PIN) == HIGH); while (digitalRead(CLOCK_PIN) == LOW); val <<= 1; val |= digitalRead(DATA_PIN); } return (val &= 0xFF); }
|
|
|
|
|
5
|
Using Arduino / Programming Questions / Re: Identifying a character from serial.read()
|
on: January 10, 2013, 05:38:52 am
|
Hey sorry for the last one. Here the compile code. char serial_data[100]; int index = 0; char data; void setup() { Serial.begin(9600); }
void loop() { if(Serial.available()>0) { data = Serial.read(); // if(ch[0] == "A" ) // { serial_data[index++] = data; // char frstchar = buff[0]; if(serial_data[0] == 'A') // for(int i = 0; i<=20; i++) {
Serial.print(1, DEC); // delay(500); } else Serial.print(0, DEC); } delay(500); }
|
|
|
|
|
6
|
Using Arduino / Programming Questions / Re: Identifying a character from serial.read()
|
on: January 10, 2013, 02:37:00 am
|
I followed the procedure what Mr. PaulS suggested yet still not able to detect the character "A". char serial_data[100]; int index = 0;
void setup() { Serial.begin(9600); }
void loop() { if(Serial.available()>0) { for(int index = 0; index <=20; index++) char data = Serial.read();
// if(ch[0] == "A" ) // { serial_data[index] = data; } // char frstchar = buff[0]; if(serial_data[0] == 'A') // for(int i = 0; i<=20; i++) {
Serial.print(1, DEC); // delay(500); } else Serial.print(0, DEC); } delay(500); }
|
|
|
|
|
8
|
Using Arduino / Programming Questions / Re: Identifying a character from serial.read()
|
on: January 09, 2013, 06:56:36 am
|
|
Yeah that i have it in my mind but the data i read from serial read is one byte i cannot store in a array or if i store it a char then the entire data is one character that i don't no any traversal technique of find a character in a one "big character"???
Thanks & Regards
Niladri
|
|
|
|
|
9
|
Using Arduino / Programming Questions / Re: Identifying a character from serial.read()
|
on: January 09, 2013, 06:44:24 am
|
|
Not exactly. I found the substring example sketch so i tried to use that but if i don't use typecasting it says " Invalid conversion from int to const char*". My main requirement is i will have set of data preceding with A B C etc so that it will be my indicator that i have to read next 4 character do engineering conversion to make floating data if you better solution/idea please post it.
Thanks & Regards
Niladri
|
|
|
|
|
10
|
Using Arduino / Programming Questions / Re: Identifying a character from serial.read()
|
on: January 09, 2013, 06:33:22 am
|
Sorry... void setup() { Serial.begin(9600); }
void loop() { if(Serial.available()>0) { String data = (String)Serial.read(); // char* serial_data[20]={data}; // char *ch= serial_data; // if(ch[0] == "A" ) // { if(data.substring(0,4) == "A1234") // for(int i = 0; i<=20; i++) {
Serial.print(1); // delay(500); } else Serial.print(0); } delay(500); }
|
|
|
|
|
13
|
Topics / Science and Measurement / problem regarding serial print
|
on: December 08, 2012, 02:13:13 am
|
Hi guys I should not start this new thread but problem is different In my previous post i asked about reading data at 10 khz and with everybody help(especially Mr. Graynomad hats off to you!) we are able to solve it But in the code we are using state machine to check the header("CAAC1F") but if i put any serial print inside those state it does not respond it keep on stuck on first state and also when we put some special character the behavior of the port changes(some time the port transmits and some times it does not arbitrarily ) const int CLOCK_PIN = 2; const int DATA_PIN = 4; const int PACKET_BITS = 1024; const int PREAMBLE_LEN = 6; const int DATA_LEN = ((PACKET_BITS / 4) - PREAMBLE_LEN);
enum { PRE_0, PRE_1, PRE_2, PRE_3, PRE_4, PRE_5, DATA } states;
byte preamble [] = {0x0C,0x0A,0x0A,0x0C,0x01,0x0F}; char hexAsciiVals[] = "0123456789ABCDEF"; byte state = PRE_0; byte nibble; int nibble_count = 0; byte firstval; //const char header = "CAAC1F";
void setup() { pinMode(CLOCK_PIN, INPUT); pinMode(DATA_PIN, INPUT); Serial.begin(57600); }
void loop() {
nibble = getNibble(); //nibble_count++; switch (state) { case PRE_0: // if (nibble_count > PREAMBLE_LEN + DATA_LEN) { // // we've read 32 bytes and still not found a match // // for the preamble so skip a clock pulse // while (digitalRead(CLOCK_PIN) == LOW); // while (digitalRead(CLOCK_PIN) == HIGH); // nibble_count = 0; // } else // { // firstval = 12; // Serial.write(hexAsciiVals[firstval]); state = (nibble == preamble[0]) ? PRE_1 : PRE_0; // Serial.write(hexAsciiVals[nibble]); // } break; case PRE_1: // Serial.write (hexAsciiVals[firstval]); // Serial.print(hexAsciiVals[12]); // Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[1]) ? PRE_2 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break; case PRE_2: // Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[2]) ? PRE_3 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break;
case PRE_3: // Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[3]) ? PRE_4 : PRE_0; // Serial.write (hexAsciiVals[nibble]); break;
case PRE_4: // Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[4]) ? PRE_5 : PRE_0; // Serial.write (hexAsciiVals[nibble]); break; case PRE_5: // Serial.print(hexAsciiVals[nibble]); // Serial.print(202, HEX); // Serial.print(172, HEX); // Serial.println(); state = (nibble == preamble[5]) ? DATA : PRE_0; Serial.print("$"); // nibble_count = 0; break; case DATA: // Serial.print("CAAC1F"); Serial.write(hexAsciiVals[nibble]); if (nibble_count == DATA_LEN) { // all done, start again state = PRE_0; nibble_count = 0; // Serial.print("\n"); // Serial.print(hexAsciiVals[12]); } nibble_count++; //Serial.println(); break; } // delay(10); }
byte getNibble() {
byte val = 0; for (byte bit_count = 0; bit_count < 4; bit_count++) { while (digitalRead(CLOCK_PIN) == HIGH); while (digitalRead(CLOCK_PIN) == LOW); val <<= 1; val |= digitalRead(DATA_PIN); } return (val &= 0x0F); }
Any idea??? 
|
|
|
|
|
14
|
Topics / Science and Measurement / Re: Reading data at 10 Khz rate
|
on: November 23, 2012, 05:36:52 am
|
Finally it's working thanks everyone for their help and support without your help it would not been possible and i am posting the working code if in case anyone wants to refer it for their future use.... Thanks... const int CLOCK_PIN = 2; const int DATA_PIN = 4; const int PACKET_BITS = 256; const int PREAMBLE_LEN = 6; const int DATA_LEN = ((PACKET_BITS / 4) - PREAMBLE_LEN);
enum { PRE_0, PRE_1, PRE_2, PRE_3, PRE_4, PRE_5, DATA } states;
byte preamble [] = {0x0C,0x0A,0x0A,0x0C,0x01,0x0F}; char hexAsciiVals[] = "0123456789ABCDEF"; byte state = PRE_0; byte nibble; int nibble_count = 0; byte firstval;
void setup() { pinMode(CLOCK_PIN, INPUT); pinMode(DATA_PIN, INPUT); Serial.begin(9600); }
void loop() {
nibble = getNibble(); //nibble_count++; switch (state) { case PRE_0: // if (nibble_count > PREAMBLE_LEN + DATA_LEN) { // // we've read 32 bytes and still not found a match // // for the preamble so skip a clock pulse // while (digitalRead(CLOCK_PIN) == LOW); // while (digitalRead(CLOCK_PIN) == HIGH); // nibble_count = 0; // } else // { // firstval = 12; // Serial.write(hexAsciiVals[firstval]); state = (nibble == preamble[0]) ? PRE_1 : PRE_0; // Serial.write(hexAsciiVals[nibble]); // } break; case PRE_1: // Serial.write (hexAsciiVals[firstval]); // Serial.print(hexAsciiVals[12]); Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[1]) ? PRE_2 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break; case PRE_2: Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[2]) ? PRE_3 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break;
case PRE_3: Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[3]) ? PRE_4 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break;
case PRE_4: Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[4]) ? PRE_5 : PRE_0; //Serial.write (hexAsciiVals[nibble]); break; case PRE_5: Serial.print(hexAsciiVals[nibble]); state = (nibble == preamble[5]) ? DATA : PRE_0; //Serial.write (hexAsciiVals[nibble]); // nibble_count = 0; break; case DATA: Serial.print(hexAsciiVals[nibble]); if (nibble_count == DATA_LEN) { // all done, start again state = PRE_0; nibble_count = 0; Serial.println(' '); Serial.print(hexAsciiVals[12]); } nibble_count++; //Serial.println(); break; } // delay(10); }
byte getNibble() {
byte val = 0; for (byte bit_count = 0; bit_count < 4; bit_count++) { while (digitalRead(CLOCK_PIN) == HIGH); while (digitalRead(CLOCK_PIN) == LOW); val <<= 1; val |= digitalRead(DATA_PIN); } return (val &= 0x0F); }
|
|
|
|
|
15
|
Topics / Science and Measurement / Re: Reading data at 10 Khz rate
|
on: November 13, 2012, 03:17:08 am
|
Sorry this is the present code const int CLOCK_PIN = 2; const int DATA_PIN = 4; const int PACKET_BITS = 128; const int PREAMBLE_LEN = 6; const int DATA_LEN = 26;
enum { PRE_0, PRE_1, PRE_2, PRE_3, PRE_4, PRE_5, DATA } states;
byte preamble [] = {0x0C,0x0A,0x0A,0x0C,0x01,0x0F}; char hexAsciiVals[] = "0123456789ABCDEF"; byte state = PRE_0; byte nibble; int nibble_count = 0;
void setup() { pinMode(CLOCK_PIN, INPUT); pinMode(DATA_PIN, INPUT); Serial.begin(115200); }
void loop() {
nibble = getNibble(); nibble_count++; // Serial.print(digitalRead(CLOCK_PIN)); switch (state) { case PRE_0: if (nibble_count > 32) { // we've read 32 bytes and still not found a match // for the preamble so skip a clock pulse while (digitalRead(CLOCK_PIN) == LOW); while (digitalRead(CLOCK_PIN) == HIGH); nibble_count = 0; } else { if(nibble == preamble[0]) state = PRE_1; else state = PRE_0; } break; case PRE_1: if(nibble == preamble[1]) state = PRE_2; else state = PRE_0; break; case PRE_2: if(nibble == preamble[2]) state = PRE_3; else state = PRE_0; break; case PRE_3: if(nibble == preamble[3]) state = PRE_4; else state = PRE_0; break; case PRE_4: if(nibble == preamble[4]) state = PRE_5; else state = PRE_0; break; case PRE_5: if(nibble == preamble[5]) state = DATA; else state = PRE_0; break; case DATA: Serial.print (hexAsciiVals[nibble]); if (nibble_count == DATA_LEN) { // all done, start again state = PRE_0; nibble_count = 0; Serial.println(); } break; } }
byte getNibble() {
byte val = 0; for (byte bit_count = 0; bit_count < 4; bit_count++) { while (digitalRead(CLOCK_PIN) == HIGH); while (digitalRead(CLOCK_PIN) == LOW); val <<= 1; val |= digitalRead(DATA_PIN); } return (val &= 0x0F); }
|
|
|
|
|