1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Microchip hints, bug fixes, and work arounds.

Discussion in 'Microcontrollers' started by 3v0, Jul 2, 2010.

  1. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    I am starting this thread in the hope that we can make it a sticky. The goal is to post known solutions for bugs related to MPLAB and all PIC compilers.
     
    Last edited: Jul 2, 2010
  2. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    MPLAB.
    The only sure way I know of fixing the C018i error is to specify the lib path from PROJECT>BUILD_OPTIONS_PROJECT
    Select the DIRECTORIES tab and followed by "LIBRARY SEARCH PATH" from the drop down box "show directories for". Add the path C:\MCC18\lib
     
  3. edeca

    edeca Active Member

    Joined:
    Apr 8, 2008
    Messages:
    589
    Likes:
    38
    Location:
    England
    I recently had trouble debugging HI-TECH C code using MPLAB. The fix (and more details) are at HI-TECH Software Forums: Debugging using MPLAB

    Only applies to HI-TECH C, but might be useful to someone else.

    I have copied that post here
    I believe I have now fixed this. To cause the problem:

    1) Start a new project in MPLAB (using the wizard or not)
    2) Add the source files (from the post above)
    3) Rebuild

    For me, this results in a build log like the first one in the attached file.

    Now to "fix" the problem:

    1) Click Project -> Build Options -> Project
    2) Toggle a random option off then on
    3) Click "Apply" then "OK"
    4) Rebuild

    For me this results in the second log in the attached file.

    Notice how they are different:

    Code:// Works
    Executing: "C:\Program Files\HI-TECH Software\PICC-18\PRO\9.63\bin\picc18.exe" --pass1 C:\MPLAB2\delay.c -q --chip=18F2321 -P --runtime=default,+clear,+init,-keep,-download,+stackwarn,-config,+clib,-plib --opt=default -D__DEBUG=1 --debugger=pickit2 -Blarge --double=24 --cp=16 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"

    // Doesn't work
    Executing: "C:\Program Files\HI-TECH Software\PICC-18\PRO\9.63\bin\picc18.exe" --pass1 C:\MPLAB2\delay.c -q --chip=18F2321 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"

    The --runtime option is different, --debugger=pickit2 is added (auto detected) and a bunch of other options are there.

    I have tried this 3 times now with new projects and this is completely reproducible.
     
    Last edited by a moderator: Jun 21, 2011
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA

    C18 compiler shortcomings.

    The following was posted by Pommie in another thread.

    I have today converted a project from BoostC to C18 and had a rather nasty bug. I finally tracked it down to one bit of code that wasn't working correctly. Here is that code in isolation,
    Code (text):

    #include <p18f1320.h>
    #pragma config WDT = OFF, LVP = OFF, OSC = INTIO2

    void main(){
    char count;
    unsigned int temp;
        OSCCON=0x60;                //Osc=4MHz
        ADCON1=0x7f;                //All digital
        count=0;
        temp=0b0000100000000000;
        while((temp&(1<<count))==0){
            count++;
        }
        while(1);
    }
     
    What this code should do is find the first non zero bit in temp and so count should contain 11 after it executes. Well, it doesn't, it contains 7. C18 works with bytes when it does 1<<count. What is even more peculiar is that temp=1<<11 will place zero in temp not 0x0800 as you would expect.

    You can fix the above by doing
    while((temp&((int)1<<count))==0) or adding -Oi command line option.

    So, why am I telling you this. Cause it took me a long time to find it and I just had to get it of my chest. Plus, it may help others.

    BTW, the above worked fine in BoostC and it is the C18 compiler that doesn't comply with ISO.

    Edit, One good point about C18 is that it's free.

    Mike.

    and


    Sure, add it to that thread. It's not really a bug but can cause some nasty bugs.

    Who would have thought that writing,
    Code (text):
     
     unsigned int temp=10*60;
     
    would not be the same as,
    Code (text):
     
     unsigned int temp=600;
     
    I always assumed that constants would be treated as 32 (or even 64) bits on the PC and cast to the appropriate size during assignment.

    Mike.
     
    Last edited: Jul 12, 2010
  6. rsp

    rsp New Member

    Joined:
    Nov 12, 2010
    Messages:
    2
    Likes:
    1
    solution

    To product a C18 assembler listing (.lst) file, you need to run mp2cod.exe...

    Project > Build Options > Project
    custom build tab
    post build options
    mp2cod.exe yourfile.cof

    Credit for this solution goes to Mark at edaboard.com
     
  7. RufusVS

    RufusVS New Member

    Joined:
    Dec 9, 2010
    Messages:
    6
    Likes:
    0
    Dang, that's bad! I'd have thought the same.

    Would this compile correctly?

    Code (text):

           unsigned int temp = (int)10 * (int) 60;
     
    not that it should be necessary.
     
  8. EN0

    EN0 Member

    Joined:
    Jul 14, 2009
    Messages:
    476
    Likes:
    11
    Location:
    Outer Space
    Hello,

    I just recently had a very frustrating problem with HI-TECH for the PIC18 series (Lite version). The I2C routines were giving me strange errors:

    Code (text):
    Executing: "C:\Program Files (x86)\HI-TECH Software\PICC-18\PRO\9.66\bin\picc18.exe" --pass1 "C:\Users\Austin Schaller\Documents\MPLAB Projects\MPLAB C Projects\I²C_18LF4520_HT.c" -q --chip=18F4520 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"

    Executing: "C:\Program Files (x86)\HI-TECH Software\PICC-18\PRO\9.66\bin\picc18.exe" -oI²C_18LF4528_HT.cof -mI²C_18LF4528_HT.map --summary=default --output=default I²C_18LF4520_HT.p1 --chip=18F4520 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"

    HI-TECH C PRO for the PIC18 MCU Family  V9.66

    Copyright (C) 1984-2011 HI-TECH SOFTWARE

    licensed for evaluation purposes only

    this licence will expire on Sat, 15 Oct 2011

    Advisory[1233] Employing 18F4520 errata work-arounds:

    Advisory[1234]  * Corrupted fast interrupt shadow registers

    Error   [499] ; 0. undefined symbol:

        _WriteI2C(I²C_18LF4528_HT.obj)



    ********** Build failed! **********
    Here is the simple code:

    Code (text):


    /************************************************************************

    *

    * Module:           I²C_18LF4520_HT.C

    * Description:      Code to determine I²C functionality with the HI-TECH

    *               compiler.

    * Line length:      120 characters [only if the length is longer than 80 chars]

    * Functions:        See Below

    *

    * Date:         Authors:                    Comments:

    * 24 Aug 2011       Austin Schaller             Created

    *

    ************************************************************************/



    #include <htc.h>

    #include <stdio.h>

    #include "peripheral\i2c.h"



    __CONFIG(1, MCLRE_ON & CP0_OFF & BOREN_OFF & WDT_OFF & PWRT_ON & OSC_INTIO67);



    void main()

    {

        unsigned char data = 0xFF;

       

        OSCCON = 0x70;      // OSC = 8 MHz

       

        StartI2C();

        WriteI2C(data);

        StopI2C();

    }
    The fix:

    As quoted from the peripheral libraries manual (section 1.2)

    In other words, do the following:

    1) Navigate to: Project -> Build Options -> Project -> Linker
    2) Under "Runtime options" select "Link in Peripheral Library"
    3) Celebrate your triumph!

    I just hope none of you have to go through the pain that I did!

    Austin
     
    Last edited: Aug 24, 2011
    • Like Like x 1
  9. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,500
    Likes:
    503
    Location:
    Vermont (GMT-5:00)
    Ok, the other night I was having some trouble with a program. I kept getting the following error:

    Code (text):


    MPLINK 4.40, Linker
    Device Database Version 1.3
    Copyright (c) 1998-2011 Microchip Technology Inc.
    Error - section '.udata_c018i.o' can not fit the section. Section '.udata_c018i.o' length=0x0000000a
    Errors    : 1

    Link step failed.
     
    After trying out a few different options, I figured out that my program was larger than the 256 byte RAM could handle. The way to fix this error (can be different from ".udata_c018i.o"--the fix is the same) is to cut down as much as you can on the code. Get rid of any extra arrays, especially, as they take up a lot of space. Also cut down on your ints as much as possible. This will hopefully allow your program to "fit".

    Der Strom
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    ONLINE
    Hey DS8 the other way is to Initialise a few variables... then they are compiled in the idata section as well as the udata section... Microchip didn't figure on folks like us declaring ALL of the variables un-initialised.... There is a couple of banks reserved for both..
     
    • Like Like x 1
  11. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,500
    Likes:
    503
    Location:
    Vermont (GMT-5:00)
    Hey Ian, thanks. Could you elaborate a bit more? I'm not sure I quite follow what you're saying....
     
    Last edited: Dec 2, 2011
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    ONLINE
    I had a program that run out of udata ( un-initialised data ) ie.
    Code (text):
     
      long var1,var2,var3;
     
    Changed ALL the variables to idata ( initialised data )
    Code (text):
     
      long var1 = 0, var2 = 0, var3 = 0;
     
    That was it!!! Tons more memory. (well a bit anyway)
     
    • Like Like x 1
  13. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,500
    Likes:
    503
    Location:
    Vermont (GMT-5:00)
    That's what I thought you were saying, and I just couldn't believe it would save that much space :D
    Thanks for posting!
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    ONLINE
    I think the ram pages are 256 byte in size.. When I last had this problem, the compiler complained about the udata ( basically it can't allocate any more).. When I looked at the memory usage gauge there was loads of ram left.. That's when I did the investigation. There was about 170 bytes of idata free. Ta da!!
     
  15. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    TIMER0 confusion

    This is not a question but an observation.

    When you step through the code with a PICkit2 SFR TMR0H can not be written to. If you run it works. The chip in this example is the PIC18F330 but I it should exist in other PICs with the same TIMER0 hardware. You will not see it using MPLAB SIM.


    Code (text):

        T0CONbits.TMR0ON = 1;   // start timer0
        while(1)
        {  
            Nop();  
            Nop();
            Nop();
            TMR0H = h++;  // if you step these 2 lines it does not work    
            TMR0L = l++;
            result = TMR0L;
            result += ((unsigned int) TMR0H)<<8;
            Nop();  // set BP here
            Nop();
            Nop();
            TMR0H = 0x00;    // if you step these 2 lines it does not work
            TMR0L = 0x00;  
            result0 = TMR0L;
            result0 += ((unsigned int) TMR0H)<<8;      
        }
    }
     
     

    Attached Files:

    Last edited: Dec 9, 2011
    • Like Like x 1
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    3v0,

    When you step and monitor the register it won't work. Try stepping without monitoring them and it should work. This is due to the fact that TMR0H is not a real register but a buffer register and so after the write to TMR0H the update will latch the existing value of the real TIMER0H. See 11.4 in the data sheet for an explanation.

    Mike.
     
  17. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Mike I was not asking a question. Maybe I should make that more clear. I am attempting to keep this thread clear of them.
     
    • Like Like x 1
  18. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    I realised you weren't asking a question, I was just explaining why it happened.

    Keeping it clear of questions is a good idea. Feel free to tidy up this thread by deleteing this and the previous 2 posts.

    Mike.
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,245
    Likes:
    911
    Location:
    Rochdale UK
    ONLINE
    In the latest version Datasheet " DS40039E " on page 19 It lists the procedure to initialize PORTA as digital IO....

    The Register value to turn off the comparator modules is 07H, and not 05H as in the listing.
     
  20. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,827
    Likes:
    138
    Location:
    morristown,tn
    I was going to post that 0x05 doesn't work on that chip It should be 0x07
    But I had to look to make sure who ever wrote the data sheet may of wrote one that did use 0x05 at the same time.
    Cant trust them data sheets LOL
     
  21. ClydeCrashKop

    ClydeCrashKop Well-Known Member

    Joined:
    Apr 28, 2005
    Messages:
    1,026
    Likes:
    172
    Location:
    Florida
    ONLINE
    This is from the PIC16(L)F1826/27 data sheet.
    They say just copy and paste

    ;This code block configures the ADC
    ;for polling, Vdd and Vss references, Frc
    ;clock and AN0 input.
    ;Conversion start & polling for completion
    ; are included.
    BANKSEL ADCON1 ;
    MOVLW B’11110000’ ;Right justify, Frc clock
    MOVWF ADCON1 ;Vdd and Vss Vref
    BANKSEL TRISA ;
    BSF TRISA,0 ;Set RA0 to input
    BANKSEL ANSEL ;
    BSF ANSEL,0 ;Set RA0 to analog

    On build it said ANSEL is undefined. That's because the 16F1827 has ADC on PORTB as well. So you need to use ANSELA and ANSELB. I found those in the 16F1827.inc file.

    I am also finding a snag using code for one chip on a different chip.
    If the compiler complains about things being undefined, check the .inc or .h files for that chip. Just lately I found these varieties and a couple more for the ADC.

    GODONE = 1; // pic16F876 start conversion
    GO_DONE = 1; // 16F88 start conversion
    BSF ADCON0,ADGO ; 16F1827 Start conversion in asm
    GO_nDONE =1; // 16F1827 start conversion

    .inc files are in C:\Program Files\Microchip\MPASM Suite
    .h files are in C:\Program Files\HI-TECH Software\PICC\9.83\include
     

Share This Page