8-Pin, 8-Bit CMOS Microcontroller with A/D Converter
PIC12C671

Using the 8-Pin PIC12C671 & PIC12C672 Microcontrollers with PicBasic Pro:

The PIC12C67X is a high performance RISC microprocessor in a small 8-pin package.  This article will help you to understand some of the commonly misunderstood features of this small 8-pin powerhouse, and help you get started using the PIC12C67X series microcontrollers with PicBasic Pro.

Besides being a fully functional microcontroller with only 8-pins, one of the most unique features of the PIC12C67X series microcontroller is the internal 4MHz oscillator and OSCCAL register used to fine tune the internal oscillator.  Unfortunately, this also seems to be one of the most confusing features, and can often lead to serious frustration for beginners wanting to implement the 8-pin PIC in new projects and designs.

Ideally, by the time you finish this article, you'll be prepared to fire-up your next PIC project using the PIC12C67X micro.  The PIC12C67X comes in two basic flavors.  The 671 has 1K program memory while the 672 has 2K.  Notice that even though the 672 has double the program memory, they both have the exact amount of data memory.  The table below shows program memory (EPROM) and data memory (RAM) for both devices.

  Device   Program Memory   Data Memory
PIC12C671   1024 X 14   128 X 8
PIC12C672   2048 X 14   128 X 8

Packed with features:

Microchip has packed quite a few features into this small 8-pin package.  Here's a list of features.

bullet8-bit A/D Converter.
bullet8-bit real time clock/counter (TMR0) with 8-bit programmable prescaler.
bulletPower-On Reset (POR).
bulletPower-up Timer (PWRT) and Oscillator Start-up Timer (OSC).
bulletWatchdog Timer (WDT) with its own on-chip RC oscillator for reliable operation.
bulletProgrammable code-protection.
bulletPower saving SLEEP mode.
bulletInterrupt on pin change (GP0, GP1, GP3).
bulletInternal pull-ups on I/O pins (GP0, GP1, GP3).

Get the Datasheet:

You can download the datasheet directly from Microchip by clicking HERE.  The datasheet goes into much more detail than we will in this article.  The primary intent of this article is to help you interpret the datasheet, and get you started with the PIC12C67X and PicBasic Pro now.

Like any other microcontroller, the PIC12C67X uses registers to configure the onboard options.  Understanding how to configure these registers will make it easy to take full advantage of all the available options the microcontroller has to offer.  We don't want to simply copy the datasheet here, but some of the register configurations [especially the OSCCAL register] are very important to understand.

Since understanding how to configure the OSCCAL register is so important, we'll start with this, and work towards configuring the onboard 8-bit A/D converter.  The PIC12C67X series only has a total of 6 I/O-pins, so using the internal oscillator has definite advantages by freeing up 2 more I/O-pins [GP4 and GP5] that would otherwise be used for the external crystal connection.

About the OSCCAL register:

The PIC12C67X microcontroller has an internal 4 MHz oscillator that can be used instead of an external crystal.  You can use an external crystal, but this  will cost you I/O-pins GP4, and GP5.  The Oscillator Calibration (OSCCAL) register is used to calibrate or fine-tune the internal 4 MHz oscillator.  It contains 4-bits for fine calibration and two other bits to either increase or decrease frequency.  

OSCCAL REGISTER ( ADDRESS 8FH )
R/W-0 R/W-1 R/W-1 R/W-1 R/W-0 R/W-0 U-0 U-0
CAL3 CAL2 CAL1 CAL0 CALFST CALSLW --- ---
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
  BIT 7-4: CAL <3:0>: Fine calibration bits for internal 4 MHz oscillator.    R = Readable bit
  W = Writable bit
   U = Unimplemented bit, read as '0'
  -n = Value at POR ( Power-On Reset )
  BIT 3: CALFST: Calibration Fast
  1 = Increase frequency
0 = No change NOTE: After a Power-On reset, the values shown by each register will be the default value for that register-bit.

Example:  R/W-1 shown above for CAL1 indicates that the value of this register [ after a Power-On Reset ], will be "1".

  BIT 2: CALSLW: Calibration Slow
  1 = Decrease frequency
0 = No change
  BIT 1-0: Unimplemented: Read as '0'
  Note: If CALFST = 1 and CALSLW = 1, CALFST has precedence.

It all looks pretty technical if you're a beginner doesn't it..?  No problem, we will explain in detail what all this really means, and show you how to use it.

Setting up the OSCCAL:

Setting up the OSCCAL looks a little intimidating, but it's actually pretty easy once you understand it.  Each PIC12C67X microcontroller is pre-programmed at the factory with an oscillator calibration value in the [last] program memory location of each device.  This value is located at 03FFH on the PIC12C671, and 07FFH for the PIC12C672.

The actual instruction programmed into the last program memory location of each device is the RETLW instruction, and it looks like this: RETLW  XX.  The XX is the actual calibration value.  The RETLW instruction means [return with literal in W].  The W register is similar to the accumulator of the 8051 microcontroller and is simply a storage space used for many different internal logic operations.

Great..!  So how do we use this..?:

If you're programming the PIC12C67X  the "Hard Way" using assembly language, you simply call the last program memory location.  For the PIC12C671, we insert a CALL 03FFH instruction in the beginning of our code.  When control returns from the called address, we will have the OSCCAL CALIBRATION VALUE in the W register.  Now you need to place this CALIBRATION VALUE in the OSCCAL register by using the instruction MOV OSCCAL,W [ or MOVWF OSCCAL ] depending on the assembler you're using.

The older Parallax assembler allows code entry very similar to 8051 style assembly. The Microchip assembler [ MOVWF OSCCAL ] looks quite different

If you're programming the PIC12C67X the "Easy Way" using PicBasic Pro, simply place the following PicBasic commands in the beginning of your code:

DEFINE OSCCAL_1K 1   ' [for the PIC12C671]

DEFINE OSCCAL_2K 1   ' [for the PIC12C672]

Presto...!  You have just configured the onboard oscillator for the PIC12C671, or PIC12C672 microcontroller.

But There's More..!:

Actually, this isn't all that confusing, but I have had a lot of people ask me about this one so I figured it was worth explaining here.  The PIC12C67X microcontrollers come in the OTP, and JW versions.  The OTP [one time programmable] version is probably the least confusing since you can't erase it, hence the name OTP [one time programmable].

The JW [windowed EPROM] version can cause you headaches if you forget to first read the osccal calibration value prior to erasing the device.  The JW version is used by designers that want an EPROM based PIC that can be erased & re-programmed several times during the initial development cycles.  The JW version will have a small clear window on the top of the PIC that will pass ultraviolet light into the PIC allowing erasure of the device.  Once erased, you can then re-program the JW version PIC.

The JW version contains the osccal calibration value in the last program memory location just like the OTP versions, but you can physically erase the osccal calibration value and the RETLW instruction once you place the device in an ultraviolet EPROM eraser.  Many people have rendered the JW versions useless by not first reading the osccal calibration values for the JW device prior to erasing it.

Note:  When you first use the JW version of the PIC12C67X, you can simply use the DEFINE OSCCAL_1K 1 [for the PIC12C671],  or DEFINE OSCCAL_2K 1 [for the PIC12C672].  Make sure you read the osccal calibration values, and record them, before you erase the JW device.

To read the osccal calibration values of a JW device, simply place it in your PIC programmer and do a read of the device.  Jot down the value shown in the last program memory location.  Still confused..?  Here's an example reading taken from a PIC12C671 and a PIC12C672 using the EPIC Plus PIC Programmer.

Select the device type you want to read:

Select the device type you want to read using the drop-down menu selector.  In this example we have selected the PIC12C671.  Once you have inserted the PIC12C671 into the EPIC programmer, simply select read from the drop-down Run menu.  You can click on the icon of the open book also to read the PIC installed in the programmer.  After you have read the JW device, you need to record the values of the last program memory location.  Write down this value, and [ if you have more than a single JW PIC ], mark each PIC with some type of identifying mark that you can later use to match with the osccal calibration values read for that particular device.  It's important to note that [ these values will vary from one PIC to another ], so an identifying mark is pretty handy.

Here's an example screen capture showing the actual osccal calibration values for both the PIC12C671, and the PIC12C672.  Take note of the program memory locations where each calibration value is found.


PIC12C671 OSCCAL Value 68H Located at 03FFH


PIC12C672 OSCCAL Value 94H Located at 07FFH

Not exactly rocket-science, but useful nonetheless.  If you erase the JW device prior to reading the osccal calibration values as shown above, [good luck] making it work again with the correct timing.  Notice how they both have the hex number 34 just before the actual osccal calibration values..?  That's the instruction RETLW.  The last two numbers are the actual osccal calibration values.

From the examples above;  The PIC12C671 has an osccal calibration value of 68H.  The PIC12C672 has a value of 94H.  These are the numbers you need to record for future programming of each JW device once you have erased it.

I have it..!  Now what do I do with it..?

Once you have recorded the osccal calibration values, programming the device again with the correct value is simple.  Using the PicBasic Pro Compiler it's extremely simple.  For this example we'll use the value recorded for the PIC12C671 as shown in our example above.  The osccal value was 68H, so here we go:

PicBasic instruction to place $68 in the OSCCAL register:

OSCCAL = $68  That's it...!  Pretty simple don't you think..?  Simply place this line of code in the beginning of your program to assign the hex value of 68 to the osccal register.  PicBasic handles everything else for you automatically.

Note:  Don't use our example of $68.  Remember to first read your PIC12C67X device with your EPIC (or other) PIC programmer, and record your own device calibration values.

Summary:

You have a PIC12C671 (one time programmable) PIC.  You calibrate the onboard 4 MHz oscillator by using the PicBasic Pro command:

DEFINE OSCCAL_1K  1

You have a PIC12C672 (one-time programmable) PIC.  You calibrate the onboard 4 MHz oscillator by using the PicBasic Pro command:

DEFINE OSCCAL_2K  1

You have either the PIC12C671 or PIC12C672 (JW), windowed EPROM version and you're using it for the first time.  You calibrate the onboard 4 MHz oscillator by using the PicBasic Pro command:

DEFINE OSCCAL_1K  1 ; For the PIC12C671

DEFINE OSCCAL_2K  1 ; For the PIC12C672

You have just programmed your PIC12C671 (JW) device for the first time using the PicBasic command: DEFINE OSCCAL_1K  1 for the PIC12C671 and your program didn't function properly.  Since you paid attention to this article, and you already know that the osccal calibration value for your PIC12C671 was $68 (because you read & recorded this value) before you erased the PIC, you simply place the following PicBasic instruction in the beginning of your code:

OSCCAL = $68

The OSCCAL = $68 instruction simply replaces your first instruction DEFINE OSCCAL_1K  1.

 
bullet

Click HERE to purchase PicBasic Compilers - and hardware.

bullet

Click HERE to learn more about the PicBasic compiler.

bullet

Click HERE to return to the PicBasic projects page.