Writing your own Program


Kits are available for this project from Talking Electronics for $12.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


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 

P2 - The Instructions
P3 - writing your own program



PIC Fx-1 module
contains a Matrix of holes where you can add components to create your own project.
The SUPPLY RAILS at the top and bottom of this area allows you to connect to the rails and place the components in the same locations as in the circuit.
This may seem a very simplistic approach to making a circuit but you will appreciate it when coming back to it after a few months. You will be able to immediately recognise the circuit and this makes it easy to work-on.
Look through the projects we have presented using this micro and you will see some of its capabilities.
The prototyping area can be cut-to-size to make the board very small.
You can use the 3 switches and 3 LEDs as a basis to your project and add further components to the prototyping area.

There are at least 4 other developmental packages available on the web and some have achieved a large following, mainly because MICROCHIP has not introduced a beginners developmental package at low-cost and have never aimed at the beginner/experimenter/hobbyist/robot enthusiast. This has allowed other manufacturers/designers/programmers to come in and grab this field, with enormous results. 
This has allowed thousands of hobbyists to get in to the robotics arena and develop lots of impressive projects.
However the methods to create a program have used techniques that involve learning a programming language that is quite complex and difficult to follow.
It uses a lot of syntax and this means the structure of each line of code must follow a definite arrangement. A coma or bracket out-of-place will prevent the program working.
But the main disappointment, as far as I am concerned, is the fact that the modules they have designed uses programming that has nothing to do with the instructions needed by the microcontroller. You are simply programming a "chip" and you don't know any of its capabilities.
This may be ok for a robot enthusiast, but it doesn't suit me. I want to know what is happening at the grass-roots level.
The other major concern is the fact that some-to-all the memory is taken up with the program supplied by the designer of the prototyping module and all that remains is a small area for your instructions in the EEPROM.
The result is the chip has much less capability and a larger chip is required to do the task of a smaller version.
In the end, you don't know how a microcontroller operates.  You are simply creating a program using a high level language. 
With OUR approach, we explain how to write a program from the beginning and show how much can be done with an 8-pin chip.
In fact we fit 4 times more than any other prototyping module, into the 8-pin chip.
Using a programming language is like getting a "ghost writer " to write your biography. You sit down for 5 minutes and say 10 sentences and he goes away and writes a whole chapter.
You are not involves in the intervening "thought-exchanges."   The same with high-level programming.
A few lines of code will produce a considerable outcome via the microcontroller.
But for those who want to know what is happening "along-the-way" you need to use the instructions supplied with the chip.
Our system is only suitable for small chips (up to 1,000 lines of code as this represents over 20 quarto pages of a  program and unless you put every sub-routine in alphabetical order, the searching through a program becomes very time-consuming.
Our method also helps you understand and develop the BUILDING BLOCKS needed to produce a program by working out what is needed in COMPUTER-SPEAK for the micro to produce an outcome.
A Building Block might be:  "output this"  or "shift this" or "divide this by 3" or "look up 5 bytes from a table."  These commands have to be converted into instructions that are understood by the micro and no matter which path you go down, correctly-written instructions must be written.
Our method uses a basis of 53 instructions, of which only about 20 are in constant use and they are all COPIED AND PASTED into a program so no mistake can be made.
In fact, whole sub-routines are generally transferred and you are aware of exactly what each sub-routine will produce.

The next thing you will want to do is write your own program. But before this can be done, you need 5 things:

1. Software to display your template on your computer: NotePad  -  free
2. Software to convert your program (called an assembly file) into .hex  - MPASM - free
3. Software to
"burn" (flash) your program into the micro via USB programmer - PICkit2 - free
. USB Programmer  - kit: $25.00 to physically "burn" or flash the data.
5. 5-6 pin connector $2.30 - to connect the USB programmer to PIC Fx module via the ICSP pins.

If you want to write your own program for an entirely different effect or project, you can use the In-Circuit Programming feature, using the 5 programming pins at the top of the board. 
This will mean you will lose all the programs supplied in the chip, but the .hex listing is available on the web for easy re-downloading.
The microcontrollers are only $2.00 each and it may be best to buy a number of chips so you can produce your own programs and keep them separate.
A complete
PIC Fx-1 module is only $12.00 and hardly requires re-thinking. 

Programming uses the same in/out pins as the project and providing the devices connected to the pins are high impedance, they will not affect the programming signals.
In our case the switches are open and the LEDs have a characteristic voltage of 3.6v before they become a "load."  The 82R resistors allow the voltage to rise another 1v before the resistor and LED becomes a significant load to the programming signals.
When creating a project, drawing a circuit and writing a program, keep to the same input lines (pins) and output lines as supplied on the module, so nothing has to be re-arranged.
Here is the circuit for the
PIC Fx-1 module:





When it all boils down, none of the other techniques teaches anything to do with PROGRAMMING A MICROCONTROLLER.
You are using instructions called a HIGH LEVEL LANGUAGE and they can be used to program almost any microcontroller.
Our course specifically targets the PIC family of microcontrollers and you are writing lines of code called mnemonic instructions.
Each line of code consists of letters to represent a set of words (called a mnemonic - something to simplify and aid memory retention).
This is called ASSEMBLY CODE and everything you write controls the microcontroller DIRECTLY.
Writing a program in assembly code is only suitable for very small microcontrollers because the program for the chip we are using will be over 1,000 lines if you want to completely fill the chip. This is more than 20 quarto pages of writing and merely searching up and down will take an effort. That's why you must structure your program in the way we explain  and place the sub-routines in alphabetical order. 
Our method is only intended to get you started in programming.
If you have been put-off by trying to learn the complexities of a high-level language, this is the place to start.
You only have to know the 63 instructions for the chip and these are provided in a table.
But we have made it even easier by creating a template with lots of sub-routines.
All you have to do is copy-and-paste the sub-routines you want into the template to produce a program.

Everything is wonderful with a high level language until something does not work.
That's when the problem starts. You don't know if the instructions are incorrect or the microcontroller has made a mistake.
By writing the code yourself, you are saved this frustration.




Turn project ON.
Allow all theC and at the same time, turn project ON.

The kit comes with a pre-programmed PIC chip but if you want to write your own program for an entirely different effect, you llo the project can be connected to all sorts of voltages.
It will work on 6v
 know these "tricks" unless you study programming. That's why we are here.

Set-up a folder called PICkit-2 in which you will place the
programs for all your projects.
Within the folder you can place MPASM and ".inc files" and PICkit-2
burning routine (from the CD that comes with PICkit-2).
If you are not using PICkit-2, call the folder All Files
and place "WinPIC.exe" or "WinPIC.zip" in the folder,
as well as MPASM and ".inc files."
You will need NotePad2.exe or NotePad2.zip so you can take a .asm file from one of
the projects we have produced and use it to write your own program.
sample.asm      sample.asm

Note 1: MPASM and WinPIC have been used to program PIC16F628A
using 8-pin to 5-pin adapter described below. The chip was programmed
successfully but WinPIC displayed "Programming failed and only the
first line of code was displayed in green.   WinPIC  does not read the
chip properly after burning.

Note 2: I have not successfully programmed a PIC10F220 via WinPIC
or PICkit-2. I do not suggest you buy a PIC10Fxxx until I have successfully
programmed one.





Here is the file you will need to write your own program:

It contains all the areas, such as Tables, Delays, Sub-routines and Main, where you place your lines of code.
But before you write a program, go through the following experiments to show how to write a program and produce a number of different effects on the PIC Fx Module:
;Blank Template.asm
; date: 

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

	__config	_mclre_off & _cp_off & _wdt_off & _intrc_osc_noclkout  ;Internal osc.

     ;_mclre_off  - master clear must be off for gp3 to work as input pin 

; variables - names and files

temp1	equ 20h	
temp2	equ 21h	

status	equ	0x03
rp1	equ	0x06
rp0	equ	0x05
GPIO 	equ 	0x05

status	equ	03h
option_reg	equ 81h

	; bits on GPIO
pin7	equ	0	;GP0  
pin6	equ	1	;GP1  
pin5	equ	2	;GP2  
pin4	equ	3	;GP3   
pin3	equ	4	;GP4  
pin2	equ	5	;GP5  

rp0	equ	5	;bit 5 of the status register

;Beginning of program
	org	0x00
SetUp	bsf	status, rp0 	;Bank 1			
       	movlw	b'11111000'	;Set TRIS  GP0,1,2 out   GP3,4,5 input
	movwf	TRISIO	
	bcf	status, rp0		;bank 0
	movlw   	07h         		;turn off Comparator ports
       	movwf   	CMCON       	;must be placed in bank 0  
	clrf 	GPIO       		;Clear GPIO of junk		
	goto 	Main			

;* Tables 		*

table1	addwf   	PCL,F   ;02h,1  add W to program counter
	retlw   	.10     
	retlw   	1ch 
        	retlw   	0Abh   
;* Delays 		*
	;5mS delay 
_5mS	movlw	05h
	movwf	temp2
_5	nop
	decfsz 	temp1,f
	goto 	_5
	decfsz 	temp2,f
	goto 	_5	
	retlw 	00	

;* Sub Routines 	*
toggle	movf	count,0
	movwf	temp1
	goto	$+1
	goto	$+1
	decfsz	temp1,1
	goto	$-3
	movlw	b'00000001'
	xorwf	gpio,1		;toggle bit 0
	retlw	00	
;* Main 			*

Main    	bsf      	gpio,0	
	call   	_1Sec
	bcf   	gpio,0	
	call   	_1Sec
	goto	Main		

Here is Blank Template.asm with each line described in full detail:

Here is the .asm and .hex files for Experiment1 Blink A LED.asm:

Burn or flash the .hex file into a NEW PIC12F629 via the In-Circuit Programming port (the 6 pin connector at the top of the module) and turn the project ON. The LED will blink.

Now load blinkA-LED.asm into Notepad and change the value 05, loaded into temp3 in _1Sec delay to 03 or .3 and save the program as blink-2.
Assemble the program in MPASM to get blink-2.asm and blink-2.hex

Open PICkit2 and load blink-2.hex and press "Write."
The LED will flash at a faster rate.

Now change the value to 02 or 2 or .2 and save the program as blink-3.
Assemble the program in MPASM to get blink-3.asm and blink-3.hex

Open PICkit2 and load blink-3.hex and press "Write."
The LED will flash very fast.



A program can be placed in the micro and only accessed when a special combination of switches is pressed.
This must be done at a particular time for the routine to be access.
The simplest way to implement this feature is to press a button before turn the project ON and the program will detect this switch in the "set-up" section of the program and go to the required program.
We will show how this is done and prove the flashing routine cannot be access without performing the required step.
If button A is not pressed before turning the project ON, the micro goes to Main and loops forever.
If button A is pressed before turning the project ON, the micro goes to Main2 and executes the flash routine.
With 3 switches you can have up to 6 hidden programs: A, B, C, AandB, AandC, BandC. 

Here is the hidden program for BandC: