inputs to a microcontroller

Discussion in 'Programmer's Corner' started by jayanthyk192, Jan 13, 2011.

  1. jayanthyk192

    Thread Starter Member

    Jan 23, 2010
    80
    0
    Hi,

    i'm using a P89V51RD2 MC for my project.i wanted to take inputs to the microcontroller through 8 pins.is there any command or function like scanf that waits until i give an input?


    also,i wanted to know if i could store these inputs on the memory of the MC.
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    There isn't a scanf function per se, but with some uCs (Forgot what 8051 variants offer), you can have on pin generate an interrupt on change, then read all 8 pins when the pin changed. The problem with that solution is if the incoming byte has a zero on the location you've set for interrupt.

    Polling, looping and looking at the pins for change is a more common way to do it, though the best way may be a parallel to serial converter that outputs a signal for interrupt when the serial buffer is ready, the bits can then be 'clocked out' of the uC one by one. This also uses many less pins.
     
  3. jayanthyk192

    Thread Starter Member

    Jan 23, 2010
    80
    0
    I have a problem with using the interrupt.i dont know how to use the interrupt service.i gooled a few pages and all the programs were in assembly,i can program only in C.I'm still a beginner and taking inputs serially is difficult for me. so could you please give the method of taking many inputs at atime?

    i would also like to understand the interrupt service programmer in C,please give a code in C,i would try understanding it.

    thank you.
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Unfortunately, I don't have any code for the 8051 series, only PICs

    There are entire forums around for the 8051 uCs, as they are very old, though there have been improvements, such as the one you are using, the base system is mostly compatible, extra features have been added is all.

    If the 8 inputs aren't a single byte, then you can poll them in a loop, saving states in registers or RAM, though you don't have much RAM to play with on microcontrollers.

    Code would look something like this:

    main()
    {
    while(1){
    if port.1 != expected {<do stuff>}
    if port.2 != expected {<do stuff>}
    if port.3 != expected {<do stuff>}
    }
    }


    Initialization and actual usable port.bit names are left out, as they are often different depending on which compiler you are using. The above pseudo-C code is the gist of what I'm referring to when speaking of "polling".

    Maybe somebody more familiar with your uC will have better input than I. If you only know C from OS based compilers, there is a big difference, since printf and scanf functions don't really exist. In their place are write to LED (on or off), or send data to LCD for output, and checking pins for high or low for "input". It's a matter of how you do this that makes the actual program's final form and function.
     
  5. jayanthyk192

    Thread Starter Member

    Jan 23, 2010
    80
    0
    thank you.i got the idea.will this program work properly:

    void main()
    {
    int a;
    i=0;
    if(input_pin1==1)
    a=1;
    else
    a=0;
    }
    i'm hoping this would store the input on input pin1 in an array.will it?
     
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    You would need to define the array beforehand, but in general, the C Syntax is the same between systems, just the libraries and identifiers for I/O change.
     
    jayanthyk192 likes this.
Loading...