You are on page 1of 18

=========================================================== TALKING ELECTRONICS Interactive website http://www.talkingelectronics.

com =========================================================== =========================================================== ROUTINES for "Copy and Pasting for PIC12F629" by Colin Mitchell talking@tpg.com.au 9-9-2008

=========================================================== The following are the ROUTINES for "Copy and Pasting" into your program - commonly called your ".asm file" They are in alphabetical order, so everything is easy to find - and easy to use. Simply copy and paste them into your program: Hold the left mouse button down as you pass the mouse over the instructions. Then click the right mouse button and a box will appear. Select "Copy." Go to your program - in an another NotePad or WordPad - and "Paste" the instructions. All the instructions should "line-up" into three columns. If you use a set of instructions as a complete routine, it is called a sub-routine. Alternatively they can be added to another routine, such as "Main." Make sure you understand what to do before using this library. All these instructions are fully explained in "Library of Routines." "Library of Routines" can be found in our PIC Theory section when you subscribe to the website or buy the CD. The instructions for your program are arranged in three columns. The first column contains the "labels." They identify the first instruction for a sub-routine. The second column contains the "instruction" - called the mnemonic. The "half-computer" "half-English" instruction that both computer and micro understands. The third column contains the "comments." It must have a ";" before each line so that they are not assembled - so they don't appear in the final .hex file! Note: All the instructions in this library will "run" when placed in a program. But sometimes, something goes wrong. If an instruction has hidden "formatting," the assembler may create the wrong code. To see if an instruction has hidden formatting, simply hold down the left button on the mouse and drag across the instruction. The background will go black and will not extend past the actual word.

Try the following:

DECFSZ del2A,1 DECFSZ del2A,1

This instruction has hidden formatting:

Simply remove any hidden formatting by holding the mouse on the unwanted spaces and use the back-space arrow to remove the spaces (or re-type the complete instruction) and the problem will be fixed. (It does not matter if "comments" have extra spaces. "Comments" are not assembled.) Start by usign the template "blank12F628.asm" Delete the instructions you do not want and add the instructions you want. Make sure the labels are correct. ----------------------------------------------------ADD A VALUE TO A FILE ----------------------------------------------------A file contains a known value. To add a value to the file use: Add MOVLW 0xCC ADDWF 2Eh ;Put CC into W ;CC will be added to the contents of file "2E."

If the file overflows, the Carry/Borrow bit in the Status file (file 03,0) will be SET. Add MOVLW 0CCh ADDWF 2Eh BTFSS 03,0 GOTO AAA GOTO BBB ;Put CCh into W ;CC will be added to the contents of file "2E" ;Test the carry bit in the Status file ;Micro will go to AAA if no overflow ;Micro will go to BBB if overflow occurs

----------------------------------------------------BEEP - TONE - this is a constant tone ----------------------------------------------------To To To To To To To use use use use use use use RB0 RB1 RB2 RB3 RB4 RB5 RB6 as as as as as as as the the the the the the the output: output: output: output: output: output: output: MOVLW MOVLW MOVLW MOVLW MOVLW MOVLW MOVLW 01h (see line 8 in subroutine below) 02h 04h 08h 10h 20h 40h ;The duration of the tone or "beep" ;The length of HIGH and LOW - frequency of tone

Tone Tone1 Tone2

MOVLW 40h MOVWF 2B MOVLW A0h MOVLW 2A NOP DECFSZ 2A,1 GOTO Tone2 MOVLW 01h XORWF GPIO,1 DECFSZ 2B,1 GOTO Tone1 RETLW 00

;Put 01h into W ;XOR 01 with GPIO. Toggle GP0

----------------------------------------------------BEEP - BEEP - BEEP - repeat ----------------------------------------------------This beep routine is suitable for debugging a program. The instruction GOTO Beep1 is inserted in a program to see how far the micro has advanced through the instructions. Beep1 produces an endless beep . . . beep . . . beep . . . In the sub-routine you are trying to debug, put the instruction: GOTO Beep1 At the beginning of memory, after Tables, put the following: Beep1 CALL Beep CALL BeepDel GOTO Beep1 MOVLW 40h MOVWF 2B MOVLW A0h MOVLW 2A NOP DECFSZ 2A,1 GOTO Beep2 MOVLW 02h XORWF GPIO,1 DECFSZ 2B,1 GOTO Beep1 RETLW 00 ;The duration of the beep ;The length of HIGH and LOW - frequency of beep

Beep Beep1 Beep2

;Put 02 into W ;XOR 02 with GPIO. Toggle GP1

BeepDel DECFSZ 2A,1 GOTO BeepDel DECFSZ 2B,1 GOTO BeepDel RETLW 00 ----------------------------------------------------BUTTON - SWITCH - PUSH BUTTON ----------------------------------------------------In this routine a push button is connected to GP0. When it is pushed, a HIGH is delivered to GP0. Or, if you want to make GP1 a button-line, the second instruction in SetUp is: MOVLW 02. To make GP3 an input, the instruction is: MOVLW 08 The button-file must be cleared in SetUp. There are two flags. Bit0 in file 2F is the Debounce Flag and Bit1 in file 2F is the Button Pressed flag. The microprocessor executes Main and CALLs Sw. If Sw detects a key-press, two flags are SET. The first is the Button Pressed flag and the second is the Debounce flag. The micro returns to Main and tests the Button Pressed flag to see if it is SET. If is is SET, the micro goes to a sub-routine such as CALL Increment, where a value can be incremented.

The Button Pressed flag is then cleared and the micro CALLs Sw. If the switch is still pressed, the micro will return. The program is looking for the button to be released. When the button is released, the Sw sub-routine is ready to detect another button-push. SetUp BSF 03,5 MOVLW 01 MOVWF TRISIO BCF 03,5 CLRF 2F GOTO Main BTFSS GPIO,0 GOTO Sw2 BTFSC 2F,0 RETURN DECFSZ 2A,1 GOTO Sw1A BTFSS GPIO,0 GOTO Sw1B BSF 2F,1 BSF 2F,0 RETURN BCF 2F,0 RETLW 00 CALL Sw BTFSC 2F,1 was pressed GOTO Main1 ;Button pressed your instruction;Button not pressed - carry out a sub-routine to your instruction; display values on a display etc. GOTO Main CALL Increment ;Increment the display. (you provide the routine) BCF 2F,1 ;Clear the button-press flag GOTO Main ;Test button-press flag to see if button ;Go to Bank 1 ;Put 01 into W to ;make GP0 input ;Go to Bank 0 ;Clear the button-press file ;Test the button. Button is "Active HIGH" ;Button not pressed ;Button pressed first time? Test deboun ;Button already pressed. Return to Main ;Create short delay ;Look again ;Is switch still pressed? ;It was only noise ;Button Pressed. Set button-pressed fla ;Set debounce flag ;Return to Main ;Clear debounce flag ;Return to Main

Sw1 ce flag Sw1A

g Sw1B Main

Main1

To connect two buttons to a micro. The second instruction in SetUp must be the addition of the hex values of the input lines E.g: GP1 and GP2 = 0000 0010 + 0000 0100 = 0000 0110 = 06h GP3 and GP5 = 0000 1000 + 0010 0000 = 0010 1000 = 28h SetUp BSF 03,5 MOVLW 03 MOVWF TRISIO BCF 03,5 CLRF 2F GOTO Main BTFSS GPIO,0 GOTO Sw2 ;Go to Bank 1 ;Put 03 into W to ; make GP0 and GP1 input ;Go to Bank 0 ;Clear the button-press file ;Test the first button. Button1 is "Active HIGH" ;Button not pressed

Sw1

BTFSC 2F,0 ce flag Sw1A RETURN DECFSZ 2A,1 GOTO Sw1A BTFSS GPIO,0 GOTO Sw1B BSF 2F,1 BSF 2F,0 RETURN BCF 2F,0 RETLW 00 BTFSS GPIO,0 GOTO Sw2 BTFSC 2F,2 ce flag Sw2A RETLW 00 DECFSZ 2A,1 GOTO Sw2A BTFSS GPIO,0 GOTO Sw2B BSF 2F,3 BSF 2F,2 RETLW 00 BCF 2F,2 RETLW 00

;Button pressed first time? Test deboun ;Button already pressed. Return to Main ;Create short delay ;Look again ;Is switch still pressed? ;It was only noise ;Button Pressed. Set button-pressed fla ;Set debounce flag ;Return to Main ;Clear debounce flag ;Return to Main ;Test the second button. Button2 is "Active HIGH ;Button not pressed ;Button pressed first time? Test deboun ;Button already pressed. Return to Main ;Create short delay ;Look again ;Is switch still pressed? ;It was only noise ;Button Pressed. Set button-pressed fla ;Set debounce flag ;Return to Main ;Clear debounce flag ;Return to Main

g Sw1B Sw2 "

g Sw2B Main

CALL Sw1 BTFSC 2F,1 ;Test button-press flag to see if button was pressed GOTO Main1 ;Button pressed your instruction;Button not pressed - carry out a sub-routine to your instruction; your instruction CALL Sw2 BTFSC 2F,3 was pressed display values on a display etc. ;Test button-press flag to see if button

Main1 Main2

GOTO Main2 ;Button pressed GOTO Main CALL Increment ;Increment the display. (you provide the routine) BCF 2F,1 ;Clear the button-press flag GOTO Main CALL Decrement ;Decrement the display. (you provide the routine) BCF 2F,3 ;Clear the button-press flag GOTO Main

----------------------------------------------------CALLS - a list of suitable Calls: ----------------------------------------------------Any names can be used. Keep the length to less than 8 letters. CALL Alarm CALL Beep

CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL

Button Count Dec Delay Display Find HeeHaw Inc Look Loop Main Send Show Siren Sound Sw Switch Table Test Toggle Tone

----------------------------------------------------CALL A TABLE ----------------------------------------------------Load a value into W and CALL Table1. The micro will look down the table. If the value you have loaded into W is 00, the first value in the table will be fetched. If the value is 01, the second value will be fetched. The micro adds the value in W to the Program Counter and this causes the micro to jump down the table. The PC naturally increments to the next instruction and that's why 3Fh is selected if the loaded value is 00.

Table1 ADDWF 02h,1 RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW Main - - - - MOVLW 00h CALL Table1 MOVWF GPIO - - - - - - - - -

;Add W to the Program Counter to create a jump. 3Fh ;0format= gfedcba 06h ;1 5Bh ;2 4Fh ;3 66h ;4 6Dh ;5 7Dh ;6 07h ;7 7Fh ;8 6Fh ;9

;load a value into W. 00h will pick up 3Fh from table ;The micro will return with 3Fh in W ;Move 3Fh to in/out Port GPIO (file 05)

-----------------------------------------------------

DECREMENT A FILE ----------------------------------------------------DECF 2A,1 ;Puts the new value into the file

----------------------------------------------------DECREMENT A FILE and SKIP WHEN IT IS ZERO ----------------------------------------------------DECFSZ 2A,1 Not zero zero! ;Puts the new value into the file ;Goes here if file is not zero ;Goes here if file is zero!

----------------------------------------------------DELAYS ----------------------------------------------------This delay creates FFh loops. Each loop takes 4uS Total time: 256 x 4 = 1024uS = say 1mS Del NOP DECFSZ 2A,1 GOTO Del RETLW 00 ;Decrement file 2A ;Loop until file 2A is zero

This delay creates 80h loops. Each loop takes 4uS Total time: 128 x 4 = 512uS Del DelA NOP DECFSZ 2A,1 GOTO DelA RETLW 00 ;Decrement file 2A ;Loop until file 2A is zero MOVLW 80h MOVWF 2A ;Put 80h into W ;Copy 80h into file 2A

NESTED DELAY This delay creates FFh loops (determined by file 2B). Each loop takes 4uS and there are 256 inner loops = 1024uS = 1mS This inner loop is executed 256 times via file 2B. Total time = 260mS Del NOP DECFSZ 2A,1 GOTO Del DECFSZ 2B,1 GOTO Del RETLW 00 ;Decrement file 2A ;Loop until file 2A is zero ;Decrement file 2B ;Loop until file 2B is zero

For a delay between 1mS and 260mS, you will need to pre-load file 2B: Del Del1 MOVLW 7Dh MOVWF 2B NOP DECFSZ 2A,1 GOTO Del1

;Decrement file 2A ;Loop until file 2A is zero

DECFSZ 2B,1 GOTO Del1 RETLW 00

;Decrement file 2B ;Loop until file 2B is zero

----------------------------------------------------EEPROM ROUTINES ----------------------------------------------------To load EEPROM: ORG 2100h DE 84h, 16h, 23h, 80h, 0CAh, 32h, 7Bh, 0A2h DE 34h, 53h, 25h, 02h, 0FFh, 20h, 03h, 04h

The sub-routine to read a value in the EEPROM is shown below. It reads EEPROM data at location specified in EEADR and returns the value in W EERead BSF Status, RP0 ;Go to Bank 1 BSF EECON1, RD ;Set the RD bit BCF Status, RP0 ;Back to bank 0 MOVF 08, W ;EE Data is file 08. Put into W RETLW 00 The sub-routine to write to EEPROM is shown below. Delay1 MOVLW 13h ;Create a 20mS delay MOVWF 2B ;20 loops Delay1a NOP DECFSZ 2A,1 ;4uS x 256 loops=approx 1mS GOTO Delay1a DECFSZ 2B,1 GOTO Delay1a RETLW 00 EEWrite MOVWF 08 ;W to EEData BSF 03,5 ;Switch to bank 1 BCF 0B,7 ;Disable interrupts BSF 08,2 ;Write Enable bit set MOVLW 55h ;Toggle EEPROM Control MOVWF 09 ; Register bits MOVLW 0AAh ;Toggle EEPROM Control MOVWF 09 ; Register bits BSF 08,1 ;Begin write sequence CALL Delay1 ;Call Delay1 BCF 08,2 ;Disable any further writes BSF 0B,7 ;Enable Interrupts BCF 03,5 ;Back to data bank 0 RETLW 00

----------------------------------------------------GOTO

----------------------------------------------------To make the micro go to another part of your program, use the GOTO instruction. Simply write the word "GOTO." Following GOTO, we place a "label" The label may be "Sw1" or "Count" or "Test." "Sw1" or "Count" or "Test." will appear in the first column of your program. The instruction "GOTO Test" will take the micro to the line containing the label: Test. The micro will not return to the instruction following GOTO Test, unless you have a label at the line such as "Togu1" and include an instruction GOTO Togu1 in your program. If you GOTO a sub-routine, the last instruction in the sub-routine cannot be RETURN as the micro does not have a return address in its memory - on the STACK. The only time a return address is placed on the stack is when a CALL is executed. GOTO GOTO GOTO GOTO GOTO GOTO GOTO GOTO GOTO Alarm Beep Button Count Dec Delay Display Find HeeHaw

At the end of the sub-routine will be a GOTO Main or GOTO another sub-routine. It does not have to be the last instruction in a sub-routine but it is the only exit instruction you can use. You cannot use "RETLW 00."

----------------------------------------------------HEX VALUES ----------------------------------------------------All the files in a micro have 8 bits. All the input/output ports have 8 lines. The lowest bit or line is shown as a "1" in the folling value: 0000 0001 The lowest bit is called Bit0 The lowest line is called GP0 The next line (in/out line) or "bit" is called GP1: 0000 0010 The The The The lowest line has a value of zero when not active and ONE when active. next has a vaue of TWO when active. next has a value of FOUR when active. next has a value of EIGHT when active.

The 4 in/out lines have the following 16 possibilities: 0000 0001 0010 0011

0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 They are given the following values: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 = = = = = = = = = = = = = = = = 0 1 2 3 4 5 6 7 8 9 A B C D E F

The four HIGH lines have the same format: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 = = = = = = = = = = = = = = = = 00 10 20 30 40 50 60 70 80 90 A0 B0 C0 D0 E0 F0

The four HIGH and LOW lines produce 256 combinations. Here are some: 0011 1011 1110 1011 0110 0101 0000 1001 0000 1110 = = = = = 35 B0 E9 B0 6E

0101 0000 = 50 1001 1011 = 9B 0110 1011 = 6B The value to make GP2 and GP5 input: 0010 0100 = 24 ----------------------------------------------------INCREMENT A FILE ----------------------------------------------------INCF 2A,1 ;Puts the new value into the file

----------------------------------------------------INPUT ----------------------------------------------------To make a line (or lines), an INPUT, it must be given the value "1." This can be done anywhere in the program but it is best to put the instruction in SetUp. This makes it easy to locate when trouble-shooting. The instructions between BSF and BCF in the routine below are operating on a register in bank 1 and this is the in/out control register for PortA. It is NOT PortA. It is the register that determines the IN or OUT condition of each line in PortA and is called the TRIS register. SetUp BSF 03,5 ;Go to Bank 1 MOVLW 0F ;Put 0000 1111 into W to MOVWF TRISIO ; make GP0, GP1, GP2, and GP3 input BCF 03,5 ;Go to Bank 0 GOTO Main

----------------------------------------------------INPUT A VALUE ----------------------------------------------------The input lines for a 12F629A are: GP0, GP1, GP2, GP3, GP4 and GP5. GP3 is input-only and this line is usually the chosen input. To input a value (A HIGH or LOW), make sure the line is an input. This is done in SetUp: SetUp BSF 03,5 ;Go to Bank 1 MOVLW 01 ;Put 0000 0001 into W to MOVWF TRISIO ; make GP0 input BCF 03,5 ;Go to Bank 0 GOTO Main

In Main, test the input line: Main BTFSS GPIO,0 GOTO - - - - - - - - - - -

----------------------------------------------------LAYOUT OF A PROGRAM ----------------------------------------------------Use the following when layingout your program: SetUp Table1 Table2 LabelA - put all labels in alphabetical order so they can be found quickly. LabelB LabelC LabelD Main ----------------------------------------------------LOOK for a Table-value ----------------------------------------------------See Table-value ----------------------------------------------------LOOP ----------------------------------------------------When writing a program, you can use a loop to create a STOP function. Loop NOP GOTO Loop Inside the loop you can put a tone routine so you can see if the micro has reached the location. Loop Tone MOVLW A0h MOVLW 2A NOP DECFSZ 2A,1 GOTO Tone MOVLW 01h XORWF GPIO,1 GOTO Loop ;The length of HIGH and LOW - frequency of tone

;Put 01 into W ;XOR 01 with GPIO. Toggle GP0

----------------------------------------------------MAIN ----------------------------------------------------Main is the last routine in your program. It is a loop routine and has a number of CALL and/or GOTO instructions: (If you GOTO

a sub-routine, the sub-routine must have a GOTO as the exit). Main - - - - - - - - - - - - CALL Delay - - - - - - - - - - - - GOTO Main

----------------------------------------------------OUTPUT ----------------------------------------------------To make a line (or lines), an OUTPUT, it must be given the value "0." This can be done anywhere in the program but it is best to put the instruction in SetUp. This makes it easy to locate when trouble-shooting. The instructions between BSF and BCF in the routine below are operating on a register in bank 1 and this is the in/out control register for PortB. It is NOT PortB. It is the register that determines the IN or OUT condition of each line in PortB and is called the TRIS register. SetUp BSF 03,5 ;Go to Bank 1 MOVLW 00 ;Put xx00 0000 into W to MOVWF TRISIO ; make all six GPIO lines output BCF 03,5 ;Go to Bank 0 GOTO Main

In Main, output a HIGH on GPIO (file 05): Main - - - - MOVLW MOVWF - - - - 0FFh GPIO - - ; ;Output a HIGH to the 6 lines of GPIO

----------------------------------------------------OVERFLOW ----------------------------------------------------If a value is added to a file, overflow may occur. To see if overflow occurs, test the Carry/Borrow bit in the Status file (file 03,0). It will be SET if overflow occurs. Add MOVLW 0CCh ADDWF 2Eh BTFSS 03,0 GOTO AAA GOTO BBB ;Put CCh into W ;CC will be added to the contents of file "2E." ;Test the carry bit in the Status file ;Micro will go to AAA if no overflow ;Micro will go to BBB if overflow occurs

----------------------------------------------------RETURN ----------------------------------------------------The PIC12F629 does not have a RETURN instruction. If you use RETURN in your program, the assembler will replace it with RETLW 00 when creating a .hex file

----------------------------------------------------SetUp ----------------------------------------------------This is the first sub-routine in your program. It sets up the in/out port in the microcontroller. GPIO has up to 6 lines and these can be inputs or outputs. (GP3 is input-only). The input lines for a 12F629 are: GP0, GP1, GP2, GP3, GP4 and GP5. GP3 is input-only and this line is usually the chosen input. The output lines are: GP0, GP1, GP2, GP4 and GP5. An input line can be changed to an output at any time during the running of a program. An output line can be changed to an input at any time during the running of a program. The input/output lines of a PIC12F629 are in file 05. To make GP0 an input, place the following instructions in SetUp: SetUp BSF 03,5 ;Go to Bank 1 MOVLW 01 ;Put 0000 0001 into W to MOVWF TRISIO ; make GP0 input BCF 03,5 ;Go to Bank 0 GOTO Main

To make GP0 an output, place the following instructions in SetUp: SetUp BSF 03,5 MOVLW 00 MOVWF TRISIO BCF 03,5 GOTO Main ;Go to Bank 1 ;Put 0000 0000 into W to ; make GP0 output ;Go to Bank 0

To make any GP line an input, place "1" in the corresponding position for the value loaded into W: SetUp BSF 03,5 ;Go to Bank 1 MOVLW 08 ;Put 0000 1000 into W to MOVWF TRISIO ; make GP3 input BCF 03,5 ;Go to Bank 0 GOTO Main

To create two input lines: SetUp BSF 03,5 ;Go to Bank 1 MOVLW 12 ;Put 0001 0010 into W to MOVWF TRISIO ; make GP1 and GP4 input BCF 03,5 ;Go to Bank 0 GOTO Main

----------------------------------------------------STATUS FILE ----------------------------------------------------The STATUS file is file 03. Bit0 is the Carry/Borrow Bit1 is the Digit Carry/Borrow Bit2 is the Zero bit Bit3 is the Power Down bit Bit4 is the Time-out bit

When bit5 is SET Bank1 is selected When bit5 is CLEAR Bank0 is selected ----------------------------------------------------STOP ----------------------------------------------------The micro does not have a STOP or HALT instruction. When writing a program, you can use a loop to create a STOP function. Loop NOP GOTO Loop Inside the loop you can put a tone routine so you can see if the micro has reached the location. Loop Tone MOVLW A0h MOVLW 2A NOP DECFSZ 2A,1 GOTO Tone MOVLW 01h XORWF GPIO,1 GOTO Loop ;The length of HIGH and LOW - frequency of tone

;Put 01 into W ;XOR 01 with GPIO. Toggle GP0

----------------------------------------------------TABLE - see also CALL A TABLE ----------------------------------------------------Load a value into W and CALL Table1. The micro will look down the table. If the value is 00, the first value in the table will be fetched. If the value is 01, the second value will be fetched. The micro adds the loaded-value into the Program Counter and this causes the micro to jump down the table. The PC naturally increments and that's why 3Fh is selected if the loaded value is 00. Any values with a letter as the first part of the hex value must begin with a "0" E.g: FFh = 0FFh CDh = 0CDh The end of a table can be identified by using 0FFh (providing FFh is not a required table value). In Main, look for 0FFh as a table value.

Table1 ADDWF RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW Main

02h,1 3Fh 06h 5Bh 4Fh 66h 6Dh 7Dh 07h 7Fh 6Fh 0FFh

;Add W to the Program Counter to create a jump. ;0format= gfedcba ;1 ;2 ;3 ;4 ;5 ;6 ;7 ;8 ;9

- - - - MOVLW 00h

;load a value into W. 00h will pick up 3Fh from table

CALL Table1 MOVWF GPIO - - - - - - - - -

;The micro will return with 3Fh in W ;Move 3Fh to output Port GPIO (file 05)

----------------------------------------------------Table-value ----------------------------------------------------To "Look for" or "fetch" a table value, you need 2 instructions. To fetch the first table value, load W with 00. To fetch the second table value, load W with 01 etc. The second instruction is CALL Table1 The micro will return with the table-value in W: Table1 ADDWF 02h,1 RETLW RETLW RETLW RETLW RETLW RETLW RETLW Main ;Add W to the Program Counter to create a jump. 3Fh ;0format= gfedcba 06h ;1 5Bh ;2 4Fh ;3 66h ;4 6Dh ;5 7Dh ;6

- - - - - - - - MOVLW 04 CALL Table1 - - - - - - - - -

The micro will return with 66h in W ----------------------------------------------------TEMPLATE ----------------------------------------------------Use this template as a start to writing your program. This is not a fully-functional program. It is a set of example-instructions. Delete the unwanted instructions. ;Expt1.asm ;Project:Turning on a ----;This program can be assembled for a PIC12F629 ORG 0 ;This is the start of memory for the program BSF 03,5 ;Go to Bank 1 MOVLW 01 ;Load W with 0000 0001 MOVWF TRISIO ;Make GP0 input BCF 03,5 ;Go to Bank 0 - the program memory area GOTO Main NOP DECFSZ 2A,1 GOTO Delay ;Create approx 250mS delay

SetUp

Delay

DECFSZ 2B,1 OTO Delay RETLW 00 Look CLRF 2C ;Count-down file - - - - CALL Delay ;250mS delay BSF 03,5 ;Go to Bank 1 MOVLW 04 ;Load W with 0000 0100 MOVWF GPIO ;Make GP2 input & GP0 output BCF 03,5 ;Go to Bank 0 - the program memory area. RETLW 00

Main

Main2

BTFSS GPIO,0 ;Test the input line on GPIO GOTO Main2 ;Button not pressed BSF GPIO,0 ;Button pressed. Turn on LED CALL Delay - - - - CALL Look BCF GPIO,0 ;Button not pressed. Turn off LED GOTO Main ;Loop Main END ;Tells assembler end of program

----------------------------------------------------TOGGLE ----------------------------------------------------Toggle changes the state of an output. You do not have to know the state of the output before the operation. The output changes from HIGH to LOW or LOW to HIGH. You do not know the output after the operation. This operation is not suitable if you want to leave an output low when not in use so that it is not left in a "driving" condition when not required. To To To To To To toggle toggle toggle toggle toggle toggle GP0 GP1 GP2 GP3 GP4 GP5 use: use: use: use: use: use: MOVLW MOVLW MOVLW MOVLW MOVLW MOVLW 01h 02h 04h 08h 10h 20h ;Put 02 into W ;XOR 02 with GPIO. Toggle GP1

Toggle MOVLW 02h XORWF GPIO,1

----------------------------------------------------TRIS ----------------------------------------------------"Tris" is the file (register) that determines if each bit in GPIO (Port 05) is input or output. See Library of Routines for more details.

You might also like