Sie sind auf Seite 1von 7

Learning Microchip Lesson 1-2 Last lesson introduced you to Microchips MPLAB software and our PicoFlow LT software

e to turn a LED on. Here you looked at installing the MPLAB software, writing simple OPCODE instructions, the Register Memory Map (with SFRs and GPRs), Numerical Values (in Hexadecimal, Decimal, and Binary), Compiling a HEX file, and programming the HEX file to your project using the PicoFlow LT software. Each lesson is designed to challenge your learning in some way. This lesson will introduce you to the GOTO instruction, Bit-Tests, and Delays. Here, you will also learn how to use a switch input and numerous ASM instructions.

Contents
Where to Start: ...................................................................................................................................... 2 Instruction Memory: ............................................................................................................................. 2 Where to GOTO: .................................................................................................................................. 3 Example ............................................................................................................................................. 3 Bit Test (Conditional GOTO) .............................................................................................................. 4 Example ............................................................................................................................................... 4 Delay (Conditional GOTO) .................................................................................................................. 5 Example ............................................................................................................................................... 5 Naming GPRs (cblock) ............................................................................................................................. 6 Lastly ....................................................................................................................................................... 7 Conclusion ............................................................................................................................................... 7 Test What Youve Learned ...................................................................................................................... 7

Philip Tallents 2011

Where to Start: By now you should have the following code in your *.asm file, compiled into a *.hex file, written to your device, and running your project. If not, please revisit Lesson 1-1. ASM CODE: #include <P16F505.INC> __CONFIG _IntRC_OSC & _MCLRE_OFF & _WDT_OFF MOVLW OPTION MOVLW TRIS MOVLW MOVWF BSF end Instruction Memory: So far you have been writing your code into the *.asm file. This code is compiled into HEX code which is stored on the microcontroller device. But where is it stored? All microchip PICs have at least two types of memory: Instruction Memory and Data (Register) Memory. When we write the hex file to the microcontroller device, it is stored in the Instruction Memory. So on the PIC16F505 devices the maximum amount of asm code you can write is 1024 because each line of asm (not including blank space, comments or #include or __CONFIG statements) converts directly into one line of b'11011111' b'11111000' PORTB b'00000000' PORTB PORTB, 2 ; ; ; ; ; ; Binary value moved into the W register and Clear T0CS to turn off CLKIN in OPTION reg. Binary value moved into the W register to select RB2, 1, 0 as Outputs in TRISB reg. Initialise the port (B) with all LEDs off

; ; Turns On a LED at RB2 for PicoDice

Instruction Memory. (Yet in actuality, there are only 1023 lines on this device because the last line holds a MOVLW command to define the calibration value of the microcontroller.)

Philip Tallents 2011

Where to GOTO: Now if all we could do was write code so as to modify peripheral register bits then we would have a problem: We would have to write the code so it went from Instruction line 0000h to line 03FFh without any deviations. But instead we can create deviations with a GOTO command, conditional deviations with a BTFSC (Bit Test File Skip if Clear) command, and conditional deviations with a DECFSZ (Decrement File Skip if Zero) command. The Program Counter (PC) keeps track of which instruction is next. The GOTO command modifies the PC, and so the code will effectively jump to that address in Instruction Memory. The GOTO command can be used in numerous ways. The easiest way is to use headings such as Main: or Init: (or a name of your choice followed by a colon)

to indicate the start of each section of code instructions. Then you refer to these headings with the GOTO command: Example Main: BSF GOTO PORTB, 2 Main ; Turns On (to +ve) pin RB2 of the PORTB file ; Loops through the BSF command indefinitely

Another way to use a GOTO command is with the $ symbol to indicate a relative position. The Above example could be stated simply as GOTO $-1 without the heading. The $ sign indicates the current instruction with + or value to indicate the relative position. You can also use the $ symbol alone (as indicated below). Main: BSF GOTO

PORTB, 2 $

; Turns On (to +ve) pin RB2 of the PORTB file ; Loops through the GOTO command indefinitely

In the first example above it was a bit silly to repeat the BSF command over and over, because once a bit is set, it stays set. But with the GOTO $ command, the Bit is set only once and the code effectively stops after this instruction.

Philip Tallents 2011

Bit Test (Conditional GOTO) GOTO is useful in combination with a BTFSC command (Bit Test File Skip if Clear). Putting these two commands together creates a conditional GOTO. This combination can be used like an IF statement as in other programming languages. For example: lets do a Bit Test on a Port that has been designated as an input, such as RC5 on the PicoLights project. With a switch connected to RC5 we will periodically test the input to trigger a LED.

Example
#include <P16F505.INC> __CONFIG _IntRC_OSC & _MCLRE_OFF & _WDT_OFF Init: MOVLW b'11011111' ; Binary value moved into the W register and OPTION ; Clear T0CS to turn off CLKIN in OPTION reg. MOVLW TRIS MOVLW MOVWF SetLED1: BSF Test: BTFSC GOTO ClearLED1: BCF GOTO end So this is how a Bit Test is used with a GOTO statement to make it a conditional GOTO. The BTFSC command says, IF input RC5 is Clear (Pressed) then Skip to the heading label ClearLED1, else IF input GP1 is Set (Un-Pressed) then GOTO the heading label SetLED1. The two loops above represent the resulting code deviation where RC5=Clear repeats the Red loop & RC5=Set repeats the Blue loop. This code turns the LED off when the switch is held down; this in turn grounds the pin to 0V. But when the switch is released, the 10kOhm Pull-Up resistor sets the pin to 4.5V un-pressed. Try this example with BTFSS (Skip if Set) & note the result. b'11111000' PORTC b'00000000' PORTC PORTC, 2 PORTC, 5 SetLED1 PORTC, 2 Test ; ; ; ; ; ; ; ; ; Binary value moved into the W register to select RC2, 1, 0 as Outputs in TRISC reg. Initialise the port (C) with all LEDs off Bit Set on pin RC2 Bit Test RC5 GOTO $-2 would also work here Bit Clear on pin RC2

Philip Tallents 2011

Delay (Conditional GOTO) The DECFSZ (Decrement File Skip if Zero) command can help us create a delay by counting down from 255 to 0. It is similar to the Bit-Test because it is conditional upon an event, but whereas the Bit-Test was conditional upon a single bit, the DECFSZ is conditional upon a whole register (8 bits) and whether this has a value of zero. So if we put the DECFSZ in a loop it will continually subtract 1 from the register, but when it reaches zero the PC (Program Counter) jumps out of the loop. This effectively wastes some time but not very much, so we need to put a loop in a loop, and so on until we have a usable delay.

Example
#include <P12F508.INC> __CONFIG _IntRC_OSC & _MCLRE_OFF & _WDT_OFF Init: MOVLW OPTION MOVLW TRIS MOVLW MOVWF Main: BSF Test: BTFSC GOTO ClearLED1: BCF Delay: DECFSZ GOTO DECFSZ GOTO DECFSZ GOTO GOTO end PORTC, 5 Main PORTC, 2 0x08 Delay 0x09 Delay 0x0A Delay Test ; ; ; ; ; ; ; ; ; Bit Test RC5 GOTO $-2 would also work here Turns OFF (clears) pin RC2 of the PORTC register 0x08 is the first GPR (General Purpose Register) in Data Memory Each DECFSZ represents a loop between itself and the heading Delay so each loop is submerged within the last The time for this delay is about 52 seconds PORTC, 2 ; Turns On (sets) pin RC2 of the PORTC register b'11011111' ; ; b'11111000' ; PORTC ; b'00000000' ; PORTC ; Binary value moved into the W register and Clear T0CS to turn off CLKIN in OPTION reg. Binary value moved into the W register to select RC2, 1, 0 as Outputs in TRISC reg. Initialise the port (C) with all LEDs off

Philip Tallents 2011

Program this example and you may be left wondering why the LED stays off initially even without pressing the switch. The reason is that the PIC16F505 requires a startup delay to balance the voltages correctly. To fix this we can create a start-up using DECFSZ loops. Add the following instructions before the Init: heading. Startup: DECFSZ GOTO DECFSZ GOTO

0x08 Startup 0x09 Startup

; ;

This Delay reuses the registers 0x08 and 0x09 The time for this delay is about 0.2 sec

The first delay we used is too long (about 52 sec) to be useful. So we can pre-load the last DECFSZ register (0x0A) with a smaller number (eg. 10) for a shorter delay. Add the following lines before the Delay: heading. Preload: CLRF CLRF MOVLW MOVWF 0x08 0x09 .10 0x0A ; ; ; ; (Clear File) Reg. 0x08 & 0x09 will be .0 (.256 equiv.) With a Decrement they will roll-over to .255 A preload of 10 (on the last register) will give us about 2 sec on this delay with three DECFSZ loops

The CLRF instructions clear the files 0x08 & 0x09 to zero. The MOVLW (Move Literal to Working) instruction places the number 10 into the working register, and the MOVWF (Move Working to File) instruction places this number into reg. 0x0A.

Naming GPRs (cblock)


Instead of using the literal values to define the Data Memory registers, we can name the GPRs with words to describe what they are being used for. This makes the code easier to read. Just add the following lines after the __CONFIG statement. cblock 0x08 DelayReg1 DelayReg2 DelayReg3 ; ; ; ; ; ; cblock defines the register start Add your register names & they will be assigned Data Memory space up to 0x1F The PIC16F505 has 24 General Purpose Registers in the first memory Bank 0 & 16 more in Banks 1-3 endc defines the end of the cblock

endc

After you have named your Data Memory registers within the cblock, you can use these names in your code instead of using literal values. For Example: Preload: CLRF CLRF MOVLW MOVWF DelayReg1 DelayReg2 .10 DelayReg3 ; ; ; ; (Clear File) Reg. 0x08 & 0x09 will be .0 (.256 equiv.) With a Decrement they will roll-over to .255 A preload of 10 (on the last register) will give us about 2 sec on this delay with three DECFSZ loops
Philip Tallents 2011

Lastly
Creating delays can be a bit cumbersome, so weve created an easy to use tool for you. So to create your own delays quickly and easily, please refer to the Automatic Delay tool in the PIcoFlow_LT software.

Conclusion
In this tutorial, you have learnt about the Program Memory, the GOTO instruction and the Program Counter (PC). You have also learnt about the combination of a BitTest such as BTFSC with the GOTO instruction to make a Conditional GOTO the example given was a Bit-Test on a Port Input such as RC5 so as to use an external switch to deviate the code and light a LED on RC2. We also created our own Delays to exemplify the conditional GOTO with the DECFSZ; thereby determining the minimum time the LED would stay off and then used a cblock to name the GPR (Data Memory) registers.

Test What Youve Learned


Try creating the following programs and have them checked by your teacher. 1. Delays a. LED1 on, Pause, LED2 on, Pause, LED3 on, Pause b. All LEDs off, Pause, Repeat from the start 2. Bit-Test a. LED1 on, then wait for switch input, LED2 on, then wait for switch input b. LED3 on, then wait for switch input, Repeat from the start

Contact Support: Email: Web: Ph.: Fax: Philip Tallents ptal@picokit.com www.picokit.com 0402 239 363 03 6337 0439

Philip Tallents 2011

Das könnte Ihnen auch gefallen