• AVR Freaks

Hot!PIC16F1459 Rotate instruction

Author
Andy123
Super Member
  • Total Posts : 621
  • Reward points : 0
  • Joined: 2005/04/25 14:20:54
  • Status: offline
2020/05/30 16:14:01 (permalink)
0

PIC16F1459 Rotate instruction

Hello,

Need some help.I am converting old PIC18F14K50 application to PIC16F1459.
This is relatively simple code (with USB HID) been converted to PIC16 to avoid external oscillator.
I was able to convert USB portion without any issues, but got stuck on this ROTATE No Carry command. 
Any idea how to convert PIC18 to PIC16 instructions below? 
asm ( "rlncf _SHIFT_REG,f,c"); 
asm ( "rrncf _SHIFT_REG,f,c"); 
In PIC18 SHIFT_REG is at address 0x00
In PIC16 I put it at 0x2020 (Bank 0, address 0x40), but can move it if necessary. This is a part of XC8 interrupt routine and needs very quick execution.These ares the only assembly lines in whole code.

I have very little PIC16 experience, so any help appreciated.
post edited by Andy123 - 2020/05/30 17:43:06
#1

14 Replies Related Threads

    NorthGuy
    Super Member
    • Total Posts : 6284
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/30 19:10:24 (permalink)
    0
    banksel _SHIFT_REG
    rlf _SHIFT_REG,f
    rrf _SHIFT_REG,f

     
     
    #2
    Andy123
    Super Member
    • Total Posts : 621
    • Reward points : 0
    • Joined: 2005/04/25 14:20:54
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/30 19:31:45 (permalink)
    0
    RRF/RLF will not make it.
    May be I did not make it clear, but I need to rotate 8 bits without Carry bit.
    I have pattern 00110011 that I need to rotate, shifting one bit per interrupt, and going to Carry breaks this pattern.
    #3
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1459 Rotate instruction 2020/05/30 19:36:27 (permalink)
    0
    The PIC16F doesn't have a rotate bypassing carry instruction.
    You can get the same effect with two instructions. i.e.
    rlf _SHIFT_REG,w
    rlf _SHIFT_REG,f

    The first one precharges carry with bit 7, without affecting the register, then the second one actually does the rotate.
    Note you do NOT use the ",c" suffix in PIC16F devices.
    You do need the BANKSEL if the register is not in common RAM, or known to be in the currently selected bank.
     

    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
    dan1138
    Super Member
    • Total Posts : 3827
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/30 19:42:39 (permalink)
    0
    ; literal translation:
    banksel _SHIFT_REG
    rlf _SHIFT_REG,f
    btfss STATUS,0
    bcf _SHIFT_REG,0
    btfsc STATUS,0
    bsf _SHIFT_REG,0

    rrf _SHIFT_REG,f
    btfss STATUS,0
    bcf _SHIFT_REG,7
    btfsc STATUS,0
    bsf _SHIFT_REG,7


    ; efficient translation:
    banksel _SHIFT_REG
    rlf _SHIFT_REG,w
    rlf _SHIFT_REG,f

    rrf _SHIFT_REG,w
    rrf _SHIFT_REG,f

    #5
    Andy123
    Super Member
    • Total Posts : 621
    • Reward points : 0
    • Joined: 2005/04/25 14:20:54
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/30 19:48:58 (permalink)
    0
    I like two RRF instruction solution, will try in the morning.
    Thank you.
    #6
    Andy123
    Super Member
    • Total Posts : 621
    • Reward points : 0
    • Joined: 2005/04/25 14:20:54
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/31 13:33:47 (permalink)
    0
    I got this working but only if I move SHIFT_REG to Bank 0 __at (0x7d)
    If I let compiler assign address, then I am getting compiler error for each Rotate instruction line
    asm ("banksel _SHIFT_REG");
    asm ( "rlf _SHIFT_REG,w");
    asm ( "rlf _SHIFT_REG,f");
     
    main.c:120:: error: (1356) fixup overflow referencing psect nvBANK3 (0x1EE) into 1 byte at 0x70/0x2 -> 0x38
    main.c:121:: error: (1356) fixup overflow referencing psect nvBANK3 (0x1EE) into 1 byte at 0x72/0x2 -> 0x39

     
    I saw some posts about this, but I am not sure if I just need to put this in Bank 0;
    asm ("banksel _SHIFT_REG");
    asm ( "rrf _SHIFT_REG&0x7f,w");
    asm ( "rrf _SHIFT_REG&0x7f,f");
     
     
    post edited by Andy123 - 2020/05/31 13:44:38
    #7
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1459 Rotate instruction 2020/05/31 15:20:07 (permalink)
    +1 (1)
    Try changing
    asm ( "rlf _SHIFT_REG,w");

    to
    asm ( "rlf _SHIFT_REG & 7Fh,w");

    The Microchip recommended version is just plain ugly
    asm("rlf " ___mkstr(BANKMASK(_SHIFT_REG)) ",w" );

     
    do the same for the second shift.
     

    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
    dan1138
    Super Member
    • Total Posts : 3827
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/31 15:22:21 (permalink)
    +1 (1)
    Andy123
    I got this working but only if I move SHIFT_REG to Bank 0 __at (0x7d) ...


    How about you post an example of a complete test program that builds using the Microchip tools for the PIC16F1459 that includes how you set up the configuration words and a sample C function that shows how you want to do an 8-bit rotate (without carry) on a memory register.
     
    Oh never mind, ric is always right, why do I bother. :)
    post edited by dan1138 - 2020/05/31 15:25:22
    #9
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1459 Rotate instruction 2020/05/31 15:27:27 (permalink)
    +2 (2)
    dan1138
    Oh never mind, ric is always right, why do I bother. :)

    I beg to differ. I'm often wrong, but I do try to correct the record if that turns out to be the case :)
     

    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
    Andy123
    Super Member
    • Total Posts : 621
    • Reward points : 0
    • Joined: 2005/04/25 14:20:54
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/31 15:58:20 (permalink)
    0
    Thanks ric,
    I actually used similar solution with 0x7F - if you take a look at my post right above yours - and it works just fine.

    But once this issue was resolved, I have found that other C code in interrupt routine is 3 times longer than the same code in PIC18 and this is critical for this application.
    So it looks like I will stick with 18F14K50
    Or move to 18F24K50 that can be used without a crystal.
    #11
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1459 Rotate instruction 2020/05/31 16:18:50 (permalink)
    0
    Andy123
    ...
    But once this issue was resolved, I have found that other C code in interrupt routine is 3 times longer than the same code in PIC18 and this is critical for this application.

    What optimisation level are you using?
    Which version XC8?
    If it's v2.xx, is it in C90, or C99 mode?
     

    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!
    #12
    NorthGuy
    Super Member
    • Total Posts : 6284
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/31 16:48:18 (permalink)
    +2 (2)
    Andy123
    But once this issue was resolved, I have found that other C code in interrupt routine is 3 times longer than the same code in PIC18 and this is critical for this application.
    So it looks like I will stick with 18F14K50



    PIC16F1459 automatically saves lots of registers when it enters an interrupt. This makes for much shorter prologue and epilogue code. Therefore, even when the code within the interrupt is longer, the overall time which interrupt takes might be less. I suggest you look at the full disassembly of your interrupt.
     
    #13
    Andy123
    Super Member
    • Total Posts : 621
    • Reward points : 0
    • Joined: 2005/04/25 14:20:54
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/05/31 17:00:38 (permalink)
    0
    What optimisation level are you using?
    Which version XC8?
    If it's v2.xx, is it in C90, or C99 mode?

    XC2.20, tried both C90 and C99 optimization level 2
     
     
    I will take a look at the performance to make sure it can run at maximum interrupt rate.
     
     
    #14
    Andy123
    Super Member
    • Total Posts : 621
    • Reward points : 0
    • Joined: 2005/04/25 14:20:54
    • Status: offline
    Re: PIC16F1459 Rotate instruction 2020/06/06 12:46:44 (permalink)
    0
    I did some interrupt testing and it looks like I can't get interrupt rate 24KHz that I was able to get on 18F14K50 and 18F24K50.
    With 16F1459 I can only get 20KHz, may be little over but it's unstable.
    Looking at the interrupt code, it significantly longer than on 18F. Overall 16F code is about 20 larger. 
    Also there is no CCP module so I have to use timer and reload preset every interrupt.
    In any case, it was good experiment and I have working USB HID routines.
     
    I think we will switch to 24K50 because I can run it without an external oscillator compare to 14K50. 
    #15
    Jump to:
    © 2020 APG vNext Commercial Version 4.5