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
| // dsPIC33FJ128MC202, development board Easy 24-33 v6
// microC PRO
// Use ADC library function
// Oscillator mode= Internal FRC
//MCU clock frequency = 7.37 MHz
//sw: microC PRO for dsPIC33
#include <built_in.h>
unsigned C1,C2,C3,C4,Vc1,Vc2,Vc3,Vc4,RawData1,RawData2,RawData3,RawData4;
signed e1,e2;
void main()
{
AD1PCFGL=0xFFF0; //Set PCFG0,PCFG1,PCFG2,PCF3 as analog input
TRISA0_bit=1; //PORTA0 is input 1(AN0)
TRISA1_bit=1; //PORTA1 is input 2 (AN1)
TRISB0_bit=1; //PORTB0 is input 3 (AN2)
TRISB1_bit=1; //PORTB1 is input 4 (AN3)
TRISB12_bit=0; // PORTB12 is output as switch 1 (S1)
TRISB13_bit=0; // PORTB13 is output as switch 2 (S2)
TRISB14_bit=0; // PORTB14 is output as switch 3 (S3)
TRISB15_bit=0; // PORTB15 is output as switch 4 (S4)
TRISB6_bit=0; //PORTB6 to display the frequency
LATBbits.LATB12=0; //Initialize PortB12
LATBbits.LATB13=0; //Initialize PortB13
LATBbits.LATB14=0; //Initialize PortB14
LATBbits.LATB15=0; //Initialize PortB15
LATBbits.LATB6=0; //Initialize PortB6
ADC1_Init(); //Initialize ADC module
while (1)
{
C1= (ADC1_Read(0)/0x3FF)*3.30; //Read capacitor voltage 1 from voltage sensor 1
C2= (ADC1_Read(1)/0x3FF)*3.30; //Read Vc2
C3= (ADC1_Read(2)/0x3FF)*3.30; //Read Vc3
C4= (ADC1_Read(3)/0x3FF)*3.30; //Read Vc3
RawData1= (C1 - 1.256);
Vc1= RawData1 * 128.42; //Actual value of capacitor voltage 1
RawData2= (C2 - 1.257);
Vc2= RawData2 * 267.68; //Actual value of capacitor voltage 2
RawData3= (C3 - 1.257);
Vc3= RawData3 * 297.14; //Actual value of capacitor voltage 3
RawData4= (C4 + 0.013);
Vc4= RawData4 * 100.92; //Actual value of capacitor voltage 4
e1= Vc1-Vc2; //error voltage 1
e2= Vc4-Vc3; //error voltage 2
LATBbits.LATB6=~LATBbits.LATB6; //Connect to oscilloscope to read the frequency
if ( (e1>=0.01) && (e2>=0.01))
{
LATBbits.LATB12=1; //S1
LATBbits.LATB13=0; //S2
LATBbits.LATB14=0; //S3
LATBbits.LATB15=1; //S4
}
else if ( (e1>=0.01) && (e2 < 0.01))
{
LATBbits.LATB12=1; //S1
LATBbits.LATB13=0; //S2
LATBbits.LATB14=1; //S3
LATBbits.LATB15=0; //S4
}
else if ( (e1<0.01) && (e2 >=0.01) )
{
LATBbits.LATB12=0; //S1
LATBbits.LATB13=1; //S2
LATBbits.LATB14=0; //S3
LATBbits.LATB15=1; //S4
}
else
{
LATBbits.LATB12=0; //S1
LATBbits.LATB13=1; //S2
LATBbits.LATB14=1; //S3
LATBbits.LATB15=0; //S4
}
delay_ms(100);
}
} |