Author Topic: MPLAB X/xc8 first program not running  (Read 10701 times)

0 Members and 1 Guest are viewing this topic.

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
MPLAB X/xc8 first program not running
« on: November 15, 2012, 09:43:54 am »
I'm using MPLAB X and the xc8 compiler for the first time and has run into a a problem. It looks like my program is not running. It does not turn on/blink the LEDs.

I have verified the code after programing the chip. I wrote a program in Jal to blink the LEDs and it runs fine, so there is no problem with the hardware or the programmer.

I'm using a 12F675. I have tried the code below and also the first example from the Gooligum mid-range tutorial. (http://www.gooligum.com.au/tut_midrange_C.html)

I suspect there is some setting in MPLAB that I'm missing but I can't think of what it may be..

Code: [Select]
#include <xc.h>
#pragma config FOSC = INTRCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config BOREN = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#define _XTAL_FREQ 4000000

void main() {
    TRISIO = 0b101011;
    GPIObits.GP2 = 1;

    while (1){
       GPIObits.GP4 = 1;
       __delay_ms(500);
       GPIObits.GP4 = 0;
       __delay_ms(200);
    }
}



The generated .hex file:
Code: [Select]
:0A0000008316FF2390008312F62BF5
:1007EC008301F82BFD308316850083128514FD2BB5
:0207FC000028D3
:02400E00B431CB
:00000001FF
 

Offline notsob

  • Frequent Contributor
  • **
  • Posts: 696
  • Country: au
Re: MPLAB X/xc8 first program not running
« Reply #1 on: November 15, 2012, 10:42:40 am »
do you have the old mplab 8 installed on the same PC, if so did you use the MPLAB Switcher to change your saettings
 

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #2 on: November 15, 2012, 10:52:14 am »
do you have the old mplab 8 installed on the same PC, if so did you use the MPLAB Switcher to change your saettings

No, I don't have MPLAB 8 or any other old version installed.
 

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #3 on: November 16, 2012, 01:16:27 pm »
Any suggestions for settings to check? or config files?

I selected PIC12F675 when creating the project. Can't find other settings than that.
 

Offline ElektroQuark

  • Supporter
  • ****
  • Posts: 1244
  • Country: es
    • ElektroQuark
Re: MPLAB X/xc8 first program not running
« Reply #4 on: November 16, 2012, 02:05:13 pm »
In what pins have you connect the leds?

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #5 on: November 16, 2012, 02:38:16 pm »
In what pins have you connect the leds?

Pin 3 and 5 when using the code above.
 

Offline ElektroQuark

  • Supporter
  • ****
  • Posts: 1244
  • Country: es
    • ElektroQuark
Re: MPLAB X/xc8 first program not running
« Reply #6 on: November 16, 2012, 02:58:52 pm »
Try enabling the PowerUp Timer.

Change:

#pragma config PWRTE = OFF

to

#pragma config PWRTE = ON

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #7 on: November 16, 2012, 05:27:34 pm »
Try enabling the PowerUp Timer.

I tried now. No change.
 

Offline TheDirty

  • Frequent Contributor
  • **
  • Posts: 440
  • Country: ca
Re: MPLAB X/xc8 first program not running
« Reply #8 on: November 16, 2012, 07:04:27 pm »
What programmer are you using?  Are you not able to single step and check the registers in debug mode?
Mark Higgins
 

Offline os40la

  • Regular Contributor
  • *
  • Posts: 122
  • Country: us
Re: MPLAB X/xc8 first program not running
« Reply #9 on: November 16, 2012, 09:01:28 pm »
Does the PIC have a MCLR pin? if so is it tied to VCC with a 10k resistor?. I think if that is left floating the chip may be in a constant reset mode.
"No, but I did stay at a Holiday Inn Express"
 

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #10 on: November 17, 2012, 01:45:46 am »
What programmer are you using?  Are you not able to single step and check the registers in debug mode?

The programmer I'm using is a Wisp628 (http://www.voti.nl/wisp628/) which I built some years ago. It doesn't have any debugging features.

I'm going to order a PICkit3, so will be able to try this when that arrives.
 

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #11 on: November 17, 2012, 02:52:28 am »
Does the PIC have a MCLR pin? if so is it tied to VCC with a 10k resistor?. I think if that is left floating the chip may be in a constant reset mode.

It's got an MCLR pin. It's connected to VCC with a resistor. I'v been using 6.8k, but have changed to 10k now. Same result.
MCLR is disabled in my code example, but I have tried enabling it. No change.
 

Offline TheDirty

  • Frequent Contributor
  • **
  • Posts: 440
  • Country: ca
Re: MPLAB X/xc8 first program not running
« Reply #12 on: November 17, 2012, 03:16:26 am »
Well, I was too lazy to look at your code before because I hate the port defines that PIC compilers use.  They are always different (namely gpiobits).

I don't see any issues with the code, though I'm not that familiar with PIC config bits.  Since you are using MPLABX it's real easy to go to Window -> PIC Memory views -> Configuration bits.  From there it gives you all the options for the config bits and you can select the button 'Generate Source Code to Output' which will give you the config setup and you can copy and paste that in your program.

Sorry if I can't be of more help, but it looks good to me.
Mark Higgins
 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 9018
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: MPLAB X/xc8 first program not running
« Reply #13 on: November 17, 2012, 06:19:38 am »
After programming the code, try measuring resistance between the I/O pins and ground (disconnect the LEDs) with the power on. Are the I/Os actually set low or just floating?
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline EmilTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: no
    • ZeptoBit
Re: MPLAB X/xc8 first program not running
« Reply #14 on: November 17, 2012, 02:54:28 pm »
After programming the code, try measuring resistance between the I/O pins and ground (disconnect the LEDs) with the power on. Are the I/Os actually set low or just floating?

They are floating.

Resistance to ground was too high to measure on my DMM (>20meg).
I measured the voltages and all the IO pins except pin 4 (pulled to vcc) and pin 7 (1.3v) where at around 0.6 volts.
 

Offline Niklas

  • Frequent Contributor
  • **
  • Posts: 395
  • Country: se
Re: MPLAB X/xc8 first program not running
« Reply #15 on: November 17, 2012, 08:24:04 pm »
I dont think its related to MCLR or the configuration bits but it is related to the compiler or the code itself. If you import the hex file into MPLAB and then have a look at the program memory, you will find a few odd things. Below is a disassembly listing of the relevant parts of the memory:

000 BSF STATUS, RP0    ; Select Bank 1
001 CALL 0x3FF            ; Call to last memory cell to get oscillator calibration
002 MOVWF T1CON       ; Save to oscillator
003 BCF STATUS, RP0   ; Bank 0
004 GOTO 0x3F6
005 ADDLW 0xFF (Empty memory)
...
3F6 CLRF STATUS
3F7 GOTO 0x3F8           ; ??? Same as 2xNOP
3F8 MOVLW 0xFD          ; Load WREG with 0XFD = 0b11111101
3F9 BSF STATUS, RP0    ; Bank 1
3FA MOVWF GPIO          ; Load WREG to TRIS
3FB BCF STATUS, RP0    ; Bank 0
3FC BSF GPIO, 1            ; Make GPIO1 go high
3FD GOTO 3FD               ; Loop forever
3FE GOTO 0                   ; Goto address 0
3FF ADDLW 0xFF           ; Should be RETLW 0x**, (calibration value)

If the programmer erases all the memory, including the last word containing the calibration value, before programming, then the program will hang forever. From reset at 0x3FF, roll over to 000 and then perform a call to 3FF. As there is no return or retlw there, the program counter will roll over to 000 again and then make a new call 3FF and so on. Eventually the built in return stack will only be pushed but never popped. On a PIC18, that would have caused a stack overflow reset.

Possible workaround? Add an assembler instruction at address 0x3FF with a RETLW and some value. Here is how I got it to work:
1. Add a function prototype before the void main line
void MyOscCal(void);
2. Inside main, but after the while(1) loop, add a call to function MyOscCal
void main()
{
  while(1)
  {....
  }
  MyOscCal();
}

3. Create a function located at 0x3FF containing just one single RETLW instruction
void MyOscCal(void) @ 0x3FF
{
  asm("retlw 0x80");  // Change 0x80 to something else if the oscillator is way off
}

On the other hand I think the generated assembly code is missing a lot compared to the C code you attached. Here are some examples:
TRISIO = ... is totally off with the wrong value
The while loop is completely absent

The missing while loop is very strange and I managed to get the same result with:
while(1)
{
  i++;
}

The complete while loop was missing and I got a message about that i was never used.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf