chief 0 Posted September 13, 2011 Report Share Posted September 13, 2011 Hi, I have 2) one wire digital sensors, one on Port E.1 and the other on E.2 . I'm calling the read function with argument 1 for inside sensor or 0 for outside sensor. This is just a snippet of the code - If I change sens in the read function to in_sens, so it works for only the inside sensor, it works fine, but I've been trying to figure out how to make the function work for either sensor, and I'm stuck. sens = in_sens didn't work, a pointer didn't work (at least the way I did it). Can anyone offer a suggestion? volatile bit out_sens @ PORTE.1 volatile bit in_sens @ PORTE.2 void read(char n) { if (n == 1) //?? this is where I'm stuck - if n == 1 I need to assign the variable sens to in_sens; if it's 0, assign sens to out_sens. if (!sens) fail(1); //sensor not ready //make sens output, pull down for 1.1mS to trigger sensor if (n == 1) clear_bit(trise,2); else clear_bit(trise,1); sens = 0; delay_ms(1); delay_100us(1); if (n == 1) set_bit(trise,2); else set_bit(trise,1); // set sens back to input do //check for start of acknowledge pulse (spec 20-40us) { if (count > 4) fail(2); //no acknowledge count++; delay_10us(1); }while (sens); Quote Link to post Share on other sites
Pavel 0 Posted September 13, 2011 Report Share Posted September 13, 2011 I'd use bitwise operations: #define OUT_MASK 1 #define IN_MASK 2 void read(unsigned char mask) { if (!(porte & mask)) fail(1); //sensor not ready ... } //Read inside sensor read( IN_MASK ); //Read outside sensor read( OUT_MASK ); Regards, Pavel Quote Link to post Share on other sites
chief 0 Posted September 14, 2011 Author Report Share Posted September 14, 2011 I'd use bitwise operations: #define OUT_MASK 1 #define IN_MASK 2 void read(unsigned char mask) { if (!(porte & mask)) fail(1); //sensor not ready ... } //Read inside sensor read( IN_MASK ); //Read outside sensor read( OUT_MASK ); Regards, Pavel Thank you Pavel for pointing me in the right direction. I don't have it working yet, but I'm reading up on mask operations to figure out where I went wrong. Best regards, Brian Quote Link to post Share on other sites
Pavel 0 Posted September 14, 2011 Report Share Posted September 14, 2011 Another option is to use function templates. This will make code much simplier compared with bitwise approach but internally code for 2 copies of the function will be generated. If you don't care about code space or your function is really small go with function templates: #define OUT_PORT 0 #define IN_PORT 1 template <unsigned char BitPosition> void read(void) { volatile bit sens @ PORTE.BitPosition; if (!sens) fail(1); //sensor not ready ... } //Read inside sensor read<IN_PORT>(); //Read outside sensor read<OUT_PORT>(); Quote Link to post Share on other sites
chief 0 Posted September 16, 2011 Author Report Share Posted September 16, 2011 I'd use bitwise operations: #define OUT_MASK 1 #define IN_MASK 2 void read(unsigned char mask) { if (!(porte & mask)) fail(1); //sensor not ready ... } //Read inside sensor read( IN_MASK ); //Read outside sensor read( OUT_MASK ); Regards, Pavel Hi Pavel, I now have it working using bit masks. It was a learning experience, but that's a good thing - I learned something I didn't know before! (porte & mask) returns the weight of the bit, not 1 or 0. See my changes and comments to your example below. Thanks for your help! #define OUT_MASK 2 // for bit 1 - mask needs to be the WEIGHT of the bit, not the number. Ex: bit 1 is mask 2, bit 2 is mask 4, etc. #define IN_MASK 4 // for bit 2 void read(unsigned char mask) { if (!(porte & mask)) do_something_if_bit_is_lo; // this will test for all mask bit(s) = 0 if ((porte & mask) != mask) do_something_if_mask_bits_don't_match_mask; // this is a more explicit test, and for >1 bit mask if ((porte & mask) == mask) do_something_if_all_mask_bits_match_mask; // this is a test for matching all mask bit(s) or testing even just one bit for 1. ... } //Read inside sensor read( IN_MASK ); //Read outside sensor read( OUT_MASK ); Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.