Jump to content
Sign in to follow this  
chief

Reassign Port Variable In Function?

Recommended Posts

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);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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>();

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×