Author Topic: How to program PIC12F508-I/MS  (Read 4542 times)

0 Members and 1 Guest are viewing this topic.

Offline IconicPCBTopic starter

  • Super Contributor
  • ***
  • Posts: 1534
  • Country: au
How to program PIC12F508-I/MS
« on: September 27, 2017, 07:47:21 am »
Room,

I am faced with answering some questions regarding programming of above chip.
The chip is an 8 pin micro controller with all pins busy doing things. It will run from a 32 KHz crystal on CR2032 battery.
I am familiar with AVR micro controller however PICs are an unknown quantity.
I understand I will require a PICKIT3 to program the micro. With the STK500 and AVR i would typically include a 6 pin header making sure the programming lines are not unduly loaded with peripheral circuitry and merrily go about developing the firmware.

As i need to have some answers for a Thursday afternoon meeting and other things are biting into my time i hope someone familiar with the PICs might just give me a few sentences on programming interface and requirements/ gotchas of an in-cicruit programming procedure ( which pin names are part of the programming interface ) and how does pickit3 handle additional circuit loading.

I also intend to use the free C compiler microchip have for this family; are there any traps for new players?
 

Offline fcb

  • Super Contributor
  • ***
  • Posts: 2117
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: How to program PIC12F508-I/MS
« Reply #1 on: September 27, 2017, 08:21:10 am »
Room,

I am faced with answering some questions regarding programming of above chip.
The chip is an 8 pin micro controller with all pins busy doing things. It will run from a 32 KHz crystal on CR2032 battery.
I am familiar with AVR micro controller however PICs are an unknown quantity.
I understand I will require a PICKIT3 to program the micro. With the STK500 and AVR i would typically include a 6 pin header making sure the programming lines are not unduly loaded with peripheral circuitry and merrily go about developing the firmware.

As i need to have some answers for a Thursday afternoon meeting and other things are biting into my time i hope someone familiar with the PICs might just give me a few sentences on programming interface and requirements/ gotchas of an in-cicruit programming procedure ( which pin names are part of the programming interface ) and how does pickit3 handle additional circuit loading.

I also intend to use the free C compiler microchip have for this family; are there any traps for new players?

1. read the data sheet
2. pic's can be programmed at Microchip if you want, or programmed off-board, or in-circuit.
3. in-circuit, it's difficult to discuss loading etc.. without you disclosing your circuit diagram or at least what you plan doing with the i/o.
4. C - depends on how much processing you need to do? this part has a small amount of program memory and 25 bytes of RAM only.!  i'd do it in ASM.
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12855
Re: How to program PIC12F508-I/MS
« Reply #2 on: September 27, 2017, 08:28:44 am »
Read the programming spec - it isn't in the main datasheet.  See http://ww1.microchip.com/downloads/en/DeviceDoc/41227E.pdf

The usual PIC ICSP header is a 6 pin 0.1" pitch SIL header ).05" pitch headers are also fairly common but require an adaptor cable.  See PICkit 3 manual for pinout.  ISCP pins on that chip are GP0(ICSPDAT), GP1(ICSPCLK) and GP3(/MCLR).   The PICkit 3 has reasonably good drive capabilities for ICSPCLK and ICSPDAT so as long as the resistive load on the pin is above a few hundred ohms, and capacitive load down around 100pF or lower, you shouldn't run into problems there as long as your application circuit doesn't have pullups on those pins.  However /MCLR/Vpp goes to +12.5V during programming, so your application circuit must tolerate that and also MUST NOT load the pin with significantly less than 5K.     

It should be noted that if external /MCLR is disabled and  GP0 and/or GP1 are outputs that are usually high,  it may not be possible to re-enter programming mode.  As you are using an external crystal in LP mode, shorting OSC1 to Vss should be sufficient to prevent the chip running and setting those pins as outputs.

Major Gotya: The PIC12F508 *CANNOT* be bulk erased with Vdd below 4.5V.  As you are using a 3V battery supply, this is problematic and the CR2032 will need to be disconnected for reprogramming.  Obviously all other devices on the same Vdd rail will need to be 5V tolerant.

As the MicrochipDirect programming service is affordable, it may be preferable to omit ICSP capability from production boards and simply order the chips preprogrammed.  However the one-off setup charge per firmware image makes this extremely unattractive for development. The best option there is to fly-wire a DIP8 socket onto the MSOP footprint so you can prototype with a removable PIC12F508-I/P part.

The XC8 compiler is perfectly usable, though the 8 bit baseline PIC architecture is not well suited to C.  As long as you treat it like a high-level assembler, and manipulate the special function registers directly, you shouldn't have any problems.  Using C standard library functions is unwise due to code bloat.   The XC8 Free mode licence permits commercial use, but Free mode tends to produce bloated code, so you can start development immediately before you have budget approval for a compiler licence.  If you run out of space before you get approval, continue using a PIC12F509 or activate the time limited trial Pro mode licence.  You can either buy the licence outright that also includes 1 year's support and free updates or rent the licence by the month, which is competitive for shorter one-off projects or occasional use.
« Last Edit: September 27, 2017, 08:33:01 am by Ian.M »
 

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: How to program PIC12F508-I/MS
« Reply #3 on: September 27, 2017, 09:27:34 am »
The PIC micros take 5 lines for ICSP.

I have used this chip a lot. It's burned into memory.

1. MCLR/Vpp (pin 4, GPIO 3)
2. Vdd          (pin 1)
3. Ground     (pin 8 )
4. Data         (pin 7, GPIO 0)
5. Clock        (pin 6, GPIO 1)

The nice thing with PICmicros, just about all of the ones identified as "low pin count," will have the same orientation of these programming pins (except the Vss will be X, where X= highest/last pin, Data will be X-1, Clk will be X-2. So, for instance, if you used a chip clip, you could use the same 8 pin chip clip and breakout to connect to most of the 8, 14, and 20 pin PIC micros of the same package.

The PK3 is the fastest way to program these things. The thing to watch out for is capacitance on MCLR in particular. Any extra capacitance/inductance will cause a failure. The gate of a small signal FET will mess it up, for instance. Clk and Data lines are also somewhat sensitive.

In any batch flashing, you will experience occasional failure to preserve OSCCAL (a banal feature of these basic devices; step up to midrange, and this problem goes away). Since you are using external crystal, I would make sure that your code does not load osccal, since you aren't using it, anyway. If it gets overwritten with an invalid number, and if you use a template code which loads osccal as the first instruction, you will have a chip that won't run. The way the chip loads it is to call the last word of program memory. A valid OSCCAL starts with w/e is the instruction for RETLW (return literal value) + 8 bit osccal value. So a corrupt osccal that is not a "RETLW" instruction will end up breaking the code by creating loop until stack overflows. 1. Call last instruction and push the stack. 2. Execute w/e the corrupt instruction ends up being 3. rollover to the beginning.

If you omit/remove this instruction, your code will run even if osccal gets fubared. The pk3 might give you verification error on the first flash, due to invalid osccal; just flash it again, accepting invalid osccal, since this is no problem.
 
« Last Edit: September 27, 2017, 09:37:45 am by KL27x »
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12855
Re: How to program PIC12F508-I/MS
« Reply #4 on: September 27, 2017, 10:13:37 am »
Errrrr..... NO.   The baseline PIC12/16F5xx OSCCAL is stored as a MOVLW at the top of program memory, which is the reset vecitor, and excecution then wraps around to address 0 leaving the factory OSCCAL value in the W register at the start of your program.  Unlike the midrange parts that use a RETLW, and require your code to call it to retrieve the factory OSCCAL, a missing factory OSCCAL MOVLW will *NOT* crash your program.

I don't see why one would want to put a MOSFET gate on GP3 as that pin cant drive it - its input only, however the advice to keep the capacitance on that pin low is good.   You cam put whatever you like on it if you have a 10K isolation resistor between the pin (+ICSP header) and the rest of the application circuit, as long as the application circuit can handle 1mA trying to pull it above Vdd during programming.   The programmer will always be capable of sinking enough clamping current to stop Vdd rising as there is a 1K resistor from Vdd to Vss inside it (so don't leave the PICkit 3 connected when not actually programming in low power battery applications as it will run down your battery).

The minimum set of connectons for ICSP is 5 lines, but some of the newer chips need the sixth Aux/LVP line as well so *MOST* Microchip ICSP connectors are six pin.   

The only low pin count parts that don't use the standardised pins for their ICSP interface are some of the USB ones.   18 pin parts are *NOT* counted as low pin count - that designation only applies to 8, 14 and 20 pin parts.

N.B. Do *NOT* cheat and try to use a 4 pin ICSP connector omitting Vdd by configuring the programmer to supply power when the boad has its own power source.  If you do that, it expects to be able to control Vdd to stop the chip running before it raised Vpp to enter programming mode, and its inability to power down the chip to stop it can cause programming to fail.
« Last Edit: September 27, 2017, 10:17:02 am by Ian.M »
 
The following users thanked this post: KL27x

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: How to program PIC12F508-I/MS
« Reply #5 on: September 27, 2017, 08:05:33 pm »
^Ok, thanks. Memory confused.

So I checked to find my malfunction. Yes, regarding 12F508, you're right. Of course. Movlw in the reset vector (which happens to be the last word in program memory).

For 12F630/629, which is midrange, there's a call 0x3ff... which is last word in program memory. And many of other more advanced midrange chips like 683/685/690, do away with osccal. So they're not all the same, in the midrange. The midrange using osccal are sortof "tweeners," which help to fuzz my memory. Going enhanced, I suppose, is the minimum threshold where none will use osccal.

I have always used 12F508 with internal clock, so I have always had to chuck chips which fail programming the first time, because some of them fail verification due to osccal; and when using PtG I can't tell how/why; losing factory calibration puts the chips internal clock off as much as 20%; they can be recalibrated, but it takes time. I suppose if you're not using the internal clock, as long as the osccal is corrupted with something that doesn't randomly cause a problem, it won't affect anything. In fact, whenever I get corrupted osccal, it's usually changed to 0000. If using a midrange with osccal and external clock, you could circumvent osccal instruction, entirely. So it's technically an improvement over basic. The basic device osccal arrangement is worse than I remembered.


« Last Edit: September 27, 2017, 08:53:22 pm by KL27x »
 

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: How to program PIC12F508-I/MS
« Reply #6 on: September 27, 2017, 08:51:58 pm »
Quote
I don't see why one would want to put a MOSFET gate on GP3 as that pin cant drive it - its input only, however the advice to keep the capacitance on that pin low is good.
I use that as example, because I have made that mistake. I put small signal FET gate on MCLR signal of my ICSP pen, itself, to drive an LED, as one simple indicator that I have initiated a flash. This works fine for many years using PK2. But PK3 is either not as patient in waiting for the signal to rise, or the Vpp boost has higher output impedance.

I have long since added audio indicator to PK2 P2G (and now PK3), so this was extraneous, but it caused me some grief with PK3, initially. Looking at circuit, ICSP cable, and completely overlooking this LED I put on some of my ICSP pens.
« Last Edit: September 27, 2017, 09:04:39 pm by KL27x »
 

Offline IconicPCBTopic starter

  • Super Contributor
  • ***
  • Posts: 1534
  • Country: au
Re: How to program PIC12F508-I/MS
« Reply #7 on: September 28, 2017, 12:56:47 pm »
Thnk You all for Your input.

Yes RTFM is always good starting point however not the most productive at times,
A Read of the manual is sometimes barely sufficient help when starting from zero on a steep learning curve.

As it stands... this is all of academic value, it is not going anywhere,
 A decision has been made which will result in an off shore effort.

Thank You all for Your consideration.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf