• AVR Freaks

Hot!Using SFR register with XC8

Page: 12 > Showing page 1 of 2
Author
newbie_pic
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2019/07/05 19:39:30
  • Location: India
  • Status: offline
2019/08/25 08:56:42 (permalink)
0

Using SFR register with XC8

Hi, In AVR we can access any registers using _SFR_IO8(Address). I want to know how to do this with PIC?
post edited by newbie_pic - 2019/08/25 08:58:47
#1

37 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 9623
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 09:33:24 (permalink)
    +1 (1)
    newbie_pic
    Hi, In AVR we can access any registers using _SFR_IO8(Address). I want to know how to do this with PIC?

    Read "How Do I Access SFRs?" in your XC8 User's Guide.
    #2
    du00000001
    Just Some Member
    • Total Posts : 3008
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 13:09:39 (permalink)
    +1 (1)
    @newbie
    What are you bragging about?
    AVRs and PICs are similar: SFRs are memory-mapped and can be accessed like any other RAM location (unless a register is write-protected, which might require a previous unlocking operation).
     
    Beyond that: read the docs.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 19:56:02 (permalink)
    0
    I will explain what I need with an example. Consider ATmega328p, I want to make PB0 HIGH. It can be done by using DDRB and PORTB macros defined in avr/io.h. But instead I want to use memory mapped address 0x17 (for DDRB) and 0x18 (for PORTB). So I do like this
    #include <avr/sfr_defs.h>
    ...
        _SFR_IO8(0x17) = (1 << 0); // DDRB = 0x01;
        _SFR_IO8(0x18) = (1 << 0); // PORTB = 0x01;
    ...

     
    Now my question is consider PIC16F688 without using MACROS (PORTx, TRISx etc) how to make RA5 HIGH with above method. Which header file to include etc...
     
    #4
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:02:41 (permalink)
    +1 (1)
    All you need to include is <xc.h>
     
    To set RA5 high:
    PORTAbits.RA5 = 1;

     
    or if you want to set all of the PORTA bits, with all low except RA5
    PORTA = 0b00100000;

     
    You can access TRISA the same way to set the in/out directions of the pins.
     
    post edited by ric - 2019/08/25 20:05:48

    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!
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 17719
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 20:07:11 (permalink)
    +2 (2)
    This is not and AVR. Why do you care?
    You can write to the SFR directly. But since the PIC is paged. You would need to insure do are in the correct bank. You would be responsible to control the banking if you want that control.
    And those are not macros. Those are addresses.
    #6
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 20:13:00 (permalink)
    0
    Can anybody tell me what these lines mean in pic16f688.h file
    // Register: PORTA
    #define PORTA PORTA
    extern volatile unsigned char PORTA __at(0x005);
    #ifndef _LIB_BUILD
    asm("PORTA equ 05h");
    #endif

    #7
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:13:47 (permalink)
    +1 (1)
    NKurzman
    ... You can write to the SFR directly. But since the PIC is paged. You would need to insure do are in the correct bank. You would be responsible to control the banking if you want that control.
    ...

    They are writing in C, so paging is not a concern...
     I am equally bemused by the reference to "macros" and feel they are making this all much more complicated than it really is.
     
    post edited by ric - 2019/08/25 20:17:12

    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
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 20:18:11 (permalink)
    0
    Oh ok, do I have some illustrations to do that... Please. A hint is sufficient for me.
    #9
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:18:39 (permalink)
    +1 (1)
    newbie_pic
    Can anybody tell me what these lines mean in pic16f688.h file
    // Register: PORTA
    #define PORTA PORTA
    extern volatile unsigned char PORTA __at(0x005);
    #ifndef _LIB_BUILD
    asm("PORTA equ 05h");
    #endif


    They are telling the compiler that the SFR for PORTA is at address 5, and that it is volatile.
    Have you tried what I already suggested?
     

    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!
    #10
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:21:31 (permalink)
    +1 (1)
    newbie_pic
    Oh ok, do I have some illustrations to do that... Please. A hint is sufficient for me.

    What is this in reference to?
    I already showed you EXACTLY what to do.
     
    I'm going to take a wild guess here. Are you actually asking how to access an SFR indirectly?
    i.e. using a variable to indicate which address to access, rather than a constant value?
    If that is the case, you confused the issue by using constant values in your example in post#4

    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!
    #11
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 20:22:58 (permalink)
    0
    Have you tried what I already suggested?

    Yes, but I don't want to do like that. I want to access with SFR address instead.
    #12
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:25:55 (permalink)
    +1 (1)
    You have gone straight back to using ambiguous language again.
    When you say "address", do you mean "indirectly"?
    i.e. the value of the address comes from a variable?
    This is what I am asking in post#11.
    post edited by ric - 2019/08/25 20:27:00

    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
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:30:04 (permalink)
    +1 (1)
    It would be a good idea to just explain what you are trying to do, NOT what you think the solution is.
     

    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!
    #14
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 20:33:42 (permalink)
    0
    When you say "address", do you mean "indirectly"?
    i.e. the value of the address comes from a variable?

    Yes, exactly something like this
    SFR(variable) = VALUE to be written;
    When variable is 0x05 then write to PORTA bit is specfied by VALUE. If variable is 0x07 write to PORTC in PIC16F688.
    #15
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:40:18 (permalink)
    +1 (1)
    This is a basic C, using pointers. Your imagined solution is written like a macro or a function call.
    unsigned char * sfr = &PORTA;  // this will load the pointer variable with the value 0x05
    *sfr = 123;    // this will write 123 to PORTA

     
    post edited by ric - 2019/08/25 20:41:24

    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!
    #16
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 20:45:01 (permalink)
    0
    #include <avr/sfr_defs.h>
    ...
        char DDRx, PORTx;
        if (PORTB) // Consider PORTB is to be written
        {
            DDRx = 0x17;
            PORTx = 0x18;
        }
        if (PORTC) // Consider PORTC is to be written
        {
            DDRx = 0x14;
            PORTx = 0x15;
        }
        _SFR_IO8(DDRx) = (1 << bit);
        _SFR_IO8(PORTx) = (1 << bit);
    ...

    I wanted to do like this with AVR. Wanted to know how to do with PIC.
    post edited by newbie_pic - 2019/08/25 20:49:39
    #17
    ric
    Super Member
    • Total Posts : 23581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using SFR register with XC8 2019/08/25 20:59:46 (permalink)
    +1 (1)
    As I mentioned, it's MUCH quicker if you reveal what you want to do, not what you think the solution is.
     
        char* PORTx, TRISx;
        if (PORTB) // Consider PORTB is to be written
        {
            PORTx = &PORTB;
            TRISx = &TRISB;
        }
        if (PORTC) // Consider PORTC is to be written
        {
            PORTx = &PORTC;
            TRISx = &TRISC;
        }
        *PORTx = (1 << bit);
        *TRISx = (1 << bit);
    ...

    Note, the last instructions makes the pin an INPUT on a PIC.
    Maybe you want
        *TRISx = (1 << bit) ^ 0xFF;

    Of course this sets all the OTHER bits to the opposite state of the bit indicated by the "bit" variable.
    I don't know if that's a problem because you still have not revealed exactly what you're trying to do.
    I can guess, but I shouldn't have to.
     

    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!
    #18
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 21:40:52 (permalink)
    -1 (1)
    Thank you all for participating in the discussion. I will try and revert back in few days.
    #19
    newbie_pic
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2019/07/05 19:39:30
    • Location: India
    • Status: offline
    Re: Using SFR register with XC8 2019/08/25 22:26:35 (permalink)
    -1 (1)
    Hi, where is __attribute__(x) defined means in which file? And how it is used.
    post edited by newbie_pic - 2019/08/25 22:28:06
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5