This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

reading and skipping from port

Hi All,,

I am trying to write a simple program that do the following.

it increments a 3-bit binary number after a delay. It looks like following.

do{

if (bin_num++ > 0x7) bin_num=0;
deley();

}while(1)

Now what i want to do is that , read Port 1, and if any bit is Zero (for example if P1^3 is zero) then leave skip the number 3 in incrementing bin_num. So the bin_num will count as 0, 1, 2, 4, ..

Any quick idea how to best do this ??

Any help would be great.

Thankzz && Bye
-Rocknmoon

  • Why not have a go yourself?

    Think about how you detect that the count is 3, and how to decide whether to skip it.

    BTW: is there any particular reason why you want to do this?

  • What a curious problem. I have not tried this, but it should get you on your way.

    It is easy to check for any 1s in a word by adding to that word a word full of 1s and looking for overflow e.g.

    skip = ( P1 + (unsigned int) 0x00FF ) > 0x00FF;
    
    Its one of those things that is even easier in assembler...

    You want to check for any zeros in P1 so just complement P1 in the test.
    skip = ( ~P1 + (unsigned int) 0x00FF ) > 0x00FF;
    
    So you will end up with something like:
    do
    {
        skip = ( ~P1 + (unsigned int) 0x00FF ) > 0x00FF;
        bin_numm++;
        if ( bin_num == 0x08 ) bin_num = 0;
        if ( ( bin_num == 0x03 ) && skip ) bin_num++;
        delay();
    
    }while(1)
    
    Have fun.

  • "(bin_num++ > 0x7);"

    Danger !!

    The "C" standard specifies that these operators (++ and >) can be handled in ANY sequence. Thus Keil is free to change it between releases or Optimization levels. Also, you have no knowledge whether "comin in" with 7 or 8 is required for a true result.

    Erik


  • Here is what the standard says:

    The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented.

    So it behaves as expected. You can run into trouble if the incremented variable is used in the expression twice. This is not the case here.
    Regards,
    Mike

  • Rock,
    This should work.

    do{
      do {
        ++bin_num;
        bin_num &= 0x07;
      } while ( ((0x01 << bin_num) & P1) == 0 );
      delay();
    } while (1);
    
    You might want some kind of safety check so if all bits are '0' the inner loop doesn't just spin.
    Best luck

  • I will repeat a previus question someone made to you in other thread:

    Are you trying to read credit cards? Please ask.

    Cya.