Выбрать главу

                ; 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.

Connecting the LEDs

For clarity, only one LED is shown but an LED and resistor should be joined to all the pins 7–13 to show the full output.

LEDs come in different colours and sizes and the cathode must be connected to a less positive voltage than the anode. The cathode is generally recognized either by a shorter connector wire or a flat moulded onto the body.

Component values

Looking at the data for a standard red LED, the typical voltage (Vf) across them when lit is 2 volts with a maximum current of (If) 20 mA. The small ‘f’ stands for ‘forward’. The light lost by reducing the current for a real project below its maximum value is not very great and it would be quite reasonable to operate the LED on, say, 10 mA.

To limit the current flow, a resistor is connected in series. Now, if the supply voltage for the microcontroller is 5 volts and about 0.7 volts are ‘lost’ inside the PIC and the LED is using 2 volts, the series resistor must be ‘using up’ the other 2.3 volts. The value of the resistor is given by R=V/I=2.3/(10×10–3)=230 Ω. If in doubt start with 470 ohms and see how it goes – this is a generally safe value for all situations.

More labels

The use of labels not only makes the program more readable but it allows modifications to be accommodated. For example, if we put in the actual address instead of the label and then modified the program by adding an extra instruction, the actual addresses would all shuffle along a bit to make room for the new instruction, making our old address inappropriate. The program would not work and it might take us hours before we see what we have done whereas a label would be sorted out by running it through the assembler with the new instruction added.

There is another useful assembler directive, EQU, which is an abbreviation for equates or ‘is equal to’. This can be used to make programs more readable by replacing some of the numbers with words. For example, register 86 is the PortB Data Direction register but the program would be easier to read if we replaced the number by the name. This would be done adding the line: PortBDDR EQU 86 before the program listing so as soon as the assembler spots the name PortBDDR it would replace it with 86. This has no affect on the final program but it makes life easier for us – which has got to be a ‘good thing’.

If we add some other labels, the final program can now be written as:

;EQUATES

PortBDDR EQU 86        ; PortB data direction reg.

                       ; is register 86

PortB    EQU 06        ; PortB data register is

                       ; register 06

Status   EQU 03        ; Status register is register

                       ; 03

RP0      EQU 05        ; Bank1 is selected by bit 5

Data     EQU 55        ; Data used is 55H

         BSF Status,RP0; Sets bit 5 of register 3 to

                       ; select Bank1

         CLRW          ; puts a zero into register

                       ; W

         MOVWF PortBDDR; copies the zero into

                       ; register 86 which is the

                       ; PortB data direction

                       ; register

         BCF Status,RP0; clears bit 5 of register 3

         MOVLW Data    ; output data to give the

                       ; on, off sequence

again    MOVWF PortB   ; 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

By making full use of labels, we have rewritten our program without any numbers at all. This is just a matter of choice – all labels, some labels or no labels, whatever we like.

Using a crystal

This gives a more accurate clock speed so that programs that involve real can be written. It may be that we want a display sequence to run at a particular rate.

To change to a crystal we need to set up the configuration bits in the PIC so that it knows that it is being controlled by a crystal instead of the RC method. This is most easily handled during the assembly process by clicking on ‘configuration bits’ and selecting the clock source from the options offered.

The two capacitors shown in Figure 16.3 in the clock circuit always have equal values and the range shown is suitable for 200 kHz and all clocks of 2 MHz and over. Other recommended values are: 32 kHz — 68/100 pF and 100 kHz — 100/150 pF. The higher values in each range results in higher stability but slower startup times.

Figure 16.3 A Crystal Controlled Clock

A ceramic resonator can be used as a plug-in replacement for the crystal.

A modification to the program

In the last program we controlled the voltages to each of the PortB outputs. With slight modifications we would be able to apply any combinations of voltages to control any external circuits. Even this first circuit has significant control capabilities but now we are going to extend the capability by applying a counting sequence to the output signals.

All programs are built on the backs of other programs that we have used before so we can save considerable time by keeping copies of our successful programs to be recycled whenever possible. This is well demonstrated in this example.

The program consists of three steps, two of which we have already designed and tested, so we know it works. If the new program refuses to work, we don’t have to start from scratch, we know two–thirds of it is OK. This is a very powerful method of designing programs and whole libraries of programs are available so new developments can be reduced to slotting together ready-made program segments.

When we make changes to a previously program, it is important to save the new version under a new name so that, in the event of a disaster, we can retreat and start again.

Here is the section that we have ‘borrowed’ from our previous work:

     ORG 000

     BSF 3,5

     CLRW

     MOVWF 86; PortB data direction = output