Êíèãà: Introduction to Microprocessors and Microcontrollers

The programming steps

The programming steps

The purpose of our first program

We want to do something really simple but not something that may have happened by accident. We are going to configure all of PortB as outputs and then make the output signal to be off, on, off, on, off, on, off, on. We can connect a series of LEDs to the output pins so alternate lights will be on.

1 We will start by selecting Bank 1 of the register file map. To do this we need to talk to the status register, so by referring to Figure 15.8, we can see that the status register is file 03. From our look at the Status register in Chapter 15, we can see that bit 5 is the RP0 which controls the selection of Bank0 or Bank1. The first line of the program must set bit 5 of register 3.

Program starts:

      BSF 3,5; Sets bit 5 of register 3 to select Bank1

It is worth mentioning at this stage, that anything written after the semicolon is just a note for us and is ignored by the assembly program. This is called the comment field. It is always worth using this area to explain the program, and this often saves hours trying to remember what we were attempting to do when we first wrote the program. This can be an even larger problem if someone else writes a program and is then off sick and we are left to find out why the program doesn’t work.

2 We now want to arrange for PortB to be an output. This involves loading a 0 into each of the controlling bits in the data direction register which we can see from Figure 15.8 is called TRISB and is register number 86. To do this, we are going to clear the W register using the CLRW instruction and then copy this zero into register 86. This involves two extra instructions so, at this stage, our program will read:

      BSF 3,5 ; Sets bit 5 of register 3 to select Bank1
      CLRW    ; puts a zero into register W
      MOVWF 86; copies the zero into register 86 which is
              ; the PortB data direction register

3 Now we have sorted out the direction of the data flow and we can input the actual data. This time we are going to clear bit 5 of register 3 to allow us to access Bank0. We can now load our data into the W register. But what is the data? If we assume the LED is going to be connected between the port output and the zero volt connection, the voltages corresponding to the light sequence off, on, off, on, off, on, off, on will be 0V, +5 V, 0 V, +5 V, 0 V, +5 V, 0 V, +5 V and the data will be 0,1,0,1, 0,1,0,1. We could enter this as a binary number written as B’01010101’ or as the hex number 55 which is a little easier to read.

Once the 55H is in the W register, we can use the code MOVWF to copy it into register 06 which in Figure 15.8, we can see is the PortB data register.

The program is now:

      BSF 3,5 ; Sets bit 5 of register 3 to select Bank1
      CLRW    ; puts a zero into register W
      MOVWF 86; copies the zero into register 86 which is
              ; the PortB data direction register
      BCF 3,5 ; clears bit 5 of register 3
      MOVLW 55; this is the output data to give the on, off
              ; sequence
      MOVWF 06; this copies the data into PortB

4 As it stands, the micro will perform each of these steps once and then stop. We have a problem here because it will take only a few microseconds to complete these instructions – certainly too fast for us to see if the correct sequence of LEDs are illuminated. We need to give the micro something to do which will keep the LEDs operational and our choice here is to reload the output port continuously. Have a look at our new program:

      BSF 3,5   ; Sets bit 5 of register 3 to select Bank1
      CLRW      ; puts a zero into register W
      MOVWF 86  ; copies the zero into register 86 which
                ; is the PortB data direction register
      BCF 3,5   ; clears bit 5 of register 3
      MOVLW 55  ; this is the output data to give the on,
                ; off sequence
again MOVWF 06  ; this copies the data into PortB
      GOTO again; this line forces the micro to return to
                ; the previous line

The words ‘again’ are called labels and the assembler program notices that the two are identical and replaces them by the correct address. The fact that we have used ‘again’, a word that makes sense in the context is just to help us to understand the program, the assembler would accept ‘asdf’ or anything else just as happily. Some assemblers put restrictions on the names chosen. It may, for example, not allow it to start with a number, or use certain words or symbols.

5 At the end of the program, we have to put the instruction END to tell the assembler to stop. It is called an ‘assembler directive’ and is there to tell the assembler program that it has reached the end of our program. Directives are not instructions to the microcontroller and are not converted to machine code.

6 At the start of the program we can use the directive ORG which means ‘origin’ and gives the starting address for the assembled program. This has been added to our final program. If we had not done this, the assembler will assume the address to be zero so, in this case it would make no difference whether we added this directive or left it out. When the PIC is reset, it always goes to address 000 so if we wanted the program to start elsewhere we would have to leave a GOTO instruction at address 000 to tell the microcontroller where it is to start. Remember that the ORG is only an instruction to the assembler telling it where to start loading the program – the PIC doesn’t know anything about this because directives are not converted to the program code.

So our final program is:

      ORG 000   ; Program starts at address 000
      BSF 3,5   ; Sets bit 5 of register 3 to select Bank1
      CLRW      ; puts a zero into register W
      MOVWF 86  ; copies the zero into register 86 which
                ; is the PortB data direction register
      BCF 3,5   ; clears bit 5 of register 3 to select
                ; Bank0
      MOVLW 55  ; this is the output data to give the on,
                ; off sequence
again MOVWF 06  ; this copies the data into PortB
      GOTO again; this line forces the micro to return to
                ; the previous line
      END       ; the end of our code to be assembled

Notice how the program is written in columns or ‘fields’. It is necessary to use the correct fields as this tells the assembler what the items are. Remember to use the semicolon to start notes that we wish the assembler to ignore.

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.757. Çàïðîñîâ Ê ÁÄ/Cache: 3 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç