Capacitance Meter MkII


Home


 
Kits are available for this project from
Talking Electronics for $10.00 plus postage.


PIC12F629 Data Sheet (.pdf  4,926KB)
Instruction Set for PIC12F629
blank12F629.asm template

PIC12F629.inc

See more projects using micros:
Elektor,EPE,Silicon Chip

Notepad2.zip     Notepad2.exe 
Library of Sub-routines "Cut and Paste
"
Library of routines:   A-E   E-P    P-Z 
 

 

This project is specially designed to measure surface mount capacitors in the range 1p to 920n and electrolytics in the range 1u to 100u.
The biggest problem with surface mount capacitors .  .  .  they are not identified!
And you cannot determine their value by the size of the component.
This makes it very difficult.
Unless you know the value when taking it off a reel, they can get mixed up and fitting a 22n in place of a 47p will create a lot of problems.

This project is a low-cost piece of test equipment especially designed to test these devices in the range 1p to about 100u .  .  . capacitors 1u and larger are identified on their body with values such as 105 for 1un, 106 for 10u and 107 for 100u.
Surface-mount capacitors are also fiddly to hold and test at the same time, so we have designed a set of tapered contacts where the capacitor is held with tweezers and moved down the contacts until it is gripped by the contacts. Then a reading can be made.
Simply hold the button ON and the display will produce a reading.

 


The CIRCUIT
The circuit diagram is very simple but a lot of "design consideration" has gone into the 4 components used to detect the value of a capacitor. The biggest problem is the range of capacitance we want to measure. The circuit detects capacitance values from 1p to 100u. This is a range of 100,000,000:1 or 108:1

The normal way to measure the value of a capacitor is to charge it via a resistor and see how long it takes to charge to 66% of the charging voltage. This length of time is called a TIME CONSTANT and applies to a capacitor when a particular "charging resistor" is used.
For instance, if we have a 1p capacitor and 100k resistor, the time constant will be:
(100,000 ohms x 1Farad) / 1,000,000,000,000 = 0.1microseconds.
This means a 1p capacitor will charge in 0.1 microseconds when a 100k resistor is used to charge it.
We are using a PIC12F629 microcontroller and each instruction-cycle takes 1 microsecond. This means we can only "look at" the charge on the capacitor every microsecond and by this time it will be already charged  - as it only takes 0.1 of a microsecond.
In fact the fastest-rate we can look at the charging of a capacitor is every 3-5 microseconds due to 3 instructions needed to create a "look" loop and this means the smallest capacitance we can detect is 50p and increments in amounts of 50p.
However there is one other feature built into the PIC micro that operates at up to 10MHz. It is called the COMPARATOR MODULE, but to make it easier to understand how it works, we will call it an OP AMP.
An Op-Amp has a NON-Inverting input (+) and an Inverting Input(-) and an output.
We have already discussed the OP-AMP in another article on Talking Electronics website, however the essential points to note are these.
The "+" input of the op amp can be supplied from an internal reference voltage and we have selected this voltage to be 60% (3v) of the supply voltage.
Since this voltage is a reference voltage, it must be fixed and that is why we have used a voltage regulator to supply an accurate 5v for the supply-rail.
When 3v is supplied to the "+" input of the op amp, the output is HIGH when the "-" input is at any voltage below 3v.
As soon as the "-" input rises a few millivolts above 3v, the output instantly goes LOW.
When the "-" input goes a few millivolts below 3v, the output goes HIGH again.
This means the "-' input only has to change a few millivolts and the output changes from HIGH to LOW.
The "-" may only need to be 1mV above and 1mV below 3v for the output to change. We don't know the exact value but it does not matter. We just need to remember this characteristic.
Now we need to create a circuit that will charge a capacitor via a resistor, then discharge the capacitor to create an oscillator. By measuring the frequency of the oscillator we can determine the  value of the capacitor.
If we place a capacitor on the inverting input with a resistor to the output of the op amp, we will create an oscillator. But the capacitor will only have to charge and discharge a few millivolts for the circuit to oscillate and this will produce a very high frequency oscillator. We cannot deal with a high frequency oscillator so we must improve the circuit.
By adding a diode across the charging resistor we will almost fully discharge the capacitor each time the output goes low and this will increase the charging time.
We have also added a 1k resistor so the charge on a large capacitor does not damage the output of the op amp when it goes low and this will also gives us a known amount of discharge.
We now have an oscillator with a frequency made up of a known charge-time and a known discharge time. However for a 1p capacitor this frequency is about 10MHz.
To reduce this frequency we have added a 100p to the circuit. The frequency is now about 100kHz and any capacitor added in parallel will reduce the frequency.
By detecting the frequency when no capacitor is being tested, the program detects the frequency when a capacitor is being tested and determines the difference between the two frequencies.
When a capacitor above 1u is fitted to the circuit, the frequency of oscillation is very low and the circuit changes so that pins 5 and 6 become normal in-out pins.
The 1u is discharged via pin 5 and allowed to charge until pin 6 detects a high at approx 1.2v
The same applies to values up to 100u.
With the 4 components connected to pins 5 and 6 we can determine the value of capacitance connected to the project and the program takes a ready and progresses through the program until a valid reading is made.
This is done by connecting the comparator as an oscillator and detecting the number of cycles for a short period of time and if the result falls within a certain range, a reading is made. If not, the program advances to the next section where a longer delay is created and another reading is made.
The rest of the circuit drives the LEDs in the 7 segment display.
The PIC12F629 does not have enough pins to drive each segment in a 7-segment display separately and so the segments must be multiplexed.
When an output pin is HIGH, it will drive two LEDs in one segment and when it is LOW it drives two LEDs in a another segment.
This means we need 3 and a half drive-lines.
To turn both the high and low segments off, we make the drive line into an input.
This means the 4 LEDs are directly across the 5v rail, however they will not turn on because each LED possesses a "characteristic voltage" of 1.8v and any voltage up to this value will not turn the LED on AT ALL. As the voltage rises above 1.8v, the LED starts to turn on and when 25mA is flowing, the voltage is 2.3v.
Contrary to popular belief, the voltage across a LED is not constant but when the LED starts to glow, the voltage does not change very much from a very dull illumination to full illumination. This is where the concept on constant voltage-drop was born.
However we must take into account absolute-no-current-flow and current-flow at full illumination.
At 1.8v per LED, 4 LEDs require 7.2v and we only have 5v available. That's why they don't illuminate.
To produce a letter or number on the display, we need to make some outputs high for 5mS then some outputs LOW for 5mS and repeat the sequence for 40 cycles to illuminate a character for 250mS.
This is called multiplexing as we are doing a number of things very fast to achieve a result that the eye sees as a constantly-illuminated item.
On top of this we are using two of the pins (5 and 7) to detect the value of a capacitor as these pins are connected to the comparator when the appropriate code is executed in the program.



We have always been told the charging of a capacitor is non-linear but the time taken for a 1u capacitor to charge to a certain voltage will be 100 times less than a 100u electrolytic.
The non-linearity of a capacitor is the rate at which the voltage across it increases and this is not the characteristic we are measuring in this project. We are measuring the time it takes to reach a certain voltage compared to another capacitor and the times are in a linear relationship to the capacitance-values.
This means a single, simple, counting routine can be used for each of the groups we will be measuring.
The first group is 1p to 1,000p 
The second group is 1n to  990n
The third group is
 1u to 220u



The circuit takes a short time to produce a steady reading.
The first reading is not accurate for electrolytics as the capacitor is not charged and the reading will be high, since it needed to get to the operating high and low voltages provided by the circuit.
After a further cycle it will be discharged via a diode and this will leave a small residual voltage and the timing is taken from this voltage-level to a reading of approx 1.2v when the input detects a HIGH.
Further readings will vary only a small amount as the electrolytic gets "formed."

 

 
To get a reading on the display requires a number of stages.
For the low value capacitors we need to set up an oscillator arrangement and count the number of cycles over a short period of time.
We have already mentioned why this is needed, as the micro will only look at an event every microsecond and if we are looking for the instant when a 1p capacitor is charged, we need to be looking 10 million times a second.
The micro is not capable of doing this, so we put the capacitor in an oscillator and count the number of cycles over a short period of time. This will not involve any of the "slowness " of the micro.
The comparator module in the micro can be used as an oscillator and the output feeds timer 1.
The oscillator is capable of operating at a frequency higher than 1MHz and the timer will detect frequencies up to about 10MHz.
The comparator is set up as an oscillator and connected to the timer then the timer is zeroed and a delay is called.
At the end of the delay, the value in the lower section of the timer is loaded into a file.
The delay is adjusted so the final count in timerL is 240 and it does not matter how many cycles spill over into timerH.
If the delay is active for a longer period of time, the number of cycles will be higher and this will allow us to produce different values, such as for 330p and 335p.
This is part of setting the value for the delay and allows increments of one or two pF to be displayed.
If we place a 1p capacitor in an oscillator circuit, the frequency will be very high, so we have added a 100p to the circuit and the unknown capacitor is placed across it.
This gives us a manageable starting frequency and by using the comparator module as an oscillator, we can detect a 1p capacitor.
The second step is to take the value saved in a file and subtract it from 240. This gives a result of 1 for 1p and about 75 for 100p.
This is nearly a 1:1 ratio but it is not linear and so we need to produce a table to connect each value in the file with a capacitance-value.
This table is done as a sub-routine in "disp_convert." There are a number of these.
We now have a binary number from 1 to 75.
The next sub-routine "b2bcd" converts the number to 0-9 in two files to represent units and tens: file_u and file_t.
Finally we have a "Display" sub-routine that takes the tens file and looks to see if it is zero, to creates leading-zero suppression.
It then calls "convert" sub-routine that is basically a table that decrements the value in file_u and when it is zero, the sub-routine calls the appropriate show0 to show9 sub-routine.
Each of these "show0 to show9" sub-routines supplies values for the TRIS and GPIO port and then the micro goes to a common sub-routine called "show" where the values are used to turn on some LEDs for 5mS then others for 5mS to produce the appropriate digit. This sequence is repeated 2Fh times to produce a digit for about 200mS.
The outputting of a digit may be complex but remember, we only have a 5 lines available and normally a 7-segment display requires 7 lines. That's why we have to toggle the lines HIGH and LOW to activate 2 segments per line.


WHERE DOES THE TIMING COME FROM?
The timing to charge and discharge the capacitor is the technical way to describe how the capacitor is charged (the size of the charging resistor) and the voltage at which the capacitor is detected as being charged.
The timing for the low values of capacitance is created by the 100k charging resistor and the high voltage is detected by pin 6 being a couple of millivolts above pin 7. Pin 7 is set to 60% of rail voltage by a divider within the chip.
The capacitor is discharged via a diode and 1k resistor and the actual LOW voltage is not know and not detected.
We do not need to know this voltage as we are comparing the frequency generated by a known value during the time when the project was developed with an unknown value from a constructor.
The only thing that is important is to produce a gap between the high and low voltages, so an oscillator can be created. This is what we have done by discharging the cap to a low value and charging it to about 60%.
For the higher values of capacitance, the capacitor is discharged via a resistor and delay that is long enough to discharge the largest value electrolytic we will be detecting, then charging it via a 100k resistor. The number of microseconds to charge it to a point where the voltage is detected by the input line as a HIGH, is detected and this is used to determine the value in microfarad.
In all these cases, we do not use any formulae or "time-constants" but merely create timing-values from known values of capacitance and use them in a table.
 

READ
You cannot possibly work out how the circuit works by looking at the schematic.
All the work is done by the program.
On top of this, you cannot work how the micro is operating without knowing some of the mechanical details of how the micro works.
That's why you have to read the notes on how the project was designed. There are lots of hidden details that will help you design your own projects, like converting binary to decimal, turning the comparator into an oscillator, and outputting to 7 segments.
 


 
 


 




   




THE POWER-ON BUTTON
The project has a push button for the power.
This serves two purposes.
The oscillator needs a "kick start" by pulsing the supply and other advantage is the project cannot be left on.
When the "Press for Zero" button is pressed, the display shows "0" "p" and "0" "n" indicating zero "puff"  and zero "n"  When a capacitor in the range 1p to 4,700p is fitted, the display shows the value up to 4700p  and any value above 4,700p (4n7) is shown as 4n7 to 1,000n (1u). This means a capacitor such as 470p will show as 4 7 0 p  and a 2n2  will show as 2200p.





 

  




 

INSTRUCTIONS FOR USE
There

TO

 


 




 
 




 

THE LEDs
The LEDs supplied in the kit are surface-mount and fit on the board to create the 7-segment display.
They require careful soldering and that's why we have provided a few extra as they will lose their illumination if heated up too much.
Put a little solder on one of the lands and pick up the LED with tweezers and solder it in place.
Then solder the other connections very quickly.
Make sure they are square on the board and and also in alignment with the other LEDs, to make the display look neat.


CAPACITORS
To understand capacitor values you have to know some basic values.
The basic values are these:
a 1p capacitor is about the smallest you can get and is equal to two pieces of enamelled wire about 2cm long.
The next value to remember is 100n. It is 0.1u or 1/10th of one microfarad and used in many places such as spike suppression across individual chips.
It is a physically small component because it a layered ceramic device and the manufacturers have reduced the size due to clever layering techniques.
As with al capacitors, you cannot determine the value by the size of the component and that's what makes it so difficult to work with them.
No surface mount capacitors are marked with any values up to 1u, so you have to know exactly how to express each value in a notation called STANDARD NOTATION.
Many circuit diagrams already use this form to show the values but we will provide a list so that all notations can be compared.
 

1p =  1p0       
10  =  10p     
101 = 100p   221 = 220p   331 = 330p    471 = 470p  
102 = 1,000p = 1n = 0.001u  222 = 2n2   332 = 3n3   472 = 4n7
103 = 10,000p = 10n = 0.01
104 =
100,000p = 100n = 0.1u
105 = 1u
106 = 10u
107 = 100u
 

 


CONSTRUCTION
You can build the circuit on any type of PC board and we have used a small piece of matrix board.
The kit of components comes with all the parts you need to get the project working, including a pre-programmed chip and the matrix board.
To modify the program you will need a PICkit-2 programmer and this comes with 2 CD's containing all the software needed for In-Circuit Programming.
You will also need a lead (comes with PICkit-2) to connect the programmer to your lap top via the USB port and an adapter we call 6pin to 5 pin Adapter to connect the PICkit-2 to your project.

PROGRAMMING THE CHIP
The kit comes with a pre-programmed PIC chip but if you want to program your own chip or modify the program, the .hex file is available as well as the assembly file, so you can see how the program has been written and view the comments for each line of code.
The PIC12F629 is one of the smallest micros in the range but you will be surprised how much can be achieved with such a tiny micro.
The program contains sub-routines to produce delays, sequences on the display and both read and write EEPROM; jobs that require accurate code - including a special sequence - called a handshaking sequence that prevents the EEPROM being written due to glitches. 
Even a program as simple as this is not easy to put together and to assist in this area, we have provided a whole raft of support material.
Not only do we provide a number of programs with full documentation but our approach to programming is simple.
It involves a method of "copy and paste" whereby sub-routines are taken from previously written code and copied into your program. Any modifications are made in very small steps so that each can be tested before adding more code.
This is exactly how we produce a complex project. Each step is written and tested before adding the next step.
This saves a lot of frustration as it is very easy to add a line of code that is incorrect and get an unsuspected result. 
If you follow our suggestions you will buy a programmer ("burner") called a PICkit-2 if you are using a laptop. It is the cheapest and best on the market and comes with a USB cable and 2 CD's containing the programs needed to "burn" the chip. If you are using a desk-top and/or tower with a serial port, you can use a cheaper programmer called MultiChip Programmer from Talking Electronics. You will also need NotePad2 to write your .asm program. This can be downloaded from Talking Electronics website. You will use LED_FX.asm or LED_FX-asm.txt as a template for your program, plus a 6 pin to 5 pin connector that fits between the burner and the project. This is also available on Talking Electronics website.
As we said before, this project is for medium-to-advanced programmers as it is very compact and does not have in-circuit programming pins.
To be able to modify the chip you will need a programming socket and this can be obtained from one of our other projects that contains the 5 pins for in-circuit programming.
You can then put the chip into the other project to be programmed and modified and re-fit it into this project for execution.

PROGRAMMING LANGUAGE
There are a number of kits, programs and courses on the market that claim and suggest they teach PIC Programming.
Most of these modules and courses use a PIC microcontroller as the chip carrying out the processes, but the actual programming is done by a proprietary language invented by the designer of the course.
Although these courses are wonderful to get you into "Programming Microcontrollers" they do not use any of the terms or codes that apply to the PIC microcontroller family.
All our projects use the 33 instructions that come with the PIC Microcontroller and these are very easy to learn.
We use the full capability of the micro and our pre-programmed chip is less than the cost of doing it any other way.
In addition, anything designed via our method can be instantly transferred to a PIC die and mass produced. And we use all the input pins and all the memory of the chip. The other approaches use less than 25% of the capability of the memory and one of the pins is not available.
In fact it would be difficult to reproduce this project via any of the opposition methods. It would require a larger chip and more expense. 
You can use our method or the opposition. Just be aware that the two are not interchangeable.
Ours is classified as the lowest "form" (level) of programming - commonly called machine code - invented in the early days of microprocessors - and now called mnemonic programming as each line of code is made up of letters of a set of words. The opposition uses a higher level language where one instruction can carry out an operation similar to a sub-routine.
But you have to learn the "higher level language" in order to create a program. And this requires a fair amount of skill and capability.  
It sounds great and it is a good idea. But if you want to learn PIC programming, it does not assist you. It is "a step removed" from learning PIC language. The other disadvantage of the opposition is the "overhead." The 1,000 spaces allocated for your program is filled with pre-written sub-routines. You may require only 10 of these sub-routines but ALL of them are loaded in the memory space. And they take up all the memory.
You have no room for your own program.
To get around this the opposition uses the 128 bytes in EEPROM to deliver instructions on how to apply the sub-routines. This provides about 30 powerful instructions using their language called BASIC (or a similar language). 
It's a bit like selling a diary filled with all the paragraphs you need to express yourself, and leaving a few blank pages at the back for you to write single lines such as: see page 24, paragraph 7, see page 63 paragraph 4, to create your diary entries.
It depends on how much you want to be in charge of writing a program. Using our method is like writing your own auto-biography. Using the opposition is like getting a "ghost writer."
When using a higher level language to create a program, you have absolutely no idea how the code is generated for the micro.
In some of the developmental kits, the code is "locked away" and you are NEVER able to access it.
Everything runs smoothly until a fault appears. With our method you can see the code. With the other methods, you cannot see the code - it's like doing key-hole surgery without the advantage of an illuminated endoscope to see what you are doing.
Everything has its place and our method of hand-assembly is only suitable for very small micros and you will eventually need to "learn a high level language."   The PIC12F629 has over 1,000 locations for code and this equates to more than 20 pages when printed, so this is about the limit to doing things by hand.
But our drive is to show how much can be done with the simplest devices on the market, at the lowest cost.
Anyone can show you high-technology at a high price but this is not where you start and this is not where you get enthusiasm.
We provide the things to get you started. That's the difference.

The PROGRAM
The program does a bit of detecting when turned on. It detects to see if a bit has been set in EEPROM to tell the micro to go to a required sequence or start with code 1.
It also detects if switch A or C has been pressed at the instant the project is turned on so that the micro is directed to the sub-routine where the user-sequence can be entered or if the EEPROM bit is to be cancelled.
All this gets done in the SetUp routine and then the micro goes to Main.
 
In Main, the program increments a "jump" file and calls a table where it finds a directive to go to a particular sub-routine.
The sub-routine is executed and the micro goes back to Main where it looks for a release of SwA. This forms part of a key debounce as the key must be fully debounced as it is advancing the micro through the sequences.
To provide a totally reliable debounce, the key is detected as not being pushed for the duration of a whole cycle of a sequence and a separate loop is then executed where the key can be detected as being pushed, to advance the program to the next sequence.
To create your own sequence as sequence1, the project is turned off and SwA pressed while turning the project ON.
This sends the micro to a sub-routine called Attract.
As soon as SwA is released, the program starts to time the duration when a switch is not pressed and it "times-out" after 2.5 seconds.
The program also times the duration when a LED is illuminated. It also accepts 2 or 3 LEDs illuminated at the same time. These are all clever instructions that need to be looked at to see how they operate.
Up to 15 steps can be entered and each step occupies three bytes. The first value identifies the illuminated LEDs, the second byte identifies the ON duration (in increments of 5mS) and the third byte identifies the OFF time.
These 45 bytes are contained in files 30h to 5Fh.
When a switch is not pressed for 2.5 seconds, the program "times out" and sends the values to the EEPROM. It then shows the sequence on the LEDs.
If the project is turned off and on again, this sequence will be displayed as sequence1.
To replace the sequence with something else, simply repeat the steps above.
If you want one of the pre-programmed sequences to appear each time the project is turned on, simply advance through the sequences by pressing SwA and when the desired sequence is playing, push SwB.
This will record your choice. Turn the project OFF then ON again and the chosen sequence will be displayed.
To remove this feature, press SwC when the project is off and at the same time, turn the project ON.
All these feature have been added to the program, one at a time, and it is important to add them in the correct order. For instance, you can only add a removal feature after the initial feature has been produced. Reading and writing to the EEPROM is a most complex operation and the instructions must be laid out as shown in the program, as they include a hand-shaking sequence. When you need this code it is copied and pasted in its entirety, to prevent a mistake.
Nearly every instruction has a comment to explain not only what it does, but why it was chosen.
If you think you can start programming without reading programs from other developers, you are wasting your time.
No individual can work how to do many of the tasks via the simplest set of instructions and you will find some programmers have used complex code to do the simplest task.
That's why you have to pick out the "wheat from the chaff" and remember a good routine, while discarding the over-complex sets of code.
This brings up an important point.
Don't expect to be an A1 programmer in a week. It takes time to absorb the skills of programming and it is really only understood by a microscopic percentage of electronics enthusiasts. If you take it up and understand it, you are one of the microscopic few.
It is a world that, once you are in, will open up a whole new field of ideas and development.
It's like taking up a new spoken language and, in fact, a program reads like a book, so the analogy is very close.   
There are some very "clever" instructions such as XOR where you can compare two files by using the XOR function and determine if they are the same. And very powerful instructions such as djnz that decrements a file and if it is zero, the micro jumps over the next instruction.
Other clever instructions transfer the contents of a file to another via the "carry."
You cannot be expected to know these "tricks" unless you study programming. That's why we are here.

Here are the files you will need:
LED_FX.asm
LED_FX-asm.txt
LED_FX.hex

 
	
;*******************************
;;LED FX.asm
;  11-3-2010 
;*******************************

	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
	errorlevel	-302	; Don't complain about BANK 1 Registers during assembly

		
				
;****************************************************************
;*EEPROM     				*
;****************************************************************
								
	org	2100h			
					
							
	END


GOING FURTHER
We have not produced all the possible codes and you can add more by simply creating a new sub-routine. This can be stored as code 1 and can be used as soon as the chip is turned on.
Or you can add it to the table and make sure you end with retlw 00 to send the micro back to Main. You will understand what we mean when you start programming.
We have provided all the hardware and software for you to do this.
Now it's now up to you.

 

Capacitance Meter MkII
Parts List

Cost: au
$15.00 plus postage
Kits are available

3   -  82R (820) SM resistors
3  -  47k  (473) SM resistors

1  -  100n SM capacitor
1  -  100u electrolytic

1  -  SPDT mini slide switch

1  -  8 pin IC socket 
1  -  LM78L05 voltage regulator
1  -  PIC12F629 chip (with Combo routine)
3  -  super bright white LEDs
3  -  mini tactile switches
1  -  9v battery snap
20cm very fine solder 
1  -  PC Board (with 7segment display using 14 LEDs)

 1/10/10