[Menu]>[Circuits Gallery]>[Count-down timer]


Processing explanation
of Count-down timer



Title
;********************************************************
;
;             The Count-down timer processing
;
;                                 Author : Seiichi Inoue
;********************************************************
    I wrote the title of the program using the comment( ; ).



LIST and INCLUDE directive
        list            p=pic16f84a
        include         p16f84a.inc
    Processor type is set by LIST directive.
    The standard label definition of PIC16F84A is read by the INCLUDE directive.
    As for the change of the standard label definition of PIC16F84A, 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 the programmer. However, it is automatically established when using CONFIG directive.
    As for the contents of operand, refer to "Processing explanation of signboard".

    ItemContentsField nameBit(s)
    Code ProtectionOFFCP1111111111
    Power-up TimerONPWRTE(inv)0
    Watchdog TimerOFFWDTE0
    Oscillator SelectionHSFOSC1 and FOSC010




Label definition

;****************  Label Definition  ********************

    The definition of the label constant to use by the processing is done. The standard label of PIC16F84A is read in INCLUDE directive.

    The pattern definition of the 7 segment LED is specified by the EQU, too.

    A pattern is shown below.



'0' : Lighting-up, '1' : Going-out.

TOP VIEW
abcdefg
00000001
11001111
20010010
30000110
41001100
50100100
60100000
70001111
80000000
90000100





Debug mode specification

;**************  Debugging mode setting  ****************
;For debugging mode, ";" of next line should be removed.
;#define  _debug
    It sometimes had better modify the part of the processing step when debugging software.
    This time, the initial value of the counter value, the set value of the timer, the condition of the stop switch are set for the debugging. However, this time, I was made to be able to change the part to assemble by the definition of the '_debug' label.
    For the details, refer to "Debugging of Count-down timer".






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 processings are done as the initialization processing after the turning on.
    It sets a mode at port A.
    It sets four of port A from the 0th to the 3rd to the output port and it sets the 4th to the input port.
    It uses the 4th port for the input port of the STOP key.
    It sets B port pull-up function(RPBU=0).

    As for the pull-up function, refer to "The specification of B port ( RB0-RB3 )".
    It sets the condition of the timer start-up.

    It uses an internal clock. The prescaler is used for TMR0. The prescaler value is 256.
    It sets the output of the 3rd port at port A to '1'.

    It sets a relay to the OFF condition.
    It writes the lighting-up pattern of 7 segments in the RAM memory.
    It sets 0 to the second counter.
    It sets a non-time-out condition to the time-out display memory (the flag).





Timer stand-by process

;*************  Timer stand-by Process  *****************

    In the condition that the count-down of the timer hasn't started, the following processing is repeatedly executed.

    It sets the set value of the BCD switch to the count value.
    It outputs the contents of the counter to the LED.
    It sets the 6th port of port B to the input mode.
    It checks the condition of the start switch.



Timer start process
;**************  Timer start Process  *******************
    The following processing is executed when detecting that the start switch became ON with the timer stand-by process.
    It makes a relay ON.
    It executes the initialization of the interruption timer.
      It is necessary to make have interruption in 1 second as correctly as possible.
      To decide the interruption time of the timer, there is a following element.
The oscillator frequency:I use 10-MHz oscillation.
The set value of prescaler.: I set to 256.
I set a maximum to make the interruption number of times little.
The count value of the hard timer: I set for an error to be minimized. ( 213 counts )
The set value is 256-213=43.
The set value of the soft counter:It sets for an error to be minimized. ( 46 counts )

    It sets an interruption possible condition.

    Processing since then is repeatedly executed until there is a time-out.
    It outputs the contents of the counter to the LED.
    The checking of a time-out condition
    At the time of the time-out, it returns to the initialization processing.

The interruption time can be calculated by the following formula.
The interruption time= (4/OSC freq) x Prescaler x Hard timer count x Soft count

= 0.4 x 10-6 x 256 x 217 x 45

= 0.999936 seconds

Above-mentioned value is a value with few errors and little interruption time.
When using 4.19 MHz as the oscillator, the precision at the time of the hard timer count value=186, the soft timer count value=22 was the best and was to be in 1.000049642 seconds.
The actual circuit doesn't become as the calculation in the frequency precision of the oscillator and the relation of the soft processing after interruption. It is necessary to adjust after making.

The error when count-downed in 99 minutes with the equipment which I made was to be in -20 seconds(The timer is faster).
It becomes 0.003367 seconds when converting to the error in 1 second. Therefore, I did to the hard timer count value=213, the soft timer count value=46. The interruption time in this case is as follows. After adjusting a value, it became an error in 1 second within 99 minutes.
The interruption time= (4/OSC freq) x Prescaler x Hard timer count x Soft count

= 0.4 x 10-6 x 256 x 213 x 46

= 1.0033152 seconds

The set value of hard timer (TMR0) becomes the value which subtracted the count value to want for from 256.
256-213=43(2Bh)




LED control subroutine

;**************  LED Control Subroutine  ****************

    The contents of the counter are output to the LED in order of the 10th of the minute, the 1st of the minute, the 10th of the second, the 1st of the second.





Change from the binary-coded decimal code(BCD) into the 7 segment data

;*******  Change BCD to 7segment data Subroutine  *******

    It changes from the data of the binary-coded decimal code into the data in 7 segments and outputs to port B.





Timer subroutine of 1 millisecond

;*************  1msec Timer Subroutine  *****************
    Port B is controlling the BCD switches, the start switch, the LEDs. It is necessary to do time a little for the transistor to become ON even if it specifies each device. This transition time is very short(Several-µ second). However, when a 10-MHz clock is used, the PIC operating time of 1 step is a 0.4-µ second. When there are few numbers of the steps in PIC, the time of the decoder control is shorter than transition time of the transistor. So, it is executing the wait processing of 1 millisecond after specifying devices such as the BCD switch. To be 1 millisecond isn't necessary. I think that there is not a problem even if it is short a little. This depends on the characteristic of the transistor. Display's flickering occurs when the waiting time is long.

    When using at the input mode like the BCD switchs, the start switch, it executes a 1-millisecond wait before reading the condition of the device after specifying a device. When using at the output mode like the 7 segment LEDs, it executes the processing of the following device after waiting 1 millisecond after presenting the lighting-up information of the LED.

    It is difficult to know a necessity in the waiting time by the simulation of MLPAB. I understood it by the operation of the actual circuit. It is necessary to consider in the operating time of the hardware, too.

    This subroutine is diverting the processing which was made in "LED flasher".






Interruption process

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


    It does the saving of W register and the saving of STATUS register and it confirms a timer interrupt.
    The point which should be careful here is in the bank condition. When the interruption occurs immediately after changing a bank to 1 in interruped processing, it sets SFR which was mistaken by the interruption processing. So, it is executed in the processing which changes a bank to 0 after the saving of STATUS register is ended.




Interruption ending process

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

    It returns STATUS register and W register and it ends the interruption processing with the interruption ending instruction (RETFIE). With this, it becomes the interruption possible condition.
    SWAPF instruction is used for the processing to return W register so as not for the contents of the STATUS register to change.




Time-out interruption process

;***********  Time-out interruption Process  ************

    This processing is executed when the timer TMR0 of the hardware does in the time-out.
    To have count-down processing every second, it is short only at the hardware timer. (A maximum of 26 milliseconds in case of the 10-MHz clock)
    So, it counts the interruption of the hard timer by the software and it is making execute count-down processing every second.
    I decide the set value of the hard timer and the count value of the software to minimize the error of the count-down.




Count-down process

;*************  Timer count-down Process  ***************

    Processing is executed every second.
    It is checking first, do subtract the 1st counter in the second and whether or not it becomes 0. In case of 0, because there is possibility of the time-out situation, it is doing the checking whether or not all counters are 0. To judge 0 of the counter, it reads counter content by the movf instruction and it is checking the Z bit of the STATUS register. The Z bit is '1' means the contents of the counter are 0.

    When either counter is not 0, because it is not in the time-out condition, count-down processing is executed.
    In the count-down processing, it checks the contents of each counter and in case of 0, a maximum is set. The maximum of the 10th of the second is 5 and as for the other digit, 9 is a maximum. When the contents of the counter are not 0, it subtracts one.

    In the interruption processing, only time-out checking and count-down processing are executed. The time-out processing(Timer stop and Relay OFF) and the LED display processing are executed by the timer start processing.



End of coding

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

        end

    At the end of coding, END directive is used.