Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ac voltage and frequency measurement using clamping and 18f452

Status
Not open for further replies.

zeroskj1988

Member level 2
Joined
Jul 7, 2011
Messages
46
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,588
dear friends,
i want to measure ac voltage and frequency using clamping principle.i used 2 1k resistor to clamp at vcc/2.i am using this program. as result i am getting correct ac value,some times. but sometime i am getting wrong result. can ant body spot any error
i am using ccs compiler and pic18f452
void main()
{
int16 value,adc_val,adc_volt,count=0,freq;
int32 cum_adc=0;
float temp;
int8 a,b,c,d,e,data;
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2); //26.2 ms overflow
//lcd_init();
//delay_ms(1000);
clear_interrupt(int_timer1);
set_timer1(0);
START:set_adc_channel(0);
delay_us(10);
adc_val=read_adc();
if((adc_val>512) & (adc_val<552))
{
adc_volt=read_adc();
if(adc_volt<adc_val)
goto START;
clear_interrupt(int_timer1);
set_timer1(0);
cum_adc=cum_adc+adc_val;
count++;
cum_adc=cum_adc+adc_volt;
count++;
while(adc_val>=512)
{
adc_val=read_adc();
cum_adc=cum_adc+adc_val;
count++;
if(interrupt_active(INT_TIMER1))
{
cum_adc=0;
d-0;
e=0;
goto skip;
}
}
freq=get_timer1();
temp=freq*.0000008;
freq=1/temp;
d=freq/10;
e=freq%10;
skip:lcd_display_char(2,4,'0'+d);
lcd_display_char(2,5,'0'+e);
value=cum_adc/count;
cum_adc=0;
count=0;
value=(value-506)*1.2550;
c=value/100;
lcd_display_char(3,4,'0'+c);
data=value%100;
b=data/10;
lcd_display_char(3,5,'0'+b);
a=data%10;
lcd_display_char(3,6,'0'+a);
}
goto START;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top