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 8781209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:14:25+00:00 2026-06-13T20:14:25+00:00

I am writing embedded C code for a microcontroller. The code has to be

  • 0

I am writing embedded C code for a microcontroller. The code has to be shared between three different circuit boards, and the input/output configurations are set at run time from some tables during initialization.

The microcontroller has 24 ADC channels, and I have a function that can either set or clear a pin as an ADC channel. This means that an input to the function can consist of 0 to 23 (which is set in the table) and nothing else.

I would like to put some kind of preprocessor or compiler “thing” that could identify that the function received a value other than 0-23 and throw some kind of error or warning and prevent the code from compiling in case someone put an invalid value in the table.

Does anyone have some advice on how best to do this?

  • 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-06-13T20:14:27+00:00Added an answer on June 13, 2026 at 8:14 pm

    On most compilers (preprocessors) you can use the #error directive.

    I.e.

    #define ADC_CHANNEL 34
    #if ADC_CHANNEL > 23
    #error ADC_CHANNEL exceeds maximum allowed value
    #endif
    

    The above would throw an error, and would not compile.

    Then use ADC_CHANNEL as the input to your function.

    Or you can make ADC_CHANNEL an enum, and define ADC_CHANNEL_0 = 0, ADC_CHANNEL_1 = 1 … ADC_CHANNEL_23 = 23. Then make your function take type ADC_CHANNEL_t, or whatever you want to call it, and that way if the function is called using the enumerated type as the argument, there will be no way to use an out-of-bounds value.

    Example:

    typedef enum {ADC_CHANNEL_0 = 0,
                  ADC_CHANNEL_1 = 1,
                  ADC_CHANNEL_2 = 2,
                  // ...etc...
                  ADC_CHANNEL_22 = 22,
                  ADC_CHANNEL_23 = 23} adc_channel_t;
    
    void setClearAdcPin(adc_channel_t adcChannel) {
        // ...function body...
    }
    

    (You don’t technically need the = 0, = 1, etc., since the compiler will infer that from the order. By default enums start at 0 and increment by 1 for each value. But defining each value manually is safer, and lets you do things like only include the 3 possible ADC channels that you might possibly use, even if they aren’t consecutive.)

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

Sidebar

Related Questions

I'm writing some example code where an embedded Jetty server is started. The server
I'm writing system-level code for an embedded system without memory protection (on an ARM
I am writing some embedded code in C and need to use the rand()
I am writing code in embedded linux since last couple of years, I use
I'm writing the startup code for an embedded system -- the code that loads
I am writing a plugin for a cms that has jQuery embedded at the
I'm writing a C app that has an embedded lua script in it. The
I am writing some embedded code to interface with an external device over SPI.
I am writing code for a real-time program running in an embedded Linux system.
I am writing C code for an embedded target, Microchip PIC24, and the runtime

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.