Unable to Program the simplest function on PIC12f509

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Unable to Program the simplest function on PIC12f509

Post by c_mario » Fri Jul 15, 2016 1:23 am

Hi, this is probably a simple error on my part but I have struggled for days trying the simplest of code on the Pic12f509.
Now I am not a total new comer, I have successfully programmed Atmel products Attiny 13 Attiny 85 Atmega328 from Zero knowledge to blink a led in a matter of minutes.
The PIC however with MPLAB has eluded me. I cannot get the simulator to work nor can I program a chip correctly.
I have tried Oshonsoft's excellent simulator and can actually make the simulation work but as for programming a real chip , no go.
So, after I create the code I compile it and a Hex output is produced.
I am not using a pickit device I have a programmer that successfully programs Atmel Products , Eproms and many many devices the TL866.
Can any body tell me what I am doing wrong and laugh at me if you like.
This code below is a super simplification just to try to get something to happen.

/*
* File: auto_eddy_main.c
* Author: mario
*
* Created on 1 May 2016, 10:21 PM
*/

//#include <stdio.h>

//#include <pic.h>
//include <xc.h>


// PIC12C509 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config OSC = IntRC // Oscillator selection bits (internal RC oscillator)
#pragma config WDT = OFF // Watchdog timer enable bit (WDT disabled)
#pragma config CP = OFF // Code protection bit (Code protection off)
#pragma config MCLRE = OFF // MCLR enable bit (MCLR tied to VDD, (Internally))

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <pic12f509.h>

// Pin Assignments
#define OutGP1 GPIObits.GP1
#define LED GPIObits.GP2 // Output to Led
#define Switch GPIObits.GP0 // Output to Switch Matrix
#define SCL GPIObits.GP5 // Input to Digital Pot
#define SDA GPIObits.GP4 // Input to Digital Pot
#define Sensor GPIObits.GP3 // Output from Eddy current Sensor IC
#define _XTAL_FREQ 4000000

int main()
{
//OPTION = 0x02; //Set option register Bit 7 Wake up on pin change enabled,Bit 6 enable weak pullups
//Bit 5 Timer0 transition on internal clock, Bit 4 Timer0 positive edge triggered,
//Bit 3 Prescaler assigned to Timer0, Bits 2 - 0 all zeros set prescaler value 1:2
TRISGPIO = 0b001000; //Set GP0,1,2,4,5 as output GP3 as input

while(1)
{

if (Switch == 0)
LED = 1;
OutGP1 =0;


}
}

Your help greatly appreciated.

jremington
Support Volunteer
Posts: 2105
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Unable to Program the simplest function on PIC12f509

Post by jremington » Fri Jul 15, 2016 5:48 pm

You need a PIC programmer to program PIC parts. Buy them from various places, or build your own.

User avatar
phalanx
Non-SFE Guru
Posts: 1949
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: Unable to Program the simplest function on PIC12f509

Post by phalanx » Fri Jul 15, 2016 6:13 pm

The programmer he has claims to be able to program almost all PICs.

c_mario, are you trying to program the PIC while in MPLAB or are you using your programmer's standalone software?

What version of MPLAB are you using? If it's X, to use the simulator you must first select it in the project properties and then debug the project from the main screen. You'll be able to step through code, set breakpoints, examine variables, etc.

-Bill

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Re: Unable to Program the simplest function on PIC12f509

Post by c_mario » Mon Jul 18, 2016 5:37 am

Hi phalanx,
Thanks for your response. I am using the programmers standalone software.
MPLAB is version MPLAB X Ide v3.35.
The Simulator is selected in project properties.
Clearly I am not understanding something or like jremington says need a different programmer.
If that's the case then I will simply go back to Atmel parts and give up on PIC. I can program Atmel parts no problem with my programmer along with many eprom and other parts.
It can't be this difficult.

jremington
Support Volunteer
Posts: 2105
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Unable to Program the simplest function on PIC12f509

Post by jremington » Mon Jul 18, 2016 7:31 am

At first I didn't understand the sentence in which you described the programmer.

Using the TL866 in standalone mode, can you read and and make changes in the memory or fuses of the 12F509?

How do you intend to load in the code to be programmed? Does MPLAB recognize the programmer?

By the way, the program you posted won't blink an LED.

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Re: Unable to Program the simplest function on PIC12f509

Post by c_mario » Tue Jul 19, 2016 5:21 am

Hi,
Yes I can read the memory of the 12F509 with the programmer.
I didn't even try to get MPLAB to recognise the programmer.
I find the hex file the compiler makes and load that into the programmer and then program the chip.
Yes I know the program wont blink a led It should just set one output high when the input goes low.
It's just simply to try to get something to work.

jremington
Support Volunteer
Posts: 2105
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Unable to Program the simplest function on PIC12f509

Post by jremington » Tue Jul 19, 2016 7:30 am

Yes I can read the memory of the 12F509 with the programmer.
But can you change it? What happens when you try to program the chip?

Perhaps your programmer is defective. Have you run the self test? See this video: http://www.eevblog.com/forum/blog/eevbl ... er-review/

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Re: Unable to Program the simplest function on PIC12f509

Post by c_mario » Tue Jul 19, 2016 2:06 pm

Sorry, yes I can change it.
I guess my point is my code must be defective.
I can't get the simplest thing to happen, I.e. Set an output pin low or high in response to an input stimulus.
Yes I watched that video before I purchased the unit and it's been working on ttl logic even, programmed many EPROMs, multiple at tiny' s ect.

jremington
Support Volunteer
Posts: 2105
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Unable to Program the simplest function on PIC12f509

Post by jremington » Tue Jul 19, 2016 2:23 pm

If you can change memory locations and read it back with changes, then you can program the PIC part.

The program you posted won't work properly. You are attempting to read a pin defined as output.

For a valid test, program a simple loop that toggles the LED, with a reasonable delay between toggles. (Get it to work on the simulator first).

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Re: Unable to Program the simplest function on PIC12f509

Post by c_mario » Wed Jul 20, 2016 4:44 am

Ok,
My point is I cant get a simple loop to work. I cant even get a pin to change state with a direct statement like GPIO=0b1111111
I have spent a week just trying to do something I did in a few minutes on the Atmel products.
So Obviously I don't know what I am doing, hence the reason I asked on the forums. At this point clues are not getting through to me , my code attempts are below.
On the simulator I can see the code jumps to the correct spot when I toggle the stimulus on SensorGP3 and single step the code, but I cannot get the output pins to change state.
Clearly I have no idea how to set the Option Register, I have tried various combinations.
Obviously I don't know what include statement to use , you can see I have tried many
I have read the datasheet, still clueless.

//#include <stdio.h>

//#include <pic.h>
//include <xc.h>


// PIC12C509 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config OSC = IntRC // Oscillator selection bits (internal RC oscillator)
#pragma config WDT = OFF // Watchdog timer enable bit (WDT disabled)
#pragma config CP = OFF // Code protection bit (Code protection off)
#pragma config MCLRE = OFF // MCLR enable bit (MCLR tied to VDD, (Internally))

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <pic12f509.h>

// Pin Assignments
#define OutGP1 GPIObits.GP1
#define LEDGP2 GPIObits.GP2 // Output to Led
#define Switch GPIObits.GP0 // Output to Switch Matrix
#define SCL GPIObits.GP5 // Output to Digital Pot
#define SDA GPIObits.GP4 // Output to Digital Pot
#define SensorGP3 GPIObits.GP3 // Input from Eddy current Sensor IC
#define _XTAL_FREQ 4000000

int main(void)
{
//OPTION = 0b0000000; //Set option register Bit 7 Wake up on pin change enabled,Bit 6 enable weak pullups
//Bit 5 Timer0 transition on internal clock, Bit 4 Timer0 positive edge triggered,
//Bit 3 Prescaler assigned to Timer0, Bits 2 - 0 all zeros set prescaler value 1:2
TRISGPIO = 0b001000; //Set GP0,1,2,4,5 as output GP3 as input
GPIO = 0b001111;
while(1)
{

if (SensorGP3 == 0)
{
//LEDGP2 = 0;
//OutGP1 = 0;
GPIO = 0b000000;
}
else
{
LEDGP2 = 1;
OutGP1 = 1;
}
}
return 0;
}

jremington
Support Volunteer
Posts: 2105
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Unable to Program the simplest function on PIC12f509

Post by jremington » Wed Jul 20, 2016 12:13 pm

Here is a PIC 12F509 program that blinks an LED on GPIO 0. Obviously not optimized, but it does not use "call" so does not use the subroutine return stack.

Code: Select all

---  C:\Users\Jim\Desktop\blink.asm  -------------------------------------------------------------
                                                  1:     ; 	Blinker on output GP0
                                                  2:     ;	No Input 
                                                  3:     ;	
                                                  4:     ;       
                                                  5:             LIST    P=PIC12F509
                                                  6:     #include "p12F509.inc"
                                                  7:     
                                                  8:     	__CONFIG _CP_OFF & _WDT_OFF & _MCLRE_OFF & _IntRC_OSC
                                                  9:     
                                                  10:    J	 equ 5;
                                                  11:    GPIO equ 6;
                                                  12:    
                                                  13:    		Org 0	
   000     C00     MOVLW 0                        14:    BEGIN	MOVLW .0	;
   001     006     TRIS 0x6                       15:    		TRIS GPIO
                                                  16:    
   002     406     BCF 0x6, 0                     17:    ON		BCF GPIO, 0 ;
   003     A06     GOTO 0x6                       18:    		GOTO DELAY1;
                                                  19:    
   004     506     BSF 0x6, 0                     20:    OFF		BSF GPIO, 0 ;
   005     A0B     GOTO 0xb                       21:    		GOTO DELAY2;
                                                  22:    
   006     C14     MOVLW 0x14                     23:    DELAY1	MOVLW .20;	Delay - appr. 250 ms
   007     025     MOVWF 0x5                      24:    		MOVWF J
   008     2E5     DECFSZ 0x5, F                  25:    LOOP1	DECFSZ J,f
   009     A08     GOTO 0x8                       26:    		GOTO LOOP1
   00A     A04     GOTO 0x4                       27:    		GOTO OFF
                                                  28:    
   00B     C28     MOVLW 0x28                     29:    DELAY2	MOVLW .40;	Delay - appr. 250 ms
   00C     025     MOVWF 0x5                      30:    		MOVWF J
   00D     2E5     DECFSZ 0x5, F                  31:    LOOP2	DECFSZ J,f
   00E     A0D     GOTO 0xd                       32:    		GOTO LOOP2
   00F     A02     GOTO 0x2                       33:    		GOTO ON
                                                  34:    
   FFF     BCF     GOTO 0x1cf                     35:    		END
Make sure the configuration register bits are set to 0xFEA

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Re: Unable to Program the simplest function on PIC12f509

Post by c_mario » Thu Jul 21, 2016 3:24 am

Well , heres the result of that one.

Doesn't even get past the LIST P=PIC12F509

CLEAN SUCCESSFUL (total time: 54ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/mario/MPLABXProjects/Blink509.X'
make -f nbproject/Makefile-default.mk dist/default/production/Blink509.X.production.hex
make[2]: Entering directory 'C:/Users/mario/MPLABXProjects/Blink509.X'
"C:\Program Files\Microchip\xc8\v1.38\bin\xc8.exe" -C --chip=12F509 -Q -G --double=24 --float=24 --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/main.obj main.asm
build/default/production\main.pre:7: error: (876) syntax error
build/default/production\main.pre:42: error: (876) syntax error
build/default/production\main.pre:54: error: (876) syntax error
build/default/production\main.pre:60: error: (876) syntax error
build/default/production\main.pre:61: error: (876) syntax error
build/default/production\main.pre:62: error: (876) syntax error
build/default/production\main.pre:63: error: (876) syntax error
build/default/production\main.pre:64: error: (876) syntax error
build/default/production\main.pre:65: error: (876) syntax error
build/default/production\main.pre:66: error: (876) syntax error
build/default/production\main.pre:69: error: (876) syntax error
build/default/production\main.pre:70: error: (876) syntax error
build/default/production\main.pre:71: error: (876) syntax error
build/default/production\main.pre:72: error: (876) syntax error
build/default/production\main.pre:73: error: (876) syntax error
build/default/production\main.pre:74: error: (876) syntax error
build/default/production\main.pre:75: error: (876) syntax error
build/default/production\main.pre:79: error: (876) syntax error
build/default/production\main.pre:80: error: (876) syntax error
build/default/production\main.pre:81: error: (876) syntax error
build/default/production\main.pre:82: advisory: (1) too many errors (21)
(908) exit status = 1
nbproject/Makefile-default.mk:106: recipe for target 'build/default/production/main.obj' failed
make[2]: Leaving directory 'C:/Users/mario/MPLABXProjects/Blink509.X'
nbproject/Makefile-default.mk:84: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/mario/MPLABXProjects/Blink509.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[2]: *** [build/default/production/main.obj] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 552ms))

User avatar
leon_heller
Support Volunteer
Posts: 5734
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Re: Unable to Program the simplest function on PIC12f509

Post by leon_heller » Thu Jul 21, 2016 3:51 am

The posted code is the output from the assembler, which won't compile. You need to assemble the source code.

Try assembling this using MPLAB IDE 8.93:

Code: Select all

;test.asm

	#include <p12F508.inc>

	org		0
	nop

	movlw	0
	tris	GPIO		;all GP pins set to output
loop:
	movlw	b'000010'	;toggle GP1
	movwf	GPIO
	movlw	b'000000'
	movwf	GPIO
	nop
	goto	loop

	END
Get a genuine PICkit 3. It will save a lot of hassle.
Leon Heller
G1HSM

c_mario
Posts: 8
Joined: Fri Jul 15, 2016 1:21 am

Re: Unable to Program the simplest function on PIC12f509

Post by c_mario » Thu Jul 21, 2016 6:53 am

thank you for the input but I'm sorry this is absolute madness now.
Clearly I have no idea.
I give up , going back to Atmel products.

CLEAN SUCCESSFUL (total time: 63ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/mario/MPLABXProjects/Blinky.X'
make -f nbproject/Makefile-default.mk dist/default/production/Blinky.X.production.hex
make[2]: Entering directory 'C:/Users/mario/MPLABXProjects/Blinky.X'
"C:\Program Files\Microchip\xc8\v1.38\bin\xc8.exe" -C --chip=12F508 -Q -G --double=24 --float=24 --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/newAsmTemplate.obj newAsmTemplate.asm
newAsmTemplate.asm:3: error: (141) can't open include file "p12f508.inc": No such file or directory
make[2]: *** [build/default/production/newAsmTemplate.obj] Error 1
make[1]: *** [.build-conf] Error 2
(908) exit status = 1
make: *** [.build-impl] Error 2
nbproject/Makefile-default.mk:106: recipe for target 'build/default/production/newAsmTemplate.obj' failed
make[2]: Leaving directory 'C:/Users/mario/MPLABXProjects/Blinky.X'
nbproject/Makefile-default.mk:84: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/mario/MPLABXProjects/Blinky.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 437ms)

jremington
Support Volunteer
Posts: 2105
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: Unable to Program the simplest function on PIC12f509

Post by jremington » Thu Jul 21, 2016 7:18 am

The first two columns of the assembler listing that I posted are memory address and value.

What I had in mind is that you would just enter those by hand into the 12F509 memory image in your programmer, and then update the PIC flash memory.

But if you regard this tiny difficulty as "absolute madness", perhaps you should stick with Atmel (which, incidentally, is now owned by Microchip, maker of PIC products).

Post Reply