/* * Digital Capacitor ESR Tester V3.1 * Copyright (c) 2009, Regulus Berdin * All rights reserved. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL REGULUS BERDIN BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include <16F876.h> #device adc=10 #FUSES NOWDT, HS, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG #use delay(clock=20M) #use fast_io(a) #use fast_io(b) #use fast_io(c) #define CHARGE_MASK (1<<0) #define DEBUG_MASK (1<<1) #define COM1_MASK (1<<2) #define COM2_MASK (1<<3) #define COM1_PIN PIN_B2 #define COM2_PIN PIN_B3 #define BUZZERP_PIN PIN_A1 #define BUZZERN_PIN PIN_A2 #define PASS_PIN PIN_A5 #define FAIL_PIN PIN_A3 float rx, fvs, fvo, fvg, fd, r1 = 3.3, r2 = 0.0; const char led_map[16] = { /* aaaa f b f b ggg e c e c ddd */ //.gfedcba 0b00111111, //0 0b00000110, //1 0b01011011, //2 0b01001111, //3 0b01100110, //4 0b01101101, //5 0b01111101, //6 0b00000111, //7 0b01111111, //8 0b01101111, //9 0b01111111, //10 0b01111111, //11 0b01111111, //12 0b01111111, //13 0b01111111, //14 0b01111111 //15 }; unsigned int8 digit[2], dptr=0, dseg=1, scan_skip=0, beep=0, buz_pol=0; #int_timer0 void tmr0_isr(void) { set_timer0(255 - 155); //4khz ++scan_skip; if (scan_skip>=4) { output_low(COM1_PIN); output_low(COM2_PIN); if (dptr==0) { output_c(~(dseg & digit[0])); output_high(COM1_PIN); } else { output_c(~(dseg & digit[1])); output_high(COM2_PIN); } dseg <<=1; if (dseg==0) { dseg = 1; dptr ^= 1; } scan_skip = 0; } if (beep) { buz_pol ^= 1; if (buz_pol) { output_high(BUZZERP_PIN); output_low(BUZZERN_PIN); } else { output_low(BUZZERP_PIN); output_high(BUZZERN_PIN); } } } void show_status(void) { if (rx < 1.05) { output_high(PASS_PIN); beep = 1; } else if (rx < 5.05) { output_high(PASS_PIN); beep = 0; } else if (rx < 15.05) { output_high(PASS_PIN); delay_cycles(2); output_high(FAIL_PIN); beep = 0; } else { output_high(FAIL_PIN); beep = 0; } } void main(void) { unsigned int16 vs, vo, vg; unsigned int8 i, c1, c2; port_b_pullups(0); set_tris_b(0x00); set_tris_c(0x00); output_c(0xFF); set_tris_a(0x01); output_a(0); setup_adc_ports(AN0); setup_adc(ADC_CLOCK_DIV_32); set_adc_channel(0); set_tris_b(0); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4); set_timer0(255 - 155); //8khz enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); i=0; for(;;) { output_b(0); delay_ms(20); vg = read_adc(); output_b(CHARGE_MASK); delay_ms(20); vs = read_adc(); delay_us(25); //critical part disable_interrupts(GLOBAL); output_b(DEBUG_MASK); //delay_us(1); vo = read_adc(); enable_interrupts(GLOBAL); output_b(0); fvo = vo; fvs = vs; fvg = vg; fd = (fvg - fvo) + 0.00001; if (fd<=0.00001) fd = 0.00001; rx = ((r1 * (fvg - fvs))/fd) - r1 - r2; if (rx>=99.0) { digit[0] = led_map[9]; digit[1] = led_map[9]; } else if (rx<0) { digit[0] = led_map[0] | (1<<7); digit[1] = led_map[0]; } else if (rx<10.0) { c1 = (rx + 0.05) * 10.0; //round c2 = c1 % 10; c1 -= c2; c1 /= 10; digit[0] = led_map[c1] | (1<<7); digit[1] = led_map[c2]; } else { c1 = rx + 0.5; //round c2 = c1 % 10; c1 -= c2; c1 /= 10; digit[0] = led_map[c1]; digit[1] = led_map[c2]; } show_status(); } }