Problem with hex file created in MPLAB

Discussion in 'General Electronics Chat' started by hunterage2000, Jan 28, 2013.

  1. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Hi,

    I have been trying for hours to find out why a simple hex file wont work on my PIC16F877A. I have used MPLAB X IDE, set the PIC I use, C8 Compiler but it didnt recognise my k150 Programmer.

    Anyway It produces a hex file when you build and clean. I opened a new .c file and used a simple program to make all port b pins high (see attached).
    I took the contents of the pic16f887a.h and put it in mypic.h.

    I uploaded the hex file to my PIC using microbrn and there is data there but it wont work on my breadboard.

    Can anyone advise what may be the problem? Im baffled and the manuals dont help much.
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I don't see any source files in your solution explorer.

    Add one called main.c and put
    Code ( (Unknown Language)):
    1. void main()
    2. {
    3. //Initilaize
    4. while(1)
    5. {
    6.  
    7. }
    8. }
     
  3. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    My computer is playing up so I'll try that tshuck and post later.
     
  4. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    I tried the code but it didnt work although there is something on the hex file, I have attached screen shots, can anyone suggest why this isnt working?
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    As I understand. The Micropro programmer will detect that a new hex file is made. So start MPLAB(X) start the micropro software and load the hex file. Then program your chip. After this the micropro software will detect and program any new hex.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Looking at your code. You do not set the configuration word(s) This may be the reason for not working
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    From the small hex file it generated, it looks like you didn't populate main() with your program:confused:

    Main() is your insertion point, so code as though main() is the first thing done...
     
  8. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Use the Example for the 16F and modify it (Change the PIC model, remove Osc. calibration compiler option).

    For small projects you don't need any additional files.

    I always start projects that way. If you use a baseline, then select the 10F example, does not matter as you can change the PIC model, but it does not include interrupt code.

    You still have to set the configuration: Go to PIC memory view::Config word, there is a button to generate code (in MPLAB 1.60)
     
  9. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    I started a PIC16 template but I couldnt figure out what InitApp() and ConfigureOscillator() is.

    I have attached each part I got to:

    1. main.c - InitApp()? and ConfigureOscillator()?
    2. configbits - I think this is correct
    3. System.c - ConfigureOscillator()?
    4. system.h - ConfigureOscillator()?
    5. user.c - InitApp()
    6. user.h - InitApp()
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    The interesting thing is these are just suggestions. You can leave these files as they are- empty.

    You can, if you like, do all your initialization just before while(1).

    I do that sometimes :)

    For larger projects, if you have 2000 lines in the main.c, you'd be interested to move some of that into additional files.
     
  11. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    hi takao21203, can guide me through what you do step by step?

    When you say use the 16f example, do you mean the template from sample projects? Shouldnt osc be there as Im using an XT ? What is the a baseline?
     
  12. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    OK. I made 8 screenshots. All these steps are relevant.

    1. Select the template.
    2. Enter the project name and set as main project.
    3. Select properties (right click on the project).
    4. Select programmer, PIC model, and compiler/assembler.
    5. Deselect "calibrate oscillator" or it won't work! Important. Have a look all options, most do nothing for the time being.
     
  13. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    6. Switch on power supply by PICKIT3 (if you use this programmer). Select 5V for 16F PICs.

    7. Select "Configuration Bits" from PIC memory menu.

    8. Set the bits there, and press the button at the bottom right. Copy the output to the configurationbits file (at the bottom of the file).

    9. Do a test build and see if there are any error messages.

    10. You should have a crystal etc. on your PIC board. Select XT for 4 MHz, or HS for 8 MHz. Attach a LED + 2.2k resistor to one I/O port. Add a 1uF capacitor as well MCLR resistor (4.7k, 2.2k etc.)

    11. Use a code like shown below to make the LED blink. You need to change it if you don't use PORTAbits.RA3

    This is for 16F59, so you may need to do more initialization, for instance ANSEL=0 is required for many PICs to use digital I/O. TRIS registers are all set to 0xFF on reset.

    Also the timer is polled directly, no interrupt is used here. You may not want to use that on regular 16F PICs.


    Code ( (Unknown Language)):
    1. /******************************************************************************/
    2. /* Files to Include                                                           */
    3. /******************************************************************************/
    4.  
    5. #if defined(__XC)
    6.     #include <xc.h>         /* XC8 General Include File */
    7. #elif defined(HI_TECH_C)
    8.     #include <htc.h>        /* HiTech General Include File */
    9. #endif
    10.  
    11. #include <stdint.h>        /* For uint8_t definition */
    12. #include <stdbool.h>       /* For true/false definition */
    13.  
    14. #include "system.h"        /* System funct/params, like osc/peripheral config */
    15. #include "user.h"          /* User funct/params, such as InitApp */
    16.  
    17. /******************************************************************************/
    18. /* User Global Variable Declaration                                           */
    19. /******************************************************************************/
    20.  
    21. /* i.e. uint8_t <variable_name>; */
    22.  
    23. /******************************************************************************/
    24. /* Main Program                                                               */
    25. /******************************************************************************/
    26.  
    27. unsigned char v_tmr;
    28. unsigned char v_tmr2;
    29.  
    30. void main(void)
    31. {
    32.     /* Configure the oscillator for the device */
    33.     ConfigureOscillator();
    34.  
    35.     /* Initialize I/O and Peripherals for application */
    36.     InitApp();
    37.  
    38.     /* TODO <INSERT USER APPLICATION CODE HERE> */
    39.     TRISA=0x00;
    40.     v_tmr=0;
    41.     v_tmr2=0;
    42.     OPTION=0x081;
    43.     PORTA=0;
    44.  
    45.     while(1)
    46.     {
    47.    //     PORTA=0xff;
    48.         if(TMR0>0x7f)
    49.         {
    50.             if ((v_tmr&0x01)==0x00)
    51.             {
    52.                 v_tmr|=0x01;
    53.                 v_tmr2++;
    54.             }
    55.         }else
    56.         {
    57.             v_tmr&=0xfe;
    58.         }
    59.  
    60.         if(v_tmr2>0xf0)
    61.         {
    62.             v_tmr2=0;
    63.             if((v_tmr&0x02)==0x02)
    64.             {
    65.                PORTAbits.RA3=0;
    66.                v_tmr&=0xfd;
    67.             }else
    68.             {
    69.                 PORTAbits.RA3=1;
    70.                 v_tmr|=0x02;
    71.             }
    72.         }
    73.     }
    74.  
    75. }
    76.  
     
  14. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    By the way I have flashed, build into circuits, sold etc. hundreds of PICs. I do not know how many I have here curently- too many to count.

    I tried many different one's.

    I prefer TQFP44 chips, or SOIC14 chips. I always use a row header for the PICKIT3, 5 pins only. And small round SMD adapters.

    The advantage is the TQFP chips don't need much space to store, and shipping costs are lower. If you order PDIP, they need to use large tubes.

    Breadboards I almost never use- error prone, large, and many components don't fit. Even if I have some breadboards here. Against all odds, they are good to use upto 20 MHz and more, just not for FM radio etc.
     
  15. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Baseline are PICs without pheripherals. No interrupts as well, and only one timer. Upto 2K FLASH, and small RAM. They are very easy to configure. Good to use for driving 7seg or LED matrix, LCD and things like that.

    Mainly 16F54, 16F57, and 16F59, as well some 10F2xx chips.

    16F59 has 32 I/O lines, and more than 100 bytes RAM, costs about $1. So you can order 25 pcs. or 50 pcs. from Microchip, and it is still not a large amount of money.

    The 16f54 and 16f57 I don't recommend- there are better PICs now also for low cost.

    I used 18F sometimes but skipped 24F altogether- changed to PIC32 recently. They also start at only 2 dollars, so it is just a saving of $1 per chip. They are more difficult to configure however, and need many small capacitors.
     
  16. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Hello again takao21203, I have tried what you said but it still doesnt work.

    My k150 programmer is not recognised by 'Hardware Tool' so I could only select 'Device' > pic16f877a and 'Compiler Toolchain' > xc8(v1.12).

    Would it matter if I couldnt define a programmer as it would still create a .hex file?

    I copy and pasted your code but it didnt recognise the OPTION variable so I defined it after v_tmr2 = 0; then it worked.

    I have attached the full main.c screen to show the code, confirmation of Output window, and configbits window,configbits.c and the system(.h)

    I am using a 20Hz crystal oscillator so I set oscillator type to HS and changed the value from 500000L to 20000000L on system.h.
     
  17. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    The OPTION is a bit hopeless for the PIC you use as it does not exist.

    But the TIMER0 on this PIC is similar, you can find the info in the datasheet. It is OPTION_REG eventually.

    The configuration normally does not appear in the main.c file, but I think this also works.

    Your programmer is not supported by MPLAB. But you get some HEX data. Isn't that what you want?

    Change the OPTION to something that makes sense on that PIC, and change the PORTAbits.PA3

    You also need to use ANSEL=0 on that PIC or something like that. Look at I/O ports in the datasheet.

    The Oscillator freq. definition does nothing as such, it is only a constant used by a delay routine. You need to use HS configuration bits.

    I hope this information helps you a little further, and you get your PIC started to run some code.
     
  18. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    We told you some months ago that your programmer could not be used from inside MPLAB(X). But that is not important regarding the hex file. It will be made correct from the compiler, programmer or no programmer.
     
  19. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Yeah I posted some months ago and got sick of trying to get it working and just started again. Can anyone recommend any other software that I can use C language and convert to a .hex file?
     
  20. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Well what is your problem then? Is the chip "dead" or is it something else. It is very seldom that people asking for help do not get a PIC up and running after a while. But PICs can also be hard for a beginner. As they requires very strict attention to details. And reading the datasheet is absolute imperative. Do you also have any other PIC than the 16F877. In your first experiment phase. I would strongly recommend using a PIC with internal oscillator.
     
Loading...