Thread: Reading Data Input

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Reading Data Input

    Hi all,

    I need a bit of advice on the best and most efficient way to read 10 I/O pins. Main will be constantly scanning the pins and anytime one of the 10 pins goes logic high it will be required to be read. I have thought of having 10 IF statements in main but i dont think this is the best way. Is there another way other than using IF and also a way of prioritising pin reading?

    Look forward to your help.

    Thanks,

    Rocketman46

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What type of system are you using?

    Is there an operating system?

    Is it some kind of embedded system?

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Unless your system has dedicated hardware for generating interrupts when a pin goes high, there's no other way you can do this. You may or may not want to add a delay between the checking btw, that depends on the power consumption and accuracy you're aiming for.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi Jim,

    Thanks for your reply.

    I am using a 16bit micro running embedded c. I just tried to keep the question simple and c related.

    Cheers,

    Rocketman46

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are all 10 pins in the same 16-bit I/O register?
    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.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Hi Salem,

    No they are not all on the same I/O port. If it is beneficial i could possibly modify to one port. Do you have a better solution?

    Thanks,

    Rocketman46

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose we could spend some time making wild guesses about which uC you're using, which pins you're trying to read and how they're organised for access from the CPU.

    Or you could fully disclose what you know already.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by RocketMan46 View Post
    Hi all,

    I need a bit of advice on the best and most efficient way to read 10 I/O pins. Main will be constantly scanning the pins and anytime one of the 10 pins goes logic high it will be required to be read. I have thought of having 10 IF statements in main but i dont think this is the best way. Is there another way other than using IF and also a way of prioritising pin reading?

    Look forward to your help.

    Thanks,

    Rocketman46
    There's nothing inherently wrong with using ten "if" statements, if that will get the job done. If this satisfies your requirements without detriment, then go for it. This is where low-level logic shines.

    As Salem alluded to, if all of the bits are on a single register, you can save the previous register value and only act if the register value changes.

    If you need to get fancy, it's surprisingly easy to implement a primitive "OS" on an embedded device, using just one interrupt (creating perfectly timed slices to break up the work). I won't give it away, since it's not my work, but if you want I'll share author who explains it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading data from the users input
    By SpEkTrE in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2005, 06:15 PM
  2. Reading input from a data file
    By prefect115 in forum C Programming
    Replies: 3
    Last Post: 09-26-2004, 12:30 PM
  3. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  4. need help?? ...reading input data
    By smd in forum C++ Programming
    Replies: 0
    Last Post: 07-17-2002, 06:44 AM
  5. reading input files with different types of data
    By sanu in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2002, 08:15 AM

Tags for this Thread