MPLAB X/xc8 first program not running - Page 1
Forum section: electronics / general

author: Emil
author_rank: Regular Contributor
timestamp: November 15, 2012, 09:43:54 am
content: 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:#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 4000000void 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::0A0000008316FF2390008312F62BF5:1007EC008301F82BFD308316850083128514FD2BB5:0207FC000028D3:02400E00B431CB:00000001FF

author: notsob
author_rank: Frequent Contributor
timestamp: November 15, 2012, 10:42:40 am
content: do you have the old mplab 8 installed on the same PC, if so did you use the MPLAB Switcher to change your saettings

author: Emil
author_rank: Regular Contributor
timestamp: November 15, 2012, 10:52:14 am
content: Quote from: notsob on November 15, 2012, 10:42:40 amNo, I don't have MPLAB 8 or any other old version installed.

author: Emil
author_rank: Regular Contributor
timestamp: November 16, 2012, 01:16:27 pm
content: Any suggestions for settings to check? or config files?I selected PIC12F675 when creating the project. Can't find other settings than that.

author: ElektroQuark
author_rank: Unknown
timestamp: November 16, 2012, 02:05:13 pm
content: In what pins have you connect the leds?

author: Emil
author_rank: Regular Contributor
timestamp: November 16, 2012, 02:38:16 pm
content: Quote from: EdoNork on November 16, 2012, 02:05:13 pmPin 3 and 5 when using the code above.

author: ElektroQuark
author_rank: Unknown
timestamp: November 16, 2012, 02:58:52 pm
content: Try enabling the PowerUp Timer.Change:#pragma config PWRTE = OFFto#pragma config PWRTE = ON

author: Emil
author_rank: Regular Contributor
timestamp: November 16, 2012, 05:27:34 pm
content: Quote from: EdoNork on November 16, 2012, 02:58:52 pmI tried now. No change.

author: TheDirty
author_rank: Frequent Contributor
timestamp: November 16, 2012, 07:04:27 pm
content: What programmer are you using? Are you not able to single step and check the registers in debug mode?

author: os40la
author_rank: Regular Contributor
timestamp: November 16, 2012, 09:01:28 pm
content: 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.

author: Emil
author_rank: Regular Contributor
timestamp: November 17, 2012, 01:45:46 am
content: Quote from: TheDirty on November 16, 2012, 07:04:27 pmThe 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.

author: Emil
author_rank: Regular Contributor
timestamp: November 17, 2012, 02:52:28 am
content: Quote from: os40la on November 16, 2012, 09:01:28 pmIt'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.

author: TheDirty
author_rank: Frequent Contributor
timestamp: November 17, 2012, 03:16:26 am
content: 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.

author: NiHaoMike
author_rank: Super Contributor
timestamp: November 17, 2012, 06:19:38 am
content: 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?

author: Emil
author_rank: Regular Contributor
timestamp: November 17, 2012, 02:54:28 pm
content: Quote from: NiHaoMike on November 17, 2012, 06:19:38 amThey 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.

author: Niklas
author_rank: Frequent Contributor
timestamp: November 17, 2012, 08:24:04 pm
content: 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 1001 CALL 0x3FF ; Call to last memory cell to get oscillator calibration002 MOVWF T1CON ; Save to oscillator003 BCF STATUS, RP0 ; Bank 0004 GOTO 0x3F6005 ADDLW 0xFF (Empty memory)...3F6 CLRF STATUS3F7 GOTO 0x3F8 ;Same as 2xNOP3F8 MOVLW 0xFD ; Load WREG with 0XFD = 0b111111013F9 BSF STATUS, RP0 ; Bank 13FA MOVWF GPIO ; Load WREG to TRIS3FB BCF STATUS, RP0 ; Bank 03FC BSF GPIO, 1 ; Make GPIO1 go high3FD GOTO 3FD ; Loop forever3FE GOTO 0 ; Goto address 03FF 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 linevoid MyOscCal(void);2. Inside main, but after the while(1) loop, add a call to function MyOscCalvoid main(){while(1){....}MyOscCal();}3. Create a function located at 0x3FF containing just one single RETLW instructionvoid 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 valueThe while loop is completely absentThe 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.