| Library of Routines
 for PIC12F629
 Nearly all these instructions also work 
		with
 PIC16F628. Just check on Port value(s) and
 first available file.
 A-E  
		E-P    P-Z
 
 
    
            End This is a directive, placed at the end of of program to tell the assembler to 
			finish the job of assembling the instructions into .hex code. The 
			directive is:
 
 end
    End 
              of TableEquates   
			
  			"equ"    
			you can also use "="The end of a table can be detected in 
            two different ways.
 If a value such as FF is not used in any of the data, it can be used 
            as an End of Table marker. The sub-routine calling the table 
            must look for 0FFh to detect End of Table.
 
               
                |  
                    
                       
                        | Table 
 
 
 
 
 
 
 
 
 
 | ADDWF 02h,1 RETLW 3Fh
 RETLW 06h
 RETLW 5Bh
 RETLW 4Fh
 RETLW 66h
 RETLW 6Dh
 RETLW 7Dh
 RETLW 07h
 RETLW 7Fh
 RETLW 6Fh
 RETLW 0FFh
 | ;Add W to the Program Counter to create a jump. ;0    format= gfedcba
 ;1
 ;2
 ;3
 ;4
 ;5
 ;6
 ;7
 ;8
 ;9
 ;End of table marker
 |  |  The other method is to count the number of items in 
              a table and make sure the sub-routine calling the table doe not 
              CALL values beyond this value. 
                
	
 
            EqualTo  
             
            detect if two files are equal, they
	 are XORed together. See XOR.
     
             Equates assign an easy to remember label to a numeric value. This 
			value can be a file or bit in a file.
 e.g:
 delay1  equ   20h    ;every time 
			"delay1" is inserted into a program, the assembler will assign file 
			20h.
 
 in your program:
 
 movlw   80h
 movwf   delay1       ;the 
			assembler will put 80h into file 20h
 
 The in/out pins on a 12F629 are 2,3,4,5,6 and 7.
 These are GP5, GP4, GP3, GP2, GP1 and GP0
 These correspond to GPIO,5  GPIO,4  GPIO,3  GPIO,2  
			GPIO,1 and GPIO,0 in a program.
 Suppose GPIO,3 is called "switch"   and GPIO,5 is called 
			"LED" (it drives a red LED).
 In the equates section of your program (at the top of your program) 
			you can equate bit "3" of the GPIO file as "switch and bit "5" as 
			"LED."
 
 The equates will appear as:
 
 switch    equ   3    or    
			switch  =   3
 LED       equ   5    
			or    LED     =   5
 
 in your program:
 
 btfss   GPIO,switch
 or
 bsf     GPIO,LED
    
 Error 
	Message  302If you get an error message when MPASM 
	is compiling your program to a .hex file, 
	 it 
	will not produce the .hex file.
 To find the error messages, open the .lst file.
 Some error messages are just a warning such as:
 Message[302] myfile.ASM 136 :
Register in operand not in bank 0.  Ensure that bank bits are correct. The above is a warning that you need to select the correct bank. You can remove the warning message by inserting the following at the top of 
	your .asm program
 ERRORLEVEL -302     ;remove messages 
     
			FieldYour assembly 
			program must be laid out in columns so the assembler can detect the 
			instructions.
 
 
	
		| Label Field must start a-zor _2set
 | Mnemonic Field (the instructions)
 movlw
 movwf
 | Operand field 
 33h (hexidecimal)
 or   0.26(digital 26)
 | Comment Field ; starts with semicolon
 (the assembler ignores all comments)
 |  
		|  |  |  |  
		| _3times | movlw movwf
 | .45                                 
		;forty-five delay1                            
		;this is the delay register
 |   
	
 
	  File Value (comparison) Detecting the value of a file or register.
 
               
                |  
                    
                       
                        | 
 
 
 
 | movlw subwf,0
 
 btfss 03,2
 
 goto xxxx
 goto yyyy
 | ;load a value into w (say 
						8C) ;subtract 8C from w to see if the result is zero 
						(store ;the result in w so that the file is not altered.
 ;test the zero bit in file 03 (status).
 ;It will be set if value=file
 ;value in w is not = value in file
 ;value in w = value in file
 |  |     FSR 
               
            See Indirect Addressing This is a special file called File Select Register. 
            It has the address 04. It is not a file like the other files in the 
            micro but a POINTER FILE and is used in conjunction with another file 
            called INDF.
 INDF has the address 00.
 INDF is not actually a file but a robot arm. It grabs the contents 
            (or delivers contents) to a file pointed to by FSR. These are two 
            special files (devices) that allow very powerful (low instruction) 
            programming to be produced.
 For instance, if FSR is loaded with 2C, it will tell INDF to grab 
            (or deliver) contents to file 2C.
 To do this, we need the instructions:
 MOVLW 2C
 MOVF 04
 If we now put 8Fh into INDF, the value will actually go into file 
            2C.
 This is called INDIRECT ADDRESSING.
     
            GOTO
	The GOTO instruction causes the micro to go to the address identified by a 
	label such as "No" or "Yes."
 You can use the GOTO instruction as many times as you like in a 
			program and the micro will follow your requests.
 But you cannot go to a subroutine that has an rtlw instruction as 
			the last instruction as the program has not remembered where you 
			came from.
 The GOTO instruction does not involve the stack and it does not 
			involve getting back to a particular place (instruction).
 If you use the CALL instruction, the micro will go to same place as 
			the GOTO but the address of the next instruction (from where you 
			came) will be placed on the stack so that rtlw will send the micro 
			back to where you came from. In addition it will remove the address 
			from the stack so that the stack does not increase in size and 
			“overflow.”
 
 You can only go to a “rtlw” instruction if you have used a CALL 
			instruction to go there.
 If not, the program will not know where to go and will use any 
			address on the stack.
 All sub-routines must end with rtlw so the sub-routine can be called 
			from many parts of the program.
 
 
               
                |  
                    
                       
                        | 
 
 No
 Yes
 | BTFSS 
						GPIO,0 GOTO No
 GOTO Yes
 RETLW 00
 MOVLW  3Fh
 etc
 | ;Is 
                          button pressed? ;No
 ;Yes
 |  |  The instruction:    
	goto   $ + 2    
 "$" means the current address.
 
 "+ 2" means to advance two instructions down the program.
 
 
               
                |  
                    
                       
                        | 
 
 
 | btfss GPIO,0 goto  $ + 2
 instruction x
 instruction 
						y
 | ;this will end the micro to 
						instruction 
						y
 
 ;the micro will advance to this instruction
 |  |   To go "up" the program, the goto instruction is:goto  $ –3
 or
 goto  $ 
 
             
            –0dh
 or     $ –.16   
			(minus decimal sixteen).  The instruction should be written 
			without any gaps:   
            $–.16
 
 When writing a program, you must make sure the micro will get back 
			to the "Main" routine.
 If you use a "call" instructions such as:
 
 call   delay_1
 
 make sure the sub-routine has:
 
 retlw   00
 
 to get the micro back to "Main."
 
 You cannot use the instruction:
 
 goto   delay_1
 
 as the  
            "retlw   00" 
             in the delay_1 routine will not take the micro back to 
			"Main" as the return address has not been remembered with a "goto" 
			instruction.
 
 You can also define a location by 
			writing the LABEL (in this case "Step") and an offset - in this case 
			+1. The micro will go to  "movwf  tmr2."  This has an 
			advantage. It is easy to see, rather than writing $–4, when a large 
			number of instructions are to be counted.
 
 
               
                |  
                    
                       
                        | Step 
 
 
 
 | movlw     
						25 movwf     tmr2
 btfsc       Sw2
 goto       newpress
 decfsz   Sw2,f
 goto      Step+1
 retlw      00
 | 
 
 |  |      
             Halt 
            Do not use the word "Halt" as a label, the assembler does 
            not like it. Use Loop, Pause, Stop, Wait.  See Loop 
            and Stop.
 
 To create a "halt" instruction:
 
 Loop        
			goto  $            
			This will cause the microcontroller to keep looping the same 
			instruction.
    
 
             
            Halve 
              (Half)To halve (half - divide by two) the value of the contents 
            of a file, it is shifted RIGHT (RRF 1A,1).    The number 
            must be an even number (it cannot have a bit in bit0).
    
  
	HEX: 
	see Decimal to Binary to HEX
    
  
	Hex,  Binary  
	
	and Decimal numbersAny mixture of binary, Hex and decimal numbers can be 
	shown in a program.
 Binary numbers are presented as: 0b'00000000'  or b'00000000'  or B'00000000' 
	or 00000000b or b'0100' 
	to indicate the lowest 4 bits.
 Hex numbers are shown as:   0x2    or 0x0F (= 
	fifteen)  or 0x3C or h'  or $  ($F or $f or $3A or $0ff )
 or <digits>h (must begin with 0 ....9) examples: 80h 3Ah  0FFh or 
	0ffh (do not put ffh or FFh as the compiler requires 0 to 9 at beginning)
 Decimal numbers are shown as:  d'250'  
	or decimal numbers without a prefix. They can 
	also be written with a decimal point. examples:   .250    
	.80   .100
    HigherTo find out if a number is higher than a know value, 
              a comparison is made. See Comparison.
    IncrementTo increment a file, use the instruction: 
              INCF 2A,1. This puts the new value back into the file.
 Using INCF 
			2A,0 puts the new value also into W!
 To add two to a file, it can be incremented twice:
 INCF 2A,1
 INCF 2A,1
 To double the value of a file, the contents is shifted left:
 RLF 2A,1
 A file can be incremented until it "rolls over to zero."  
              Normally a file is decremented to zero and a skip occurs when it 
              is zero. But the same effect can be produced by incrementing a file:
 INCFSZ, 2A,1
 To increment W, use ADDLW, thus:  ADDLW  01   or  
			ADDLW 3Bh
 
    IOR - inclusive OR  
				
    			Turn ON 2 or more outputs
				with 
            
 
            	IOR 
             
            	
	
 The PIC12F629 does not like:
 
 bsf   GPIO, 0
 bsf   GPIO, 2
 
 gpio,2 will not turn ON.
 
 To turn ON, say gpio,0  gpio,1 and gpio,2 at the same time, use the 
	following:
 You do not need to know the state of the other outputs, and they will not 
	be altered.
 
 movlw    b'00000111'
 iorwf       gpio,1
 
 the result:  gpio,0  gpio,1 and gpio,2   will be "set" 
	(go to "1" - high) and gpio,3 gpio,4 gpio,5 will not be affected.
 
 The same can be done to turn OFF 3 outputs:
 
 movlw    b'00000000'
 iorwf       gpio,1
 
 result:  gpio,0  gpio,1 and gpio,2   will be "clear" (go 
	to "0" - low) and gpio,3 gpio,4 gpio,5 will not be affected.   
	This is NOT the same as: clrf    gpio
    Indirect 
            Addressing
 A number of files can be addressed by a sub-routine and the information 
            can be moved into each file or read from each file. The files must 
            be a group.
 Suppose we have 8 files and need to read the contents and output it 
            to a display.
 The files are:  21h, 22h, 23h, 24h, 25h, 26h, 27h, and 28h.
 There are two special files that allow a sub-routine to be created 
            to look at the 8 files and read the contents.
 They are: INDF and FSR
 The INDF file is not a real file. It is like a Robot Arm. It reaches 
            down the list of files and picks up the contents or delivers the contents 
            of a file to the programmer. The file it reaches is determined by 
            the value in FSR.
 FSR is loaded with the address of the file you wish to read or write.
 This arrangement has an advantage. By loading FSR with a value, you 
            can reach a file and by incrementing FSR, you can reach the next file 
            etc.
 If you load a value into INDF, you will actually load the value into 
            the file pointed to by FSR.
 If you read INDF, you will actually read the contents of the file 
            pointed to by FSR.
 You can consecutively read 8, 10 or 20 files or clear 20 files or 
            load into 20 or more files with a simple looping sub-routine. It's 
            a very powerful feature.
 The following instructions put a value of 8Fh into file 21h.
 
               
               
                |  
                    
                       
                        | 
 
 
 | MOVLW 
                          21h MOVWF 04
 MOVLW 8Fh
 MOVWF 00
 | ;Load 
                          W with start of 8 files ;Load 21h into FSR
 ;Put 8F into W
 ;Put 8Fh into file 21h
 |  |  The animation below shows how the information passes 
              to the files: 
              Using INDF 
              and FSR
   The following instructions put a value of 8Fh 
              into files 21h, 22h, 23h, 24h, 25h, 26h, 27h and 28h.  
             
               
                |  
                    
                       
                        | 
 
 
 
 Loop1
 | MOVLW 
                          08 MOVWF 20h
 MOVLW 21h
 MOVWF 04
 MOVLW 8Fh
 MOVWF 00
 INCF 04
 DECFSZ 20h
 GOTO Loop1
 RETURN
 | ;8 
                          loops of the program ;File 20h is the decrementing file
 ;Load W with start of 8 files
 ;Load 21h into FSR
 ;Put 8F into W
 ;Put 8Fh into file 21h
 ;Increment FSR to make INDF go to next file
 |  |  The following instructions read files 21h, 22h, 23h, 
              24h, 25h, 26h, 27h and 28h and outputs to GPIO (file 05).  
			Output Port 05 has only 5 lines: GP0, GP1, GP2, GP4 and GP5. GP3 is 
			missing and this makes it difficult to display values from a file. 
               
                |  
                    
                       
                        | 
 
 
 Loop1
 
 | MOVLW 
                          08 MOVWF 20h
 MOVLW 21h
 MOVWF 04
 MOVF 00,0
 MOVWF GPIO
 CALL Delay
 INCF 04
 DECFSZ 20h
 GOTO Loop1
 RETURN
 | ;8 
                          loops of the program ;File 20h is the decrementing file
 ;Load W with start of 8 files
 ;Load 21h into FSR
 ;Copy file 21h (or next file) into W
 ;Move W to output Port GPIO
 ;Show value LEDs etc
 ;Increment FSR to make INDF go to next file
 |  |     INDF 
               
            See Indirect Addressing 
            and FSRThis is a special file called INDirect File.
 INDF has the address 00.
 INDF is not actually a file but a robot arm. It grabs the contents 
            (or delivers contents) to a file pointed to by FSR.
 This is used in an operation called INDIRECT ADDRESSING.
     Input 
            The six bits of the in/out port GPIO can be made input or output by 
			the value of the bits in a file called TRISIO. GP3 can only be 
			INPUT.
 To make a line INPUT, the corresponding TRISIO bit must be "1."
 To make a line OUTPUT, the corresponding TRISIO bit must be "0."
 To make a line INPUT (or OUTPUT), the instructions must be placed 
            inside  BSF 03,5 and BCF 03,5.
 For example, to make the lowest line of GPIO, an INPUT, the following 
            instructions are needed:
 
               
                |  
                    
                       
                        | 
 
 
 | BSF 03,5 MOVLW 01
 MOVWF GPIO
 BCF 03,5
 | ;Go to Bank 1 ;Load W with 0000 0001
 ;Make GP0 input
 ;Go to Bank 0 - the program memory area.
 |  |  The other individual lines are:movlw 02         ;Load W 
              with 0000 0010
 movwf GPIO    ;Make 
              GP1 input
 
 movlw 04         ;Load W with 0000 0100
 movwf GPIO    ;Make 
              GP2 input
 
 movlw 08         ;Load W with 0000 1000
 movwf GPIO    ;Make 
              GP3 input
 
 movlw 10h       ;Load W with 0001 0000
 movwf GPIO    ;Make 
              GP4 input
 
 movlw 20h       ;Load W with 0010 0000
 movwf GPIO    ;Make 
              GP5 input
 
 To make more than one line (with a single instruction) an input, 
              the hex values are added.
 
 movlw 0F        ;Load W with 0000 1111
 movwf GPIO    ;Make
			GP0, 
			GP1, 
			GP2, 
			GP3 input
 
 movlw 12h       ;Load W with 0001  0010
 movwf GPIO    ;Make
			GP1, 
			GP4 input
 
 movlw 33h       ;Load W with 0011  0011
 movwf GPIO    
            ;Make 
			GP0, 
			GP1, 
			GP4, 
			GP5 input
 
 Port direction can be changed at any time during the running of a 
            program. You must make sure that any input or output devices 
            on the line will not upset the running of the program.
 In this case it is best to SET or CLEAR a BIT. This involves setting 
            or clearing an individual bit. This prevents touching any other lines.
 Eg: To make the lowest line of port B an input:
 
               
                |  
                    
                       
                        | 
 
 | bsf 03,5 bsf   
                           GPIO,0
 bsf   
                           03,5
 | ;Go to Bank 1 ;Make GP0 input
 ;Go to Bank 0 - the program memory area.
 |  |  Carry out instructions using the input line, then make the line an 
            output:
 
               
               
                |  
                    
                       
                        | 
 
 | bsf   
                           03,5 bsf   
                           GPIO,0
 bsf   
                           03,5
 | ;Go to Bank 1 ;Make GP0 output
 ;Go to Bank 0 - the program memory area.
 |  |      Int   
			IntegerThe abbreviation:
 int   n_bytes    or   int  
			nEmpty etc  means integer.
 An integer is a whole number, such as:   2,  346,   
			-458,  but 1.5  is not an integer.
 You will also find INT used in a program to refer to INTerrupt, or 
			INTCON.
     
 
             
	InterruptThis program 
			Loops until input GPIO,0 changes state. The micro then goes to address 4, 
			then to Interrupt sub-routine where it changes the state of a LED 
			connected to GPIO,0. It then clears the GPIF flag and returns to 
			Main where it Loops.
 
 
               
               
                |  
                    
                       
                        | 
 
 
 
 
 Interrupt
 
 
 
 
 
 
 Main
 
 
 
 
 
 
 Loop
 
 
 | Org 0x00 Goto Main
 
 Org 0x04
 Goto   Interrupt
 
 btfss   Interrupt,gpif
 retfie
 movlw b'00000001'
 xorwf gpio,0
 bcf    intcon,gpif
 retfie
 
 bsf   status,rp0
 movlw b'00000001'
 movwf  trisio
 bcf   status,rp0
 movlw b'00001000'
 movwf   intcon
 bsf  intcon,gie
 nop
 goto Loop
 | ;Go to Bank 1 ;Make GP0 output
 ;Go to Bank 0 - the program memory area.
 
 
 
 ;test if gpio,0 changed state
 ;return to Main
 
 ;blink LED
 ;clear gpif flag
 ;return to Main
 
 
 ;make gpio,0 input
 
 
 ;enable port-change interrupt
 
 ;enable all interrupts - bit7
 ;loops HERE until interrupt occurs
 |  |      Jump  
             
            There is no "jump" instruction, however the "jump command" is 
	included in instructions such as
 decfsz,  btfsc, with the actual instruction meaning to skip or "jump 
	over" the next instruction if the file is not zero, or the bit is not clear.
 The closest instruction is:  "goto"
 
 Normally the micro advances down the program, one instruction at a time and 
	reads each instruction as it comes to it. If you want to jump down a 
	program, you can add a number (literal) to the Program Counter and the micro 
	will carry out the command.
 The instruction is:
 addwf  pcl,1
 Suppose you need to go to one of 5 different sub-routines. This is done by 
	placing a value in "w:"
 movlw  01,   or movlw 02,   movlw  03,   
	movlw 04,   movlw  05
 then the instruction: addwf  pcl,1
 
 To prevent a jump beyond the 5 "goto's, the instruction:    
	andlw 05h   is added here.
 
 The next instructions will be:
 
 nop                  
	;this instruction is equal to:  "movlw  00"
 goto   sub-1
 goto   sub-2
 goto   sub-3
 goto   sub-4
 goto   sub-5
     Label This is the name given 
			to each sub-routine. It is placed in the first column of your 
			program (called an assembly program).
 Some names cannot be used as they are reserved by the assembler. Keep 
			the length to less than 8 letters. Do not use "-" or "/"  Use 
			"_" to separate.
 Here are some examples:
 Alarm   Alarm_1   Beep   Button    
			Count   Dec   Delay   Display   
			Fast   Find   Flow   Halt   
			HeeHaw   Inc   Look   Loop   
			Main   Send   Show   Siren   
			Sound    Sw   Switch   Table   
			Table2  Table_3   Test  Try   Try_2    
			Toggle   Tone  Unit
     LayoutYour assembly 
			program must be laid out in columns so the assembler can detect the 
			instructions.
 
 
	
		| Label Field must start a-zor _2set
 | Mnemonic Field (the instructions)
 movlw
 movwf
 | Operand field 
 33h (hexidecimal)
 or   0.26(digital 26)
 | Comment Field ; starts with semicolon
 (the assembler ignores all comments)
 |  
		|  |  |  |  
		| _3times | movlw movwf
 | .45                                 
		;forty-five delay1                            
		;this is the delay register
 |   
	
 
	  Less than- see Comparison     Load 
              a file This operation cannot be done directly. A number (a value) is called 
              a LITERAL. It is loaded into W then the value in W is moved to a 
              file. The two instructions are:
 
               
               
                |  
                    
                       
                        | 
 | MOVLW 
                          0FFh MOVWF 2A
 | ;Load 
                          a value (called a Literal) (00 to 0FFh) into W ;Move the value in W to a file
 |  |      Look 
              at an Input 
            There is no instruction called "look."  If a switch 
            or button is connected to an input line such as the lowest line on 
            GPIO, the instruction is:
 
               
              This assumes the switch is connected to the positive rail and the 
            input goes HIGH when the button is pressed. 
                |  
                    
                       
                        | 
 | BTFSS 
						GPIO,0 GOTO No
 GOTO Yes
 | ;Is 
                          button pressed? ;No
 ;Yes
 |  |  This instruction also works for a signal on line GPIO,1. You must make 
            sure line GPIO,1 is an INPUT via the SetUp routine.
 The two instructions after BTFSS 
	GPIO,1 
            can be "GOTO Yes", "GOTO No"   by changing 
            the first instruction. The decision will depend on the number of instructions 
            for the "Yes" or "No" answer, as the instruction 
            placed directly after BTFSS GPIO,1 must be a GOTO.
 
               
                |  
                    
                       
                        | 
 | BTFSC  
            GPIO,1 GOTO Yes
 GOTO No
 | ;Is 
                          button pressed? ;Yes
 ;No
 |  |   
 LoopThe action of looping is carried out 
              for a number of reasons. The micro does not have a Halt or Stop 
              feature and must carry out instructions at all times. A loop will 
              hold the micro in one place.
 To get out, a set of instructions such as "look" is needed 
              inside the loop. These instructions see if a button has been pressed 
              etc. Alternatively, if the watchdog timer is SET, the micro will 
              come out of the loop and go to location 04. The instructions to 
              create a loop are as follows:
  To create a "loop" instruction:
 Loop        
			goto  $            
			This will cause the microcontroller to keep looping the same 
			instruction.
    Lower 
             To find out if a number is lower than a 
            know value, a comparison is made. See Comparison.
  
            
            
            
              MacroibbleA Macro is similar to a sub-routine. You can call it from anywhere 
            in a program. The aim of a macro is to 
            save lines of code.
 
 Some assemblers have built-in macros and recognise 
            abbreviations such as the following:
 Do not use these instructions unless you know EXACTLY what you are 
            doing.
 fr = file register
 For instance, we will explain the following instruction in the table 
            below:
 Branch on No Zero to 
            addr     =   
 
  
  
  
 
 
 
      
   
   
  
      
   
   
     
  
      
   
  
     
 
      
   
   
     
            
  
              btfss   3, 2    
            goto addr.   (file 3, bit 2 is the zero flag)
 Test the zero flag. Skip if it is set. In other words skip if the 
            zero flag is set, but BRANCH if it is not zero!
 The normal instructions are as follows:
 btfss  3,2
 goto   tune1
 next instruction
 
 alternately, you can use:
 bnz  tune1
 next instruction
 
 
               
                |  
                    
                        
                         
                        | Mnemonic addcf fr, d
 subcf fr, d
 negf fr, d
 b   addr
 bz   addr
 bnz   addr
 bc   addr
 bnc   addr
 skpc
 skpnc
 skpz
 skpnz
 clrz
 setz
 clrc
 setc
 tstf   fr
 decbnz fr,addr
 | Description Add carry to fr
 Subtract carry from fr
 Negate file register fr
 Branch to addr
 Branch on Zero to addr
 Branch on No Zero to addr
 Branch on Carry to addr
 Branch on No Carry to addr
 Skip on Carry
 Skip on No Carry
 Skip on Zero
 Skip on No Zero
 Clear Zero flag
 Set Zero flag
 Clear Carry flag
 Set Carry flag
 Test file register fr
 Decrement fr, if zero branch to addr
 | Function btfsc   3, 0   incf  f,d
 btfsc   3, 0   decf  fr,d
 comf   fr, 1   incf  fr,d
 goto    adddr
 btfsc   3, 2    goto addr
 btfss   3, 2    goto addr
 btfsc   3, 0    goto addr
 btfss   3, 0    goto addr
 btfss   3, 0
 btfsc   3, 0
 btfss   3, 2
 btfsc   3, 2
 bcf    3, 2
 bsf    3, 2
 bcf    3, 0
 bsf    3, 0
 movf  fr, f
 decfsz  fr  goto addr
 |  |  A macro can be created to move a number (a literal) into a file, using a 
	single instruction. This normally requires two instructions:
 movlw  64h     ;put 64h into W
 movwf  2Ch    ;move 64h to file 2C
 
 The single instruction we will create is:
 
 movlf  64h,2Ch  ;this instruction will put 64h into file 
	2C. (a macro must be included in the program)
 To create a macro for the 
	instruction "movlf"    the following is placed at the top of 
	your program:
 movlf   macro  literal,file    ;literal -> 
	file
 movlw  literal
 movwf  file
 endm
 
 When you write the instruction:  movlf  4Ah,2Fh     
	;4A will be placed into file 2F.
 
     MainThe Main routine is constantly looped 
              and generally consists of sub-routines that are CALLed.
 
               
                |  
                    
                         
                         
                        | Main 
 
 | CALL 
                          Switch CALL Display
 CALL Beep
 GOTO Main
 | 
 
 ;Loop Main
 |  |  
    Mask 
              - see also AND for a "2-instruction" codeIf you want to remove a number of bits from a file, the operation 
              is called MASKING.
 You can remove the high or low nibble (a nibble is a set of 4 bits) 
              or any other bits. Any number from 0 - 7 can be obtained by masking 
              (removing) bits 3,4,5,6,7, and leaving only bits 0, 1 and 2.
 To mask (remove) the upper nibble, the number is ANDed with 0F. 
              To mask the lower nibble, the number is ANDed with F0.  (this 
              is written: 0F0h in the program)
  
              
                 
                  | number:W:
 answer:
 | 1001 0111 1111 0000
 1001 0000
 |  
               
                |  
                    
                       
                        | 
 
 | MOVLW 
                          97h MOVWF 2A
 MOVLW 0F0h
 ANDWF 2A,1
 | ;Put 
                          97h into W ;Move 97h into file 2A
 ;put the "masking value" into W
 ;AND 97h with file 2A. The result will be in file 2A.
 |  |      More  than- see Comparison    Move 
              a file to W 
             The contents of a file can be moved to W with the following instruction:
 MOVF 2A,0  The contents are actually COPIED. The original 
            file still holds the contents.
    Move 
              a file to another file 
             The contents of a file can be moved to another file via the following 
            instructions.
 It is firstly copied to W then W is copied to the new file:
 
               
               
                |  
                    
                       
                        | 
 | MOVF 
						2A,0 MOVWF 
						2B
 | ;The 
                          contents of file 2A is copied to W ;W is copied to file 2B
 |  |      Multiply 
              Simple multiplication such as multiply by 2 can be performed by 
              the RLF instruction. Successive RLF's will multiply by 4, 8, sixteen 
              etc. You need to be careful as this is called a "blind" 
              operation.
 A number such as 80h (128) will not be doubled as 1000 0000 will 
              be moved to the left and the top bit will be passed to the Carry. 
              Only numbers up to 7F (127) can be doubled.
 To multiply by 2:
 
               
                |  
                    
                       
                        |  | RLF 
						2A,1 | ;The 
                          contents of file 2A is doubled |  |   
             To multiply any two numbers together 
            requires a program. Since the PIC12F629 does not have any multiply 
            function, it is carried out by successive ADDITIONS. A number from 
            01 to 255 can be multiplied by 01 to 255. = 
            0000 0000 0000 0000To multiply  75(4Bh) by 
            122(7A), 122 is added to 
            a file 75 times. It needs two files to hold the answer.
 
              The result is a 16 bit binary number of the form: file 2C, file 2B 
                |  
                    
                       
                        | 
 
 
 
 M1
 
 
 
 M2
 
 | CLRF 
						2B CLRF 2C
 MOVLW 7Ah
 MOVWF 2A,1 MOVLW 4B
 ADDWF 2B,1 BTFSS 03,0
 GOTO M2
 INCF 2C,1
 DECFSZ 2A,1
 GOTO M1
 RETURN
 | ;Clear 
                          the receiving file ;Clear the receiving file
 ;122
 ;file 1A holds 122
 ;75
 ;ADD 75 to file 2B
 ;Test Carry bit in status
 ;file. CLEAR = no carry
 ; SET = carry
 
 |  |  To multiply two numbers and obtain a decimal result requires a different 
            program.
    Nested 
              DelaySee Delay
    N Nibble is 4 bits - each byte has two nibbles - called the Low-Nibble 
			and High-Nibble.  Here is a simple routine that takes the lower 
			nibble and puts it in another file called LowNibble and the high 
			nibble into a file called HighNibble:
 
 
                 
                  |  
                      
                         
                          | 
 
 
 
 | movf    Byte,w andlw  0x0F
 movwf LowNibble
 swapf Byte,w
 andlw 
							0x0F
 movwf HighNibble
 | ;move the byte into w ;anding w with 0Fh will make the top 4 bits = 0
 ;move w to a new file called LowNibble. 1st part 
							finished.
 ;swap the high nibble with the lower nibble and put 
							in w
 ;anding 
							w with  
                            0Fh will make the top 4 bits = 0
 ;move 
							w to a new file called HighNibble. 2nd part 
							finished.
 ;The lower 4 bits will be in the 4 lower places of  
                             LowNibble ;and the 4 upper bits of Byte will 
							be in the 4 lower places of
 ;HighNibble
 |  |     
            OPTION  - Option RegisterWriting to OPTION Register:
 
 movlw     b'00000000'
 option
 
    
            Origin  -  ORGThis is a pseudo instruction (also called a directive) that tells the assembler where to place 
            the next instruction.   ORG must have a value. For
            ORG 
            000, the real instruction will be placed at memory location 
            000.
 For ORG 2Ch, the first instruction in Main will be placed at address 
            location 2Ch as shown below:
 
 
                 
                  |  
                      
                         
                          | SetUp
 
 
 
 
 
 Main
 
 
 
 | ORG 000 MOVLW 08
 MOVWF
                          TRISIO
 OPTION 0DFh
 - - - - - - - - - -
 - - - - - - - - - -
 ORG 2Ch
 CALL 
                          Switch
 CALL Display
 CALL Beep
 GOTO Main
 | ;Start of program in memory ;
 
 ;
 
 
 ;Next following instruction will be placed at location 
							2Ch
 |  |     
 
 
            Oscillator Calibration value
                 
                  |  
                      
                         
                          | 
 
 
 
 
 
 | call        0x3ff movwf   	OSCCAL
 
 
 
 org       0x3ff
 retlw  	0x20
 
 END
 | ;get the calibration value ;calibrate oscillator
 
 
 
 ;OSCCAL calibration value is located at the last line of
 ;    program memory and has the 
							instruction to return with
 ;   20h in W
 |  |     Output 
              a Table Value   
              see also Table 
             
            
  
              
                 
                  |  
                      
                         
                          | Table 
 
 
 
 
 
 
 
 
 
 | ADDWF 
                            02h,1 RETLW 3Fh
 RETLW 06h
 RETLW 5Bh
 RETLW 4Fh
 RETLW 66h
 RETLW 6Dh
 RETLW 7Dh
 RETLW 07h
 RETLW 7Fh
 RETLW 6Fh
 | ;Add W to the Program Counter to create a jump. 
 
 
 
 
 
 
 
 
 
 |  |     Output 
              a     Value 
            The output port for a PIC12F629 is actually 
			a 
            FILE or REGISTER!  It is file 05.
 The 6 lines of the port are called: GP0, 
             GP1, 
             GP2, 
             GP3, 
             GP4 and
             GP5.
 Each line can deliver approx 25mA. The maximum total current for the 
            chip is about 150mA.
 An output line can be HIGH or LOW. Each output line corresponds to 
            a bit in the file associated with the port. When the bit is SET, the 
            line is HIGH. When the bit is CLEAR, the line is LOW.
 Before you can make a line HIGH or LOW, the file must be "configured." 
            This means each bit must be made an OUTPUT. This is done via the TRISIO 
			file. This file is located at 85h - in Band 1.
 Any line can be made either an input or an output at any time during 
            the running of a program and to make a line INPUT, the corresponding 
            bit in the TRISIO file is made "1." To make a line OUTPUT, 
            the corresponding bit in the TRISIO file is made"0."
 There are two ways to get to the TRISIO file. One is directly 
            via the instruction:
 
               
                |  
                    
                       
                        | 
 | MOVLW 
						2Bh TRISIO
 | ;Load 
                          xx10 1011 into W ;Make GP2 and GP4 output.
 |  |  The other is via the two 
              instructions: BSF 03,5 and BCF 03,5. These instructions allow you 
              to go to  bank1 where the TRISIO file is located. It is 
			in 
              Bank1 and the TRISIO file is called 05.  
             
               
                |  
                    
                       
                        | 
 
 | BSF 03,5 MOVLW 3Fh
 MOVWF 05
 BCF 03,5
 | ;Go to Bank 1 ;Load W with 0011 1111
 ;Make all GPIO input
 ;Go to Bank 0 - the program memory area.
 |  |   Any lines that are made 
              output can be made HIGH or LOW. 
             
               
                |  
                    
                       
                        | 
 | MOVLW 
                          16h MOVWF GPIO
 | ;Load 
                          0001 0110 into W ;Make GP1 and GP2 and GP4 HIGH.
 |  |  Output 8 bits of a file via bit0 of GPIO:. 
             
               
                |  
                    
                       
                        | 
 Loop
 | movlw    8 movwf    count
 btfss      temp,0
 bcf         
						GPIO,0
 btfsc      temp,0
 bsf         
						GPIO,0
 rrf          
						temp,f
 call        delay
 decfsz   count,f
 goto      Loop
 | ; ; create file to hold the 8 loops
 ;
 ;temp will be 0 so clear bit0 of gpio and send
 ;temp will be set, so set bit0 of gpio and send
 ;
 ;shift temp file so bit1 becomes bit0
 ;
 ;
 ;perform 8 loops
 |  |      
			Output Pin      
			
			Input/Output Pin  Theses are the same terms 
			for a microcontroller pin that accesses the outside world.  
			Each pin is capable of delivering a current of 20mA when HIGH (set) 
			or sinking 20mA when LOW (clear).
 You must not prevent the pin going to its full 4.8v HIGH or 0.2v LOW 
			as the MOSFET transistor connected to the pin is microscopically 
			small.
 The PIC12F629 has 6 pins with 5 pins configurable as input/output 
			and pin4 configurable as INPUT ONLY.
 Pin 7 is General Purpose In/Out  (gpio,0)
 Pin 6 is General Purpose 
			In/Out  (gpio,1)
 Pin 5 is General Purpose In/Out  
			(gpio,2)
 Pin 4 is input ONLY                    
			(gpio,3)
 Pin 3 is General Purpose In/Out  (gpio,4)
 Pin 2 is General Purpose In/Out  (gpio,5)
 File 6 in the file register holds the data for the 6 pins.
 The 6 lower bits of the file hold the data.
 When a bit is 0, the output is LOW  when a bit is 1, the output 
			is HIGH.
 Another register in the file holds the information that determines 
			if the pin is an input or output.
 This register is called the trisio register. When a bit is 0, the 
			pin will be an output. When a bit is 1, the pin will be input.
 Bit3 of the trisio register can only be 1.
 Any pin can be changed from input to output at any time during the 
			running a of a program.
 The pins are initially set by the following lines of code, or a 
			combination of "0" and "1:"
  bsf              
	status,rp0      ;bank1movlw         b'00000000'     
	;this will make all pins output  (gpio,3 
	will be input ONLY)
 movwf         trisio
 bcf              
	status,rp0      ;bank0
  or
 bsf              
	status,rp0      ;bank1
 movlw         b'11111111'     
	;this will make all pins input
 movwf         trisio
 bcf              
	status,rp0      ;bank0
     
              To Top 
              
 |