Professional Documents
Culture Documents
KHOA IN IN T
LUN VN TT NGHIP I HC
SVTH
MSSV
CBHD
B MN
TP H Ch Minh, 01/2007
ii
iii
LI CM N !
iv
MC LC
CHNG 1:
GII THIU V NG C KB V PHNG PHP IU KHIN
2
2
CHNG 2:
GII THIU V BIN TN NGUN P IU KHIN V/f=const
5
5
CHNG 3:
GII THIU V PIC Microcontrollers (MCUs)
22
22
CHNG 4 :
THIT K PHN CNG
51
51
4.1> YU CU C BN : .......................................................................................51
4.2> S KHI CA H THNG : ....................................................................52
4.3> MCH NG LC : .......................................................................................53
4.3.1) B chnh lu:..............................................................................................53
4.3.2) B nghch lu:............................................................................................54
4.3.3) Mch li ( driver) & cch ly: .......................................................................55
4.2> MCH IU KHIN: .......................................................................................59
CHNG 5:
LP TRNH
60
60
CHNG 6:
KT QU T C
64
64
CHNG 7:
TI LIU THAM KHO
CHNG 8:
PH LC
69
69
70
70
vi
CHNG 1:
GII THIU V NG C KB V PHNG PHP IU KHIN
1.1> TNG QUAN V NG C NG B:
1.1.1) Gii thiu:
CHNG 2:
GII THIU V BIN TN NGUN P IU KHIN V/f=const
2.1> BIN TN NGUN P:
c s dng hu ht trong cc bin tn hin nay. Tc ca ng c khng
ng b t l trc tip vi tn s ngun cung cp. Do , nu thay i tn s ca
ngun cung cp cho ng c th cng s thay i c tc ng b, v tng
ng l tc ca ng c.
Tuy nhin, nu ch thay i tn s m vn gi nguyn bin ngun p cp
cho ng c s lm cho mch t ca ng c b bo ha. iu ny dn n dng t
ha tng, mo dng in p v dng in cung cp cho ng c gy ra tn hao li
t, tn hao ng trong dy qun Stator. Ngc li, nu t thng gim di nh mc
s lm gim moment ca ng c.
V vy, khi gim tn s ngun cung cp cho ng c nh hn tn s nh mc
thng i i vi gim in p cung cp cho ng c. V khi ng c hot ng vi
tn s nh mc th in p ng c c gi khng i v bng nh mc do gii
hn ca cch in ca Stator cng nh ca in p ngun cung cp, moment ca
ng c s b gim.
2.2> PHNG PHP IU KHIN V/f:
2.2.1) Phng php E/f
Ta c cng thc sau:
a=
(2.1)
fm
+ Vi f: tn s hot ng ca ng c,
+ fm: tn s nh mc ca ng c.
Gi s ng c hot ng di tn s nh mc (a<1). T thng ng c c gi
gi tr khng i. Do t thng ca ng c ph thuc vo dng t ha ca ng
c, nn t thng c gi khng i khi dng t ha c gi khng i ti mi
im lm vic ca ng c.
Ta c phng trnh tnh dng t ha ti im lm vic nh mc nh sau:
Im =
Em
1
.
fm 2L m
(2.2)
+ Vi Lm l in cm mch t ha
Ti tn s lm vic f:
Im =
E
1
.
a.fm 2L m
(2.3)
(2.4)
R'
2
Vm
. 2
3
s
M=
.
2
b
'
R 1 + R 2 + X1 + X '2
s
(2.5)
V moment cc i ch nh mc:
Mmax
2
Vm
3
.
=
2.b
R 1 R 12 + X1 + X '2
(2.6)
R'
Vm2 . 2
3
;(a < 1)
a
s
.
M=
.
2
'
b R1 R2
2
+ + X1 + X '
2
a as
(2.7)
Mmax =
3
2.b
Vm
.
2
R
R
1 1 + X1 + X '2
a
a
, a < 1
(2.8)
Vdc
(2.9)
2
V
Vcontrol < Vtri th VAO = dc
2
Nh vy, to ra ngun in 3 pha dng iu rng xung, ta cn c ngun sin
3 pha mu v gin kch ng ca 3 pha s c biu din nh hnh v di y:
Vcontrol > Vtri th VAO =
Hnh 2.3: nguyn l ca phng php iu rng SIN 3 pha v dng sng in p ng
ra
2.3.2) Cc cng thc tnh ton:
Ta cn tnh c bin hi bc nht ca in p ng ra t t s bin gia
sng mang v sng tam gic
Ta c cng thc sau tnh bin ca hi bc nht:
USIN(1) = ma.
UDC
2
(2.10)
Trong ma l t s gia bin sng sin mu v bin sng mang cn gi
l t s iu bin.
ma =
USINsmp
(2.11)
Ucarry
S0
S2
S4
10
Bng tm tt :
Vector
in
p
V0
V1
V2
V3
V4
V5
V6
V7
Trng thi ca cc
kha
Q1
Q3
Q5
0
0
0
1
0
0
1
1
0
0
1
0
0
1
1
0
0
1
1
0
1
1
1
1
in p pha
Van
0
2/3
1/3
-1/3
-2/3
-1/3
1/3
0
Vbn
0
-1/3
1/3
2/3
1/3
-1/3
-2/3
0
in p dy
Vcn
0
-1/3
-2/3
-1/3
1/3
2/3
1/3
0
Vab
0
1
0
-1
-1
0
1
0
Vbc
0
0
1
1
0
-1
-1
0
Vca
0
-1
-1
0
1
1
0
0
11
u (t ) =
2
ua + ub .e j (2 / 3) + uc .e j (2 / 3) )
(
3
(2.13)
+VDC/2
S1
a
Ra
S5
S3
b
c
N
S0
S2
Rb
S4
-VDC/2
Rc
-VDC/2
Ra
Rb
Rc
uur uur
uur uur uur
Xt trn h ta : trong Vs = V 1 = K * (Va + Vb + Vc) ; K=2/3 l h s
bin hnh
Vb
12
13
Hnh 2.12: in p 3 pha ng ra trong min thi gian tng ng Hnh 2.11
uur
Vector Vs lin quan n cc trng thi kha transtior trong b bin tn ngun
p VSI ( Voltage Source Inverter). Trong phng php SVM th VSI cuung
ngt
r
tn s rt ln (FPWM). FPWM quyt nh thi gian ly mu Ts cho vector Vs ( Ts=1/
FPWM)
uur
C rt nhiu cch ng ngt cc kha BJT to ra vector Vs t cc vector
uur uur uur uur uur uur uur uur
V 0 ; V1 ; V 2 ; V 3 ; V 4 ; V 5 ; V 6 ; V 7 .
14
(2.14)
(2.15)
m=
Vs
2
Vdc
3
(2.16)
Chiu phng trnh (2.14) ln trc X - Y ; s dng thm phng trnh (2.16) v
t s m (2.15)
15
T1 = Ts . 3 m.sin ( / 3 )
m.sin ( )
T2 = Ts .
3
T07 = Ts T1 T2
(2.17)
17
18
2.4.2) S tm tt ca qu trnh iu ch :
T1 = Ts . 3 m.sin ( / 3 )
m.sin ( )
T2 = Ts .
3
T07 = Ts T1 T2
Trong :
+ m=
Vs
l t s iu bin
2
Vdc
3
+ l gc lch gia VA v VB
19
(2.18)
2.4.3) Tnh ton gc update ca vector Vs theo phng php iu khin V/f:
TP
W
M
TPWM
min =
60
()
n
(2.19)
2) Ti tn s t f => ( T=1/f):
Vector Vs quay 360 trong thi gian T
Vector Vs quay ? trong thi gian TPWM
=>
'=
3) Xy dng
TPWM
360
T
() : gc update ca vector Vs
(2.20)
20
TPWM
60
360 = K *
T
n
T
n
=> K = PWM * 360 *
T
60
=>
M T=1/f
=> K =
1
f PWM
.360.
n
.f
60
1
512
.360.
.f
5000
60
=> K = 0.6144 f = step size
=> K =
(2.21)
Ta c tn s f t thay i t 0 -> 60 Hz
=> K= (0 ->36.684 )
21
(2.22)
CHNG 3:
GII THIU V PIC Microcontrollers (MCUs)
3.1>TNG QUAN:
22
23
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
CHNG 4 :
THIT K PHN CNG
4.1> YU CU C BN :
Thit k b bin tn truyn thng ( 6 kha) ba pha iu khin ng c KB 1.5 kW
Thng s tiu biu ca ng c 1.5 kW ( 2 HP) tn s 50 Hz nh sau :
Pm
Vm
Im
cos
RPM
Cc thng s
Cng sut nh mc
in p nh mc
Dng in nh mc
H s cng sut
Vn tc
n v
(KW)
(Vac)
(A)
( vng /pht)
51
ng c u / sao
1.5
380/220
5.9/3.4
0.81
1420
B nghch lu
+
VDC
-
Mch li
Cch ly
Tn hiu
xung kch
Bin tr
Nt n
PIC
LEDs
RS 232
52
3 phase
AC motor
4.3> MCH NG LC :
4.3.1) B chnh lu:
Yu cu:
+ ng c vn hnh ch nh mc ( ) tr bin
V pha = (220 * 2) / 3
uuuuur
=>VDC 3 * V pha 311(V )
53
9 Ghi ch:
Trong iu kin thc t, nu ch c ngun 1 pha thc hin chnh lu th in
p VDC sau chnh lu :
VDC =
2 2 *Vpha
mc c hai ch
C hai la chn chnh cho vic s dng kho ng ct cng sut trong iu
khin ng c l MOSFET v IGBT. C hai loi MOSFET v IGBT u l linh kin
c iu khin bng in p, ngha l vic dn v ngng dn ca linh kin c
iu khin bng mt ngun in p ni vi cc gate ca linh kin thay v l dng
in trong cc b nghch lu s dng transitor nh trc y. V vy cch s dng
loi linh kin ny lm cho vic iu khin tr nn d dng hn.
54
55
56
+ Bin p xung :
Gm mt cun dy s cp v c th nhiu cun th cp. Vi nhiu cun dy
pha th cp, ta c th kch ng nhiu transistor mc ni tip hoc song song.
Bin p xung cn c cm khng tn nh v p ng nhanh. Trong trng hp
xung iu khin c cnh tc ng ko di hoc tn s thp, bin p xung sm t
trng thi bo ha v ng ra ca n khng ph hp yu cu iu khin.
+ Opto :
Gm ngun pht tia hng ngoi dng diode (IR - LED) v mch thu dng
phototransistor. Do tha mn yu cu cch ly v in, ng thi p ng ca
opto tt hn my bin p xung.
57
58
59
CHNG 5: LP TRNH
CHNG 5:
LP TRNH
5.1> GII THUT LP TRNH :
5.1.1) Chng trnh chnh:
60
CHNG 5: LP TRNH
61
CHNG 5: LP TRNH
STOP button: => set cc duty cycle v zero => qua li v tr (4) : IDLE
F/R button: => gi hm RAM_DOWN gim tc ng c v zero => o chiu
quay vector Vs => gi hm RAM_UP tng tc ng c n tn s t
..
62
CHNG 5: LP TRNH
63
CHNG 6: KT QU T C
CHNG 6:
KT QU T C
6.1> PHN CNG:
6.1.1> Mch ng lc:
64
CHNG 6: KT QU T C
+ Gii php khc phc:
- Nhit cc kha cng sut kh cao => thay th cc kha cng sut bng
loi cht lng tt, p ng tt hn .
- Pht trin thm khu hi tip dng => ngn chn qu dng ng c
- Pht trin khu hi tip tc => iu khin vng kn ng c
- Pht trin khu hi tip nhit ca kha cng sut => ngn chn hin
tng qu nhit
u im:
Mch iu khin c kh nng p ng cc yu cu iu khin ng c trong
thc t:
+
65
CHNG 6: KT QU T C
]
Hnh 6.3: phn mm iu khin
66
CHNG 6: KT QU T C
6.2.2) M t:
Phn mm iu khin c vit trn ngn ng Visiual Basic 6.0
Phn mm giao tip vi vi x l PIC18F thng qua cng COM ( chun RS232)
Cc nt iu khin:
RUN / SEND: Khi ng ng c / gi tn s yu cu n vi x l
+
STOP: dng ng c
+
CHANGE: o chiu ng c
+
Cc hin th v nhp liu:
f request: nhp liu tn s t bn phm
+
f out: hin th gi tr tn s ng ra
+
V out: hin th dng in p ng ra ( V/f = const)
+
Status: hin th trng thi ng c ( RUNNING , STOP)
+
Direction: hin th chiu quay ca ng c ( thun ; nghch )
+
67
CHNG 6: KT QU T C
Hnh 6.7: in p dy ng ra ( ti ng c)
68
CHNG 7:
69
CHNG 8: PH LC
CHNG 8:
PH LC
8.1> S MCH (V TRN ORCAD):
8.1.1) S mch cch ly
5V_1
5V_2
J8
R33
1
2
3
4
5
6
7
U10
220
1
2
3
4
R34
220
R35
R42
110
110
+ C7
0.1uF
Vin1+ VCC
Vin1- Vout1
Vin2- Vout2
Vin2+ GND
HCPL2631
0
5V_1
5V_2
R37
220
U11
1
2
3
4
R38
220
Vin1+ VCC
Vin1- Vout1
Vin2- Vout2
Vin2+ GND
R43
R44
110
110
+ C8
0.1uF
7
6
5
HCPL2631
5V_1
5V_2
+ C9
R40
220
U12
1
2
3
4
R41
220
Vin1+ VCC
Vin1- Vout1
Vin2- Vout2
Vin2+ GND
8
7
6
5
HCPL2631
70
R36
R45
110
110
0.1uF
H1
H2
H3
L1
L2
L3
CHNG 8: PH LC
5V_1
J5
6V_AC
D15
OUT
3
D17
LED
C11
C12
470uF
100uF
C13
10uF
C16
104
4 -
1
2
IN
+ 1
LM7805C
GND
U3
BRIDGE_3A
R1
330
15V
J6
12V_AC
D16
OUT
3
D18
LED
C10
C14
470uF
100uF
C15
10uF
C17
104
R2
1k
4 -
1
2
IN
+ 1
GND
U4 LM7815C
BRIDGE_3A
5V_2
J7
6V_AC
IN
OUT
3
D20
LED
C18
C19
470uF
100uF
C20
10uF
C21
104
4 -
1
2
D19
LM7805C
+ 1
GND
U5
BRIDGE_3A
71
R3
330
CHNG 8: PH LC
HO1
VS1
HO2
VS2
HO3
VS3
LO1
LO2
LO3
COM
D2 DIODE XUNG
D1 DIODE XUNG
H1
H2
H3
R26
R27
R28
100
100
100
1
2
3
4
L1
L2
L3
R29
R30
R31
100
100
100
5
6
7
FAULT
ENABLE
8
10
VCC
HIN1
HIN2
HIN3
VB1
U1
HO1
VS1
LIN1
LIN2
LIN3
VB2
FAULT
EN
HO2
VS2
VB3
15V
HO3
15V
+ C6
10uF
R32
100
VS3
25
17
21
11
ITRIP
+
C4
.1uF
9
12
25
17
21
LO1
LO2
RCIN
LO3
ITRIP
VSS
COM
ITRIP
28
27
24
23
1R 5W
COM
15V
ENABLE
FAULT
ITRIP
20
19
C3
1uF
18
R20
100
R21
100
R22
100
HO1
VS1
HO2
VS2
HO3
VS3
16
R23
100 LO1
15
R24
100 LO2
14
R25
100 LO3
COM
13
12V
RVAR2
ENABLE
72
C2
1uF
22
PR
J3
CONTROL
1
2
3
4
5
C1
1uF
26
IR2136
RVAR1
J1
OUTPUTS
1
2
3
4
5
6
7
8
9
10
C5
10uF
CHNG 8: PH LC
R2
10K
S1
2
Q3
R1
10K
H2
H3
R3
10K
S2
Q5
1
3
Q1
H1
FUSE
F1
VDC
J1
S3
P1
P2
P3
J2
1
2
P1
CON10
R4
10K
Q6
L2
1
R5
10K
COM
VDC
D1
D3
J4
2
1
AC_VOLTAGE
C1
C
D2
D4
COM
73
Q4
L1
P3
1
2
3
4
5
6
7
8
9
10
H1
S1
H2
S2
H3
S3
L1
L2
L3
COM
MOTOR
P2
J3CON2
L3
Q2
1
R6
10K
VDC
COM
1
2
3
CHNG 8: PH LC
VCC
VCC
C_Vcc1
C_Vcc2
104
ZENNER
104
U16_1
3 MCRL
SW_RESET
PORTA_1 MCRL
6 RA0
6 5 RA1
5 4 RA2
4 3 RA3
3 2 RA4
2 1 RA5
1
RUN
FR
STOP
22P
C3
Y2
22P
1
2
3
4
5
6
7
8
9
10
VCC 11
12
OSC1
OSC2
C4
MCRL
VCC
PGD
PGC
Programing conector
R_RESET_SW
1K
CON6N
1
1 2
2 3
3 4
4 5
5 6
6
VCC
OSC1
20MHZ
OSC2
1
2
3
4
5
6
7
8
1 MODE
2 ENTER
3 UP
4 DOWN
5 LEFT
6 RIGHT
7RC6
8RC7
PORTC_1
13
14
15
16
17
18
23
24
25
26
MCRL
RA0/AN0
RA1/AN1
RA2/AN2
RA3/AN3
RA4/AN4
RA5/AN5
RB7/PGD
RB6/PGC
RB5/PWM4
RB4/PWM5
RB3/PWM3
RB2/PWM2
RB1/PWM1
RB0/PWM0
RE0/AN6
RE1/AN7
RE2/AN8
40
39
38
37
36
35
34
33
PWM4
PWM5
PWM3
PWM2
PWM1
PWM0
PGD 8
PGC 7
6
5
4
3
2
1
PORTB_1
8
7
6
5
4
3
2
1
PORTB_PWM
7
PWM5 6
PWM4 5
PWM3 4
PWM2 3
PWM1 2
PWM0 1
PORTB_1
Vdd
Vss
Vdd
Vss
OSC1/RA7
OSC2/RA8
RC0
RC1/CCP2
RC2/CCP1
RC3/INT0
RC4/SDA
RC5/SCK
RD6/TX/CK
RC7/RX/DT
RD7/PWM7
RD6/PWM6
RD5/PWM4
RD4
RD3/SCK
RD2/SDI
RD1/SDO
RD0
32
31
PORTB_PWM
VCC
30
29
28
27
22
21
20
19
PIC18F4431
D7 8
D6 7
D5 6
D4 5
D3 4
D2 3
D1 2
D0 1
PORTD_1
8
7
6
5
4
3
2
1
PORTD_1
SW_DIP
LED8
LED7
LED6
LED5
LED4
LED3
LED2
LED1
D7
D6
D5
D4
D3
D2
D1
D0
SW DIP-8
74
7
6
5
4
3
2
1
CHNG 8: PH LC
VCC
D2
D1
D0
D7
D6
D5
D4
U18
2
10k
3
2
1
E
R/W
RS
Vee
VCC
GND
6
5
4
14
13
12
11
10
9
8
7
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Lam+
Lamp-
16
15
VCC
U17
LCD
16
VCC
C+
1UF
C_C2
T2IN
R2IN
RS3
13
T1IN
T1OUT
GND
14
R1IN
MAX232
R1OUT
10
11
RC6
12
RC7
RS3
RS2
U7
9
8
7
6
COM9NS
15
C_C5
C_cocuc
RS2
C_C4
C_cocuc
T2OUT
C2-
C_cocuc
R2OUT
C2+
C_C3
V+
V-
C1-
4
1UF
VCC
U10
1
C_1
BUTTON
VCC
VCC
R_RUNSW
4k7
C_FRSW
VCC
VCC
R_UPSW
4k7
UP
SW_UP
C_MODESW
STOP
4k7
DOWN
SW_DOWN
75
4k7
SW_STOP
MODE
SW_MODE
VCC
VCC
R_DOWNSW
R_MODESW
104
C_DOWNSW
104
4k7
SW_FR
VCC
R_STOPSW
104
FR
104
C_STOPSW
4k7
SW_RUN
C_UPSW
R_FRSW
104
RUN
VCC
C_LEFTSW
R_LEFTSW
4k7
104
LEFT
SW_LEFT
C_RIGHTSW
R_RIGHTSW
4k7
104
RIGHT
SW_RIGHT
CHNG 8: PH LC
76
CHNG 8: PH LC
1573,1579,1586,1592,1598,1605,1611,1617,1623,1630,
1636,1642,1648,1655,1661,1667,1673,1680,1686,1692,
1698,1704,1710,1717,1723,1729,1735,1741,1747,1754,
1760,1766,1772,1778,1784,1790,1796,1802,1808,1814,
1820,1826,1832,1839,1845,1851,1857,1863,1868,1874,
1880,1886,1892,1898,1904,1910,1916,1922,1928,1934,
1940,1946,1951,1957,1963,1969,1975,1981,1986,1992,
1998,2004,2010,2015,2021,2027,2033,2038,2044,2050,
2056,2061,2067,2073,2078,2084,2090,2095,2101,2107,
2112,2118,2124,2129,2135,2140,2146,2151,2157,2163,
2168,2174,2179,2185,2190,2196,2201,2207,2212,2218,
2223,2228,2234,2239,2245,2250,2256,2261,2266,2272,
2277,2282,2288,2293,22982304,2309,2314,2320,2325,
2330,2335,2341,2346,2351,2356,2361,2367,2372,2377,
2382,2387,2392,2398,2403,2408,2413,2418,2423,2428,
2433,2438,2443,2448,2453,2458,2463,2468,2473,2478,
2483,2488,2493,2498,2503,2508,2513,2518,2522,2527,
2532,2537,2542,2547,2551,2556,2561,2566,2571,2575,
2580, 2585,2589,2594,2599,2604,2608,2613,2618,2622,
2627,2631,2636,2641,2645,2650,2654,2659,2664,2668,
2673,2677,2682,2686,2691,2695,2699,2704,2708,2713,
2717,2722,2726,2730,2735,2739,2743,2748,2752,2756,
2761,2765,2769,2773,2778,2782,2786,2790,2795,2799,
2803,2807,2811,2815,2820,28242828,2832,2836,2840,
2844,2848,2852,2856,2860,2864,28682872,2876,2880,
2884,2888,2892,2896,2900,2903,2907,2911,2915,2919,
2923,2927,2930,2934,2938,2942,2945,2949,2953,2957,
2960,2964,2968,2971,2975,2978,2982,2986,2989,2993,
2996,3000
};
#define
#define
#define
RUN
FR
STOP
PIN_C0
PIN_E0
PIN_E1
#define
#define
#define
#define
#define
#define
MENU
OK
UP
DOWN
BACK
NEXT
PIN_E2
PIN_C1
PIN_C2
PIN_C3
PIN_C4
PIN_C5
//--------------------------------------------------------caculation varible
float
f_float=0,temp_float=0;
signed long
long
Vs_angle;
//long=int16
update_angle;
long
long
M,Vref,Vdc=311;
TS=2000,TA,TB,Tz;
//TS=PTPER*4
77
CHNG 8: PH LC
unsigned int
int
// Fpwm=5Khz=> PTPER=500;
// real TS is PTPER*0.2uS, when Fosc=20M )
//Tz= T0/2
temp_int=0,sector,adc,count_timer1_interupt;
f,f_req;
int1
first_run_flag,direction_flag;
disable_update_freq=0;
//1= active
#INT_PWMTB HIGH //It will generate code to save and restore the machine state,
and will clear the interrupt flag
void PWM_INTERRUPT()
//caculating base on "Vref" and "stepsize"
{
interrupt_number=interrupt_number+1;
TB=data[Vs_angle];
//data=PTPER*4*sqrt(3)*SIN ;
// at 1st RUN: n=0
TA=data[511-Vs_angle];
M=Vref*16/Vdc;
// mutiply 16=> will shift right 4 bit later
TA=TA*M;
TB=TB*M;
TA=(TA>>4)&0x0FFF;
//4TA
TB=(TB>>4)&0x0FFF;
//4TB
Tz=(TS-TA-TB)/2;
//TS=4TS
if(direction_flag==1)
//FORWARD direction//
{
Vs_angle=Vs_angle+update_angle;
if(Vs_angle>511)
{
Vs_angle=Vs_angle-511;
sector=sector+1;
if(sector>6)
//sector (1)->(6)
{
sector=1;
}
}
}
78
CHNG 8: PH LC
else
//REVERSE direction//
{
Vs_angle=Vs_angle-update_angle; //n<0 => overflow to value (2^16)"negative value" if n is unsigned int
if(Vs_angle<0)
{
Vs_angle=Vs_angle+511;
sector=sector-1;
if(sector==0) //sector (1)->(6)
{
sector=6;
}
}
}
switch (sector)
{
case 1:
set_power_pwm0_duty(TS-Tz);
set_power_pwm2_duty(Tz+TB);
set_power_pwm4_duty(Tz);
break;
case 2: set_power_pwm0_duty(TA+Tz);
set_power_pwm2_duty(TS-Tz);
set_power_pwm4_duty(Tz);
break;
case 3: set_power_pwm0_duty(Tz);
set_power_pwm2_duty(TS-Tz);
set_power_pwm4_duty(Tz+TB);
break;
case 4: set_power_pwm0_duty(Tz);
set_power_pwm2_duty(Tz+TA);
set_power_pwm4_duty(TS-Tz);
break;
case 5:
set_power_pwm0_duty(Tz+TB);
set_power_pwm2_duty(Tz);
set_power_pwm4_duty(TS-Tz);
break;
case 6:
set_power_pwm0_duty(TS-Tz);
set_power_pwm2_duty(Tz);
set_power_pwm4_duty(Tz+TA);
break;
}
}
#INT_TIMER1
void READ_AD_RESULT()
//With an internal clock at 20mhz and with the
T1_DIV_BY_8 mode, the timer will increment every 1.6us. It will overflow every
104.8576ms.
{
if(count_timer1_interupt==10) //
1s
{
adc=read_adc();
79
CHNG 8: PH LC
f_req=adc/4;
255 => f_req=1-60 (Hz)}
if(f_req>60)
{f_req=60;}
count_timer1_interupt=1;
}
if(disable_update_freq==1)
{count_timer1_interupt=9;}
else
{count_timer1_interupt=count_timer1_interupt+1;}
}
void PORTS_INIT()
//1
{
set_tris_a(0b00000001);
set_tris_b(0b11000000);
set_tris_c(0b11111111);
set_tris_d(0b00000000);
}
void PWM_MODULE_INIT()
//2
{
setup_power_pwm_pins(PWM_COMPLEMENTARY,PWM_COMPLEMENTAR
Y,PWM_COMPLEMENTARY,PWM_OFF );
//module 0(PWM0,PWM1) = COMPLEMENTARY
//module 1(PWM2,PWM3) = COMPLEMENTARY
//module 2(PWM4,PWM5) = COMPLEMENTARY
//module 3(PWM6,PWM7) = OFF
setup_power_pwm(PWM_CLOCK_DIV_4|PWM_UP_DOWN|PWM_DEAD_CL
OCK_DIV_4,1,0,500,0,1,10);
// 1) mode:PWM_CLOCK_DIV_4; PWM_UP_DOWN;
PWM_DEAD_CLOCK_DIV_4,
// 2) postscale:1
// 3) time_base:=> first value of timebase
// 4) period:chu ky` xung 6 PWM =500 =>200uS
// 5) compare:0
// 6) compare_postscale:1
// 7) dead_time:10 => Tdeatime=10*0.2=2uS
set_power_pwm0_duty(0);
set_power_pwm2_duty(0);
set_power_pwm4_duty(0);
}
void INTERRUPTS_INIT() //3
pressed !
80
CHNG 8: PH LC
{
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
}
void ADC_INIT()
//4
{
setup_adc_ports(sAN0);
//AN0 as analog INPUT
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(10);
}
void TIMER_INIT()
//5
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(62500);
//All timers count up. When a timer
reaches the maximum value it will flip over to 0 and continue counting (254, 255, 0, 1,
2...)
//62500*1.6 us =0.1s
}
void PARAs_CAL()
{
f_float=f;
temp_float=f_float*3;
Vref=temp_float;
//Vref(Vphase; motor in deltal mode) at f
frequency to maintain V/f=cont=(220*sqrt(2)/sqrt(3))/60=3
temp_float=0.6144*f_float;
update_angle=temp_float;
//0.6144=Tpwm*360*n/60 ; n=512
//stepsize is INTERGER after this line
}
void RAM_DOWN_SPEED()
{
while(f>f_req)
{
f=f-1;
PARAs_CAL();
delay_ms(T_ramp_down_ms);
// 0.05s/Hz
if(f<f_req)
{
f=f_req;
//f=f_req when ram speed finished !
}
switch(mode_select)
{
case 1:
lcd_gotoxy(4,0);
printf(lcd_putc,"%2.0d",f_req);
81
CHNG 8: PH LC
lcd_gotoxy(10,0);
printf(lcd_putc,"%2.0d",f);
break;
case 2:
lcd_gotoxy(10,0);
printf(lcd_putc,"%2.0d",f);
break;
case 3:
fputc(f); //send value of f for PC
lcd_gotoxy(4,0);
printf(lcd_putc,"%2.0d",f_req);
lcd_gotoxy(10,0);
printf(lcd_putc,"%2.0d",f);
break;
}//end switch
}
}
void RAM_UP_SPEED()
{
while(f<f_req)
{
f=f+1;
PARAs_CAL();
delay_ms(T_ramp_up_ms);
// 0.1s/Hz
if(f>f_req)
{
f=f_req;
//f=f_req when ram speed finished !
}
switch(mode_select)
{
case 1:
lcd_gotoxy(4,0);
printf(lcd_putc,"%2.0d",f_req);
lcd_gotoxy(10,0);
printf(lcd_putc,"%2.0d",f);
break;
case 2:
lcd_gotoxy(10,0);
printf(lcd_putc,"%2.0d",f);
break;
case 3:
fputc(f); //send value of f for PC
lcd_gotoxy(4,0);
printf(lcd_putc,"%2.0d",f_req);
lcd_gotoxy(10,0);
printf(lcd_putc,"%2.0d",f);
break;
}//end switch
82
CHNG 8: PH LC
}
}
void defaul_value_in_EEPROM()
//use in MODE2: AUTO ; P18F has 256 bytes
eeprom which address from 0x00 -> 0xFF
{
write_eeprom(0X00,100); //temp_eeprom for checking at 1st reading
write_eeprom(0X10,30); //f1
write_eeprom(0X20,60); //f2
write_eeprom(0X03,6);
//T_ramp_up
write_eeprom(0x04,3);
//T_ramp_down
}
/////////////////////////////////////////////////////////////////////
void MAIN ()
{
PORTS_INIT();
lcd_init();
PWM_MODULE_INIT();
INTERRUPTS_INIT();
ADC_INIT();
TIMER_INIT();
//1
// this subrotine in flex_LCD.C file
//2
//3
//4
//5
MODE_SELECT:
return_2_mode_select=0;
//return_2_mode_select=0 as default
;return_2_mode_select=1 when mode button is pressed
switch (mode_select)
{
case 1:
lcd_gotoxy(1,1);
printf(lcd_putc,"< M1:Read_AD >");
lcd_gotoxy(1,0);
printf(lcd_putc,"
ok");
while( 1)
{
if(!input(OK))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_R_VAR;
}
if(!input(NEXT))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
mode_select=2;
goto MODE_SELECT;
}
83
CHNG 8: PH LC
if(!input(BACK))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
mode_select=3;
goto MODE_SELECT;
}
}
break;
case 2:
lcd_gotoxy(1,1);
printf(lcd_putc,"< M2:Set freq >");
lcd_gotoxy(1,0);
printf(lcd_putc,"
ok");
while( 1)
{
if(!input(OK))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_AUTO;
}
if(!input(NEXT))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
mode_select=3;
goto MODE_SELECT;
}
if(!input(BACK))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
mode_select=1;
goto MODE_SELECT;
}
}
break;
case 3:
lcd_gotoxy(1,1);
printf(lcd_putc,"< M3:COMPUTER >");
lcd_gotoxy(1,0);
printf(lcd_putc,"
ok");
while( 1)
{
if(!input(OK))
{
output_bit(PIN_D3,1);
delay_ms(200);
84
CHNG 8: PH LC
output_bit(PIN_D3,0);
goto MODE_COMPUTER;
}
if(!input(NEXT))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
mode_select=1;
goto MODE_SELECT;
}
if(!input(BACK))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
mode_select=2;
goto MODE_SELECT;
}
}
break;
}
//==================================================
//
MODE_R_VAR
//==================================================
MODE_R_VAR:
lcd_gotoxy(1,1);
printf(lcd_putc,"T ramp up :?? s"); //clear screen
lcd_gotoxy(1,0);
printf(lcd_putc,"
");
//clear screen
T_RAMP_UP_MODE1:
while(1)
{
if(!input(OK))
// OK button is pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto NEXT_MODE1;
}
if(!input(UP))
// UP button is pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_up=T_ramp_up+1;
if(T_ramp_up>20)
{T_ramp_up=5;}
}
if(!input(DOWN))
// DOWN button is
pressed?
85
CHNG 8: PH LC
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_up=T_ramp_up-1;
if(T_ramp_up<5)
minimum frequency for motor can RUN
{T_ramp_up=20;}
}
lcd_gotoxy(13,1);
printf(lcd_putc,"%2.0d",T_ramp_up);
if(!input(NEXT))
{
//6Hz is
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(500);
output_bit(PIN_D3,0);
T_ramp_up_ms=T_ramp_up*5;
goto T_RAMP_DOWN_MODE1;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while T ramp up mode1:
T_RAMP_DOWN_MODE1:
lcd_gotoxy(1,0);
printf(lcd_putc,"T ramp down:?? s");
while(1)
{
if(!input(UP))
{
// UP button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down=T_ramp_down+1;
if(T_ramp_down>20)
{T_ramp_down=5;}
}
if(!input(DOWN))
// DOWN button is
pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
86
CHNG 8: PH LC
T_ramp_down=T_ramp_down-1;
if(T_ramp_down<3)
minimum frequency for motor can RUN
{T_ramp_down=20;}
}
lcd_gotoxy(13,0);
printf(lcd_putc,"%2.0d",T_ramp_down);
if(!input(NEXT))
{
//6Hz is
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down_ms=T_ramp_down*5;
goto NEXT_MODE1;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while T ramp down mode1:
NEXT_MODE1:
MODE_R_VAR_return_from_stop_button:
lcd_gotoxy(1,1);
printf(lcd_putc,"M1 freq READY "); //clear screen
lcd_gotoxy(1,0);
printf(lcd_putc,"Rv ?? 2 RUN "); // ?? wil be cleard when value update
count_timer1_interupt=10;
// get 1st value of A/D
//-------------------------------------------------------------------------while (1) //MAIN of MODE 1
87
CHNG 8: PH LC
{
lcd_gotoxy(4,0);
printf(lcd_putc,"%2.0d",f_req); //int_timer1 is enable as default to read AD
result =>f_req
if(return_2_mode_select==1)
//return_2_mode_select=0 as default
;return_2_mode_select=1 when mode button is pressed
{
goto MODE_SELECT;
//return to MODE select
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
//RUN Button is pressed ? ---------------------------------------------------if(!input(RUN))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
enable_interrupts(INT_PWMTB);//int_PWM must be enable after RUN
button is pressed to prevent HIGH CURRENT ( don't know why it is, just seen it in
testing if int_PWM enable b4 run button is pressed !!!)
lcd_gotoxy(1,1);
printf(lcd_putc,"
");
//clear screen
lcd_gotoxy(1,0);
printf(lcd_putc,"
");
lcd_gotoxy(1,1);
printf(lcd_putc,"M1 freq fo DIR");
//clear screen
lcd_gotoxy(1,0);
printf(lcd_putc,"AD ?? ?? ? ");
// ?? wil be cleard when value update
switch(direction_flag)
{
case 1:
// direction display
lcd_gotoxy(15,0);
printf(lcd_putc,"F");
break;
case 0:
lcd_gotoxy(15,0);
printf(lcd_putc,"R");
break;
}
if(first_run_flag==1) //RAM UP SPEED at 1st RUN
{
RAM_UP_SPEED();
88
CHNG 8: PH LC
first_run_flag=0;
RUNNING
}
}
//end if(!PIN_E0)
//------------------------------------------------END of "RUN Button is pressed ?"
while (first_run_flag==0)
{
RAM_UP_SPEED();
RAM_DOWN_SPEED();
if(!input(FR))//FR Button is pressed ? --------------------------------------------------{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
disable_interrupts(INT_TIMER1);
temp_int=f_req;
//save current f_req
f_req=0;
RAM_DOWN_SPEED();
direction_flag=direction_flag+1; //complement
direction_flag=direction_flag+1
switch(direction_flag)
//change direction display
{
case 1:
lcd_gotoxy(15,0);
printf(lcd_putc,"F");
break;
case 0:
lcd_gotoxy(15,0);
printf(lcd_putc,"R");
break;
}
f_req=temp_int;
RAM_UP_SPEED();
//restore f_req
enable_interrupts(INT_TIMER1);
//enable reading A/D
}//------------------------------------------------END of "FR Button is pressed
?"
if(!input(STOP))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
STOP_MOTOR_MODE1:
button is pressed => stop motor
//disable_interrupts(INT_TIMER1);
disable_update_freq=1;
89
CHNG 8: PH LC
f_req=0;
RAM_DOWN_SPEED();
first_run_flag=1;
RUN button is pressed next time
//enable_interrupts(INT_TIMER1);
disable_update_freq=0;
disable_interrupts(INT_PWMTB);
run button is pressed
goto MODE_R_VAR_return_from_stop_button;
//return to
current mode
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
return_2_mode_select=1;
goto STOP_MOTOR_MODE1;
}
}//end while(first_run=0)
}//while(1)
//==================================================
//
END of MODE R_VAR
//
//==================================================
//==================================================
//
MODE AUTO
//
//==================================================
MODE_AUTO:
lcd_gotoxy(1,1);
printf(lcd_putc,"T ramp up :?? s"); //clear screen
lcd_gotoxy(1,0);
printf(lcd_putc,"
");
//clear screen
T_RAMP_UP_MODE2:
while(1)
{
if(!input(OK))
// OK button is pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto NEXT_MODE2;
}
90
CHNG 8: PH LC
if(!input(UP))
{
// UP button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_up=T_ramp_up+1;
if(T_ramp_up>20)
{T_ramp_up=5;}
}
if(!input(DOWN))
// DOWN button is
pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_up=T_ramp_up-1;
if(T_ramp_up<5)
minimum frequency for motor can RUN
{T_ramp_up=20;}
}
lcd_gotoxy(13,1);
printf(lcd_putc,"%2.0d",T_ramp_up);
if(!input(NEXT))
{
//6Hz is
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(500);
output_bit(PIN_D3,0);
T_ramp_up_ms=T_ramp_up*5;
goto T_RAMP_DOWN_MODE3;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while T ramp up mode3:
T_RAMP_DOWN_MODE2:
lcd_gotoxy(1,0);
printf(lcd_putc,"T ramp down:?? s");
while(1)
{
if(!input(UP))
{
// UP button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
91
CHNG 8: PH LC
output_bit(PIN_D3,0);
T_ramp_down=T_ramp_down+1;
if(T_ramp_down>20)
{T_ramp_down=5;}
}
if(!input(DOWN))
// DOWN button is
pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down=T_ramp_down-1;
if(T_ramp_down<3)
minimum frequency for motor can RUN
{T_ramp_down=20;}
}
lcd_gotoxy(13,0);
printf(lcd_putc,"%2.0d",T_ramp_down);
if(!input(NEXT))
{
//6Hz is
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down_ms=T_ramp_down*5;
goto NEXT_MODE2 ;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while T ramp down mode2:
NEXT_MODE2:
MODE_AUTO_return_from_stop_button:
if(return_2_mode_select==1)
//return_2_mode_select=0 as default
;return_2_mode_select=1 when mode button is pressed
{
goto MODE_SELECT;
//return to MODE select
}
disable_interrupts(INT_TIMER1);
92
CHNG 8: PH LC
lcd_gotoxy(1,1);
printf(lcd_putc,"
lcd_gotoxy(1,0);
printf(lcd_putc,"
lcd_gotoxy(1,1);
printf(lcd_putc,"M2 f1
lcd_gotoxy(1,0);
printf(lcd_putc,"Au ??
");
//clear screen
");
->"); //clear screen
"); // ?? wil be cleard when value update
eeprom_check=read_eeprom(0x00);
if(eeprom_check!=100)
eeprom_check
{
defaul_value_in_EEPROM();
}
f1_select:
//f1=read_eeprom(0x10);
f1=30;
while(1)
{
if(!input(OK))
{
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto NEXT_MODE2;
}
if(!input(UP))
// UP button is pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
f1=f1+1;
if(f1>60)
{f1=6;}
}
if(!input(DOWN))
// DOWN button is
pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
f1=f1-1;
if(f1<6)
frequency for motor can RUN
{f1=60;}
}
//6Hz is minimum
93
CHNG 8: PH LC
lcd_gotoxy(4,0);
printf(lcd_putc,"%d",f1);
if(!input(NEXT))
{
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
write_eeprom(0X10,f1);
in eeprom for using when POWER ON next time
goto f2_SLECT;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while f1_select:
f2_SLECT:
lcd_gotoxy(1,1);
printf(lcd_putc,"
lcd_gotoxy(1,0);
printf(lcd_putc,"
lcd_gotoxy(1,1);
printf(lcd_putc,"M2 f2
lcd_gotoxy(1,0);
printf(lcd_putc,"Au ??
");
//clear screen
");
"); //clear screen
ok"); // ?? wil be cleard when value update
//f2=read_eeprom(0x20);
f2=60;
while(1)
{
if(!input(UP))
{
// UP button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
f2=f2+1;
if(f2>60)
{f2=6;}
}
if(!input(DOWN))
// DOWN button is
pressed?
94
CHNG 8: PH LC
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
f2=f2-1;
if(f2<6)
motor can RUN
{f2=60;}
}
lcd_gotoxy(4,0);
printf(lcd_putc,"%d",f2);
if(!input(OK))
{
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
write_eeprom(0X20,f2);
goto NEXT_DEFAULT_VALUE_MODE3;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while f2_select:
NEXT_DEFAULT_VALUE_MODE3:
//----------------- DEFAULT VALUE -----------------------------------------first_run_flag=1;
direction_flag=1;
f=0,f_req=0;
Vs_angle=0;
update_angle=0;
sector=1;
//count_timer1_interupt=10;
=> different from MODE1: we don't need to
interupt timer to get AD result
//-------------------------------------------------------------------------lcd_gotoxy(1,1);
screen IN mode 2 AUTO
printf(lcd_putc,"
lcd_gotoxy(1,0);
printf(lcd_putc,"
// AVAILABLE to RUN
");
");
95
CHNG 8: PH LC
lcd_gotoxy(1,1);
printf(lcd_putc,"M1 f1 f2 READY ");
lcd_gotoxy(1,0);
printf(lcd_putc,"Au ?? ?? 2 RUN "); // ?? wil be cleard when value update
lcd_gotoxy(4,0);
printf(lcd_putc,"%2.0d",f1);
lcd_gotoxy(7,0);
//print value of f1
printf(lcd_putc,"%2.0d",f2);
//print value of f1
lcd_gotoxy(7,0);
printf(lcd_putc,"%2.0d",f2);
//print value of f1
switch(direction_flag)
{
case 1:
// direction display
lcd_gotoxy(15,0);
96
CHNG 8: PH LC
printf(lcd_putc,"F");
break;
case 0:
lcd_gotoxy(15,0);
printf(lcd_putc,"R");
break;
}
if(first_run_flag==1) //RAM UP SPEED at 1st RUN
{
f_req=f1;
//as defauflt of MODE2
RAM_UP_SPEED();
first_run_flag=0;
//disable RUN button when motor is
RUNNING
}
}//------------------------------------------------END of "RUN Button is pressed ?"
while (first_run_flag==0)
{
if(!input(FR))//FR Button is pressed ? --------------------------------------------------{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
temp_int=f_req;
//save current f_req
f_req=0;
RAM_DOWN_SPEED();
direction_flag=direction_flag+1; //complement
direction_flag=direction_flag+1
switch(direction_flag)
//change direction display
{
case 1:
lcd_gotoxy(15,0);
printf(lcd_putc,"F");
break;
case 0:
lcd_gotoxy(15,0);
printf(lcd_putc,"R");
break;
}
f_req=temp_int;
RAM_UP_SPEED();
//restore f_req
97
CHNG 8: PH LC
delay_ms(200);
output_bit(PIN_D3,0);
STOP_MOTOR_MODE2:
MODE button is pressed => stop motor
disable_interrupts(INT_TIMER1);
f_req=0;
RAM_DOWN_SPEED();
first_run_flag=1;
RAM_UP if RUN button is pressed next time
//lable for
//stop reading A/D
//prepare for
disable_interrupts(INT_PWMTB);
// it'll enable later
when run button is pressed
enable_interrupts(INT_TIMER1);
//enable reading A/D
goto MODE_AUTO_return_from_stop_button;
//return to current mode
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
return_2_mode_select=1;
goto STOP_MOTOR_MODE2;
}
if(!input(NEXT))
(f1) as muplti speed mode ( mode2)
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
f_select=f_select+1;
f_select
if(f_select==0)
{
f_req=f1;}
else
{ f_req=f2;}
//change freq = f2
//complement bit
RAM_UP_SPEED();
to reach the new request frequency
RAM_DOWN_SPEED();
}
}//end while(first_run=0)
}//while(1)
//==================================================
98
// RAM_SPEED
CHNG 8: PH LC
//
MODE COMPUTER
//==================================================
//
MODE_COMPUTER:
disable_interrupts(INT_TIMER1);
disable_interrupts(INT_PWMTB);
lcd_gotoxy(1,1);
printf(lcd_putc,"T ramp up :?? s"); //clear screen
lcd_gotoxy(1,0);
printf(lcd_putc,"
");
//clear screen
T_RAMP_UP_MODE3:
while(1)
{
if(!input(OK))
// OK button is pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto NEXT_MODE3;
}
if(!input(UP))
// UP button is pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_up=T_ramp_up+1;
if(T_ramp_up>20)
{T_ramp_up=5;}
}
if(!input(DOWN))
// DOWN button is
pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_up=T_ramp_up-1;
if(T_ramp_up<5)
minimum frequency for motor can RUN
{T_ramp_up=20;}
}
lcd_gotoxy(13,1);
printf(lcd_putc,"%2.0d",T_ramp_up);
if(!input(NEXT))
{
//6Hz is
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(500);
output_bit(PIN_D3,0);
99
CHNG 8: PH LC
T_ramp_up_ms=T_ramp_up*5;
goto T_RAMP_DOWN_MODE3;
}
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while T ramp up mode3:
T_RAMP_DOWN_MODE3:
lcd_gotoxy(1,0);
printf(lcd_putc,"T ramp down:?? s");
while(1)
{
if(!input(UP))
{
// UP button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down=T_ramp_down+1;
if(T_ramp_down>20)
{T_ramp_down=5;}
}
if(!input(DOWN))
// DOWN button is
pressed?
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down=T_ramp_down-1;
if(T_ramp_down<3)
minimum frequency for motor can RUN
{T_ramp_down=20;}
}
lcd_gotoxy(13,0);
printf(lcd_putc,"%2.0d",T_ramp_down);
if(!input(NEXT))
{
//6Hz is
// OK button is pressed?
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
T_ramp_down_ms=T_ramp_down*5;
goto NEXT_MODE3 ;
}
100
CHNG 8: PH LC
if(!input(MENU))
{
output_bit(PIN_D3,1);
delay_ms(200);
output_bit(PIN_D3,0);
goto MODE_SELECT;
//return to MAIN menu (motor is
running => user press stop BUTTON => want to return to main menu)
}
}//end while T ramp down mode3:
NEXT_MODE3:
//----------------- DEFAULT VALUE -----------------------------------------first_run_flag=1;
direction_flag=1;
f=0,f_req=0;
Vs_angle=0;
//default value for 1st Vs; direction=1
update_angle=0;
sector=1;
//-------------------------------------------------------------------------lcd_gotoxy(1,1);
printf(lcd_putc,"M3 freq fo DIR");
lcd_gotoxy(1,0);
printf(lcd_putc,"PC ?? ?? ? ");
//clear screen
// ?? wil be cleard when value update
loop_mode3:
f_req=fgetc();
if(first_run_flag==1)
{
enable_interrupts(INT_PWMTB);
first_run_flag=0;
}
if(f_req==0)
//stop button is pressed
{
RAM_DOWN_SPEED();
first_run_flag=1;
disable_interrupts(INT_PWMTB);
}
if(f_req==70)
//Change direction button is pressed
{
temp_int=f;
//save current f_out =
f_req !!!
f_req=0;
RAM_DOWN_SPEED();
101
CHNG 8: PH LC
direction_flag=direction_flag+1; //complement
direction_flag=direction_flag+1
switch(direction_flag)
{
case 1:
lcd_gotoxy(15,0);
printf(lcd_putc,"F");
break;
case 0:
lcd_gotoxy(15,0);
printf(lcd_putc,"R");
break;
}
f_req=temp_int;
RAM_UP_SPEED();
//restore f_req
}
RAM_UP_SPEED();
RAM_DOWN_SPEED();
goto loop_mode3;
}//main
As Double
As Double
As Double
As Integer
Dim strtemp
Dim strdata
Dim datavu
Dim j
Dim intdigvu
Dim digdata
As Integer
As Integer
102
CHNG 8: PH LC
dir_flag = 1
Else
Text_direction = "REVERSE"
dir_flag = 0
End If
MSComm1.Output = Chr(70)
PIC
End Sub
Private Sub RUN_SEND_button_Click()
j = txt_f_request.Text
'send the request value of frequency to PIC
If (j > 60) Then
MsgBox ("Frequency must be in range from 0 to 60 Hz")
txt_f_request = ""
txt_f_request.SetFocus
Else
MSComm1.Output = Chr(j)
End If
If (RUN_SEND_button.Caption = "RUN") Then
RUN_SEND_button.Caption = "SEND" 'Change caption of RUN button
RUN_SEND_button.BackColor = &H8000000F
Text_motor_status = "RUNNING"
End If
End Sub
Private Sub STOP_button_Click()
MSComm1.Output = Chr(0)
103
CHNG 8: PH LC
104
CHNG 8: PH LC
digdata = Asc(strdata)
intdigvu = Asc(datavu)
'txtFreg = digdata
'txt_f_out = intdigvu
txt_f_out = digdata
txt_u_out = digdata * 3.66 ' V/f=const
End Select
End With
End Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.Output = Chr(0)
'send stop signal for PIC to stop motor
MSComm1.PortOpen = False
End Sub
'Dong cong
105