[Menu]>[Circuits Gallery]>[DC motor]


Processing explanation
for DC motor speed controller

LIST and INCLUDE directive
        list            p=pic16f873
        include         p16f873.inc
    Processor type is set by LIST directive.
    The standard label definition of PIC16F873 is read by the INCLUDE directive.


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.
    The following specification is done as configuration word.
    Oscillator:HS
    Watchdog Timer:OFF
    Power-up Timer:enabled
    Low Voltage ICSP : OFF
    (RB3 can not be used for the input/output port when not making this OFF).

    The result is 3F72h.
    The item except the above is included in Configuration Word of PIC16F873. Generally, it is to be OK in above-mentioned directine.


Suppressing of the bank warning
        errorlevel      -302    ;Suppress bank warning
    In PIC16F873, SFR is divided into four banks. There are SFRs which are common to all banks, but there are SFRs which are peculiar to the bank. The specification of the bank is done with RP0 bit and RP1 bit of the STATUS register. Even if the specification of the bank to the STATUS register is normally done, the message to confirm bank specification is displayed.
Ex.
Message[302] C:\MPLAB\PROJECT\MOTOR_~2\MOTOR.ASM 32 : Register in operand not in bank 0.
                                                         Ensure that bank bits are correct.
    Above-mentioned example is a warning message about the TRISA register (bank 1) which is specified at the line of 32nd of the source code.
    The content with the 32nd line is hereinafter.
032        movwf   trisa           ;Set TRISA register
    Bank specification is normally set at the 30th line. In the case, this message is displayed. This message is displayed even if a bank is specified at the 31th line. It is none of your business. "errorlevel" is used to suppress the displaying of message (302). There is a way of changing bank specification with p16f873.inc, too.


Label definition
;****************  Label Definition  ********************
    In the processing this time, three labels are defined.
    "speed" is a reference speed value. It is controlled for the value which changed the voltage which occurred with the control voltage input circuit by the A/D and the value which was specified by speed to become equal. The value which multiplied the value which was specified by "speed" on the value which divided 5V in 256 is reference speed voltage. Because 8 is specified this time, 5*8/256=0.156V is reference speed voltage. When making the value which is specified by "speed" big, the reference speed voltage becomes high. It should change the value of "speed" in the output voltage of the circuit which detects the number of rotations of the motor.
    "change" is the value which specifies the speed to change the control electric current of the motor to. The detection of the rotational speed of the motor is done every 10 milliseconds. When there is a difference in the voltage of the control voltage input circuit and the reference speed voltage, the value which was specified by "change" is added or subtracted to or from the value of the PWM duty. The duty specification for PWM is done by the CCPR1 register. 256 kinds of duties can be set by CCPR1 because it is 8 bits. When the motor drive electric current stops when the value of CCPR1 is 255 and the value is 0, it is driven in the maximum electric current. When 1 is set to "change", 1 is added to or subtracted from the value of CCPR1 every 10 milliseconds. For example, in the time which the maximum drive condition(CCPR1=0) takes from the stop condition(CCPR1=255), it is 10ms x 256=2.56 seconds. Because it is 2.56 seconds in the time which the 5V change takes, as for 1 value, it is possible to assume that the value of change is about 2mV/ms. The variation with the control value of 10 milliseconds becomes big when making the value of "change" big. With the characteristic of the motor to use, this value should be adjusted.
    "led" is the work area which is saved the duty value temporarily to make LEDs for the control situation monitor light up. Address 20h is used.


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 processing after the turning on.
    Initialization of the mode of ports A , B and C
    I use the 0th of port A for an analog input. All other A ports are set to output mode to avoid the influence from other input port. There may are not an influence.
    All B ports are set to output mode for LED control.
    All C ports are set to output mode for CCP.
    Initialization of the A/D converter

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

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

    CCP2 is used as the compare mode and makes interruption occur periodically.
    The interruption period is 10 milliseconds. I set the internal clock as the count source of timer1.
    The prescaler of timer1 is set to 1:1 and is set timer1 to operate.
    The CCP2 mode is selected to start an A/D converter at the same time when interruption occurs.
    Initialization of interruption

    The interruption enable bit of CCP2 is set. Also GIE and PIE are set.

    The initialization processing ended above. After this, it waits for the interruption only. As the main processing, 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  *****************
    Clearing of interruption flag
    The interruption occurs every 10 milliseconds with CCP2. The interruption flag of CCP2 should be cleared first. When not clearing this, the following interruption occurs without waiting desired time.
    Wait until A/D converter completion

    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 should be provided before start the A/D conversion.
    Comparison with reference speed voltage

    If the A/D conversion completes, the higher rank byte of the converted result is compared with the reference speed value. A converted value is composed of 10 bits. However, at the circuit this time, because it doesn't need 1024 pieces of division. So, only the higher rank byte is used and makes 256 pieces of division.
    An A/D conversion value and reference speed value (speed) are compared and a difference between the number of rotations and the reference speed of the motor is judged.
    Speed down process

    When the turning of the motor is higher than the reference speed, a duty ratio is made big and a motor drive electric current is suppressed. The rate that the duty ratio becomes big is decided by "change".
    Speed up process

    When the turning of the motor is lower than the reference speed, a duty ratio is made small and a motor drive electric current is increased. The rate that the duty ratio becomes small is decided by "change".
    LED lighting-up control

    An LED level meter is used to confirm the drive situation of the motor. Seven LEDs are housed in the LED level meter which is used this time. I thought that eight LEDs were needed. However, I made this processing and knew that it was enough in seven LEDs. In the logic this time, 3 bits of higher ranks of CCPR1 are used to control a level meter. There are eight kinds of condition which it is possible to express by 3 bits from 000 to 111. So, I thought that I needed eight LEDs. When making one LED light up every code, eight LEDs are needed. However, in case of the level meter, the number of the LEDs which are lit up as the level rises is increased. Also, 000 becomes all LED going-out conditions. So, the LED is enough at seven.

    In case of drive stop, it makes the bit of CCPR1 00000000 and in case of the maximum drive, it makes it 11111111. So, to use this information for the level meter control, 0 and 1 should be reversed. The "comf" instruction is for its purpose. Only 3 bits of higher ranks of the reversed data of CCPR1 are used for the lighting-up of a level meter. The lighting-up of the LEDs are controlled according to the code. The circuit prepared 8 ports to RB7 from RB0. The 4th bit from the higher rank of CCPR1 is used for the control of RB0. At the circuit this time, the control of RB0 is unnecessary.





Interruption ending process

;************  END of Interruption Process **************
    The RETFIE instruction is executed at end of the interruption processing. With this, it becomes the interruption possible condition. Because it isn't saving registers in case of interruption, there is no need to resave.
    The control of the B port is done before executing RETFIE instruction. This is to reduce the processing step of the PORTB register.




End of coding

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

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