A way to define specific ports for output bits? start output in middle port?


Results 1 to 12 of 12

Threaded View

  1. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: A way to define specific ports for output bits? start output in middle port?

    Hi,
    First of all, there's an error in my previous example. The shift left operator is of course << and nothing else, sorry about that. The "edit window" is closed so I can't go back and fix it.

    With your code as an example, try this:
    Code:
    MyLoop:
      Temp = PortB & %11110001
      PortB = Temp | (X << 1)
      X = X + 1
      IF X > 7 THEN X = 0
      PAUSE 1000
    Goto MyLoop
    Lets go thru the two first lines:
    Temp = PortB & %11110001 - This reads the state of PortB into a variable called Temp and ANDS it with the value 14. What's happening there is that Temp will be whatever PortB is except for bits 1-3 which will be cleared to 0.
    PortB = Temp | (X << 1) - This takes the value of Temp and ORs with the value of X shifted one "step" to the left. Remember that Temp is a copy of what PortB was except bits 1-3 which are now 0. So if X is 3 (%00000011) PortB will be %xxxx011x where x is whatever was there before because X is shifted one "step" before ORing it with Temp.

    When doing a bitwise AND on two numbers each bit must be '1' in BOTH numbers for the bit to be '1' in the "result. So when doing a bitwise and with a value where certain bits are '0' those bits will be '0' in the result, example.
    %11111111 & %00000000 = %00000000
    %11111111 & %11110000 = %11110000
    %11001000 & %00001111 = %00001000

    When doung a biteise OR on two numbers it's enough that a bit in either number is '1' for that bit to be '1' in the result, example:
    %11111111 | %00000000 = %11111111
    %00000000 | %00001111 = %00001111
    %11110000 | %00001111 = %11110000

    So in the above code we read in PortB, clear bits 1-3 and put the value of X in those 3 bits.

    /Henrik.
    Last edited by HenrikOlsson; - 12th January 2013 at 12:46.

Similar Threads

  1. Changing bits in a byte for multiple output
    By tazntex in forum Serial
    Replies: 3
    Last Post: - 11th August 2008, 20:10
  2. Replies: 5
    Last Post: - 30th April 2008, 21:01
  3. 28 bits --> DEC output on LCD
    By SteveB in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st August 2006, 20:33
  4. Serial Output 10 Bits
    By GEEZER in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th May 2005, 03:14
  5. Duplicating port input to port output
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 26th April 2004, 22:43

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts