I am using a mega8 and have 2 IR receivers connected to it on different pins. Is it possible to have the same routine to process each pin without having to have 2 nearly identical copies?
Sort of, but - off the top of my head - I think it will be slightly less speed/byte-efficient. At the entry to the routine, determine which pin you're interested in. Create a mask in a temp register that masks out all but that one bit. Use the mask instead of an immediate constant.
I think you'll be juggling values through read-modify-write scratch registers, though.
If you have a good macro assembler and it's duplicate source you're worried about rather than code space -- write a macro. The actual machine code will be duplicated, but you'll only be maintaining one bit of source.
--
Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
Posting from Google? See http://cfaj.freeshell.org/google/
"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
It is easier if they are pins on the same port, as then you can use a mask system (and can code up to 8 one-at-a-time IRs on one port ).
If they are on different ports, you will need conditional jumps around lines like your ones above.
If this is not carrier decode, but the lower speed data-decode, then you might simply put call GetIR_Bit's in the code whereever you tested a single pin, and then in the subroutine, selectively ready the physical pin.
I think a CheckPin routine will probably suit me best. I already have a Flags register with a couple of spare bits. Neither code space or speed are a problem. I just don't like code duplication.
The macro idea would be good but macros in AVR Studio seem too limited based on the small amount of documentation I can find.
Yes, very well. I do this for 4 inputs. I maintain the data of each software uart in a datastructure. using the bst instruction, I get the state of the particular pin into the T bit, load a pointer to the appropraite datastructure into Z and call my soft-uart routine which processes the bit in T, storing the state and eventually received byte in the structure pointed to by Z. Given enough processing power, I can serve as many serial inputs I need with only one software-uart routine.
Your problem is entirely in the realm of the psychological. Duplicate the code and be done with it. The result will be better performance and easier to understand souce code. Would anyone in his/her right mind ever call you on this? Any fool can parameterize a function, but only a chosen few can get the job done on time.
And much worse to maintain. AVR studio is not the best programming environment to do it this way. Last time I tried it, it wouldn't accept local labels in a macro. So writing the same code 4 times is very tedious since you have to number all the labels.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.