Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:38:20+00:00 2026-05-31T12:38:20+00:00

This is an academic question (I’m not necessarily planning on doing it) but I

  • 0

This is an academic question (I’m not necessarily planning on doing it) but I am curious about how it would work. I’m thinking of a userland software (rather than hardware) solution.

I want to produce PWM signals (let’s say for a small number of digital GPIO pins, but more than 1). I would probably write a program which created a Pthread, and then infinitely looped over the duty cycle with appropriate sleep()s etc in that thread to get the proportions right.

Would this not clobber the CPU horribly? I imagine the frequency would be somewhere around the 100 Hz mark. I’ve not done anything like this before but I can imagine that the constant looping, context switches etc wouldn’t be great for multitasking or CPU usage.

Any advice about CPU in this case use and multitasking? FWIW I’m thinking of a single-core processor. I have a feeling answers could range from ‘that will make your system unusable’ to ‘the numbers involved are orders of magnitude smaller than will make an impact to a modern processor’!

Assume C because it seems most appropriate.

EDIT: Assume Linux or some other general purpose POSIX operating system on a machine with access to hardware GPIO pins.

EDIT: I had assumed it would be obvious how I would implement PWM with sleep. For the avoidance of doubt, something like this:

while (TRUE)
{
    // Set all channels high
    for (int c = 0; x < NUM_CHANNELS)
    {
        set_gpio_pin(c, 1);
    }

    // Loop over units within duty cycle
    for (int x = 0; x < DUTY_CYCLE_UNITS; x++)
    {   
        // Set channels low when their number is up
        for (int c = 0; x < NUM_CHANNELS)
        {
            if (x > CHANNELS[c])
            {
                set_gpio_pin(c, 0);
            }
        }

        sleep(DUTY_CYCLE_UNIT);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:38:21+00:00Added an answer on May 31, 2026 at 12:38 pm

    Use a driver if you can. If your embedded device has a PWM controller, then fine, else dedicate a hardware timer to generating the PWM intervals and driving the GPIO pins.

    If you have to do this at user level, raising a process/thread to a high priority and using sleep() calls is sure to generate a lot of jitter and a poor pulse-width range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem to be an academic question, but still I would be very
This is an almost academic question but I'm curious as to its answer. Suppose
I know this is a rather academic question, but I wonder how one would
This question is mostly academic. I ask out of curiosity, not because this poses
Disclaimer: This question is strictly academic. The example I'm about to give is probably
Just an academic question. I'm curious what version of this code is better (faster)
Somewhat academic question, but: How do value types like Int actually work ? I've
This is more of an academic question about performance than a realistic 'what should
This question is purely academic, because I'd never dream of doing this in real
This is a purely academic question, but what's the difference between using == and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.