This routine runs in my background task which executes every 1.024ms. "rampg1" & "rampg0" are macros that set the appropriate page bits. Those are a single instruction for the 12f675 but would expand to two instructions for PICs with 4 RAM pages. A/D is configured for 10 bit result (right-justified). The a/d start command occurs when the background task is about to finish and is not shown here. ;AD routines: 1 ch with 16 sample IIR filter. Choose 16 samples 'cuz ; 16 * 1.024ms ~ 16.667ms (period of 60Hz signal). ; Would need to go to more than 20 samples for 50Hz signal (use 32 samples). ;divide sum by 16 AD1:AD1L /16 -> BGTMP2:BGTMP1 swapf AD1L,W ;LO byte andlw b'00001111' movwf BGTMP1 ;hi nib AD1L -> lo nib BGTMP (>>4) swapf AD1,W ;HI byte movwf BGTMP2 ;swap nibs AD1 -> BGTMP2 (>>4) andlw b'11110000' ;clr lo nib in W xorwf BGTMP2,F ;clr hi nib BGTMP2 (XOR value with itself is 0) iorwf BGTMP1,F ;lo nib AD1 -> hi nib BGTMP1 (>>4) ;subtract old sample from sum movfw BGTMP1 ;LS byte subwf AD1L,F ;F-W ->d movfw BGTMP2 skpc incfsz BGTMP2,W subwf AD1,F ;add new sample to sum rampg1 movfw RB1(ADRESL) rampg0 addwf AD1L,F movfw ADRESH skpnc incfsz ADRESH,W addwf AD1,F ;done. Do all line voltage tests on sum (AD1,AD1L) At 11:11 AM 3/9/2010, Dario Greggio wrote: >Dwayne Reid ha scritto: > > In my case, I threw extra parts at the problem to make the software > > easier and to allow the PIC pin to work as both an input and an > > output. The 120Vac input is sampled with a 100K resistor, then > > [...] > > > The software samples the input every 1.024ms and feeds a 16 times IIR > > filter. The output of that filter is then fed into a peak detector > > with a period of 20ms (20 ticks of 1.024ms each). > > > > This allows me to accurately measure the 120Vac line voltage and make > > decisions on whether to allow system operation or not. > > >this is perfectly good :) >I will consider the shunting diodes, and I do have transformer so >isolation should not be an issue. > >Do you have a quick link for the IIR filter? I know it has been >published and commented tons of times but... > >thanks > >Ciao, Dario -- Dwayne Reid Trinity Electronics Systems Ltd Edmonton, AB, CANADA (780) 489-3199 voice (780) 487-6397 fax www.trinity-electronics.com Custom Electronics Design and Manufacturing -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist