-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrushed_motor.asm
117 lines (99 loc) · 3.05 KB
/
brushed_motor.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
; Driving Brushed 5V DC Motor using PIC 10F206
; Speed can be adjusted using Up/Down push buttons
;
; Copyright (C) Henryk Paluch, hpaluch at seznam dot cz, http://henryx.info
; Few ideas taken from Gooligum Tutorials at
; http://www.gooligum.com.au/tutorials.html
; defaults
CONSTANT DEF_DUTY = .200 ; default Duty ratio, 128 = 50%
; smaller value => more power,
; greater value => less power
; allowed values 1 to 255
LIST P=PIC10F206
INCLUDE <P10F206.INC>
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF
; PIN assignment
CONSTANT nMOTOR = GP2 ; motor Drive via MOSFET P-channel, LOG0 Active
CONSTANT nDown = GP1 ; input for Down button, LOG0 Active, PIC Pull-Up
CONSTANT nUp = GP0 ; input for Down button, LOG0 Active, PIC Pull-Up
; *** general File Registers
MY_DATA UDATA
sGPIO RES 1
vDuty RES 1 ; Duty ratio, 128 = 50%
cWaitPWM RES 1 ; counter for delay in WaitPWM
cDuty RES 1 ; current duty counter
;***** RC CALIBRATION
RCCAL CODE 0x1FF ; processor reset vector
RES 1 ; holds internal RC cal value, as a movlw k
;**** RESET, Page 0
RES_VECT CODE 0x0000 ; processor reset vector
MOVWF OSCCAL
GOTO START ; go to beginning of program
;**** CALLS must be in Page 0
ProcessUpKey
BTFSS GPIO,nUp
DECF vDuty,f ; now realy decrement vDuty
RETLW 0
ProcessDownKey
BTFSS GPIO,nDown
INCF vDuty,f ; now realy decrement vDuty
RETLW 0
; Wait about 124uS
WaitPWM
MOVLW .29
MOVWF cWaitPWM
w1
NOP
DECFSZ cWaitPWM,f
GOTO w1
RETLW 0
MAIN_PROG CODE ; let linker place main program
START
MOVLW DEF_DUTY
MOVWF vDuty
MOVLW ~(1<<T0CS | 1 << NOT_GPPU) ; clear T0CS to enable GP2 pin
; and enable Pull-Up on Inputs
OPTION
MOVLW ~(1<<CMPON)
MOVWF CMCON0 ; Turn Off Comparator to enable GP1 & GP0
MOVLW ~0 ; all outputs inacitve (LOG1)
MOVWF sGPIO
MOVWF GPIO ; Motor Off
MOVLW ~(1<<TRISIO2 ) ; GP2 OUTPUT, other are Inputs
TRIS GPIO
MOVF sGPIO,w
MOVWF GPIO
; main motor drive loop
MY_LOOP
CLRF cDuty
wOff
; wait for 128us
CALL WaitPWM
INCF cDuty,f
MOVF cDuty,w
XORWF vDuty,w
BTFSS STATUS,Z
GOTO wOff
; active duty starts here
BCF sGPIO,nMOTOR
MOVF sGPIO,w
MOVWF GPIO
wOn
; wait for 128us
CALL WaitPWM
; wOn
INCFSZ cDuty,f ; wait on Duty till cDuty overflows...
GOTO wOn
; turn duty inactive again
BSF sGPIO,nMOTOR
MOVF sGPIO,w
MOVWF GPIO
; check keys/ajdust vDuty
; Possible Up Key (vDuty decreases)
DECFSZ vDuty,w ; just test vDuty -1 (in w)
CALL ProcessUpKey
; Possible Down Key (vDuty increases)
INCFSZ vDuty,w ; just test vDuty +1
CALL ProcessDownKey
GOTO MY_LOOP ; loop forever
END