Pages: [1]   Go Down
Author Topic: Reading pulses (HIGH's and LOW's) from external device  (Read 2239 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i am trying to get the information being sent from a device which sends 28 bits, the first 14 is a bearing next 2 is zeroes next 10 is distance and finally last 2 are zeroes again.
The pulse max volt is 2. I would like to read these pulses in order to send these information to pc. The cycle is  4,74msec of every 28 bits. Has anyone any idea which pin to use?
Because i used a digital pin (even pin0), but i couldnt get proper reasaults. Thanks in advance.(I am an arduino beginner)
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is each pulse 169.29 microseconds (4.74 mSec/28) long?

How do you know when the stream of bits starts?

I don't know if this is accurate enough but it should be a start:
Code:
while (digitalRead(inputPin) == LOW)   // Wait for a rising edge
       /* Do Nothing */ ;
unsigned long startTime = micros();
for (int bit = 0; bit < 48; bit++)
    {
    unsigned long bitTime = bit * 169.29;
    while (micros() - startTime < bitTime)   // Wait for bit to arrive
        /* Do Nothing */ ;
    bits[bit] = digitalRead(inputPin);
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

better skip halve a bit for the first bit or adapt the multiplication factor so one is not at the edge of the signal but in the middle.

here an version build upon John's one.

Note: you may need to adjust the magic number 170 to get the timing compensated for the time the loop instructions - e.g. digitalRead() - take.

Code:
while (digitalRead(inputPin) == LOW)   // Wait for a rising edge
       /* Do Nothing */ ;

unsigned long startTime = micros();
unsigned long bitTime = 50;                // skip ~halve a bit to be in the middle
for (int bit = 0; bit < 48; bit++)
    {
    while (micros() - startTime < bitTime)   // Wait for bit to arrive
        /* Do Nothing */ ;
    bits[bit] = digitalRead(inputPin);           // read the bit
    bitTime += 170;                                 // update bittime (with an addition iso float multiplication)
    }
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your task is not unlike that of a software serial uart function. But instead of converting 8 serial bits frame into single byte size characters, you have to convert the whole serial bit stream on the run. First you have to determine a valid start of the frame, then do mid bit sampling on the bit stream to extract the ones and zeros, and finally validate the end of frame markers, the two final zero bits, then get ready for the next valid frame start . Not an easy task but doable if you stick to it.

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you guys. I am going to try the code at the lab tomorrow. I'll lwt you know of the resaults. It is of great importance to finally read these bits.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you guys. I am going to try the code at the lab tomorrow. I'll lwt you know of the resaults. It is of great importance to finally read these bits.

Do you have any more details about the data stream?  Is it documented somewhere?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok this is the situation:
There are two wires coming from the device:
The first is clock: 20msec low - 20msec high , pulse has 3 volt(high)
the second holds the 28 bit information i want to retreive:
when the clock pulse turns to low the 28 bit tranmission begins.
each bit has a 156μsec duration(3.2 volt for high pulse). All the bits together have 4,37msec duration (28/4.37msec=156μsec)
The arduino doesnot recognize the high pulses (maybe its the voltage).
|____________________________________|-------------------------------------------------| (CLOCK)

                   LOW                                                              HIGH=3Volt
<-               20msec                              -><-                        20msec                     ->

|_-_-_-_-_-----___| (BINARY(28 bits): HIGH=3,2VOLT)
<-    4,37msec    ->

the first 14 bits is INFORMATION "a" next 2 bits are 0 (zero) next 10 bits is INFORMATION "b" and next 2 bits are high.
So i want to retreive info "a" & "b"
Thanks in advance!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// Wait till the clock goes from HIGH to LOW:
while (digitalRead(ClockPin) == LOW)   // Wait for the clock to be HIGH
       /* Do Nothing */ ;
while (digitalRead(ClockPin) == HIGH)   // Wait for the clock to go LOW
       /* Do Nothing */ ;

delayMicroseconds(156/2);
for (int bit = 0; bit < 48; bit++)
    {
    bits[bit] = digitalRead(inputPin);
    delayMicroseconds(156);
    }

Since digitalRead() takes a few microseconds you will probably have to reduce the delay between reads to something less than 156.  You can put timing code around the loop and adjust the timing until the total loop take 4370 microseconds.
   
Code:
unsigned long start = micros();
for (int bit = 0; bit < 48; bit++)
    {
    bits[bit] = digitalRead(inputPin);
    delayMicroseconds(156);
    }
unsigned long stop = micros();
Serial.print(stop-start);   // Adjust the delay until the loop time is as close as you can get to 4370 microseconds.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The arduino does not recognize the high pulses (maybe its the voltage).

Well three volts is marginal, but should still be seen as a valid high state. You are of course wiring a ground wire between the arduino and the source of these two signals? It is required.

Lefty
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The arduino does not recognize the high pulses (maybe its the voltage).

Well three volts is marginal, but should still be seen as a valid high state. You are of course wiring a ground wire between the arduino and the source of these two signals? It is required.

Lefty

For a less marginal signal you can power the Arduino with 4V instead of 5V.  That would put the HIGH signal level at 2.4V instead of 3.0V.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much i will try the code at the lab!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Problem SOLVED. Thank u very much. The code needed minor changes but it worked.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Can you post the final working code? Could be useful for future reference.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: