Bitwise not working


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115

    Default Bitwise not working

    I know it is silly but cannot see why is not working the bitwise function. The code is:

    for i=240 to 250
    temp=portb
    portb=temp & i
    pause 100
    next i

    Port B high nibble is input while the low nibble is output. I read the whole port and want to keep the upper since they are related to Interrupt on change. So on the lower I want to follow the counting of i.

    The lower nibble always reads and presents as 0000 !

    If anyone sees what I don't please reply. It drives me mad!

    Ioannis

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Hi Ioannis,

    Here I made it clear to see.
    But, as you said, it should not provide 0000.

    Code:
    Ignore high nibble on Portb. PORTB is just an example here.
    .
    .
    i=240 to 250	11110000	11110001	11110010	11110011	11110100	11110101	11110110	11110111	11111000	11111001	11111010
    PORTB (example)	11110000	11110001	11110011	11110111	11111111	11111110	11111100	11111000	11111010	11110101	11111101
    Temp=PORTB	11110000	11110001	11110011	11110111	11111111	11111110	11111100	11111000	11111010	11110101	11111101
    PORTB=Temp & i	11110000	11110001	11110010	11110011	11110100	11110100	11110100	11110000	11111000	11110001	11111000
    
    
    
    
    .
    .
    .

    Note:
    The & (bitwise AND) operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, it sets the corresponding result bit to 0.


    ---------------------------
    Last edited by sayzer; - 18th October 2006 at 12:14.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink The stupid question ...

    Hi, Ioannis

    And what about your I/O "defines" ( TRISB = ??? ) did you declare the input and output pins ???

    Or use individual "HIGH" 's .or "LOW" 's..

    Just an idea ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    I think I found the problem.

    Here it is.

    Code:
    1st	i=240		11110000
    	PortB		11110000 'an example.
    	Temp=PORTB	11110000
    	PORTB=Temp & i	11110000
    		
    2nd	i=241		11110001
    	PORTB		11110000
    	Temp=PORTB	11110000
    	PORTB=Temp & i	11110000
    		
    3rd	i=242		11110010
    	PORTB		11110000
    	Temp=PORTB	11110000
    	PORTB=Temp & i	11110000
    .
    ..
    ...
    Etc..

    The first time you get i=240 in decimal. Which is 11110000.
    Then, whatever you "bitwise AND" it with, you will always get 11110000.

    Thus, after 240, you will always have 11110000 because each time loop increments, you assign the previous PORTB value to the temp variable.


    -------------------
    Last edited by sayzer; - 18th October 2006 at 13:13.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Code:
    For i=0 to $0F
        PORTB= i 
    next
    Last edited by mister_e; - 18th October 2006 at 13:15.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default It needs & and after needs |

    Thanks everyone for the response.

    Well sayzer gave me the kick-start!

    What I was needed was to keep the upper bits while setting or reseting the lower according to the counter in the for - next loop. This is done first by clearing the lower bits, then OR-ing with the new value. I was too tired to see it immediatly. But, thanks for the try...

    The new code should look like this:

    for i=240 to 250
    temp=portb
    temp=temp & $F0
    temp=temp|i
    portb=temp
    pause 1000
    next i

    Thanks again,

    Ioannis

  7. #7
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Steve's does it... but if you do not want to fiddle with the upper nibble latch

    Code:
    For i=0 to $0F
        temp = PORTB & $F0 ' keep upper nibble
        PORTB= temp | i       ' keep upper, set lower to i
    next
    EDIT - Wow - a lot happens in 10 minutes - I was replying to Steve's #5 and 3 more got in there! :-)
    and attachments did not work for me yesterday - I got corrupt error when tried to open
    Last edited by paul borgmeier; - 18th October 2006 at 13:50.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

Similar Threads

  1. 16F877A with 20MHz XTAL not working?
    By ustredna in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 19th August 2011, 15:04
  2. 2x16 lcd not working with pic16f72
    By vu2iia in forum Schematics
    Replies: 4
    Last Post: - 16th February 2011, 14:59
  3. Blink.Bas on 18f45k20 Newbie seeks working example.
    By DiscoEd in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th December 2009, 03:36
  4. Pic 16 F628A not working
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th March 2009, 12:26
  5. Hserin not working...
    By robert0 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 22nd August 2005, 12:25

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