• AVR Freaks

Hot!Strange Code generated with XC8

Page: 12 > Showing page 1 of 2
Author
Marcelino
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2012/10/24 09:48:57
  • Status: offline
2019/12/11 16:23:13 (permalink)
0

Strange Code generated with XC8

Hi
I am new to C and XC8 Compiler and i try to program a simple test for a PIC18F45K20 with Pickit3. All this extracted program should do is:
 
Subtract 6 from 25 until Zahl is <6. This simple test should end with LED 1 on. What is wrong with this Code?
Unfortunately XC8 generates wrong Assembler Code which is attached at the end.
Can anybody help? Thanks in advance.
 
C-Code (without Config)
 
#include "pic18f45k20.h"
#include <xc.h>
#define _XTAL_FREQ 1000000
#define Switch_Pin PORTBbits.RB0

    unsigned char Zahl;
    
void main(void)
{
/*  TRISB = 0b00000001;      // Port B all Output except RB0 Input (Switch)
    ANSELH = 0x00;                // digital Input Buffer RB0-RB4 enabled
    INTCON2bits.RBPU = 0;      // enable PORTB internal pullups
    WPUBbits.WPUB0 = 1;       // enable pull up on RB0
*/
    TRISD = 0b00000000;       // Port D alle Output (LEDS)  
    while (1)
    Zahl = 25;
    {
      PORTD = 0;                  // all LEDS off
      while (Zahl > 6);
      {
          Zahl = Zahl-6;
      }
        
      LATD = Zahl;    
      __delay_ms(2000);      // LED 2 Sec lit
    }

}


Assembler Output
!    TRISD = 0b00000000;       // Port D alle Output (LEDS)  
0x7CAC: MOVLW 0x0
0x7CAE: MOVWF TRISD, ACCESS
!    while (1)
0x7CB8: GOTO 0x7CB0
0x7CBA: NOP
!    Zahl = 25;
0x7CB0: MOVLW 0x19                                    <
0x7CB2: MOVWF Zahl, ACCESS                       <
0x7CB4: GOTO 0x7CB0                                  Here is a infinite loop!
0x7CB6: NOP
!    {
!      PORTD = 0;            // alle LEDS aus
0x7CBC: MOVLW 0x0
0x7CBE: MOVWF PORTD, ACCESS
!      while (Zahl > 6);
0x7CC0: GOTO 0x7CC8
0x7CC2: NOP
0x7CC4: GOTO 0x7CC8
0x7CC6: NOP
0x7CC8: MOVLW 0x7
0x7CCA: CPFSLT Zahl, ACCESS                     Should be CPFSGT Zahl, ACCESS!
0x7CCC: GOTO 0x7CD4
0x7CCE: NOP
0x7CD0: GOTO 0x7CD8
0x7CD2: NOP
0x7CD4: GOTO 0x7CC8
0x7CD6: NOP
0x7CD8: GOTO 0x7CDC
0x7CDA: NOP
!      {
!          Zahl = Zahl-6;
0x7CDC: MOVF Zahl, W, ACCESS
0x7CDE: ADDLW 0xFA
0x7CE0: MOVWF Zahl, ACCESS
!      }
!        
!      LATD = Zahl;    
0x7CE2: MOVFF Zahl, LATD
0x7CE4: NOP
!      __delay_ms(2000);      // LED 2 Sec lit
0x7CE6: MOVLW 0x3
0x7CE8: MOVWF 0x2, ACCESS
0x7CEA: MOVLW 0x8A
0x7CEC: MOVWF __pcstackCOMRAM, ACCESS
0x7CEE: MOVLW 0x56
0x7CF0: DECFSZ WREG, F, ACCESS
0x7CF2: BRA 0x7CF0
0x7CF4: DECFSZ __pcstackCOMRAM, F, ACCESS
0x7CF6: BRA 0x7CF0
0x7CF8: DECFSZ 0x2, F, ACCESS
0x7CFA: BRA 0x7CF0
!    }
!}
0x7CFC: GOTO 0x2
0x7CFE: NOP


#1

26 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 18186
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Strange Code generated with XC8 2019/12/11 16:39:09 (permalink)
    +1 (1)
      while (1)
        Zahl = 25;
        {
     
     Is assume you want:
     Zahl = 25;
    while (1)
     {
     
    since what you have is
    while (1) Zahl = 25;  // This will loop forever.
     
    #2
    du00000001
    Just Some Member
    • Total Posts : 3314
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/11 16:44:58 (permalink)
    0
    "PORTD = 0" is nonsense. Should be "LATD = 0".
    Overall your sw would then result in PORTD.0 emitting "1" spikes.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    ric
    Super Member
    • Total Posts : 25225
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Strange Code generated with XC8 2019/12/11 16:45:42 (permalink)
    +1 (1)
    I agree, the start of the loop is an obvious bug
    I think
        while (1)
        Zahl = 25;
        {

    was meant to be
        while (1)
        {
            Zahl = 25;

     

    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
    ric
    Super Member
    • Total Posts : 25225
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Strange Code generated with XC8 2019/12/11 16:49:56 (permalink)
    +1 (1)
    du00000001
    "PORTD = 0" is nonsense. Should be "LATD = 0".

    It doesn't matter if you write the entire port. It's only the RMW instructions setting individual bits that require LATD.
    I do agree that it's good practice to do all writes to the LAT port though.
     

    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
    1and0
    Access is Denied
    • Total Posts : 10223
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/11 17:54:17 (permalink)
    +2 (2)
    Marcelino
    Subtract 6 from 25 until Zahl is <6. This simple test should end with LED 1 on. What is wrong with this Code?
    Unfortunately XC8 generates wrong Assembler Code which is attached at the end.
    Can anybody help? Thanks in advance.

    The assembly code looks correct. It is your C code that is wrong. This as pointed out by others

       while (1)
       Zahl = 25;
       {

    and the semicolon here

         while (Zahl > 6);

     
    Edit: You probably have ignored the compiler warning of unreachable code. ;)
     
    post edited by 1and0 - 2019/12/11 17:57:26
    #6
    Aussie Susan
    Super Member
    • Total Posts : 3649
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/11 18:11:41 (permalink)
    +1 (1)
    Also only include the <xc.h> file and not the processor specific one.
    When you tell the IDE which device you are using, this is passed (via the compiler command line) to the xc.h file which selects the correct processor-specific include file for you.
    If you happen to specify the wrong file (say, when you copy code from one project to another) then that is where you can start giving the compiler wrong information about the characteristics of the device.
    Susan
    #7
    Marcelino
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2012/10/24 09:48:57
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/12 16:45:20 (permalink)
    0
    Thanks to all for your help.
    I have now corrected the while (1) error, but there is still the problem that the
    while (Zahl > 6); loop does not work. I marked the assembler steps the program follows with x.
    If i change it to (Zahl<6); it then executes the loop once.
    What is now wrong?
     
     
    #include "pic18f45k20.h"
    #include <xc.h>
    #define _XTAL_FREQ 1000000
    #define Switch_Pin PORTBbits.RB0

        unsigned char Zahl;
        
    void main(void)
    {
        TRISD = 0b00000000;       // Port D alle Output (LEDS)  
        while (1)
        {
          Zahl = 25;
          LATD = 0;            // alle LEDS OFF
          while (Zahl > 6);
          {
              Zahl = Zahl-6;
          }
            
          LATD = Zahl;    
          __delay_ms(2000);      // LED 2 Sec lit
        }

    }


    Assembler Output
    !void main(void)
    !{
    !    TRISD = 0b00000000;       // Port D alle Output (LEDS)  
    0x7CAC: MOVLW 0x0
    0x7CAE: MOVWF TRISD, ACCESS
    !    while (1)
    !    {
    !      Zahl = 25;
    0x7CB0: MOVLW 0x19
    0x7CB2: MOVWF Zahl, ACCESS
    !      LATD = 0;            // alle LEDS OFF
    0x7CB4: MOVLW 0x0
    0x7CB6: MOVWF LATD, ACCESS
    !      while (Zahl > 6);
    0x7CB8: GOTO 0x7CC0
    0x7CBA: NOP
    0x7CBC: GOTO 0x7CC0
    0x7CBE: NOP
    0x7CC0: MOVLW 0x7                   x
    0x7CC2: CPFSLT Zahl, ACCESS     x
    0x7CC4: GOTO 0x7CCC                x
    0x7CC6: NOP                               x
    0x7CC8: GOTO 0x7CD0
    0x7CCA: NOP
    0x7CCC: GOTO 0x7CC0                x
    0x7CCE: NOP
    0x7CD0: GOTO 0x7CD4
    0x7CD2: NOP
    !      {
    !          Zahl = Zahl-6;
    0x7CD4: MOVF Zahl, W, ACCESS
    0x7CD6: ADDLW 0xFA
    0x7CD8: MOVWF Zahl, ACCESS
    !      }
    !        
    !      LATD = Zahl;    
    0x7CDA: MOVFF Zahl, LATD
    0x7CDC: NOP
    !      __delay_ms(2000);      // LED 2 Sec lit
    0x7CDE: MOVLW 0x3
    0x7CE0: MOVWF 0x2, ACCESS
    0x7CE2: MOVLW 0x8A
    0x7CE4: MOVWF __pcstackCOMRAM, ACCESS
    0x7CE6: MOVLW 0x56
    0x7CE8: DECFSZ WREG, F, ACCESS
    0x7CEA: BRA 0x7CE8
    0x7CEC: DECFSZ __pcstackCOMRAM, F, ACCESS
    0x7CEE: BRA 0x7CE8
    0x7CF0: DECFSZ 0x2, F, ACCESS
    0x7CF2: BRA 0x7CE8
    0x7CF4: GOTO 0x7CB0
    0x7CF6: NOP
    !    }
    0x7CF8: GOTO 0x7CB0
    0x7CFA: NOP
    !}
    0x7CFC: GOTO 0x2
    0x7CFE: NOP


     
    #8
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/12 16:50:49 (permalink)
    0
    It's still wrong !
    while (Zahl > 6);
          {
              Zahl = Zahl-6;
          }

    edit: legit, just stuck.
    post edited by Gort2015 - 2019/12/12 16:58:11

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #9
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/12 16:55:37 (permalink)
    0
    It ended up as 1
    25 - 6 - 6 - 6 - 6

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #10
    ric
    Super Member
    • Total Posts : 25225
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Strange Code generated with XC8 2019/12/12 17:03:20 (permalink)
    +1 (1)
    Marcelino
    while (Zahl > 6); loop does not work.
    ...

    How many times do you need to be told that the semi colon should NOT be on the end of the "while()" line ?
     

    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
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/12 17:14:53 (permalink)
    0
    0x7CC0: MOVLW 0x7                   x
    0x7CC2: CPFSLT Zahl, ACCESS     x
     
    It skips if less than 7, the Invert logic would be greater than 6.
    You are perceiving it wrong.
     
    post edited by Gort2015 - 2019/12/12 17:16:06

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #12
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11526
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/12 17:44:17 (permalink)
    +6 (6)
    Your time would be better spent reading a C book and working on your C skills on your PC rather than looking for non-existent bugs in the assembler listing.
    #13
    ric
    Super Member
    • Total Posts : 25225
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Strange Code generated with XC8 2019/12/12 20:21:46 (permalink)
    +1 (1)
    Try:
    #include <xc.h>
    #define _XTAL_FREQ 1000000

    volatile unsigned char Zahl;
        
    void main(void)
    {
      TRISD = 0b00000000;       // Port D alle Output (LEDS)  
      while (1)
      {
        Zahl = 25;
        LATD = 0;              // all LEDS off
        while (Zahl > 6)
          Zahl -= 6;
            
        LATD = Zahl;    
        __delay_ms(2000);      // LED 2 Sec lit
      }
    }


    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
    andersm
    Super Member
    • Total Posts : 2696
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/12 23:24:48 (permalink)
    +2 (2)
    This
    while (Zahl > 6);

    can be rewritten as
    while (Zahl > 6) {
    }

    Do you see the problem?
    #15
    Marcelino
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2012/10/24 09:48:57
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/13 02:03:37 (permalink)
    +1 (1)
    Yes i have seen the problem. The ; after while (Zahl > 6) screwed up my wish!
    It now works beautifully ... and i can extende the program.
    Thank you so much
    #16
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/13 04:39:04 (permalink)
    0
    It does not have to be compound.
     
    while (Zahl > 6) Zahl = Zahl - 6;


    It does not do anything, it is the same as:
    Zahl = 1;
    It would cause a delay if that is the intention.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #17
    Marcelino
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2012/10/24 09:48:57
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/13 05:42:25 (permalink)
    0
    This code was just a test of a larger program to produce a dice. In the original a keypress generates a random number which is reduced to a number betwee 1 and 6.
    Initially i programmed this in assembler. While i switch now to C this was one of my problems.
    #18
    JPortici
    Super Member
    • Total Posts : 876
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/13 06:26:24 (permalink)
    +2 (2)
    you can also write it as
    number = 1 + (number % 6)
    (read on the modulo operator)
    #19
    1and0
    Access is Denied
    • Total Posts : 10223
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Strange Code generated with XC8 2019/12/13 07:54:22 (permalink)
    0
    Marcelino
    This code was just a test of a larger program to produce a dice. In the original a keypress generates a random number which is reduced to a number betwee 1 and 6.

    Use this
        Zahl = rand() % 6 + 1;

    or this with a free-running timer
        Zahl = TMR0L % 6 + 1;

    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5