Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

Clock Edge Detection...In Excel

September 25, 2014

Since I'm talking about Android at the ARM TechCon in Santa Clara next week, I decided to take a break from talking about Android and, instead, talk about a little data analysis with a modern oscilloscope.

I've always said that anyone working on an electronic system really needs an oscilloscope. Without it, you are effectively blind and just guessing at what the signals on a PC board or on a wire look like. When I first started (a long time ago), those images were mostly transient. Sure, you could get a big Polaroid camera that looked like a science fiction gun, take a picture of the screen, and use that nasty smelling chemical stick to keep the picture from fading. But that would only show you a screen's worth of data.

Back around 1980 or so, I saw an ad for something that struck me as revolutionary. It was a Nicolet oscilloscope with a floppy disk drive (and 8" floppy drive, no less). That scope was crude by today's standards (and astronomically expensive), but it was clear to me that this was something that was going to happen.

Today I have a Rigol scope that costs maybe $400 at the most. It can send data to my PC or save it to a USB stick. It can take two channels and store a half million samples of each channel's data. That leads to another problem.

I was working on a system recently that used a custom synchronous serial protocol (not one of mine). The box provided to test the serial line was giving funny results and I wanted to check to see if there was really a problem, or if the monitor was lying. So I broke out the scope. Before long, I had a giant CSV file that showed the clock and the data. Then I had to face analyzing those half million lines of data.

The data looked like this (well, the first few lines of it, anyway):

Second	Volt	Volt
-2.18E-03	2.56E+00	2.48E+00
-2.18E-03	2.56E+00	2.48E+00
-2.18E-03	2.56E+00	2.48E+00
-2.18E-03	1.11E-03	2.48E+00
-2.18E-03	1.14E-03	2.48E+00
-2.18E-03	1.22E-03	2.48E+00

I did what any good developer would do when faced with a large task. I decided to not do it and write some software to do it instead. I'm not much of a spreadsheet guy (although you might argue that I am or that I like to use spreadsheets inappropriately).

Here's the approach I took:

First, I created two columns to "digitize" the input. I decided that 2V would be my threshold. The data was inverted, so I did that in the formula, too.

The data formula for row 3 is: =IF($B3<2,1,0). The clock just has the 1 and the 0 reversed (and refers to C3): =IF($C3<2,0,1). These appear in columns E and F, respectively. The next problem is how to look at the data on the falling edge of the clock. That's another formula, but since it is time-sensitive, you can't put it in the first row of data (row 3). So the "decoded" column for row 4 looks like: =IF(AND($F3=1,$F4=0),$E4,"") .

That's a mouthful, but essentially it says, "If the last clock was a 1 and the current clock is a 0, the value of this cell is $E4. Otherwise it is blank." This winds up with mostly blank cells, with a few 1s and 0s interspersed. Of course, you could easily write it for the rising edge just as well by testing $F3 for 0 and $F4 for 1.

The big column of mostly blanks isn't much easier to read. Unless you filter the column and hide all the blanks. Then you get a nice, vertical tickertape of the decoded data. This can be exported as a CSV file (with one column it is really just as ASCII file with no commas). Then you can process it with awk, Perl, or any other language that is handy at text file manipulation. I just wanted to see my data in 16-bit words so I wound up with the awk program seen below:

/1/ {
    word = word + mask
CT%16==0 {
    printf("04X\n", word)

I don't want to get into the details of awk, but the code basically reads each line of the file. If the line has a 1 in it (/1/) the word variable gets updated. Every line shifts the mask over by one place. When the code has read 16 lines, it prints the accumulated value out in hex and resets everything for the next data word. Of course, you could just as easily reverse the bit order, swap bytes, or even decode to characters as you do this.

Granted, it probably took me 15 or 20 minutes to get this working. But even if I had not made any mistakes, combing through 500,000 lines of raw data was going to be tough. I'm guessing I would have made a mistake or two, as well.

Even though most people think of spreadsheets as a financial tool (or a poor man's database), I've used them to model systems, decode hex data, and even build Verilog test benches. Not a traditional embedded systems tool, but useful none the less.

Have you ever used (or abused) a tool to solve an embedded systems problem? Leave a comment and share your experiences. If you are at ARM TechCon, be sure to stop by and say hello.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.