Pages: 1 [2] 3 4   Go Down
Author Topic: Reading data at 10 Khz rate  (Read 15890 times)
0 Members and 1 Guest are viewing this topic.
Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Two obvious things I think to look at - firstly clock1 isn't initialised, so first pass through it won't have a reliable value.  You can fix that in your declaration:
Code:
int clock1 = 0;
Given that clock1 is a global variable, the compiler/runtime system guarantees that it will be 0 when the program starts up.  This is required by the ISO C and C++ standards.  Now, if clock1 were an auto variable, then yes, you would need the initializer.
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1122
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Given that clock1 is a global variable, the compiler/runtime system guarantees that it will be 0 when the program starts up.  This is required by the ISO C and C++ standards.  Now, if clock1 were an auto variable, then yes, you would need the initializer.
That's good to know - thanks.  Scratch that one then.
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You cannot do a serial.print() in the middle of collecting data at 10kHz. You should collect everything then print the results. I probably should not have put that in there but it depends on how many bits in total are you grabbing. I wrote that for a one-off grab of 4 bits, how many are you grabbing?

Also this

clock1 = digitalRead(pin2);

should be changed to

clock1 = clock;

as has been mentioned and I think it should be moved outside the if block. So loop() looks like this

Code:
void loop()
{
  static byte val  = 0b00001000;

    clock = digitalRead(pin2);
    if ((clock == HIGH) && (clock1 == LOW) {   
       val <<= 1;
       val |= digitalRead(pin2);
       if(val | 0x80) {   // flag bit has reached the top of the byte so we've done 4 iterations
           Serial.print(val & 0x7F, HEX);
           val = 0b00001000;
         }
     }
     clock1 = clock;   
}

Still only good for one sample though because of the serial.print()

What is this data and what is it coming from? At that speed you won't be able to keep up unless there's a break at some point to allow you time to do something with the data.


______
Rob
« Last Edit: October 02, 2012, 07:39:03 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33962
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a major problem
Code:
  Serial.begin(9600);
At this speed you can only output 960 chars per second. You are printing hex, that is 2 chars per byte so that is twenty times too slow.

Plus other stuff mentioned already.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks to everybody for their response
the Data is coming from a decoding device and it is continuously running for 1-2 hours
I just need to send those data to PC as like serial data because pc cannot read discrete input.   
Thanks in advance
Niladri
Logged

Thanx & Regards

Niladri

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to up the serial port bit rate as Mike said, use 115200 and get the serial over with fast.

But I suspect that you will have another problem. What is the format of this data? Is each 4 bits a unique value or just part of a continuous bit stream. If it's a unique value you have to sync with the nibbles and with SPI that's all but impossible without another input or a longish time delay between nibbles.

Before working any more with code we need to know the exact nature of the incoming bits and the timing between the groups of four.

Quote
Data is coming from a decoding device
Data sheet?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Decoding device is under process(We are building our own homemade BCH decoder)
You can say it is filter tuned for 5.5 khz and 3.125 khz and it generates pulses and clock recovery system recover 10 khz clock now output from the 3.125 khz is passed through not gate to get stream of 128 bits(0 and 1) now each positive edge we need to read the value and each nibble we need convert to serial data and send it to pc. AS i said before it is like data acquisition system which can sample at 10 khz rate discrete input
If you have any other inquiry please let me know
Thanks in advance
Niladri     
Logged

Thanx & Regards

Niladri

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok,

1. If there's 128 bits why do you need to grab them in nibbles.
2. Is there any space/extra time between the nibbles?
3. Is there any space/extra time between the 128-bit packets?
4. What is the format of each nibble?
5. Is there any way to detect the start/end of a 128-bit packet?

All this is leading to how do you sync with the data stream? If there is no spaces between nibbles and/or packets it is well nigh impossible to sync if you get out of whack.

EDIT: #5 added.
______
Rob
« Last Edit: October 03, 2012, 03:22:42 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No first 24 bit will be CAAC1F(in HEX) if it is then receive next 96 bits otherwise slide to next bit until it matches the string but the string matching is done on a PC where LabVIEW software is running so my job is to silde it until i get send all 96 bits command from labview

And this code is somewhat working can you take a look at it.
Code:
const int pin2 = 2;
const int pin4 = 4;
int clock;
int clock1;
int val1;
int val2;
int val3;
int val4;
int val5;
int val6;
int val7;
int val8;
byte value =0000000;

void setup()
{
  pinMode(pin2, INPUT);
  
  pinMode(pin4, INPUT);
  
  Serial.begin(2400);
}
void loop()
{
  static byte val  = 0b00000001;

    clock = digitalRead(pin2);
    if ((clock == HIGH) & (clock1 == LOW)) {  
       val <<= 1;
       val1 = digitalRead(pin4);
       val2 = digitalRead(pin4);
       val3 = digitalRead(pin4);
       val4 = digitalRead(pin4);
       val5 = digitalRead(pin4);
       val6 = digitalRead(pin4);
       val7 = digitalRead(pin4);
       val8 = digitalRead(pin4);
             if(val | 0x80) {   // flag bit has reached the top of the byte so we've done 8 iterations
          
           value=(value | (val1<<7));
           value=(value | (val2<<6));
           value=(value | (val3<<5));
           value=(value | (val4<<4));
           value=(value | (val5<<3));
           value=(value | (val6<<2));
           value=(value | (val7<<1));
           value=(value | (val8<<0));
          
          
          
           Serial.print(value & 0xFF, HEX);
           val = 0b00000001;
         }
        
    }
    clock1= clock;
}


And for your questions
1. I need to send them as hex format for labview understanding
2. No
3. No
4. HEX
5. Keep on sending that Data till i receive a command from pc send next 96 bits of data that data also need to in HEX format fro labview understanding.
Thanks in Advance
Niladri  
« Last Edit: October 03, 2012, 05:12:29 am by newbee_2 » Logged

Thanx & Regards

Niladri

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for that last code
Code:
const int pin2 = 2;
const int pin4 = 4;
int clock;
int clock1;

void setup()
{
  pinMode(pin2, INPUT);
   
  pinMode(pin4, INPUT);
 
  Serial.begin(2400);
}
void loop()
{
  static byte val  = 0b00000001;

    clock = digitalRead(pin2);
    if ((clock == HIGH) && (clock1 == LOW)) {   
       val <<= 1;

         val |= digitalRead(pin4);
             if(val | 0x80) {   // flag bit has reached the top of the byte so we've done 8 iterations
           

           
                     
           Serial.print(val & 0x7F, HEX);
           Serial.println();
           val = 0b00000001;
         }
   
    }
    clock1= clock;
}
Logged

Thanx & Regards

Niladri

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1122
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No first 24 bit will be CAAC1F(in HEX) if it is then receive next 96 bits otherwise slide to next bit until it matches the string but the string matching is done on a PC where LabVIEW software is running so my job is to silde it until i get send all 96 bits command from labview

And this code is somewhat working can you take a look at it.
...
And for your questions
1. I need to send them as hex format for labview understanding
2. No
3. No
4. HEX
5. Keep on sending that Data till i receive a command from pc send next 96 bits of data that data also need to in HEX format fro labview understanding.
Thanks in Advance
Niladri 
If I'm understanding you correctly, until you get a CAAC1F pattern match of the fixed pre-amble for the next frame/packet you won't know the bit boundary to start your 4-bit values on.  Since that's the information you can use to synchronise your serial comms you will need to do the pattern matching on your Arduino rather than the PC or you'll not be guaranteed to be sending data that's aligned to the correct 4 bit boundaries.  You can encode any arbitrary 4 bit pattern as hex and send it to the PC, but unless it's conforming to the correct 4-bit boundary your data will be meaningless.

If you know the CAAC1F pattern will be the preamble to every 128 bit data frame received, you can also use this as a mechanism to partially validate the most recent data received (ie once you've received 96 bits the next received data must be CAAC1F, or that previous data buffer is invalid).

Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Sorry for that last code
Thank goodness, I was getting worried.

So as I understand it you keep feeding bytes (with the sampled nibbles in the lower 4 bits) to the PC, the PC reads 32 ASCII HEX chars, if it doesn't see 'C','A','A','C','1','F' after 32 bytes it tells you to move a bit and the whole process starts again. At some point the start sequence is found and you are off and racing.

I think you should do this on behalf of the PC because I doubt there is enough time to get the command from the PC, you only have 100nS and a PC can't do anything reliably in that time.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Through another arduino i am feeding 0x0F but in other arduino(receiver) i am reading all kind of junk values but not 0x0F what could be the mistake on my part
Code:
const int pin2 = 2;
const int pin4 = 4;
int clock;
int clock1;



void setup()
{
  pinMode(pin2, INPUT);
   
  pinMode(pin4, INPUT);
 
  Serial.begin(2400);
}
void loop()
{
  static byte val  = 0b00000001;
  static byte value = 0b00000000;

    clock = digitalRead(pin2);
    if ((clock == HIGH) && (clock1 == LOW)) {   
       val <<= 1;

         value |= digitalRead(pin4);
         value <<= 1;
             if(val | 0x80) {   // flag bit has reached the top of the byte so we've done 8 iterations
           

           
                     
           Serial.print(value, HEX);
           Serial.println();
           val = 0b00000001;
          // value = 0b00000000;
         
         }
   
   
    }
    clock1= clock;
}
Thanks in advance
Niladri
Logged

Thanx & Regards

Niladri

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Through another arduino i am feeding 0x0F but in other arduino(receiver) i am reading all kind of junk values but not 0x0F what could be the mistake on my part
Code:
if(val | 0x80) {   // flag bit has reached the top of the byte so we've done 8 iterations
This doesn't do what you think it does, since the '|' operator is an OR, and the if statement will always be true.  What you want is '&' which is an AND.  I.e.:

Code:
if (val & 0x80) {

Now personally, I tend to prefer writing the above expression as:

Code:
if ((val & 0x80) != 0) {


You have mixed up the logic in that you should do the shift of value before doing the read and not after.

I would make 'val' a simple counter (and call it a more meaningful name).  Something like:

Code:
void loop()
{
     static byte num_bits  = 0;
     static byte value = 0;

     clock = digitalRead(pin2);
     if ((clock == HIGH) && (clock1 == LOW))
        {   
            value = (value << 1) | digitalRead (pin4);
            if (++num_bits >= 8)                          // have we done 8 iterations?  If so print it out
                {
                     Serial.print(value, HEX);
                     Serial.println();
                     num_bits = 0;
                    value = 0;
                }
        }

    clock1= clock;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot sir it is working cann't express the joy of seeing it works in words
Thanks thanks thanks and also my respect and gratitude to all Rob, Geoff , Mike without your help and suggestion it would have not been possible

Thanks to everybody for response will come back to you when stuck with another problem(i have very little knowledge so it is highly unlikely that i can say something anybody problem)

Thanks a lot
Niladri
Logged

Thanx & Regards

Niladri

Pages: 1 [2] 3 4   Go Up
Jump to: