📄 ad.lst
字号:
1: /*
2: The file is used to convert adc for uC pic16f688
3: There are two functions:
4: int ADC(void) :Convert adc and return a result
5: void Arith_AD(void) :Get atithmetic value each ad channel
6: */
7:
8: #include <htc.h>
9: #include <pic16f688.h>
10: #include "AD.H"
11: #include "IntServ.h"
12: #include "Type.h"
13: //extern ADCs adc[5];
14: extern int VP_I,VP_U,PMC_I,PMC_U;
15: char ADC_Chn[5]={2,4,5,6,7}; //Each channel AD locate register ADCON0
16: //unsigned int adc[5];
17: int adc[5]; //Store 5AD value
18: bit bShortCurrent; //set flag when short current
19: //bit bSC_first; //test delay time of short current
20:
21: /*
22: void Switch_ADC_Ch(void)
23: {
24: //ADCON1=0x30; //x11 = FRC,system internal clock about 500khz
25: ADCON0 = (Num_ADC << 2) + 0x81; //AN4,RIGHT ALIGN; enable ADC, RC osc.
26: }
27: //INI AD2
28: */
29: int ADC(void)
30: { int data16=0;
31: GODONE = 1; //start convert
32: while(GODONE); // wait for conversion complete
33: ADIF=0;
34: data16=ADRESH&0x3; //high 2bit bin
35: data16=(data16<<8)|ADRESL; //low byte
36: return(data16);
37: }
38:
39: void Arith_AD(void)
40: {
41: // int sum=0;
42: int sum[5];
43: unsigned char Num_ADC;
44: char i=0,j=0,k=0,m=0; //m: sample times to relative channel
45: int max1=0,max2=0,min1=1023,min2=1023,tmp=0,tmp1=0,tmp2=0;
46: // int n=0;
47: int n=3000;
48: while(n--){;} //must at least delay 3000*2us=6ms before sampling adc until power supply stable
49: // if(bSC_first)
50: // {
51: // n=30000;bSC_first=0;
52: // while(n--){;}
53: // }////test delay time of short current
54: for(i=0;i<5;i++)
55: { sum[i]=0;
56: }
57: m=10; //10 times sample
58: for(j=0;j<m;j++)
59: {
60: for(i=0;i<5;i++)
61: {
62: Num_ADC=ADC_Chn[i];
63: //ADCON0 = (Num_ADC << 2) + 0x81; //Switch_ADC_Ch();
64: Num_ADC=Num_ADC << 2;
65: ADCON0 = Num_ADC + 0x81;
66:
67: k=10; //must at least delay 10*2us=20us
68: while(k--){;}
69: if(i>1)
70: {
71: sum[i]=sum[i]+ADC();//Get adc value and sum if not AD1 AND AD2
72: }
73: else
74: {
75: if(!i)
76: {
77: tmp1=ADC(); // adc1 value
78: }
79: else if(i==1)
80: {
81: tmp2=ADC(); //adc2 value
82: tmp=tmp1-tmp2; //Diff=AD1-AD2
83: if(tmp<0) tmp=0;//Diff=0 if Diff<0;
84:
85: if(tmp > max1)
86: {
87: max2 = max1;
88: max1 = tmp;
89:
90: } else if (tmp > max2)
91: {
92: max2 = tmp;
93: } //Find out two max values
94:
95: if(tmp < min1)
96: {
97: min2 = min1;
98: min1 = tmp;
99: } else if(tmp < min2)
100: {
101: min2 = tmp;
102: } //Find out two min values
103: sum[i]=sum[i]+tmp; //Sum all 10 Diffs
104:
105: }
106: }
107:
108: }
109: }
110:
111: for(i=1;i<5;i++)//sum[0] is not available
112: {
113: if(i>1)
114: {
115: adc[i]=sum[i]/m; //Sum if not AD1 AND AD2
116: }
117: else
118: {
119: sum[i]=sum[i]-max2-max1-min2-min1; //SUM EXCEPT TWO MAX AND TWO MIN
120: adc[i]=sum[i]/(m-4); //AVERAGE
121: }
122: }
123:
124: VP_I=adc[2-1];
125: if(VP_I<4) //See VP_I=0 WHEN VP_I<4; (4 means around AD1-AD2=40mV)
126: VP_I=0;
127: else if(VP_I>=150) //See VP_I TOO BIG AS SHORT CURRENT WHEN VP_I>=150;(150 means around AD1-AD2=1.5V)
128: bShortCurrent=1; //set short current flag
129: VP_U=adc[5-1];
130: if(VP_U<154) //See VP_U=0 when external device offers voltage smaller than 3.75V voltage(around 154*5mV*5=3.75V)
131: VP_U=0;
132: PMC_I=adc[3-1];
133: if(PMC_I<=8) //See PMC_I=0 when external device offers 40mV
134: PMC_I=0;
135: PMC_U=adc[4-1];
136: // if(PMC_U<50) PMC_U=0;
137:
138: }
139:
140: /*
141: void Arith_AD(void)
142: {
143: int sum=0;
144: char i=0,j=0,k=0,m=0; //m: sample times to relative channel
145: // int n=0;
146: char Num_ADC=0;
147: int n=50000;
148: while(n--){;} //delay several hundreds ms before sampling adc
149:
150:
151: for(i=0;i<5;i++) //may improve to swich channel after continue to sample 3times one channel
152: {
153: Num_ADC=ADC_Chn[i];
154: //ADCON0 = (Num_ADC << 2) + 0x81; //Switch_ADC_Ch();
155: Num_ADC=Num_ADC << 2;
156: ADCON0 = Num_ADC + 0x81;
157: if(i==0 || i==1)
158: m=10;
159: else m=5;
160:
161: for(j=0;j<m;j++)
162: {
163: k=200; //200*2us=400us
164: while(k--){;}
165: sum=sum+ADC();//;//delay(50);ValAverage
166: // adc[i].ValAD[j]=ADC();
167: // sum=sum+adc[i].ValAD[j];
168: }
169: adc[i]=sum/m;
170: sum=0;
171: } //VP_I=0;VP_U=0;PMC_I=0;PMC_U=0;
172: VP_I=adc[1-1]-adc[2-1];
173: if(VP_I<4)
174: VP_I=0; //up limith??
175: VP_U=adc[5-1];
176: if(VP_U<154)
177: VP_U=0;
178: PMC_I=adc[3-1];
179: if(PMC_I<=8)
180: PMC_I=0;
181: PMC_U=adc[4-1];
182: // if(PMC_U<50) PMC_U=0;
183:
184: }
185:
186: // TO test short current :VP:I
187: void Arith_AD(void)
188: {
189: int sum=0;
190: char i=0,j=0,k=0,m=0; //m: sample times to relative channel
191:
192: for(i=0;i<2;i++) //may improve to swich channel after continue to sample 3times one channel
193: {
194: Num_ADC=ADC_Chn[i];
195: //ADCON0 = (Num_ADC << 2) + 0x81; //Switch_ADC_Ch();
196: Num_ADC=Num_ADC << 2;
197: ADCON0 = Num_ADC + 0x81;
198: if(i==0 || i==1)
199: m=10;
200: else m=5;
201: for(j=0;j<m;j++)
202: {
203: // k=200; //200*2us=400us
204: // while(k--){;}
205: sum=sum+ADC();//;//delay(50);ValAverage
206: // adc[i].ValAD[j]=ADC();
207: // sum=sum+adc[i].ValAD[j];
208: }
209: adc[i]=sum/m;
210: sum=0;
211: } //2*10*400us=8ms each time
212: VP_I=adc[1-1]-adc[2-1];
213: if(VP_I<100)
214: VP_I=0; //up limith??
215:
216: }
217: */
218:
219:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -