forked from KentuckySolarCar/Drivetek_MPPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwm.asm
More file actions
175 lines (137 loc) · 5.41 KB
/
pwm.asm
File metadata and controls
175 lines (137 loc) · 5.41 KB
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
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
174
;**********************************************************************
; P R O G R A M M MPPTnG *
; *
; Traking program for MPPT new generation *
;**********************************************************************
; *
; Filename: pwm.asm *
; Date: 15.11.00 *
; Last Update: 03.01.03 *
; File Version: V3.0 *
; *
; Author: M. Zuercher *
; *
; Company: HTA-Biel/Bienne Indulab *
; *
; Changes: added PWM interrupt to start ADC *
; *
;**********************************************************************
; *
; Notes: *
; - Main program : mpptng.asm *
; *
;**********************************************************************
; *
; Config: *
; *
; PWM1 -> CCP1 (pin 36) -> Aux pulse (Sa') *
; Duty cycle : 016h (0000010110b) *
; *
; PWM2 -> CCP2 (pin 35) -> Duty cycle (Sm') *
; Duty cycle : 190h (0110010000b) *
; *
;**********************************************************************
;**********************************************************************
; INIT PWM MODULES
;**********************************************************************
InitPWM BANK1
movlw PERIODE ; set PWMs period
movwf PR2
BANK0
movlw AUX_PW ; PWM1 duty cycle (Sa')
movwf CCPR1L
bsf CCP1CON,5 ; These are the 2 LSBs of the PWM duty cycle
bcf CCP1CON,4
movlw INITDUTY ; PWM2 duty cycle (Sm')
movwf DUTY2_H ; init duty variable
movwf CCPR2L
bsf TRISC,1
bsf TRISC,2
movlw B'11110100' ; Timer2 on / prescaler is 1 / postscaler is 16
movwf T2CON
BANK1
bcf PIE1_P,TMR2IE ; Timer 2 to PR2 match interrupt disable
BANK0
call PWMoff
RETURN
;**********************************************************************
; Turn PWMs on, HS-FET remains off
;**********************************************************************
PWMon nop
bsf CCP1CON,3 ; set PWM1 mode
bsf CCP1CON,2
bsf CCP2CON,3 ; set PWM2 mode
bsf CCP2CON,2
bcf ENA_FET
RETURN
;**********************************************************************
; Turn PWMs and HS-FET off
;**********************************************************************
PWMoff nop
nop
bcf CCP1CON,3 ; set PWM1 mode
bcf CCP1CON,2
bcf CCP2CON,3 ; set PWM2 mode
bcf CCP2CON,2
bcf PORTC,2 ; turn off AUX_PUL
bcf PORTC,1 ; turn off Duty Cycle
bsf ENA_FET
RETURN
;**********************************************************************
; Load duty cycle of PWM2
;
; IN: DUTY2_H, DUTY2_L (xxxxxxxx xx000000)
;**********************************************************************
LoadDuty movf DUTY2_H,W
movwf CCPR2L ; write H-Bite of Duty
rrf DUTY2_L,F ; rotate the 2 L-Bits by 2
rrf DUTY2_L,W
rlf DUTY2_L,F ; restore DUTY2_L
andlw B'00110000' ; clear all bits except Bit4 and 5
bcf CCP2CON,4
bcf CCP2CON,5
iorwf CCP2CON,F ; write L-Bits of Duty
EndLoad RETURN
;**********************************************************************
; Increment / Decrement duty cycle of PWM2
;
; IN: DUTY2_H, DUTY2_L (xxxxxxxx xx000000)
;**********************************************************************
IncDuty BANK1
movf STEP_L,w
BANK0
addwf DUTY2_L,W
andlw B'11000000' ; mask L-Byte
movwf DUTY2_L
btfsc _C
incf DUTY2_H,F ; skip if flag C (carry) = 0
BANK1
movf STEP_H,w
BANK0
addwf DUTY2_H,F ; DUTY2_H:DUTY2_L = DUTY2_H:DUTY2_L + STEP_H:STEP_L
call LoadDuty
RETURN
;----------------------------------------------------------------------
DecDuty BANK1
movf STEP_L,w
BANK0
subwf DUTY2_L,W
andlw B'11000000' ; mask L-Byte
movwf DUTY2_L
btfss _C
decf DUTY2_H,F ; skip if flag C (borrow) = 1
BANK1
movf STEP_H,w
BANK0
subwf DUTY2_H,F ; DUTY2_H:DUTY2_L = DUTY2_H:DUTY2_L - STEP_H:STEP_L
; lower limitat of duty
jmpFgtL DUTY2_H,DUTY2_MIN_H,WhitinLimit ; don't limit, if greater
jmpFltL DUTY2_H,DUTY2_MIN_H,LimitIt ; limit, if lower
jmpFgeL DUTY2_L,DUTY2_MIN_L,WhitinLimit ; consider L-Bytes
LimitIt movlw DUTY2_MIN_H
movwf DUTY2_H
movlw DUTY2_MIN_L
andlw B'11000000' ; mask L-Byte
movwf DUTY2_L ; duty = duty_max
WhitinLimit call LoadDuty
RETURN