• AVR Freaks

Hot!Individual bit handling

Page: 12 > Showing page 1 of 2
Author
rfjhh
Starting Member
  • Total Posts : 63
  • Reward points : 0
  • Joined: 2012/07/26 09:19:36
  • Location: 0
  • Status: offline
2020/04/08 12:22:43 (permalink)
0

Individual bit handling

Hi
I'm trying to write a bit of a variable to port D bit, for instance:
Bit 2 of variable named value in PORTBbits.RD0
value(2) --> PORTD(2)
I am using XC8.
Can anyone tell me how may I do it please
 
Best regards
 
#1

20 Replies Related Threads

    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bit handling 2020/04/08 13:17:48 (permalink)
    +1 (1)
    PORTDbits.RD0 = value >> 2;
     
    You could do:
    PORTDbits.RD0 = (value >> 2) & 1;
    but the mask isn't really needed, as only bit 0 of the result will get written to RD0 anyway.
     
     
    Edit: Corrected "PORTB" to "PORTD" where copied from post#1
     
    post edited by ric - 2020/04/08 14:57:17

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/08 14:49:19 (permalink)
    0
    But in this case, the entire PORTD is affected and I want to insert just this bit without change the rest of this port
    #3
    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bit handling 2020/04/08 14:53:33 (permalink)
    +1 (1)
    rfjhh
    But in this case, the entire PORTD is affected

    No it is not.
    You are writing to a single bit field in the port. If the entire port was affected, that would be a severe bug in the compiler.
     
    You did not mention which PIC device you are using.
    If it is a device with a LATD register, you should be writing to LATD, not PORTD.
    If it's an old PIC16F device with no LATD register, then you must consider RMW (read-modify-write) effects on the PORT register, which has ramifications for all writes to the PORT.
     
     
    post edited by ric - 2020/04/08 14:55:49

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/08 15:08:54 (permalink)
    0
    I'm using 16f887. I'll test the solution
    Regards
    #5
    1and0
    Access is Denied
    • Total Posts : 10779
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Individual bit handling 2020/04/08 15:22:18 (permalink)
    +1 (1)
    Try this:

    #define bit(var,k) (!!((var)&(1UL<<(k))))

    PORTDbits.RD0 = bit(value,2);

    #6
    1and0
    Access is Denied
    • Total Posts : 10779
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Individual bit handling 2020/04/08 15:57:20 (permalink)
    +2 (2)
    ... and here's another one:
    typedef struct {
        unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
    } bits_t;
    #define bits(var) (*(bits_t*)&(var))
     
    PORTDbits.RD0 = bits(value).b2;

    Edit: Added another ().
    #7
    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bit handling 2020/04/08 16:12:41 (permalink)
    +1 (1)
    All different ways of achieving the same end.
    The assembly code output will be much the same in all cases.
    If there's an underlying RMW problem, none of them would help.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    1and0
    Access is Denied
    • Total Posts : 10779
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Individual bit handling 2020/04/08 16:21:29 (permalink)
    0
    ric
    All different ways of achieving the same end.

    Yes.
     

    The assembly code output will be much the same in all cases.

    I think your snippet may actually uses shift instruction (in a loop); whereas mines use bit tests.
     

    If there's an underlying RMW problem, none of them would help.

    Yup. ;)
     
     
    #9
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/09 12:47:04 (permalink)
    0
    Hi
    I've fix the problem other way.
    Now I have a question:
    I'm using Proteus 8 to simulate the project. In debugging mode I don't know how to see the contents of my variables.
    I right click over the project --> click on debug, proteus starts, I set some breakpoints but I can't see my variables (by name).
    Do you know how to do it?
     
    Regards
    #10
    1and0
    Access is Denied
    • Total Posts : 10779
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Individual bit handling 2020/04/09 13:36:43 (permalink)
    0
    rfjhh
    I've fix the problem other way.

    Care to share?
     
    #11
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/09 16:09:40 (permalink)
    0
    No, I'm glad to share:
     
    #define e        PORTDbits.RD0
    void escr_nibble(char dato, char r_s)
    {
    char puerto;
    puerto = (dato << 4) & 0xF0; // aislo el nibble
    /* This is a way of insert data(4) in PORTD(4), data(5) in PORTD(5) and so on..... */ if(r_s == 1)
    puerto = puerto | 0b0000010; // rs = 1
    PORTD = puerto;
    e = 1;
    __delay_ms(1);
    e = 0; // Validación de la escritura
    __delay_ms(2);
    }
    #12
    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bit handling 2020/04/09 16:38:55 (permalink)
    0
    That code does not appear to answer the question you asked in post #1.
    That code just manually sets RD0 high, waits a short delay, then sets it low again
    (Yes, I know what you're doing to the hardware there).
     
    Your question was how to copy a variable value to a port pin.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/09 17:34:58 (permalink)
    0
    It's insert a combination of bits in a port. The bits may change depending on the data and what I need to do with hardware.
    In this case in a LCD management.
     
    Regarding my doubt about debugging, may you give me some hint please?
     
    Regards
    #14
    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bit handling 2020/04/09 18:06:39 (permalink)
    0
    Regarding your question about Proteus, I have no idea.
    I have never used Proteus, that is not a Microchip product.
    They have their own support forum over at https://support.labcenter.com/forums/
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #15
    1and0
    Access is Denied
    • Total Posts : 10779
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Individual bit handling 2020/04/10 07:20:30 (permalink)
    +1 (1)
    rfjhh
    #define e        PORTDbits.RD0
    void escr_nibble(char dato, char r_s)
    {
    char puerto;
    puerto = (dato << 4) & 0xF0; // aislo el nibble
    /* This is a way of insert data(4) in PORTD(4), data(5) in PORTD(5) and so on..... */ if(r_s == 1)
    puerto = puerto | 0b0000010; // rs = 1
    PORTD = puerto;
    e = 1;
    __delay_ms(1);
    e = 0; // Validación de la escritura
    __delay_ms(2);
    }

    rfjhh
    But in this case, the entire PORTD is affected and I want to insert just this bit without change the rest of this port

    But your code in fact writes to the entire PORTD, changing and clearing RD2 and RD3 too.


    #16
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/10 14:44:24 (permalink)
    0
    Yes, but with the data I need to be  there
    #17
    rfjhh
    Starting Member
    • Total Posts : 63
    • Reward points : 0
    • Joined: 2012/07/26 09:19:36
    • Location: 0
    • Status: offline
    Re: Individual bit handling 2020/04/10 15:45:33 (permalink)
    0
    Ok
    But I'm talking about debugging with MPLAB X.
    How may I see the variables content, something like the watch window in MPLAB 8, The stimulus window, etc.
    #18
    1and0
    Access is Denied
    • Total Posts : 10779
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Individual bit handling 2020/04/10 22:27:10 (permalink)
    0
    rfjhh
    Yes, but with the data I need to be  there

    I don't quite understand what you mean.

    rfjhh
    But I'm talking about debugging with MPLAB X.
    How may I see the variables content, something like the watch window in MPLAB 8, The stimulus window, etc.

    MPLAB X has the Watches window too.
    #19
    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bit handling 2020/04/10 22:32:38 (permalink)
    0
    rfjhh
    Ok
    But I'm talking about debugging with MPLAB X.

    Since when?
    In post#2 you specifically said you were debugging with Proteus.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5