djc
Advanced Member level 1
Hello,
I am writing a C code using mikroc and PIC16F676. Here is the code,
However when i test it on board, Dl_Relay pin transit between ON and OFF multiple times for particular ADC value,showing glitches on oscilloscope. Dl_Relay pin gets ON and OFF for 2-3 different ADC values. When i test it on hardware, same thing happens. D_relay pin switches ON and OFF for different ADC values. Why is the happening? Can any one guide me?
I am writing a C code using mikroc and PIC16F676. Here is the code,
Code:
#define Dl_Relay PORTC.B2
#define Relay_1 PORTC.B1
#define Relay_2 PORTC.B2
#define Relay_3 PORTC.B3
#define test PORTC.B1
char i=0,j=0,cycle=1,Flag=0,biggest_val[5];
unsigned int mains_val=0,a=0,b=0,k=0,d=0,Average=0;
void peak_detector(){
while(a<b){
a = ADC_Read(0);
//Delay_us(750);
b = ADC_Read(0);
//Delay_us(750);
}
k = a;
biggest_val[i++]=k;
if(i==5){
Average = (biggest_val[0] +biggest_val[1] +biggest_val[2] +biggest_val[3] +biggest_val[4])/5;
if(Average<155) Dl_Relay = 0;
if((Average>155) && (Average<200)) Dl_Relay = 1;
if(Average>200) Dl_Relay = 0;
i=0;
}
}
void falling_edge(){
while(a>b){
a = ADC_Read(0);
//Delay_us(750);
b = ADC_Read(0);
//Delay_us(750);
}
}
void led(){
PORTC.B5 = 0; //LED ON-OFF
PORTC.B4 = 1;
Delay_ms(200);
PORTC.B4 = 0;
Delay_ms(200);
}
void main() {
TRISA = 0x11111101;
TRISC = 0x00; //PORTC as O/p
ADC_Init();
led();
led();
while(1){
mains_val = ADC_Read(0);
//Delay_us(200);
while(mains_val<5){
mains_val = ADC_Read(0);
//Delay_us(200);
}
if(mains_val>5){
a = ADC_Read(0);
//Delay_us(200);
b = ADC_Read(0);
//Delay_us(70);
if(a<b) peak_detector();
if(a>b) falling_edge();
}
}
}
However when i test it on board, Dl_Relay pin transit between ON and OFF multiple times for particular ADC value,showing glitches on oscilloscope. Dl_Relay pin gets ON and OFF for 2-3 different ADC values. When i test it on hardware, same thing happens. D_relay pin switches ON and OFF for different ADC values. Why is the happening? Can any one guide me?