kavin_eees
Member level 3
hi all
right now am designing three phase protection circuit
for that i planned to measure the voltage using resistor net work method, herewith i had attached my circuit and my volt measuring code,
everything working good but in sometimes the R phase connected to AN0 pin shows high reading, kindly go through my code and give your valuable suggestions.
right now am designing three phase protection circuit
for that i planned to measure the voltage using resistor net work method, herewith i had attached my circuit and my volt measuring code,
everything working good but in sometimes the R phase connected to AN0 pin shows high reading, kindly go through my code and give your valuable suggestions.
Code C - [expand] 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 #include "Includes.h" __CONFIG (DEBUGDIS & LVPDIS & BOREN & MCLRDIS& PWRTDIS & WDTDIS & INTIO ); //MCLREN //__CONFIG (DEBUGDIS & LVPDIS & BOREN & MCLRDIS& PWRTDIS & WDTDIS & EXTIO ); //MCLREN //#define _XTAL_FREQ 20000000 #define Tank RB7 #define Sump RB6 #define Auto RB0 #define Off RB5 #define On RB4 #define Inc RB3 #define Dec RB2 #define Menu RB1 #define Mon RC0 #define Dry_run RC2 #define Over_load RC1 #define Volt_error RC5 #define Tank_full RC6 #define Sump_empty RC7 #define Buzz RE2 void Menu_Set(); void delay(int); void Buzz_delay_small(); //unsigned int GetVolt(); unsigned int GetCurrent(); void InitADC(void); float GetADCValue(unsigned char Channel ); int GetVolt(unsigned char C ); void GetVolt3(); void Motor_On(); void Motor_Off(); //void display(unsigned int); void display(unsigned int disp); void displaycurrent(float value); void displayamps(unsigned int disp1); void InitTimer1(); void InitTimer0(); void Motor_amps_status(); void Phase_seq(); void Dry_pump(); char temp=0; //unsigned int ADC_value = 0; int ADC_value = 0; int a,b,l=0,t1=0,t2=0; float ADC_value2 = 0, Current=0,Ol_amp=0,Dry_amp=0,Raw_Byte; unsigned int ADC_value1 = 0; unsigned int digit1, digit2, digit3,VloByte,VhiByte,Vlow,Vhigh,i,k=0; unsigned int Volt=220 ,Dry_time=0,Volt_R=0,Volt_Y=0,Volt_B=0,Menu_temp=0; char amp_temp,Dry_temp=0,pwr=0,Volt_temp=0,Seq_temp=0; //char Tank_temp=0,Sump_temp=0; //char int_temp=0; unsigned int temp0=0; unsigned max_point0 = 0; void main(void) { TRISB=0xCF; PORTB=0xCf; TRISA=0xFF; PORTA=0x00; TRISC=0X00; //TRISD=0X00; PORTC=0X00; //PORTD=0X00; INTEDG=0; INTCON=0x00; GIE=1; PEIE=1; //INTE=1; //INTF=0; //RBPU=0; OSCCON=0x75; ANSELH=0x00; //ANSEL=0X0F; ANSEL=0Xff; //C1ON=C2ON=0; Auto=0; InitTimer1(); InitADC(); ADCON1 = 0x80; Buzz=1; __delay_ms(500); Buzz=0; InitLCD(); // Initialize LCD in 4bit mode ClearLCDScreen(); // Clear LCD screen while(1) { ADCON1 = 0x80; __delay_ms(250); GetVolt3(); WriteCommandToLCD(0xcc); display(Volt_R); Volt_R=Volt_R*1.732; Volt_Y=Volt_Y*1.732; Volt_B=Volt_B*1.732; WriteCommandToLCD(0x80); WriteStringToLCD("V-"); WriteCommandToLCD(0x82); display(Volt_R); WriteCommandToLCD(0x86); display(Volt_Y); WriteCommandToLCD(0x8a); display(Volt_B); if((Volt_R>=Vhigh || Volt_R<=Vlow) || (Volt_Y>=Vhigh || Volt_Y<=Vlow) || (Volt_B>=Vhigh || Volt_B<=Vlow)) { Volt_error=1; Volt_temp=1; } else { Volt_error=0; Volt_temp=0; } if(Volt_temp==1 && temp==1) { //On=0; //Off=1; //delay(1); //Off=0; //Mon=0; temp=0; //WriteCommandToLCD(0xc0); //WriteStringToLCD("M-OFF"); Motor_Off(); } } } void interrupt isr(void) { if (TMR1IF) { TMR1IF = 0; TMR1H = 0x3c; TMR1L = 0xb0; k++; if(k==300) { Dry_time--; k=0; } } if(T0IF) { T0IF = 0; TMR0 = 6; l++; } } void InitTimer1() { T1CON = 0x31; TMR1IF = 0; TMR1H = 0x3c; TMR1L = 0xb0; TMR1IE = 1; INTCON = 0x00; //INTCON = 0xC0; } void InitTimer0() { OPTION = 0x83; TMR0 = 6; //INTCON = 0xA0; INTCON = 0x00; } void InitADC(void) { //ADCON1 = 0xB0; // amps // Make PORTA and PORTE analog pins // Also, Vref+ = 5v and Vref- = GND // ADCON1 = 0x80; // volt TRISA = 0xff; // Make RA5, RA3, RA2, RA1, RA0 input TRISE = 0x0B; // Make RE0, RE1 and RE2 input //ADCON0 = 0x81; // Turn on the A/D Converter } int GetVolt(unsigned char C ) { ADC_value1=0; ADC_value2=0; ADC_value=0; ADCON0 = 0x81; ADCON0 &= 0xc7; // Clear Channel selection bits ADCON0 |= (C<<2); // Select channel pin as ADC input __delay_us(50); // Time for Acqusition capacitor // to charge up and show correct value GODONE = 1; // Enable Go/Done while(GODONE); // Wait for conversion completion ADC_value= ((ADRESH<<8)+ADRESL); // Return 10 bit ADC value return ADC_value; } void GetVolt3() { max_point0 = 0; for(i=0;i<1000;i++) { //ADCON0 = 0b00000001; if(temp0 = GetVolt(0),temp0>max_point0) //find the peak point { max_point0 = temp0; } Volt_R=max_point0*1.16; } max_point0 = 0; for(i=0;i<1000;i++) { //ADCON0 = 0b00000001; if(temp0 = GetVolt(1),temp0>max_point0) //find the peak point { max_point0 = temp0; } Volt_Y=max_point0*1.145; } max_point0 = 0; for(i=0;i<1000;i++) { //ADCON0 = 0b00000001; if(temp0 = GetVolt(4),temp0>max_point0) //find the peak point { max_point0 = temp0; } Volt_B=max_point0*1.089; } } void displaycurrent(float value) // Assume value = 123.456 { a = value; // a = 123 value = (value - (float)a) * (10); // value = 45.6 b = a; // b = 45 displayamps(a); WriteDataToLCD('.'); b = value; WriteDataToLCD(b+0x30); //displayamps(b); } void display(unsigned int disp) { digit1 = (unsigned int)(disp/100); // Calculate digit1 of ADC_value digit2 = (unsigned int)((disp - digit1*100)/10); // Calculate digit2 of ADC_value digit3 = (unsigned int)(disp - (digit1*100+digit2*10)); // Calculate digit3 of ADC_value //digit4 = (unsigned int)(disp - (digit1*1000+digit2*100+digit3*10)); // Calculate digit4 of ADC_value WriteDataToLCD(digit1+0x30); // Display digit1 of ADC_value on LCD WriteDataToLCD(digit2+0x30); // Display digit2 of ADC_value on LCD WriteDataToLCD(digit3+0x30); // Display digit3 of ADC_value on LCD //WriteDataToLCD(digit4+0x30); // Display digit4 of ADC_value on LCD } void delay(int x) { for(i=0;i<x;i++) { __delay_ms(1000); } } void Buzz_delay_small() { __delay_ms(1); Buzz=1; __delay_ms(100); Buzz=0; }