Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
May 13th, 2013, 04:52 AM
 ISFJ
Registered User

Join Date: May 2013
Posts: 1
Time spent in forums: 24 m 53 sec
Reputation Power: 0
Detecting One Digital Signal Cycle

Hi, I am very new to C language having only used C++ when I was in university. I am using C at work and am faced with several difficulties:

I am trying to write a program that would detect levels in digital signal (1 or 0) and then get the duration from the first 0 of a cycle to the 0 on the next cycle, ie. duration of one cycle. For example, the data obtained for a digital signal cycle is 0 0 0 1 1 1 0. I will need to get the duration from the first 0 to the last 0 to indicate the total duration of a cycle.

How do I write this in C? The key point here would be to get the duration for one cycle using level detection.

Thanks in advance!

#2
May 13th, 2013, 12:22 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
Let's suppose the input source is stdin.

Code:
```#include <stdio.h>
#include <stdlib.h>

void show(int a) {
printf("duration %d\n",a);
}

int main() { /* Lambert Electronics, LLC.  USA, NY */
char s[2];
int n = 0;
while (1 == scanf("%1s", s)) {
if ('0' == *s) {
if (n) {
show(n);
n = 0;
}
} else if ('1' == *s)
++n;
else
fputs("input error\n",stderr);
}
if (n)
show(n);
return EXIT_SUCCESS;
}```
__________________
[code]Code tags[/code] are essential for python code!

#3
May 13th, 2013, 12:58 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,856
Time spent in forums: 3 Months 1 Day 5 h 6 m 32 sec
Reputation Power: 2199
You would write it in C largely the same way you would write it in C++, though without resorting to OOP. How would you write this in C++? How would that differ from C?

It sounds like this is an embedded programming problem, so console I/O (ie, stdin and stdout that in C++ you learned to access with cin and cout) would not apply.

Read the schematic to see what pin of the microprocessor that signal is coming in on. Is that an peripheral I/O port (PIO)? Is it a serial input port? Is it an interrupt input. What kind of a pin that signal is hooked up to will determine how you would detect it. We do not have the device's schematic before us, nor could you post it due to its undoubtedly being proprietary in nature. I would think that the signal should be coming in on an interrupt input which you should set for edge detection, but that must already have been decided by the hardware designer.

Your system should have a timer interrupt that you're using to keep track of time. So detect the level transition (AKA "edge") and store the start time. When you detect the level transition that marks the end of the cycle, store the stop time and take the difference between start and stop times. If the signal is coming in on an interrupt line and hence is used to trigger an interrupt, then the edge detection and start/stop time storage would go in the interrupt service routine (ISR) along with the setting of a flag to indicate the capture of a cycle time. Then in other non-interrupt code that's run periodically you can detect that flag and perform whatever calculations and reporting that you need to.

Again, all that can be done in either C or C++. There's very little that I can think of that you would be doing differently in C than you would have in C++.

#4
May 13th, 2013, 02:36 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,965
Time spent in forums: 1 Month 4 Days 3 h 25 m 52 sec
Reputation Power: 1801
I am struggling with the plausibility of this question.

First with that fact that you seem to think that knowledge of C++ rather than C is any sort of a barrier - if you don't know how to do it in C then you don't know how to do it in C++ either, so you cannot really blame that.

Second with the fact that this is a real work requirement rather than an academic assignment. It is far too trivial for that, and in a work environment you would normally have more experienced colleagues to turn to rather than a forum.

Thirdly that you assert that this is a problem of "level detection" when it is in fact one of "edge detection". You need to know when the signal changes from 1 to 0.

Anyhow, the answer to your question will depend entirely on the hardware and target platform you are coding for. It may also depend on the frequency range of the signal, the mark-space ratio, and the accuracy required. A generic C/standard library solution might be possible, but may only be practical for suitably slow signals with a low precision requirement depending on the target and what other tasks must be carried out while timing.

Let us say for example that the target is a microcontroller. In that case the best solution would be to connect the signal to an on-chip timer/counter gate such that the signal latches the count which will directly measure the length of the last cycle with zero software overhead. Most timer-counter peripherals will generate an interrupt when the count is latched, so your code can get on with other work independently.

If it is not possible to connect the signal to a timer/counter, then the second best option would be to connect the signal to an external interrupt pin, and set it to generate an interrupt on a falling edge of the signal. The interrupt handler can then read some internal clock source (an on-chip timer) to determine the period since the previous edge. This still has a low software overhead, but is likely to be less accurate and have greater jitter if the system has to handle other interrupts of a higher priority.

The worst possible solution is to continuously poll the input testing for a transition from 1 to 0 and logging the time of such an occurrence and subtracting the previous time logged from that. This might be very accurate given a suitable clock source and if that is all the system has to do, but will be less deterministic and/or precise if the system has to do other work while monitoring the signal.

On the other hand, if a solution is required on a general purpose computer running Windows or Linux for example, then since these are not real-time operating systems, precision and resolution will be limited. This may not matter for slow signals and low precision requirements - so you need to specify those if you want a sensible answer.

So:
1. What is the signal frequency range?
2. What is the signal mark-space ratio (or range)?
3. What is the target hardware platform (chip, PC etc.)?
4. How is the signal connected to the hardware (GPIO, Interrupt, Timer Input Capture, external I/O device etc)?
5. What OS is running on the system (bare-metal, RTOS, GPOS etc. )?

The answer to those is germane to the most appropriate solution. It may also be helpful to know what compiler/tool-chain you are using.

Last edited by clifford : May 14th, 2013 at 02:46 PM.

#5
May 13th, 2013, 10:47 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
Hmm. I may need to change my sucker fish threshold.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Detecting One Digital Signal Cycle

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap