element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Robotics
  • Technologies
  • More
Robotics
Forum Trying to read dip switches with microchip pic, can not compile firmware. Mplab X on Linux.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Robotics to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 3 replies
  • Subscribers 53 subscribers
  • Views 1103 views
  • Users 0 members are here
Related

Trying to read dip switches with microchip pic, can not compile firmware. Mplab X on Linux.

madthescientist
madthescientist over 1 year ago

I am doing my first robot with a Pic microcontroller.

It has a dip switich in the PCB wich has to be read at startup to change it's behaviour.

i had connect each swicht into a pic's pin. In the code, i had #defined each pin as rb(x).

Now, i wish to, at startup, had them read and set 3 different "int = x" variables" wich the void main () has to read and proceed in consequence. I tried to use "scanf (%d, switch1) and so on, yet whenever

i try to compile it and mmlab's xc8 tells me "scanf" and "printf" are not supported. I had install other compilers from Microchip, yet i have the same results.

I have a basic understanding of C language, yet i don't know how can i read 3 switches and set 3 variables, i would like to ask if someone could help me with this.

Just let me thank you for your attention.

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 1 year ago +4
    Hi, there's several things here. Firstly, you mention trying to use scanf to read the switch, i.e. scanf (%d, switch1) . You're assuming that your code will transparently do with a switch on a microcontroller…
  • jc2048
    jc2048 over 1 year ago +1
    A couple of links that might be useful https://www.microchipdeveloper.com/projects:mcu1101-project-2 https://www.microchipdeveloper.com/mcc:mccgpio
  • scottiebabe
    scottiebabe over 1 year ago +1
    If you provide the pic PN and pin you are using I can give you exact code, but you should be able to follow along https://picguides.com/beginner/digital.php
  • shabaz
    0 shabaz over 1 year ago

    Hi, there's several things here.

    Firstly, you mention trying to use scanf to read the switch, i.e. scanf (%d, switch1). You're assuming that your code will transparently do with a switch on a microcontroller what would occur with a keyboard on a PC, and that's not the case. C doesn't know what a keyboard is. There is an input/output library on PCs that knows what to do on a PC. Microcontrollers do not know what to do with a button.

    Secondly, printf and scanf are part of a library, and at a minimum require

    #include <stdio.h>

    to use the library (and the minimum might not be enough to enable it). However as mentioned in the first point, having access to printf and scanf do not mean they will do anything, they may be stubbed out until the programmer (you) do something about that with custom code that either uses buttons or a serial port, and so on.

    Thirdly, you should closely look over this code, which reads a dip switch:

    // ********** global variables **********
    // DIP switch input pins are GPIO 10, 11, 12
    const uint8_t dipswitch[]={10, 11, 12};
    // output pins:
    const uint8_t led = 13;
    // LED state
    uint8_t ledState = 0;
    
    // the special function named "setup" is automatically run once at startup
    void setup() {
      int i;
      Serial.begin(9600); // initialize the serial port
      Serial.print("hello");
      // set up inputs
      for (i=0; i<3; i++) {
        pinMode(dipswitch[i], INPUT_PULLUP);
      }
      // set up outputs
        pinMode(led, OUTPUT);
    }
    
    // the special function named "loop"is run repeatedly, forever
    void loop() {
      int i;
      // check if any dipswitch is enabled:
      for (i=0; i<3; i++) {
        if (digitalRead(dipswitch[i]) == LOW) {
          Serial.print("dipswitch #");
          Serial.print(i);
          Serial.print(" is pressed.\n");
        }
      }
      // toggle LED if the user presses the space bar
      if (Serial.available() > 0) { // one or more bytes are available to read
        if (Serial.read() == ' ') { // space bar was pressed
          ledState = ledState ^ 1; // toggle the variable
          digitalWrite(led, ledState);
        }
      }
    
      delay(100); // pause 100 msec before looping
    }
    

    That code is from a beginner-friendly environment (Arduino), and as you can see, it is very  easy-to-follow. If you're a beginner, I wouldn't recommend PIC as a starting point nowadays. You'll find thousands of examples and tutorials for more modern beginner-friendly microcontroller boards than PIC (it's not really the board, more the development environment support, libraries with functions specifically designed for beginners, and tutorials and examples). I understand you may already have a PIC board, but is it worth your time to struggle as a beginner when you could (for a few tens of $) work with a board and an environment designed for learning, where you can study and experiment at a very decent pace without needing a lot of help.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • jc2048
    0 jc2048 over 1 year ago

    A couple of links that might be useful

    https://www.microchipdeveloper.com/projects:mcu1101-project-2

    https://www.microchipdeveloper.com/mcc:mccgpio

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • scottiebabe
    0 scottiebabe over 1 year ago

    If you provide the pic PN and pin you are using I can give you exact code, but you should be able to follow along

    image

    https://picguides.com/beginner/digital.php 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube