Êíèãà: Introduction to Microprocessors and Microcontrollers

Connecting the LEDs

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.

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


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