Combination Lock MkII


Home


 
Kits are available for this project from
Talking Electronics for $15.00 plus postage.
Plus you will need:
6pin to 5pin adapter @ $2.50

You will also need:
PIC2 USB Burner
(MPASM and MPLAB come with PIC2)
and it includes USB lead


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 puts a new emphasis on an electronic combination lock.
With just 3 buttons, we challenge you to crack the combination.


LED FX built on matrix board

This project is built on the same board as LED FX and uses the same components and the same circuit.
The main purpose of the project is to teach programming, however it also offers a challenge to prove that a digital combination with just 3 buttons offers a very high level of security.
All sorts of tricks can be added to a design that uses a microcontroller, including a time-delay if the wrong code is entered and complete lock-out for 15 minutes if more than 10 different codes are entered.
The project has 5 different combinations and each one is completely different. It also has a feature where you can load your own sequence and see if you can "find it again."
With our codes, you have absolutely no idea what we have have done with the coding and it will be a challenge to try and crack it. It is doubtful if you will succeed and it will be even more difficult to repeat the sequence. The first two combinations are very easy, so don't think a normal lock will be this easy.
The project is designed for all sorts of uses, including security for your house, garage or back-shed. You can even include an alarm that detects tampering and this will discourage anyone who thinks they can work on the lock for hours.
The normal digital lock has up to 13 keys or buttons and 5,500 combinations and by progressively going through the combinations, you will be able to open the door. The main reason why these locks are difficult to "break" is the psychological aspect of letters instead of numbers.
Simply replace the letters with numbers and the lock becomes much easier to work with.
On top of this, they all have a "C" cancel button at the lower corner so don't touch this when trying a code.

A digital lock has an unknown number of sequences and and the final code in our series consists of two parts. When you crack the fist section, the 3 LEDs flash and then you need to enter the next code for the lock to open. The chances of getting two sequences in a row is minuscule. That's the same as the web introducing a password as well as a username.
 


models such as train layouts, alarms and similar effects.
It can also be expanded to accept more LEDs and these can be placed on a separate display board.
But the real thing we want to get across, is programming.
This is another example of using a simple 8 pin chip to provide a number of features that would take many logic chips (such as counters and gates) and lots of components to duplicate.
It also highlights our method of hand-coding as an effective way to produce a program.
It is doubtful if the same effects could be produced on any of the pre-packaged microcontroller modules, using a tiny 8 pin chip.
This project uses about 400 instructions to produce the effects and it uses the EEPROM to store the sequence produced by the user (sequence 1) - and show it at turn-on.
In this respect, some of the sub-routines in the program are quite complex and suitable for the advanced programmer. However, if you are a beginner, you can read through the program and most of the sub-routines will be easy to follow as each line of code is explained. You have to start somewhere and this project offers a challenge.
Most projects with a program of this complexity are only available as a pre-programmed chip or only the hex code is available. There is usually no attempt at educating the reader in programming.
That's the difference between our projects and all others.
We offer a learning curve.
For every hour of effort you put into reading, building and using one of our microcontroller projects, you get the experience of 100 hours of effort that has been put into the design to make it appear simple.
All you have to do is start . . .
 

INSTRUCTIONS FOR USE
There are 6 codes.
The first
code can be created by the user. It is currently not programmed. The other 5 sequences are pre-programmed.
Turn project ON.
Push the first button (called SwA) and hold it down and the program will change to the next code.
Release the button and solve the code. The three LEDs will flash when the code is broken.
Turn the project off and push SwA two times quickly for the second code. The program waits for 500mS between pushes to index the codes. If SwA is not pressed for 500mS, the program goes to the appropriate sequence and waits for the correct sequence of button-presses. The fisr LED will flash the appropriate number of times to indicate the code number you are trying to "break."




TO CREATE YOU OWN SEQUENCE.
1. Press SwA and at the same time, turn project ON.
2. Release SwA and press the switches in any order (up to 15 steps).  A step or delay cannot be longer than 2 seconds as the program will "time out." When finished, wait 3 seconds and the sequence will show on the LEDs.
3. Turn project off and on. The new sequence will appear as the first sequence.

TO MAKE ANY SEQUENCE THE FIRST SEQUENCE
Any of the sequences can be saved as the first sequence, as follows:
1. Turn the project ON and increment the sequences.
2. To save the desired sequence, press SwB. The display will die.
3. Turn project OFF then ON.  The desired sequence will show at start-up.
4. To delete this feature, push SwC and at the same time, turn project ON.

 


Combination Lock Circuit

The CIRCUIT
The circuit is very simple. It is just 3 LEDs and 3 switches. All the work is done by the micro.
We have added a 5v regulator and diode so the project can be connected to all sorts of voltages.
It will work on 6v if the regulator is removed or on 7v to 15v (AC or DC) with the regulator fitted to the board.
This makes it suitable for a 9v battery or the AC supply from a model railroad. I know you are going to say "it is inefficient using a 9v battery" but it is convenient.
 

THE LEDs
The LEDs supplied in the kit are ultra high-bright white LEDs. They are too bright to look at directly but can be used for all sorts of applications and effects. You can change them to suit your own application.


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.


6pin to 5pin Adapter


Adapter connected for In-Circuit Programming
(the chip is placed in another project for in-circuit programming)

 

  
Combination Lock MkII            Underside of board     

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.

 

Combination Lock 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  -  Combination Lock PC board

 12/7/10