[Menu]>[Circuits Gallery]>[LED flasher]


Processing explanation of LED flasher


Title
;********************************************************
;
;            The LED flash control processing
;
;                                 Author : Seiichi Inoue
;********************************************************
    I wrote the title of the program using the comment( ; ).


LIST directive
        list    p=pic16f84a
    Set processor type.


INCLUDE directive
        include   p16f84a.inc
    The standard label definition of PIC16F84A is read by the INCLUDE directive.
    Definition files are generally installed in the place below.
    c:\Program Files\Mplab\p16f84a.inc
    ( The drive name depends on your PC. )


Configuration Word
        __config _hs_osc & _wdt_off & _pwrte_on & _cp_off


ERRORLEVEL directive
        errorlevel      -302    ;Suppress bank warning
    When using the standard definition of the Microchip Inc., a warning message about TRISA and TRISB is displayed. Warning messages are suppressed by this directive.




Label definition
;****************  Label Definition  ********************
    Here, the label to use by the program is defined. A literal value can be defined as the label. The label definition can be put on the freelier position before the step which refers to the defined data. I made a section of the label definition in the reason for that it is easy to see. It is not need to define the label which is put to the program step.


LED lighting-up pattern data definition
;*************  Pattern Data Definition  ****************
;                 '1':OFF   '0':ON
    Because the data of the LED lighting-up control is a byte, the literal data is used. Therefore, I attached a label to each data pattern and defined a set value with the EQU directive.


Program start
;****************  Program Start  ***********************
    Instruction is executed from Zero addresses of the program memory when making the power ON of the PIC. When there is interruption processing, processing is begun from the addresse 4. Because it isn't using interruption this time, there is not program execution from the addresse 4. Tentatively, I was as OK even if the program started from the addresse 4.


Initial process
;****************  Initial Process  *********************
    The setting of the input/output of the port is done first. As for PIC16F84A, immediately after the turning on, all ports are input mode. This time, I set input port, too.
    After setting of ports, the going-out processing of LEDs is done.


Switch scan process
;****************  Key Scan Process  ********************
    This routine checks the condition of the switch which connected with RA4 from RA0. The input pin becomes L level ( 0 ) when a switch is pushed and becomes H level ( 1 ) when a switch isn't pushed. This routine executes an LED blink pattern processing subroutine according to the kind of the pushed switch.
    The routine checks the condition of each switch in the order. When all switches are pushed, the LED blink control of all the patterns is executed in the order.
    When a switch isn't pushed, key scan processing is repeatedly executed.


LED blink control subroutine
;***********  Pattern 0 Output Subroutine ***************
    I made subroutines every each pattern. The processing repeats the setting of blink data, output to PORTB, the execution of the timer subroutine. It is possible to decrease the use of the program memory by making PORTB output and a timer call a subroutine. But, in the case, the execute step increases by CALL and RETURN instruction.
    This time, because there is space in the program memory, I am not doing to the subroutine.
    When making a subroutine, it is necessary to be careful of the stack.
    This time, a stack is used as follows. ( When a 1 second timer is used )
    StackCall conditionReturn address
    1Call LED blink processing from the key scanKey scan
    2Call 1 sec timer from the LED blink processing.LED blink processing
    3Call 500 msec timer from the 1 sec timer.1 sec timer
    4Call 100 msec timer from the 500 msec timer.500 msec timer
    5Call 1 msec timer from the 100 msec timer.100 msec timer

    Because the stack is a maximum of 8, it is to be OK.


Timer subroutine
;********************************************************
;          Timer  Subroutine for 10MHz clock
;********************************************************
    Sometimes which continues a signal in the constant time or controls the time interval to scan, the execution of instruction must be delayed. To make the execution timing of the instruction, there are some ways. Generally, there are a way by the software and a way of using a hard timer. This time, I am using the way of the software.
    There is following plus and minus when comparing two ways.
    MeritDemerit
    Software timer*The processing is simple. *In the time count, it isn't possible to do the other processing.
    *The use of the program memory is big comparatively.
    *To decide a timer value is troublesome.(It computes the execute cycle)
    *The error is big.
    Hardware timer *The other soft processing is possible in the time count.
    *The time is correct comparatively.
    *The processing is troublesome because it uses interruption processing.

    Creating a 1-millisecond timer first as the timer processing. Because it is using 10 MHz for the processor frequency this time, the period is 0.1 microseconds. Because the execute cycle of the instruction is the 4 times, in the time with 1 instruction execution cycle, it becomes 0.4 microseconds. So, the execute cycle within the 1-millisecond time is 2,500 cycles. In the processing this time, it is 2,501 cycles. There are a few errors but substantively, there is not a problem.
    The 100-millisecond timer repeats a 1-millisecond subroutine 100 times. 500 milliseconds repeat a 100-millisecond subroutine five times. In 1 second, it repeats a 500-millisecond subroutine twice.


Configuration Word
    The configuration word should be set to '3FF2' . The setting contents are as follows.
    ItemContentsField nameBit(s)
    Code ProtectionOFFCP1111111111
    Power-up TimerONPWRTE(inv)0
    Watchdog TimerOFFWDTE0
    Oscillator SelectionHSFOSC1 and FOSC010