No announcement yet.

bitwise operations

  • Filter
  • Time
  • Show
Clear All
new posts

  • bitwise operations

    I have a project which controls & monitors a process via the
    parallel port of my PC. This was originally driven by a Qbasic
    program, which works OK. I have tried to substitute Powerbasic
    code as it is easier to use & much more versatile as regards screen
    graphics etc, but I have trouble with the analogue to digital
    conversion routine. I suspect it may be to do with the way
    Powerbasic treats a code line in the program.This line is:
    V=V OR (INP(BASE0 + 1) AND &H10)*(2^(8-CLK))/&H10 .With Qbasic, a zero
    input voltage reads zero but with Powerbasic it reads 2.5 volts.
    I do not understand the mathematics of this code, so if any reader
    has any ideas as to whether this code is the problem or anything else I
    would be grateful.
    Frank Williams
    New Zealand
    [email protected]
    PS I also use Powerbasic for Windows-It is a great program!


  • #2
    Originally posted by frank williams:
    V=V OR (INP(BASE0 + 1) AND &H10)*(2^(8-CLK))/&H10
    Hi Frank, it's nice to see another fellow-Kiwi in these here parts. <IMG SRC="" ALT="smile">
    The above expression is combining port I/O with bitwise operators, thus:
    • BASE0 + 1 = the address of the port to read with INP()
    • AND &H10 = mask out all but Bit 4 of the value (bit numbers start at zero: &H10 -> &B00010000 -> Bit 4 is set.)
    • 2^(8-CLK) = exponentiation

    Expanded, the expression looks like this:
    x% = INP(BASE0 + 1)
    x% = (x% AND &B00010000)
    y% = 2^(8-CLK)
    z% = x% * y% / &B00010000
    v = v OR z%
    From this we can see that if the port data is zero, then x% = 0, and thus z% = 0 too. In this case, V will not change its value (since a bitwise OR with the value Zero leaves the original value unchanged).

    Are you sure that 2.5V is not actually the correct result for a "zero volt" reading? While many A/D boards read the range 0V to 5V, almost all use DC input isolation and this allows them to be configured to read a relative 0v to 5V, rather than an absolute 0V to 5V, just by setting the reference (earth/ground) voltage appropriately.

    Also, some boards can be configured to read -2.5V to +2.5V or 0V to 5V. For the former, a zero volt reading will appear to read as 2.5V if its being interpreted in the 0V to 5V range.

    The bottom line is that the "interpretation" of the actual voltage range being measured may need to be known and taken into account by the [your] software.

    Does this help?

    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    mailto:[email protected]


    • #3
      Thanks for your reply. Your explanation simplifies the
      logic for me. Will try it out & see what happens.