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


Source code file for DC motor speed controller

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
;********************************************************
;
;              DC motor speed controller
;
;                                 Device : PIC16F873
;                                 Author : Seiichi Inoue
;********************************************************

        list            p=pic16f873
        include         p16f873.inc
       __config _hs_osc & _wdt_off & _pwrte_on & _lvp_off
        errorlevel      -302    ;Suppress bank warning

;****************  Label Definition  ********************
speed   equ     d'8'   ;Reference speed (5x8/256=0.156V)
change  equ     d'1'    ;Change value (2mV/ms)

led     equ     h'20'   ;LED control data save area

;****************  Program Start  ***********************
        org     0               ;Reset Vector
        goto    init
        org     4               ;Interrupt Vector
        goto    int

;****************  Initial Process  *********************
init

;*** Port initialization
        bsf     status,rp0      ;Change to Bank1
        movlw   b'00000001'     ;AN0 to input mode
        movwf   trisa           ;Set TRISA register
        clrf    trisb           ;Set TRISB to uotput mode
        clrf    trisc           ;Set TRISC to output mode
        bcf     status,rp0      ;Change to Bank0

;*** A/D converter initialization
        movlw   b'10000001'     ;ADCS=10 CHS=AN0 ADON=ON
        movwf   adcon0          ;Set ADCON0 register
        bsf     status,rp0      ;Change to Bank1
        movlw   b'00001110'     ;ADFM=0 PCFG=1110
        movwf   adcon1          ;Set ADCON1 register
        bcf     status,rp0      ;Change to Bank0

;*** PWM initialization
        clrf    tmr2            ;Clear TMR2 register
        movlw   b'11111111'     ;Max duty (low speed)
        movwf   ccpr1l          ;Set CCPR1L register
        bsf     status,rp0      ;Change to Bank1
        movlw   d'255'          ;Period=1638.4usec(610Hz)
        movwf   pr2             ;Set PR2 register
        bcf     status,rp0      ;Change to Bank0
        movlw   b'00000110'     ;Pst=1:1 TMR2=ON Pre=1:16
        movwf   t2con           ;Set T2CON register
        movlw   b'00001100'     ;CCP1XY=0 CCP1M=1100(PWM)
        movwf   ccp1con         ;Set CCP1CON register

;*** Compare mode initialization
        clrf    tmr1h           ;Clear TMR1H register
        clrf    tmr1l           ;Clear TMR1L register
        movlw   h'61'           ;H'61A8'=25000
        movwf   ccpr2h          ;Set CCPR2H register
        movlw   h'a8'           ;25000*0.4usec = 10msec
        movwf   ccpr2l          ;Set CCPR2L register
        movlw   b'00000001'     ;Pre=1:1 TMR1=Int TMR1=ON
        movwf   t1con           ;Set T1CON register
        movlw   b'00001011'     ;CCP2M=1011(Compare)
        movwf   ccp2con         ;Set CCP2CON register

;*** Interruption control
        bsf     status,rp0      ;Change to Bank1
        movlw   b'00000001'     ;CCP2IE=Enable
        movwf   pie2            ;Set PIE2 register
        bcf     status,rp0      ;Change to Bank0
        movlw   b'11000000'     ;GIE=ON PEIE=ON
        movwf   intcon          ;Set INTCON register

wait
        goto    $               ;Interruption wait

;***************  Interruption Process  *****************
int
        clrf    pir2            ;Clear interruption flag
ad_check
        btfsc   adcon0,go       ;A/D convert end ?
        goto    ad_check        ;No. Again
        movfw   adresh          ;Read ADRESH register
        sublw   speed           ;Ref speed - Detect speed
        btfsc   status,c        ;Reference < Detect ?
        goto    check1          ;No. Jump to > or = check

;--- control to low speed ---
        movfw   ccpr1l          ;Read CCPR1L register
        addlw   change          ;Change value + CCPR1L
        btfss   status,c        ;Overflow ?
        movwf   ccpr1l          ;No. Write CCPR1L
        goto    led_cont        ;Jump to LED control

check1
        btfsc   status,z        ;Reference = Detect ?
        goto    led_cont        ;Yes. Jump to LED control

;--- control to fast speed ---
        movlw   change          ;Set change value
        subwf   ccpr1l,f        ;CCPR1L - Change value
        btfsc   status,c        ;Underflow ?
        goto    led_cont        ;Jump to LED control
        clrf    ccpr1l          ;Set fastest speed

;****************  LED control Process ******************
led_cont
        comf    ccpr1l,w        ;Complement CCPR1L bit
        movwf   led             ;Save LED data
        movlw   b'00010000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led1            ;No. 
        movlw   b'00000000'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led1    movlw   b'00100000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led2            ;No. 
        movlw   b'00000001'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led2    movlw   b'01000000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led3            ;No. 
        movlw   b'00000011'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led3    movlw   b'01100000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led4            ;No. 
        movlw   b'00000111'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led4    movlw   b'10000000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led5            ;No. 
        movlw   b'00001111'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led5    movlw   b'10100000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led6            ;No. 
        movlw   b'00011111'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led6    movlw   b'11000000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led7            ;No. 
        movlw   b'00111111'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led7    movlw   b'11100000'     ;Set compare data
        subwf   led,w           ;LED - data
        btfsc   status,c        ;Under ?
        goto    led8            ;No. 
        movlw   b'01111111'     ;Set LED control data
        goto    int_end         ;Jump to interrupt end
led8    movlw   b'11111111'     ;Set LED control data

;************  END of Interruption Process **************
int_end
        movwf   portb           ;Set PROTB
        retfie

;********************************************************
;            END of DC motor speed controller
;********************************************************

        end



Listing file for DC motor speed controller

MPASM 02.50.02 Intermediate     MOTOR.ASM   11-24-2001  19:54:44         PAGE  1


LOC  OBJECT CODE     LINE SOURCE TEXT
  VALUE

                      00001 ;********************************************************
                      00002 ;
                      00003 ;              DC motor speed controller
                      00004 ;
                      00005 ;                                 Device : PIC16F873
                      00006 ;                                 Author : Seiichi Inoue
                      00007 ;********************************************************
                      00008 
                      00009         LIST            P=PIC16F873
                      00010         INCLUDE         P16F873.INC
                      00001         LIST
                      00002 ; P16F873.INC  Standard Header File, Version 1.00
                      00358         LIST
2007   3F72           00011        __CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF
                      00012         ERRORLEVEL      -302    ;Suppress bank warning
                      00013 
                      00014 ;****************  Label Definition  ********************
  00000008            00015 SPEED   EQU     D'8'   ;Reference speed (5x8/256=0.156V)
  00000001            00016 CHANGE  EQU     D'1'    ;Change value (2mV/ms)
                      00017 
  00000020            00018 LED     EQU     H'20'   ;LED control data save area
                      00019 
                      00020 ;****************  Program Start  ***********************
0000                  00021         ORG     0               ;Reset Vector
0000   2805           00022         GOTO    INIT
0004                  00023         ORG     4               ;Interrupt Vector
0004   282D           00024         GOTO    INT
                      00025 
                      00026 ;****************  Initial Process  *********************
0005                  00027 INIT
                      00028 
                      00029 ;*** Port initialization
0005   1683           00030         BSF     STATUS,RP0      ;Change to Bank1
0006   3001           00031         MOVLW   B'00000001'     ;AN0 to input mode
0007   0085           00032         MOVWF   TRISA           ;Set TRISA register
0008   0186           00033         CLRF    TRISB           ;Set TRISB to uotput mode
0009   0187           00034         CLRF    TRISC           ;Set TRISC to output mode
000A   1283           00035         BCF     STATUS,RP0      ;Change to Bank0
                      00036 
                      00037 ;*** A/D converter initialization
000B   3081           00038         MOVLW   B'10000001'     ;ADCS=10 CHS=AN0 ADON=ON
000C   009F           00039         MOVWF   ADCON0          ;Set ADCON0 register
000D   1683           00040         BSF     STATUS,RP0      ;Change to Bank1
000E   300E           00041         MOVLW   B'00001110'     ;ADFM=0 PCFG=1110
000F   009F           00042         MOVWF   ADCON1          ;Set ADCON1 register
0010   1283           00043         BCF     STATUS,RP0      ;Change to Bank0
                      00044 
                      00045 ;*** PWM initialization
0011   0191           00046         CLRF    TMR2            ;Clear TMR2 register
0012   30FF           00047         MOVLW   B'11111111'     ;Max duty (low speed)
0013   0095           00048         MOVWF   CCPR1L          ;Set CCPR1L register
0014   1683           00049         BSF     STATUS,RP0      ;Change to Bank1
0015   30FF           00050         MOVLW   D'255'          ;Period=1638.4usec(610Hz)
MPASM 02.50.02 Intermediate     MOTOR.ASM   11-24-2001  19:54:44         PAGE  2


LOC  OBJECT CODE     LINE SOURCE TEXT
  VALUE

0016   0092           00051         MOVWF   PR2             ;Set PR2 register
0017   1283           00052         BCF     STATUS,RP0      ;Change to Bank0
0018   3006           00053         MOVLW   B'00000110'     ;Pst=1:1 TMR2=ON Pre=1:16
0019   0092           00054         MOVWF   T2CON           ;Set T2CON register
001A   300C           00055         MOVLW   B'00001100'     ;CCP1XY=0 CCP1M=1100(PWM)
001B   0097           00056         MOVWF   CCP1CON         ;Set CCP1CON register
                      00057 
                      00058 ;*** Compare mode initialization
001C   018F           00059         CLRF    TMR1H           ;Clear TMR1H register
001D   018E           00060         CLRF    TMR1L           ;Clear TMR1L register
001E   3061           00061         MOVLW   H'61'           ;H'61A8'=25000
001F   009C           00062         MOVWF   CCPR2H          ;Set CCPR2H register
0020   30A8           00063         MOVLW   H'a8'           ;25000*0.4usec = 10msec
0021   009B           00064         MOVWF   CCPR2L          ;Set CCPR2L register
0022   3001           00065         MOVLW   B'00000001'     ;Pre=1:1 TMR1=Int TMR1=ON
0023   0090           00066         MOVWF   T1CON           ;Set T1CON register
0024   300B           00067         MOVLW   B'00001011'     ;CCP2M=1011(Compare)
0025   009D           00068         MOVWF   CCP2CON         ;Set CCP2CON register
                      00069 
                      00070 ;*** Interruption control
0026   1683           00071         BSF     STATUS,RP0      ;Change to Bank1
0027   3001           00072         MOVLW   B'00000001'     ;CCP2IE=Enable
0028   008D           00073         MOVWF   PIE2            ;Set PIE2 register
0029   1283           00074         BCF     STATUS,RP0      ;Change to Bank0
002A   30C0           00075         MOVLW   B'11000000'     ;GIE=ON PEIE=ON
002B   008B           00076         MOVWF   INTCON          ;Set INTCON register
                      00077 
002C                  00078 WAIT
002C   282C           00079         GOTO    $               ;Interruption wait
                      00080 
                      00081 ;***************  Interruption Process  *****************
002D                  00082 INT
002D   018D           00083         CLRF    PIR2            ;Clear interruption flag
002E                  00084 AD_CHECK
002E   191F           00085         BTFSC   ADCON0,GO       ;A/D convert end ?
002F   282E           00086         GOTO    AD_CHECK        ;No. Again
0030   081E           00087         MOVFW   ADRESH          ;Read ADRESH register
0031   3C08           00088         SUBLW   SPEED           ;Ref speed - Detect speed
0032   1803           00089         BTFSC   STATUS,C        ;Reference < Detect ?
0033   2839           00090         GOTO    CHECK1          ;No. Jump to > or = check
                      00091 
                      00092 ;--- control to low speed ---
0034   0815           00093         MOVFW   CCPR1L          ;Read CCPR1L register
0035   3E01           00094         ADDLW   CHANGE          ;Change value + CCPR1L
0036   1C03           00095         BTFSS   STATUS,C        ;Overflow ?
0037   0095           00096         MOVWF   CCPR1L          ;No. Write CCPR1L
0038   2840           00097         GOTO    LED_CONT        ;Jump to LED control
                      00098 
0039                  00099 CHECK1
0039   1903           00100         BTFSC   STATUS,Z        ;Reference = Detect ?
003A   2840           00101         GOTO    LED_CONT        ;Yes. Jump to LED control
                      00102 
                      00103 ;--- control to fast speed ---
MPASM 02.50.02 Intermediate     MOTOR.ASM   11-24-2001  19:54:44         PAGE  3


LOC  OBJECT CODE     LINE SOURCE TEXT
  VALUE

003B   3001           00104         MOVLW   CHANGE          ;Set change value
003C   0295           00105         SUBWF   CCPR1L,F        ;CCPR1L - Change value
003D   1803           00106         BTFSC   STATUS,C        ;Underflow ?
003E   2840           00107         GOTO    LED_CONT        ;Jump to LED control
003F   0195           00108         CLRF    CCPR1L          ;Set fastest speed
                      00109 
                      00110 ;****************  LED control Process ******************
0040                  00111 LED_CONT
0040   0915           00112         COMF    CCPR1L,W        ;Complement CCPR1L bit
0041   00A0           00113         MOVWF   LED             ;Save LED data
0042   3010           00114         MOVLW   B'00010000'     ;Set compare data
0043   0220           00115         SUBWF   LED,W           ;LED - data
0044   1803           00116         BTFSC   STATUS,C        ;Under ?
0045   2848           00117         GOTO    LED1            ;No. 
0046   3000           00118         MOVLW   B'00000000'     ;Set LED control data
0047   2873           00119         GOTO    INT_END         ;Jump to interrupt end
0048   3020           00120 LED1    MOVLW   B'00100000'     ;Set compare data
0049   0220           00121         SUBWF   LED,W           ;LED - data
004A   1803           00122         BTFSC   STATUS,C        ;Under ?
004B   284E           00123         GOTO    LED2            ;No. 
004C   3001           00124         MOVLW   B'00000001'     ;Set LED control data
004D   2873           00125         GOTO    INT_END         ;Jump to interrupt end
004E   3040           00126 LED2    MOVLW   B'01000000'     ;Set compare data
004F   0220           00127         SUBWF   LED,W           ;LED - data
0050   1803           00128         BTFSC   STATUS,C        ;Under ?
0051   2854           00129         GOTO    LED3            ;No. 
0052   3003           00130         MOVLW   B'00000011'     ;Set LED control data
0053   2873           00131         GOTO    INT_END         ;Jump to interrupt end
0054   3060           00132 LED3    MOVLW   B'01100000'     ;Set compare data
0055   0220           00133         SUBWF   LED,W           ;LED - data
0056   1803           00134         BTFSC   STATUS,C        ;Under ?
0057   285A           00135         GOTO    LED4            ;No. 
0058   3007           00136         MOVLW   B'00000111'     ;Set LED control data
0059   2873           00137         GOTO    INT_END         ;Jump to interrupt end
005A   3080           00138 LED4    MOVLW   B'10000000'     ;Set compare data
005B   0220           00139         SUBWF   LED,W           ;LED - data
005C   1803           00140         BTFSC   STATUS,C        ;Under ?
005D   2860           00141         GOTO    LED5            ;No. 
005E   300F           00142         MOVLW   B'00001111'     ;Set LED control data
005F   2873           00143         GOTO    INT_END         ;Jump to interrupt end
0060   30A0           00144 LED5    MOVLW   B'10100000'     ;Set compare data
0061   0220           00145         SUBWF   LED,W           ;LED - data
0062   1803           00146         BTFSC   STATUS,C        ;Under ?
0063   2866           00147         GOTO    LED6            ;No. 
0064   301F           00148         MOVLW   B'00011111'     ;Set LED control data
0065   2873           00149         GOTO    INT_END         ;Jump to interrupt end
0066   30C0           00150 LED6    MOVLW   B'11000000'     ;Set compare data
0067   0220           00151         SUBWF   LED,W           ;LED - data
0068   1803           00152         BTFSC   STATUS,C        ;Under ?
0069   286C           00153         GOTO    LED7            ;No. 
006A   303F           00154         MOVLW   B'00111111'     ;Set LED control data
006B   2873           00155         GOTO    INT_END         ;Jump to interrupt end
006C   30E0           00156 LED7    MOVLW   B'11100000'     ;Set compare data
MPASM 02.50.02 Intermediate     MOTOR.ASM   11-24-2001  19:54:44         PAGE  4


LOC  OBJECT CODE     LINE SOURCE TEXT
  VALUE

006D   0220           00157         SUBWF   LED,W           ;LED - data
006E   1803           00158         BTFSC   STATUS,C        ;Under ?
006F   2872           00159         GOTO    LED8            ;No. 
0070   307F           00160         MOVLW   B'01111111'     ;Set LED control data
0071   2873           00161         GOTO    INT_END         ;Jump to interrupt end
0072   30FF           00162 LED8    MOVLW   B'11111111'     ;Set LED control data
                      00163 
                      00164 ;************  END of Interruption Process **************
0073                  00165 INT_END
0073   0086           00166         MOVWF   PORTB           ;Set PROTB
0074   0009           00167         RETFIE
                      00168 
                      00169 ;********************************************************
                      00170 ;            END of DC motor speed controller
                      00171 ;********************************************************
                      00172 
                      00173         END
MPASM 02.50.02 Intermediate     MOTOR.ASM   11-24-2001  19:54:44         PAGE  5

The label list has been deleted.

MEMORY USAGE MAP ('X' = Used,  '-' = Unused)

0000 : X---XXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXX-----------
2000 : -------X-------- ---------------- ---------------- ----------------

All other memory blocks unused.

Program Memory Words Used:   114
Program Memory Words Free:  3982


Errors   :     0
Warnings :     0 reported,     0 suppressed
Messages :     0 reported,     6 suppressed

motor_source.zip
motor_hex.zip