# How to program a 12F508

#### q12x

Joined Sep 25, 2015
618
I am in need of your assistance. If you can, of course.
If you know other ways of programming this specific PIC, then tell me.
This is what I have already and what I tried:

It is a side project but is still important to get it rolling. I have a pickit2 ready and 10pcs of PIC 12f508.
I stay like 14h to figure out how to install all the necessary software for pickit2 and to actually see the pickit2 listed as normal option to select, because it was gray out and I could not select it as an option. In MPLAB :

I did this thing in the past, and programmed some 16f84A and some others, but not many.
This is not my first time but it is a long time from when I did it in 2001 or something.
Now that my pickit2 is alive, all I need is the correct code to send to it and to it's 12f508 that I installed on a "pickit2 DemoBoard" exactly like this one:

Please tell me what to send to it. I started with these lines yesterday.
As you can see, PICkit 2 is recognized.

But I am not sending something important to him in the code. And I have no clue what is the correct way to send instructions anymore. Maybe you can help me. I know I made it work in 2001 with a special "recipe" in the beginning. But I dont know what it was anymore. Or it was only for certain PIC's like 16F84? And is not working for others like 12F508?
Or at least where to look and search how to transmit the correct header or start of the code?
Thank you !
If I try to program it, I get this error: (the red line)

And here is how it looks in reality.

The position of the PIC chip is correct, it's chip key pointing up towards the pickit2 black thing programmer.
The 4 Red leds are connected through those 4 white jumper wires, because this is a 8pin chip and you have to rewire those pins.
I connected:
07-10 = RA0-RC0 = led1=DS1
08-11 = RA1-RC1 = led2=DS2
09-12 = RA2-RC2 = led3=DS3
03-06 = RA3-RC3 = led4=DS4

Here is the datasheet for this particular pic 12f508:
https://datasheetspdf.com/pdf-file/688428/MicrochipTechnology/12F508/1
Maybe you have luck and you can find the initializing code for it. Take a quick look, maybe you are lucky. If not, then is good that I try it through you as well. That code that i write there already, is from other codes I had from the past, I didnt look (too hard) in this datasheet yet. I did peek in it but not for too long. I had the hardware issues to resolve first. But I will in the end if nothing else come out. Im counting on you, maybe you are doing this thing more often than me. I am an artist, not an engineer. But I like the cool stuff, and this is one of it.
Thank you !

Last edited:

#### ericgibbs

Joined Jan 29, 2010
12,928
hi q12,
Just tried my MPLAB V8.56, with the 12F508 selected as a device, the PICKit2 is not greyed out.?

Used it OK in past with this PIC.
E
Its reporting Not Found as I do not have the Kit2 plugged in.

#### Attachments

• 22.5 KB Views: 5

#### Ian Rogers

Joined Dec 12, 2012
888
The chip is seen but the ID address is read as 0xfff which is wrong... The IDC pins (RA0 and RA1) are clocking all 1's.. either the chip is duff or one of these pins is being kept high... The white wire on the header are not needed here.... This pinout is the same as the pic12f625 I use and I use the same tool as you have there...

I have had instances where the USB port causes this error.. Try another port, BUT also try another USB cable..

#### q12x

Joined Sep 25, 2015
618
If the pickit2 programmer is seen on that USB and cable connection by the MPLAB, then the connection is good.
I will try to change the usb port as you recommend.
As an alternative, I forget to mention, this is the entire list of PICs I have
12F508
12F629
16F505
16F690
16F84
16F684
but 1 of each, exception for 12F508 where I have 10pcs.
We can test 1 PIC that we have in common and figure it out like this what I am doing wrong.
I also know I have to include some other files and also add them in that list. But i dont remember all the stuff.

#### q12x

Joined Sep 25, 2015
618
Ive added 16F690 to demoboard and took out all the white jumpers now (they were needed only for 8pin chips) and this one has 20pins.
I get this error now, is there a jumper I forget to add somewhere?

#### Ian Rogers

Joined Dec 12, 2012
888
ve added 16F690 to demoboard and took out all the white jumpers now (they were needed only for 8pin chips) and this one has 20pins.
I use all those chips.... None require jumpers.. All the 8 14 and 20 pin chips in that range use the same pins..

FAILED error is there is no reply back from the chip in the socket

#### q12x

Joined Sep 25, 2015
618
Im not sure exactly what i did back then. I think those saved (working) files were working for this pickit2? or for a serial port? I am not sure.

#### q12x

Joined Sep 25, 2015
618
I changed the USB port as you instructed. It was in the begining in a usb 3.0 port Hub. Now is connected directly to 1/4 usb on the motherboard. Still the same error. I think I manage to get rid of the error, but I got another one instead of that. By connecting pin3-6 with a jumper for the led4 to function on the testboard, it is rising this error about power supply. If I remove that jumper the error is no more BUT... i get a "invalid value" red thing.

#### Ian Rogers

Joined Dec 12, 2012
888
OSCCAL isn't important... In the tools menu, you can regenerate that value.
This is always lost when re-programming these devices..

Can you program the chip now... The config looks good, and there is a checksum..

#### q12x

Joined Sep 25, 2015
618
Thank you mister @Ian Rogers
I managed to make it work.
It was a simple file manipulation issue.
I was loading and trying to run a *.c file. I figure it out it should be a *.HEX file.
I was also doing a simple tutorial to remember everything. But it was way too general and it didn't cover this little detail. After I correctly loaded the .HEX into the program and run specifically that file to be compiled, and also paying attention to a couple of errors and warnings, It worked flawlessly.

Thank you very much for your support! By the way, changing the usb port didn't made any difference. But it was worth to try it. I also encounter a very nasty bug of the MPLAB itself, it really needs 1 or more computer restarts and the usb cable connected all the time (though in some datasheet i read they actually specify not to leave it plugged, but i am lazy). SO these little things helped the software to be recognized , operational and behave normally.
Little things !
I also made a little movie where I present some other stuff but also this resolve as well.

Last edited:

#### Ian Rogers

Joined Dec 12, 2012
888
I'm glad you sorted it.. Well done.

#### q12x

Joined Sep 25, 2015
618
I write this code and it worked. But then I modified something and it didnt work anymore.
Can you correct it for me?
I write it for a PIC 12f508 using hi-tech C in mplab v8.92.

C:
//12f508
#include <htc.h>
#include <pic.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define _XTAL_FREQ 4000000
//it doesnt recognise anything from this CONFIG and throws an error for each
//__CONFIG(MCLRE_ON & CP_OFF & WDT_OFF & OSC_IntRC);

void main()
{

TRIS = 0b000000;
GPIO = 0b000000;

while(1)
{
GP0= 1;
__delay_ms(190);
GP1= 1;
__delay_ms(50);
GP2= 1;
//    GP3 is input only
__delay_ms(50);
GP4= 1;
__delay_ms(50);
GP5= 1;

GP0= 0;
__delay_ms(50);
GP1= 0;
__delay_ms(50);
GP2= 0;
//    GP3 is input only
__delay_ms(50);
GP4= 0;
__delay_ms(50);
GP5= 0;
}
}

#### Ian Rogers

Joined Dec 12, 2012
888
The old CONFIG was weird... two underscores.. then a number..
The wording was rubbish....

__CONFIG(WDTDIS & HS & UNPROTECT) would disable the WDT and set CP off and select HS oscillator..

I used to work out what I wanted an put the hex number in..

For instance yours would be:-
__CONFIG(0x3FA).. MCLRE on,CP off IntRC and WDT disabled..

#### q12x

Joined Sep 25, 2015
618
Excelent, now is working again ! Thank you.
But another problem I had also before when it worked the first time. Led 3 is not lit. While the others are doing their job fine as they are programmed.
C:
//12f508
#include <htc.h>
#include <pic.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define _XTAL_FREQ 4000000

__CONFIG(0x3FA); //this line repaired the problem

void main()
{

TRIS = 0b000000;
GPIO = 0b000000;

while(1)
{
GP0= 1;
__delay_ms(190);
GP1= 1;
__delay_ms(50);
GP2= 1;
//    GP3 is input only
__delay_ms(50);
GP4= 1;

GP0= 0;
__delay_ms(50);
GP1= 0;
__delay_ms(50);
GP2= 0;
//    GP3 is input only
__delay_ms(50);
GP4= 0;
}
}
and I also have all the time, after each programming, click this button to see the thing functioning.
I know there is a code for this option. But I dont know the line code for it.

Last edited:

#### q12x

Joined Sep 25, 2015
618
I find the problem why the 3'd led wasn't working ! But I was very lucky to find it !
I had to include this line:
OPTION = 0b11000111;
I didn't read anything so far, im just using bits of codes to see something is doing anything and so far, little by little I managed to make it work as I want. Which is very great.
I think, if i remember correctly, I find all these commands in the datasheet of the particular PIC, in this case, the datasheet of 12f508. IS this correct?
I actually open this datasheet and kind of scanned, like looking but not reading in depth, just a gaze on each page, and I remember I read these books before, and actually using the information from them. But now is just alien language for me and I dont know where to start... yet.
Or there is other resources where I should look and extract these informations, bits of code to implement?
Thank you.

#### Ian Rogers

Joined Dec 12, 2012
888
The TOCK pin is GP2.. this can be used to increment the internal timer.. It is set to this by default.

OPTION REG
_________________________________________________________
| GPWU | GPPU | TOCKI | TOSE | PSA | PS2 | PS1 | PS0 |

TOCKI must be low to use GP2

datasheet said:
3:The TRIS of the T0CKI pin is overridden when T0CS = 1.

#### q12x

Joined Sep 25, 2015
618
Thank you ! Great answer! So you are reading the datasheet of the PIC itself as well.
I am glad that i did the right thing. I wasn't sure after all this time.

Another problem:
After each [programming] I have to manually press this button:

If it will work in the final circuit, I dont mind pressing that button !
But I am afraid I should code this option as well: [Bring target MCLR to Vdd]
What is the code for it?
Thank you !
-------------------
I think that MCLR to Vdd in reality it means adding a resistor from pin X to +5V. I didnt look which pin is responsable for MCLR yet. But I think this is the thing it is asking me to do. But it can be set from code. I'm pretty sure it is.

Last edited:

#### q12x

Joined Sep 25, 2015
618
A bit more progress:
I managed to find the proper way of setting this MCLR.
from menu: Configure - Configuration Bits
From there I can set this MCLR as on or off, and every time I program a pic, it will be activated automatically, without me pressing that button.
NOW... If im checking "Configuration Bits set in code"
I must insert these commands from code.
My problem is that hitech c program that i use inside my MPLAB v.8.92
does not recognizing any configuration code that I put so far:
For example:
__CONFIG(MCLRE_OFF);
iget: Error [800] 01 with hitech-c.as; 44. undefined symbol "MCLRE_OFF
and is the same for every configuration code
__CONFIG(MCLRE_OFF & CP_OFF & WDT_OFF & OSC_IntRC);
Error [800] 01 with hitech-c.as; 44. undefined symbol "OSC_IntRC"
Error [800] 01 with hitech-c.as; 44. undefined symbol "WDT_OFF"
Error [800] 01 with hitech-c.as; 44. undefined symbol "CP_OFF"
Error [800] 01 with hitech-c.as; 44. undefined symbol "MCLRE_OFF"

So it is clear i am not using the correct configuration commands.
Your " __CONFIG(0x3FA);" worked perfectly and im using since, but is very criptic and I can not individually select what to turn on or off.
We must find the proper configuration commands to work with hitech-c.
-WHERE can I find these configuration commands ? What file should I open to actually see them?
Thank you !

#### q12x

Joined Sep 25, 2015
618
More progress, more success !
I find this information so far:
Definition\Syntax: Located in respective product's header file. Use the pic12fxxx.h header file at:
C:\Program Files\HI-TECH Software\PICC\<version>\include\.
Example for PIC12F675.h:
__CONFIG(UNPROTECT & BORDIS & MCLREN & WDTDIS & PWRTEN & INTIO );

Yaaaaah, so I don't have this particular 12f508.h in my folder and I can not find it online either.
but I have this file: C:\Program Files (x86)\HI-TECH Software\PICC\9.60\include\pic.h
I am not completly sure, but this appear to include my 12f508 as:
defined(_12F508) || defined(_12F509) ||\
But I can not see any configuration bits, inside this file.
except these ones:
#define CLRWDT() asm("clrwdt")
#define SLEEP() asm("sleep")
#define NOP() asm("nop")

#define __CONFIG(x) asm("\tpsect config,class=CONFIG,delta=2");\
asm("\tdw "___mkstr(x))

I assume they are configuration bits, it is not specified anywhere what they are.
I am looking for setting MCLR configuration from code !!!

#### Ian Rogers

Joined Dec 12, 2012
888
__CONFIG(UNPROTECT & BORDIS & MCLREN & WDTDIS & PWRTEN & INTIO );
I said that way back...

What I would recomend.... Moving to XC Hi-tech is old now and doesn't support any of the newer chips.. Microchip has completely bought the Hi-tech compiler for pic and has massive support for it... Support is now for all the AVR's as well.

Take a look... Commands have far more documentation.