[Menu]>[Circuits Gallery]>[Light controller]


Processing explanation
of Light controller



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



LIST and INCLUDE directive
        list            p=pic16f873
        include         p16f873.inc
    Processor type is set by LIST directive.
    The standard label definition of PIC16F873 is referred by the INCLUDE directive.
    In the standard label definition, the warning messages are displayed by the definition except bank 0. There are no influence in the operation but I corrected.
    As for the change of the standard label definition, refer to "Processing explanation of signboard".
    Or you can use ERRORLEVEL directive to suppress the Warning messages.



Configuration Word
    Configuration Word is specified using CONFIG directive.
        __config _hs_osc & _wdt_off & _pwrte_on & _cp_off
    Configuration Word can be set when writing a program by programmer. However, it is automatically established when using CONFIG directive.
    ItemContentsField nameBit(s)
    Code ProtectionOFFCP11
    Power-up TimerONPWRTE(inv)0
    Watchdog TimerOFFWDTE0
    Oscillator SelectionHSFOSC1 and FOSC010

    The item except the above is included in Configuration Word of PIC16F873. Generally, it is to be OK in above-mentioned 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.
    It makes each processing jump with the GOTO instruction.



Initialization process

;****************  Initial Process  *********************
    The following processing is done as the initialization process after the turning on.
    Initialization of the mode of ports A and C
    I use the 0th of port A for an analog input.
    I set all other A ports to output mode to avoid the influence from other input port. There may are not an influence.
    I set all C ports to output mode for CCP.
    Initialization of the A/D converter

    Because 10 MHz were used as the clock of PIC, I set an A/D conversion clock to Fosc/32.
    The input channel is AN0 and set A/D converter ON bit.
    I set a conversion result left justification to use a higher byte.
    As for the input port configuration control, I select the pattern of "Only 0th port is analog".
    Initialization of PWM

    CCP1 is used as the PWM mode.
    I clear the counter of timer2 and duty setting for the certain operation.
    The period of the output pulse is 1638.4 microseconds (about 610 Hz).
    The setting value of prescaler is 1:16. I set timer2 ON bit.
    Initialization of the compare mode

    CCP2 is used as the compare mode and makes interruption occur periodically.
    The interruption period is 1 millisecond. I set the internal clock as the count source of timer1.
    I set prescaler of timer1 to 1:1 and set timer1 to operate.
    I select the mode of CCP2 to start an A/D converter at the same time when interruption occurs.
    Interruption's initialization

    I set the interruption enable bit of CCP2. Also GIE and PIE are set.

    The initialization process is ended above. After this, the program waits for the interruption only. As the main process, it repeats the execution of the same address. '$' with operand means its address. '$+1' means an next address from its address.




Interruption process

;***************  Interruption Process  *****************

    The interruption occurs every millisecond with CCP2.
    It clears the interruption flag of CCP2 first. When not executing this, the following interruption occurs without waiting desired time. The conversion of the A/D converter has started simultaneously with interruption of CCP2. So, it waits until change completion.(The GO bit of ADCON0 becomes '0') This time, because the analog channel is only 0 channel, it is not need to wait until the input taking-in (the about 20 microseconds). When A/D converting while switching more than one channel, after channel selecting, the waiting time must be provided before start the A/D conversion.
    If the conversion ends, it sets the higher byte of the conversion result to the duty counter of CCP1 (the PWM mode).
    The conversion result comes out by 10 bits but this circuit is using only a higher byte. So, the resolution is 256.
    When the input voltage is 0 V, the conversion result becomes 00h and the duty is 0%. In this case, the output pulse doesn't become H level.
    When the input voltage rises, the conversion result becomes a big value with it. The duty becomes big and the output pulse duration increases.
    When the input voltage becomes a maximum, the conversion result becomes FFh and the duty becomes by nearly 100%. Strictly, it doesn't become 100% because it doesn't use lower 2 bits of converted result (10bits). In case of practical use, there is no problem.

    In this circuit, the W and STATUS registers are not used in the main process. So, these registers are not saved when the interruption occurs. When using some registers which are used in main process and interrupt process by the sharing, those registers must be saved.




Interruption ending process

;************  END of Interruption Process **************

    The RETFIE instruction is executed at end of the interruption process. With this, it becomes the interruption possible condition.
    In this circuit, because there is no saving registers in case of interruption, there is no need to resave.



Ending of coding

;********************************************************
;          END of signboard control processing
;********************************************************

        end
    At the end of coding, END directive is used.