Like Tree3Likes
  • 3 Post By Salem

Introductory Microcontroller Help

This is a discussion on Introductory Microcontroller Help within the C Programming forums, part of the General Programming Boards category; I am trying to learn how to use C to control PIC16C57 Microcontroller chip that is built into the BASIC ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Introductory Microcontroller Help

    I am trying to learn how to use C to control PIC16C57 Microcontroller chip that is built into the BASIC Stamp 2. I have a decent, although beginner's yet, understanding of how to write code in C, but I have had no training as of yet with interfacing with hardware from C. In the end, I would like to control a simple 12V DC motor. Forward, backward, and with 3 speed settings. I have already designed the circuit necessary to drive the motor, now I just need help writing the code to control 3 pins. I was planning on using PWM for the speed control, but have been unable to find any information how to do it for someone who is just learning about this subject. Any help is appreciated. Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Have you tried searching here. The microchip website has everything you need to program the chip.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Yes, I've actually come across that site while researching this chip, but unfortunately, the information is far too technical for me to understand. I was hoping someone could point me in the direction of a "Microcontroller's for Dummies" type of site.
    This project I am working on has also raised another question: Is it possible to have a loop running in the background in C? For instance: If I want a loop running that is counting numbers, but I don't want to see it, but I want the loop to end when I push "s" and then print what number the loop got up to.
    Something along these lines:

    Code:
    #include <stdio.h>
    
    int main ()
    {
        int i, count, stop;
        for (i = 0, count = 0; i >= 0; i++) count ++; //endless loop, I know
        scanf ("%d", &stop);  //Use this input to stop the loop
        printf ("Stopped at %d.\n", count);
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    If that's too much, then I would suggest you spend a few more months getting really comfortable writing C for your desktop machine.

    Debugging a bare bones system takes some effort, even when you're totally clued up.
    Even when you think you're ready, try as many of the pre-written samples as you can, and try and tweak them towards what you want them to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    I remember my first experiences with PIC. With no formal education on the subject, it took me a long time of research and troubleshoot to get it to finally work. And that was with a degree of competence in the programming part of it. (I used RISC assembly code at the time instead of 'C').

    I don't know of any "for dummies" approach, so I can't help you there. This is a very complicated topic when one has little experience and no formal training. But maybe if I shared some advice from my own experiences, which is admittedly not about "getting it to work" but about understanding it to a degree where you know what's going on.

    - I second Salem's advice of getting comfortable coding first, as this will greatly determine the ease/difficulty of actually implementing the functions you want the PIC to perform.

    - Once you're familiar with the coding, the second beast to tackle is figuring out what needs to be done to configure the micro (initialization is not as simple as it might seem). This will require a fairly deep study of the data sheet for the device.

    - Which leads me to my third bit of advice: Study the data sheet so you know what the registers are, how they work, and so on. You do have a copy of the data sheet ... right?

    I would also recommend, if you haven't already, doing searches for "PIC16C57 tutorial." I don't know of any sites that have *all* the information you require (though others here may), but for me it was more like searching for pieces of a puzzle. After a while, I was able to put those pieces together and make an LED blink! Once you get there, you'll only get better with it.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yes you can have things going on in the background. Either using multiple executables and letting the operating system manage them or by using non-blocking I/O. For example, you could use a function which checks whether there was any keyboard input without actually waiting for it. kbhit() or something like that... but this stuff is platform dependent. Then you embed such calls in your loops and continue with the counting unless some value gets returned that indicates there's something new to do.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sanglant View Post
    This project I am working on has also raised another question: Is it possible to have a loop running in the background in C? For instance: If I want a loop running that is counting numbers, but I don't want to see it, but I want the loop to end when I push "s" and then print what number the loop got up to.
    Something along these lines:

    Code:
    #include <stdio.h>
    
    int main ()
    {
        int i, count, stop;
        for (i = 0, count = 0; i >= 0; i++) count ++; //endless loop, I know
        scanf ("%d", &stop);  //Use this input to stop the loop
        printf ("Stopped at %d.\n", count);
        return 0;
    }
    Presuming you don't mean on the microcontroller itself, it is possible. You need either a fork() with some IPC networking, or threads. I think this will be a bit of a wild distraction for you right now.

    You could also use an I/O library which will take care of some details and provide you with an "event based" API to work with (this is what nonoob is hinting at). Let us know what OS you are using and someone will have an idea.

    Primitively, you could use too separate programs, one which does the counting and checks a file at each iteration; if the file contains (eg) "STOP", then it will stop. The other program waits for input, and when you want to stop, it alters the file checked by the first program so that at the next iteration it reads "STOP".
    Last edited by MK27; 06-17-2011 at 01:35 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sanglant View Post
    I am trying to learn how to use C to control PIC16C57 Microcontroller chip that is built into the BASIC Stamp 2. I have a decent, although beginner's yet, understanding of how to write code in C, but I have had no training as of yet with interfacing with hardware from C. In the end, I would like to control a simple 12V DC motor. Forward, backward, and with 3 speed settings. I have already designed the circuit necessary to drive the motor, now I just need help writing the code to control 3 pins. I was planning on using PWM for the speed control, but have been unable to find any information how to do it for someone who is just learning about this subject. Any help is appreciated. Thanks!
    Get yourself some tutorials and/or a textbook on C... learn the language first. Then when you approach the microcontroller problem you only need to learn the specializations... not the whole thing at once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. microcontroller to C++
    By ticrou in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2011, 07:04 AM
  2. Introductory Graphics in C, Info & Tutorial
    By Adak in forum Game Programming
    Replies: 5
    Last Post: 03-16-2009, 06:57 PM
  3. A Standard CS introductory problem
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2005, 03:33 PM
  4. Replies: 3
    Last Post: 12-20-2003, 03:05 PM
  5. visual c++ introductory pak
    By sanchezero in forum Game Programming
    Replies: 3
    Last Post: 07-03-2002, 02:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21