• AVR Freaks

Hot!Unexpected code with pic12f1572

Page: 12 > Showing page 1 of 2
Author
Murton Pike Systems
Super Member
  • Total Posts : 139
  • Reward points : 0
  • Joined: 2020/09/10 02:13:01
  • Location: 0
  • Status: offline
2020/11/24 08:47:55 (permalink)
0

Unexpected code with pic12f1572

I used the code SIGNAL2=!SIGNAL2 to toggle LATA5 value.
However it didnt change the pin.
I looked at the disassembly and while the code reads from the latch it writes back to porta.
For the code to work it should write back to the latch.

132: SIGNAL2=!SIGNAL2;
07F2 0022 MOVLB 0x2
07F3 080C MOVF LATA, W
07F4 1903 BTFSC STATUS, 0x2
07F5 2FF7 GOTO 0x7F7
07F6 2FFA GOTO 0x7FA
07F7 0020 MOVLB 0x0
07F8 168C BSF PORTA, 0x5
07F9 2FFC GOTO 0x7FC
07FA 0020 MOVLB 0x0
07FB 128C BCF PORTA, 0x5
post edited by Murton Pike Systems - 2020/11/24 08:49:38
#1

20 Replies Related Threads

    upand_at_them
    Super Member
    • Total Posts : 765
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 08:57:26 (permalink)
    -1 (1)
    What compiler?
    #2
    upand_at_them
    Super Member
    • Total Posts : 765
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 08:57:32 (permalink)
    +1 (1)
    [stupid forum software double post]
    #3
    rpg7
    Super Member
    • Total Posts : 1424
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 08:57:55 (permalink)
    +1 (1)
    I think it would be useful to see the macro for SIGNAL2
    #4
    Murton Pike Systems
    Super Member
    • Total Posts : 139
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:07:33 (permalink)
    0
    #define SIGNAL2     LATA,RA5
     
    Using xc8 compiler.
     
    #5
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:09:20 (permalink)
    -1 (1)
    nigelwright7558
    I used the code SIGNAL2=!SIGNAL2 to toggle LATA5 value.
    However it didnt change the pin.
    I looked at the disassembly and while the code reads from the latch it writes back to porta.
    For the code to work it should write back to the latch.

    132: SIGNAL2=!SIGNAL2;
    07F2 0022 MOVLB 0x2
    07F3 080C MOVF LATA, W
    07F4 1903 BTFSC STATUS, 0x2
    07F5 2FF7 GOTO 0x7F7
    07F6 2FFA GOTO 0x7FA
    07F7 0020 MOVLB 0x0
    07F8 168C BSF PORTA, 0x5
    07F9 2FFC GOTO 0x7FC
    07FA 0020 MOVLB 0x0
    07FB 128C BCF PORTA, 0x5

    How is SIGNAL2 defined?

    While its writing back to PORTA is wrong, it should work provided there is no RMW issue. That being said, what is really wrong with that snippet is it's reading from the LATA register instead of the LATA5 bit, assuming that is what it is defined as.


    P.S. Anyone here experiencing this forum is very very slow and unresponsive today?
     
    #6
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:09:26 (permalink)
    0 (2)
    ... stupid forum
     
    post edited by 1and0 - 2020/11/24 09:14:15
    #7
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:09:50 (permalink)
    0 (2)
    ... stupid forum
    post edited by 1and0 - 2020/11/24 09:12:43
    #8
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:09:56 (permalink)
    0 (2)
    ... stupid forum
    post edited by 1and0 - 2020/11/24 09:11:53
    #9
    Murton Pike Systems
    Super Member
    • Total Posts : 139
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:13:36 (permalink)
    0
    The previous line of code is:
    SIGNAL1=!SIGNAL1;
    Which works fine.
    I tried putting a couple of NOP(); between them but it still doesnt work as desired.
    SIGNAL1=!SIGNAL1;
    NOP();
    NOP();
    SIGNAL2=!SIGNAL2;
     
     
    The code below works fine:

    if (RL1==0)
    {
    RL1=1;
    SIGNAL1=2;
    SIGNAL2=1;
    }
    else
    {
    RL1=0;
    SIGNAL1=1;
    SIGNAL2=0;
    }
     
     
    post edited by Murton Pike Systems - 2020/11/24 09:15:59
    #10
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:15:52 (permalink)
    +3 (5)
    nigelwright7558
    #define SIGNAL2     LATA,RA5
     
    Using xc8 compiler.

    That is WRONG!  XC8 is not an assembler. ;)
     
    Try
    #define SIGNAL2  LATAbits.LATA5

     
    #11
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:20:13 (permalink)
    +1 (3)
    Better yet, use this to toggle a bit:
    #define SIGNAL2  LATAbits.LATA5
     
    SIGNAL2 ^= 1;

     
    #12
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:21:41 (permalink)
    0 (2)
    nigelwright7558
    The previous line of code is:
    SIGNAL1=!SIGNAL1;
    Which works fine.
    I tried putting a couple of NOP(); between them but it still doesnt work as desired.
    SIGNAL1=!SIGNAL1;
    NOP();
    NOP();
    SIGNAL2=!SIGNAL2;
     
     
    The code below works fine:

    if (RL1==0)
    {
    RL1=1;
    SIGNAL1=2;
    SIGNAL2=1;
    }
    else
    {
    RL1=0;
    SIGNAL1=1;
    SIGNAL2=0;
    }

    Your #define is WRONG!!!  It will expand to a comma statement.
     
    post edited by 1and0 - 2020/11/24 09:24:48
    #13
    Murton Pike Systems
    Super Member
    • Total Posts : 139
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:24:58 (permalink)
    -1 (1)
    The 
    1and0
    nigelwright7558
    #define SIGNAL2     LATA,RA5
     
    Using xc8 compiler.

    That is WRONG!  XC8 is not an assembler. ;)
     
    Try
    #define SIGNAL2  LATAbits.LATA5

     


    No, but XC8 generates assembler. I looked at its output to see what was wrong.
    The lata5 fixed it.
    The compiler shouldnt have allowed the combination I used.
     
    #14
    Murton Pike Systems
    Super Member
    • Total Posts : 139
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:34:22 (permalink)
    0
    1and0
    Better yet, use this to toggle a bit:
    #define SIGNAL2  LATAbits.LATA5
    SIGNAL2 ^= 1;


    Strangely I dont think twice about using LATAbits in my PIC32mx work.
    I got a mental block when it came to the pic12f1572.
    I am used to no port LATCH on the small PIC's.
     
     
    #15
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 09:50:05 (permalink)
    0 (2)
    nigelwright7558
    No, but XC8 generates assembler. I looked at its output to see what was wrong.

    All compilers generate assembly. It is still a compiler. So when you feed it C source code, it will compile the C code as such.
     

    The lata5 fixed it.

    Yes, LATA5 is a bit, even thought it's deprecated.  LATA,RA5 is NOT a bit -- this is a register AND a port bit.
     

    The compiler shouldnt have allowed the combination I used.

    Why not?  It is valid C.  May I suggest you to read up on the C comma operator.
    post edited by 1and0 - 2020/11/24 09:52:17
    #16
    Murton Pike Systems
    Super Member
    • Total Posts : 139
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 10:02:29 (permalink)
    0
    1and0
    Why not?  It is valid C.  May I suggest you to read up on the C comma operator.



    Just because C thinks its valid doesn't mean it is.
    "Read from port, write to latch" is possibly the most basic PIC rule.
    Write to port should be flagged up as an error in "LATCH" type PIC's.
     
     
    #17
    crosland
    Super Member
    • Total Posts : 2142
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 10:23:19 (permalink)
    0
     

    No, but XC8 generates assembler.

    Irrelevant. The input is C, regardless of what the output is.
     

    The compiler shouldnt have allowed the combination I used.

    Give a good reason why not.
     

    Just because C thinks its valid doesn't mean it is.

    It is, to a C compiler.
     

    Write to port should be flagged up as an error in "LATCH" type PIC's.

    Why? Have you read a PIC data sheet and understood the difference between writing to the port and the latch?
     



     
    #18
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 10:45:33 (permalink)
    0 (2)
    nigelwright7558
    Just because C thinks its valid doesn't mean it is.

    What kind of logic is that?!  If it's valid C code, the compiler will NOT flag an error.  As long as the code is valid, the compiler will compile it as you've written it and NOT as you've intended it.
     

    "Read from port, write to latch" is possibly the most basic PIC rule.

    Yes, and that is YOUR responsible to code it.
     

    Write to port should be flagged up as an error in "LATCH" type PIC's.

    Why? Write to PORT is a valid operation. For your PIC device, I may even write to PORT bits, instead of LAT bits, when necessary since they are located in different banks.
    post edited by 1and0 - 2020/11/24 17:13:16
    #19
    1and0
    Access is Denied
    • Total Posts : 11747
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Unexpected code with pic12f1572 2020/11/24 17:12:06 (permalink)
    0 (2)
    On a side note, OP's statement in Post #1 expands to this
    LATA, RA5 = !LATA, RA5;

    The disassembly shown in Post #1 is that of the middle part. But what happens to the accesses of the first LATA and the second RA5, as both are volatile?  Jeff (mad_c) and all, any comment would be appreciated.
     
    <edit> That is, I'd expect LATA to generate MOVF LATA,W and RA5 to generate something?.
     
    post edited by 1and0 - 2020/11/24 17:24:07
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2021 APG vNext Commercial Version 4.5