Example #1
0
void CheckPins(uint8_t HighPin, uint8_t LowPin, uint8_t TristatePin) {	// Do the tests on the Probe pins, get device characteristics
									// HighPin   - Starts at H, logic 1.
									// LowPin    - Starts at L, logic 0.
									// TriState  - Starts Hi-Z, is put both H and L during the tests.
unsigned int adcv[6];
uint8_t tmpval, tmpval2;
									// HighPin is Vcc, LowPin is Gnd, TriState is HiZ
  wdt_reset();
									// Pin set
  tmpval = (LowPin * 2);						// necessarily because of the arrangement of the resistances
  R_DDR = (1<<tmpval);							// Low pin on exit and over R_L to ground
  R_PORT = 0;
  ADC_DDR = (1<<HighPin);					// High pin on exit
  ADC_PORT = (1<<HighPin);					// High pin to Vcc
  _delay_ms(5);
									// With some MOSFETs the gate (TriState act pin) must be unloaded first
									// N-channel:
  DischargePin(TristatePin,0);						
  adcv[0] = ReadADC(LowPin);						// Read the Voltage at the Low pin
  
  if(adcv[0] < 200) 
    goto next;								// Does the Device close now?
									
  DischargePin(TristatePin,1);						// otherwise: Unloaded for p-channel (gate on pluses)
  adcv[0] = ReadADC(LowPin);						// Read the Voltage at the Low pin

  next:

  if(adcv[0] > 19) {							// Device leads something without control current
									// Test on N-JFET or leading N-MOSFET
    R_DDR |= (2<<(TristatePin*2));					// Tristate pin (assumed gate) over R_H to ground
    _delay_ms(20);
    adcv[1] = ReadADC(LowPin);						// Voltage at the assumed SOURCE measure
    R_PORT |= (2<<(TristatePin*2));					// Tristate pin (assumed gate) over R_H on pluses
    _delay_ms(20);
    adcv[2] = ReadADC(LowPin);						// Voltage at the assumed SOURCE measure again
									// If it concerns a leading MOSFET or JFET, would have adcv [1] > adcv [0] its
    if(adcv[2]>(adcv[1]+100)) {						// Voltage at the gate measure, to the distinction between MOSFET and JFET
      ADC_PORT = 0x00;
      ADC_DDR = (1<<LowPin);					// Low pin to ground
      tmpval = (HighPin * 2);						// necessarily because of the arrangement of the resistances
      R_DDR |= (1<<tmpval);						// High pin on exit
      R_PORT |= (1<<tmpval);						// High pin over R_L on Vcc
      _delay_ms(20);
      adcv[2] = ReadADC(TristatePin);					// Voltage at the assumed gate measure

      if(adcv[2]>800) {							// MOSFET
	PartFound = PART_FET;						// N-channel-MOSFET
	PartMode = PART_MODE_N_D_MOS;					// Depletion MOSFET
      } else {								// JFET (pn transition between G and S leads)
	PartFound = PART_FET;						// N-channel-JFET
	PartMode = PART_MODE_N_JFET;
      }
      
      b = TristatePin;
      c = HighPin;
      e = LowPin;
    }
    
    ADC_PORT = 0x00;

									// Test on P-JFET or leading P-MOSFET
    ADC_DDR = (1<<LowPin);					// Low pin (assumed drain) to ground, Tristate pin (assumed gate) still is over R_H on pluses
    tmpval = (HighPin * 2);						// necessarily because of the arrangement of the resistances
									
    R_DDR |= (1<<tmpval);						// High pin on exit
    R_PORT |= (1<<tmpval);						// High pin over R_L on Vcc
    _delay_ms(20);
    adcv[1] = ReadADC(HighPin);						// Voltage at the assumed SOURCE measure
									
    R_PORT = (1<<tmpval);						// Tristate pin (assumed gate) over R_H to ground
    _delay_ms(20);
    adcv[2] = ReadADC(HighPin);						// Voltage at the assumed SOURCE measure again
									// - If it concerns a leading P-MOSFET or P-JFET, would have adcv [0] > adcv [1] its
    if(adcv[1]>(adcv[2]+100)) {
									// - Voltage at the gate measure, for distinction between MOSFET and JFET
      ADC_PORT = (1<<HighPin);				// High pin firmly on pluses
      ADC_DDR = (1<<HighPin);				// High pin on exit
      _delay_ms(20);
      adcv[2] = ReadADC(TristatePin);					// Voltage at the assumed gate measure
      
      if(adcv[2]<200) {							// MOSFET
	PartFound = PART_FET;						// P-channel-MOSFET 
	PartMode = PART_MODE_P_D_MOS;					// Depletion MOSFET
      } else {								// JFET (pn transition between G and S leads)
	PartFound = PART_FET;						// P-channel-JFET
	PartMode = PART_MODE_P_JFET;
      }

      b = TristatePin;
      c = LowPin;
      e = HighPin;
    }
  }
									// Pins erneut setzen - Pin set again
  tmpval = (LowPin * 2);						// necessarily because of the arrangement of the resistances
  R_DDR = (1<<tmpval);							// Low pin on exit and over R_L to ground
  R_PORT = 0;
  ADC_DDR = (1<<HighPin);					// High pin on exit
  ADC_PORT = (1<<HighPin);					// High pin to Vcc
  _delay_ms(5);	

  if(adcv[0] < 200) {							// If the Device does not have a passage between HighPin and LowPin
									// Test auf pnp
    tmpval2 = (TristatePin * 2);					// necessarily because of the arrangement of the resistances
    R_DDR |= (1<<tmpval2);						// Tristate pin over R_L to ground, to the test on pnp
    _delay_ms(2);
    adcv[1] = ReadADC(LowPin);						// Voltage measure
    
    if(adcv[1] > 700) {
									// Device leads => Pnp transistor or the like.
									// Amplification factor in both directions measure
      R_DDR = (1<<tmpval);						// Tristate pin (basis) high impedance
      tmpval2++;
      R_DDR |= (1<<tmpval2);						// Tristate pin (basis) over R_H to ground

      _delay_ms(10);
      adcv[1] = ReadADC(LowPin);					// Voltage at the Low pin (assumed collector) measure
      adcv[2] = ReadADC(TristatePin);					// Base voltage measure
									// Examine whether test already times run
      if((PartFound == PART_TRANSISTOR) || (PartFound == PART_FET)) 
	PartReady = 1;

      hfe[PartReady] = adcv[1];
	uBE[PartReady] = adcv[2];

      if(PartFound != PART_THYRISTOR) {
	if(adcv[2] > 200) {
	  PartFound = PART_TRANSISTOR;					// PNP transistor found (basis is " " upward; pulled)
	  PartMode = PART_MODE_PNP;
	} else {
	    if(adcv[0] < 20) {						// - Durchlassspannung in the closed condition small enough? (otherwise D-mode-FETs are falsely recognized as E-mode)
	      PartFound = PART_FET;					// P-channel-MOSFET found (basis/gate is not " " upward; pulled)
	      PartMode = PART_MODE_P_E_MOS;
									// Measurement of the gate threshold voltage
	      tmpval = (1<<LowPin);
	      tmpval2 = R_DDR;
	      ADMUX = TristatePin | (1<<REFS0);
	      gthvoltage = 0;

	      for(b=0;b<13;b++) {
		wdt_reset();
		DischargePin(TristatePin,1);

		while (!(ADC_PIN&tmpval));				// Control rooms, until the MOSFET scolded and on high goes to drain

		R_DDR = 0;
		ADCSRA |= (1<<ADSC);

		while (ADCSRA&(1<<ADSC));
		
		gthvoltage += (MAX_ADC - ADCW);
		R_DDR = tmpval2;
	      }

	      gthvoltage *= 3;						// Conversion in mV, together with the division by 8 (with the LCD announcement)
	    }
	  }
	  b = TristatePin;
	  c = LowPin;
	  e = HighPin;
	}
      }

									// Tristate (basis assumed) on pluses, to the test on npn
      ADC_PORT = 0x00;						// Low pin to ground
      tmpval = (TristatePin * 2);					// necessarily because of the arrangement of the resistances
      tmpval2 = (HighPin * 2);						// necessarily because of the arrangement of the resistances
      R_DDR = (1<<tmpval) | (1<<tmpval2);				// High pin and Tristate pin on exit
      R_PORT = (1<<tmpval) | (1<<tmpval2);				// High pin and Tristate pin over R_L on Vcc
      ADC_DDR = (1<<LowPin);					// Low pin on exit
      _delay_ms(10);
      adcv[1] = ReadADC(HighPin);					// Voltage at the High pin measure
      
      if(adcv[1] < 500) {
	if(PartReady == 1) goto testend;
									// Device leads => NPN transistor or the like.
									// Test on thyristor:
									// Gate unload
	R_PORT = (1<<tmpval2);						// Tristate pin (gate) over R_L to ground
	_delay_ms(10);
	R_DDR = (1<<tmpval2);						// Tristate pin (gate) high impedance
									
	_delay_ms(5);
	adcv[3] = ReadADC(HighPin);					// Again Voltage at the High pin (anode assumed) measure

	R_PORT = 0;							// High pin (anode assumed) to ground
	_delay_ms(5);
	R_PORT = (1<<tmpval2);						// High pin (anode assumed) pluses
	_delay_ms(5);
	adcv[2] = ReadADC(HighPin);					// Again Voltage at the High pin (anode assumed) measure

	if((adcv[3] < 500) && (adcv[2] > 900)) {			// After switching the holding current off the thyristor must close
									// was switched before disconnection of triggering Rome and is still switched although gate out => Thyristor
	  PartFound = PART_THYRISTOR;
									//  Test on Triac
	  R_DDR = 0;
	  R_PORT = 0;
	  ADC_PORT = (1<<LowPin);				// Low-Pin fest auf Plus - Low pin firmly on pluses
	  _delay_ms(5);
	  R_DDR = (1<<tmpval2);						// HighPin over R_L to ground
	  _delay_ms(5);

	  if(ReadADC(HighPin) > 50) 
	    goto savenresult;						// - Voltage at the High pin (more assumed a2) measure; if too highly: Device leads now => no triac

	  R_DDR |= (1<<tmpval);						// Gate also over R_L to ground => Triac would have to ignite
	  _delay_ms(5);

	  if(ReadADC(TristatePin) < 200) 
	    goto savenresult;						// - Voltage at the Tristate pin (assumed gate) measure; Abort if Voltage too small

	  if(ReadADC(HighPin) < 150) 
	    goto savenresult;						// Device does not lead now => no triac => Abort

	  R_DDR = (1<<tmpval2);						// TriState act pin (gate) again high impedance
	  _delay_ms(5);

	  if(ReadADC(HighPin) < 150) 
	    goto savenresult;    					// - Device does not lead after switching the gate stream off any longer => no triac => Abort

	  R_PORT = (1<<tmpval2);					// HighPin over R_L on pluses => Holding current out
	  _delay_ms(5);
	  R_PORT = 0;							// HighPin again over R_L to ground; Triac would have to now close
	  _delay_ms(5);

	  if(ReadADC(HighPin) > 50) 
	    goto savenresult;						// - Voltage at the High pin (more assumed a2) measure; if too highly: Device leads now => no triac

	  PartFound = PART_TRIAC;
	  PartReady = 1;
	  goto savenresult;
	}
									// Test on transistor or MOSFET
	tmpval++;
	R_DDR |= (1<<tmpval);						// Tristate pin (basis) on exit
	R_PORT |= (1<<tmpval);						// Tristate pin (basis) over R_H on pluses
	_delay_ms(50);
	adcv[1] = ReadADC(HighPin);					// Voltage at the High pin (assumed collector) measure
	adcv[2] = ReadADC(TristatePin);					// Base voltage measure
  
	if((PartFound == PART_TRANSISTOR) || (PartFound == PART_FET)) 
	  PartReady = 1;						// examine whether test already times run

	hfe[PartReady] = MAX_ADC - adcv[1];
	uBE[PartReady] = MAX_ADC - adcv[2];

	if(adcv[2] < 500) {
	  PartFound = PART_TRANSISTOR;					// NPN transistor found (basis is " " downward; pulled)
	  PartMode = PART_MODE_NPN;
	} else {
	    if(adcv[0] < 20) {						// - Durchlassspannung in the closed condition small enough? (otherwise D-mode-FETs are falsely recognized as E-mode)
	      PartFound = PART_FET;					// N-channel-MOSFET found (basis/gate is not " " downward; pulled)
	      PartMode = PART_MODE_N_E_MOS;
	      tmpval2 = R_DDR;						// Gate threshold voltage measure
	      tmpval=(1<<HighPin);
	      ADMUX = TristatePin | (1<<REFS0);
	      gthvoltage = 0;

	      for(b=0;b<13;b++) {
		wdt_reset();
		DischargePin(TristatePin,0);

		while ((ADC_PIN&tmpval));	 			// Control rooms, until the MOSFET scolded and on low goes to drain 

		R_DDR = 0;
		R_PORT = 0;
		ADCSRA |= (1<<ADSC);

		while (ADCSRA&(1<<ADSC));

		gthvoltage += ADCW;

		R_DDR = tmpval2;
		R_PORT = tmpval2;
	      }

	      gthvoltage *= 3;						// Conversion in mV, together with the division by 8 (with the LCD announcement)
	    }
	  }

	  savenresult:
	  b = TristatePin;
	  c = HighPin;
	  e = LowPin;
	}

	ADC_DDR = 0x00;
	ADC_PORT = 0x00;
									// Finished
	} else {							// Passage
									// Test auf Diode
	    tmpval2 = (2<<(2*HighPin));					// R_H
	    tmpval = (1<<(2*HighPin));					// R_L
	    ADC_PORT = 0x00;
	    ADC_DDR = (1<<LowPin);				// Low pin to ground, High pin still is over R_L on Vcc
	    DischargePin(TristatePin,1);				// Unloaded for P-channel-MOSFET
	    _delay_ms(5);
	    adcv[0] = ReadADC(HighPin) - ReadADC(LowPin);
	    R_DDR = tmpval2;						// High pin over R_H on pluses
	    R_PORT = tmpval2;
	    _delay_ms(5);
	    adcv[2] = ReadADC(HighPin) - ReadADC(LowPin);
	    R_DDR = tmpval;						// High pin over R_L on pluses
	    R_PORT = tmpval;
	    DischargePin(TristatePin,0);				// Unloaded for N-channel-MOSFET
	    _delay_ms(5);
	    adcv[1] = ReadADC(HighPin) - ReadADC(LowPin);
	    R_DDR = tmpval2;						// High pin over R_H on pluses
	    R_PORT = tmpval2;
	    _delay_ms(5);
	    adcv[3] = ReadADC(HighPin) - ReadADC(LowPin);
		/*      Without unloading it can come to wrong identifications, since the gate of a MOSFETs can be still loaded. 
		        The additional measurement with " großen" R_H resisted is accomplished, around antiparallel diodes of 
		        Resistances differentiate to be able.
		        A diode has a Durchlassspg relatively independent of the passage stream. 
		        With a resistance the voltage drop changes strongly (linear) with the river. 
		*/
	    if(adcv[0] > adcv[1]) {
	      adcv[1] = adcv[0];					// the higher value wins
	      adcv[3] = adcv[2];
	    }

	    if((adcv[1] > 30) && (adcv[1] < 950)) {			// Voltage is over 0,15V and under 4,64V => Ok one 
	      if((PartFound == PART_NONE) || (PartFound == PART_RESISTOR)) 
		PartFound = PART_DIODE;					// Diode by default.
									// Otherwise there would be problems with transistors with protection diode 
	      diodes[NumOfDiodes].Anode = HighPin;
	      diodes[NumOfDiodes].Cathode = LowPin;
	      diodes[NumOfDiodes].Voltage = (adcv[1]*54/11);		// - Multiply by approx 4,9, in order to receive from the ADC the Voltage in millivolts
	      NumOfDiodes++;

	      for(uint8_t i=0;i<NumOfDiodes;i++) {
		if((diodes[i].Anode == LowPin) && (diodes[i].Cathode == HighPin)) {	// two antiparallel diodes: Defectively or duo LED 
		  if((adcv[3]*64) < (adcv[1] / 5)) {			// Durchlassspannung falls with smaller test stream strongly off => Defectively
		    if(i<NumOfDiodes) {
		      for(uint8_t j=i;j<(NumOfDiodes-1);j++) {diodes[j].Anode = diodes[j+1].Anode;\
			                                      diodes[j].Cathode = diodes[j+1].Cathode;\
			                                      diodes[j].Voltage = diodes[j+1].Voltage;
		      }
		    }

		    NumOfDiodes -= 2;
	          }
	        }
	      }
	    }
	}

									// Test on resistance
	tmpval2 = (2<<(2*HighPin));					// R_H
	tmpval = (1<<(2*HighPin));					// R_L
	ADC_PORT = 0x00;
	ADC_DDR = (1<<LowPin);				// Low pin to ground
	R_DDR = tmpval;							// High pin over R_L on pluses
	R_PORT = tmpval;
	adcv[2] = ReadADC(LowPin);
	adcv[0] = ReadADC(HighPin) - adcv[2];
	R_DDR = tmpval2;						// High pin over R_H on pluses
	R_PORT = tmpval2;
	adcv[3] = ReadADC(LowPin);
	adcv[1] = ReadADC(HighPin) - adcv[3];

									// Measurement of the voltage difference between the positive terminal of R_L and R_H and Vcc
	tmpval2 = (2<<(2*LowPin));					// R_H
	tmpval = (1<<(2*LowPin));					// R_L
	ADC_DDR = (1<<HighPin);				// High pin on exit
	ADC_PORT = (1<<HighPin);				// High pin firmly on pluses
	R_PORT = 0;
	R_DDR = tmpval;							// Low pin over R_L to ground
	adcv[2] += (MAX_ADC - ReadADC(HighPin));
	R_DDR = tmpval2;						// Low pin over R_H to ground
	adcv[3] += (MAX_ADC - ReadADC(HighPin));

	if(((adcv[0] - adcv[2]) < 900) && ((adcv[1] - adcv[3]) > 20)) goto testend; 	// Voltage drops with small test stream not far enough

	if(((adcv[1] * 32) / 31) < adcv[0]) {				// - Sloping Voltage does not drop with smaller test stream strongly and it exists " Almost Kurzschluss" => Resistance
	  if((PartFound == PART_DIODE) || (PartFound == PART_NONE) || (PartFound == PART_RESISTOR)) {
	    if((tmpPartFound == PART_RESISTOR) && (ra == LowPin) && (rb == HighPin)) {
					/* The Device was tested already once with reverse polarity. Now compare both results with one another.
					If they are quite similar, it concerns (in all probability) a resistance. 
					*/
	      if(!((((adcv[0] + 100) * 6) >= ((rv[0] + 100) * 5)) &&  \
		   (((rv[0] + 100) * 6) >= ((adcv[0] + 100) * 5)) &&  \
		   (((adcv[1] + 100) * 6) >= ((rv[1] + 100) * 5)) &&  \
		   (((rv[1] + 100) * 6) >= ((adcv[1] + 100) * 5)))) {	// min. 20% deviation => no resistance
		tmpPartFound = PART_NONE;
		goto testend;
	      }

	      PartFound = PART_RESISTOR;
	    }

	    rv[0] = adcv[0];
	    rv[1] = adcv[1];

	    radcmax[0] = MAX_ADC - adcv[2];				// - V at the Low pin is not completely zero, but approximately 0,1V (however one measures).
	    radcmax[1] = MAX_ADC - adcv[3];
	    ra = HighPin;
	    rb = LowPin;
	    tmpPartFound = PART_RESISTOR;
	  }
	}

	testend:
	ADC_DDR = 0x00;
	ADC_PORT = 0x00;
	R_DDR = 0;
	R_PORT = 0;
}									// End of CheckPins()
void checkpins(u8 HighPin, u8 LowPin, u8 TristatePin){
	u16 InitialADC, HighPinADC,TristatePinADC, LowPinADC;

	//LowPin 680ohm R to ground
	R_680(LowPin, LOW);
	//HighPin to output/high
	R_0(HighPin, HIGH);

	Delay_MS(5);

	DischargePin(TristatePin, LOW); //for MOSFETs	

	//Read ADC on LowPin
	InitialADC=ReadADC(LowPin);

	//if ADC >= 200{ //discharge other way for P MOSFETs
		//discharge; 
		//read ADC again;
	//}

	//if(ADC>19) {
	// N-JFET, N-MOSFET
	//P-JFET P-MOSFET
	//}

	//reset pins
	//LowPin 680ohm R to ground
	R_680(LowPin, LOW);
	//HighPin to output/high
	R_0(HighPin, HIGH);

	if(InitialADC<200){
		/****************************************/
		//
		//	PNP tests
		//	If this PNP:
		// 	then collector should be high when base ground and emitter at +5
		//
		/*****************************************/
		//C, B low through 680R, E high
		R_680(TristatePin, LOW); //base to ground through 680R
		Delay_MS(2);
		
		//read LowPin ADC
		//PNP allows current from Emitter to Collector and should be high
		LowPinADC=ReadADC(LowPin);

		if(LowPinADC>700){//PHP active, current flowing to Emitter
			HiZ(TristatePin);//Base HiZ
			R_470K(TristatePin, LOW); //Base to 470K low
			
			Delay_MS(10);

			//read LowPin ADC for hfe
			LowPinADC=ReadADC(LowPin);
			
			//read TristatePin ADC, see if base is transistor (>2volts) or FET (<2volts)
			//save value for uBE too
			TristatePinADC=ReadADC(TristatePin);
			if((PartType==PART_TRANSISTOR) || (PartType==PART_FET))RepeatDetect=1;
			hfe[RepeatDetect]=LowPinADC;
			vBE[RepeatDetect]=TristatePinADC;

			//if(PartFound != PART_THYRISTOR) {
			if(TristatePinADC>200){ //high base voltage is transistor
				//PNP transistor found
				PartType=PART_TRANSISTOR;
				PartMode=PART_MODE_PNP;
			}else{ //low base voltage is MOSFET
				//MOSFET and tests
				PartType=PART_FET;
				PartMode=PART_MODE_PNP;
			}
			c=LowPin;
			e=HighPin;
			b=TristatePin;

		}

		/****************************************/
		//
		//	NPN tests
		//	If this NPN:
		// 	then collector should hold pullup low when base high and emitter to ground
		//
		/*****************************************/
		//B,C high through pullup, E low
		//LowPin ground
		R_0(LowPin, LOW);
		//HighPin and TristatePin 680R high
		R_680(HighPin, HIGH);
		R_680(TristatePin, HIGH);
		
		//read HighPin ADC to see if NPN holds the 680R pullup low...
		HighPinADC=ReadADC(HighPin);

		if(HighPinADC<500){//NPN grounds the weak pullup
			//THYRISTOR test

			/***************************************/
			//	Transistor or MOSFET test
			//	Transistor is current controlled, the base voltage after the resistor will be ~0.6-1volts
			//	MOSFET is voltage controlled, the base voltage after the resistor will be close to the supply (5volts)
			/***************************************/
			//TristatePin 470K high
			R_470K(TristatePin, HIGH);				
			//read HighPin ADC for hfe
			HighPinADC=ReadADC(HighPin);
			
			//read TristatePin ADC, see if base is transistor (<2volts) or FET (>2volts)
			//save value for uBE too
			TristatePinADC=ReadADC(TristatePin);
			if((PartType==PART_TRANSISTOR) || (PartType==PART_FET))RepeatDetect=1;
			hfe[RepeatDetect]=1023-HighPinADC;
			vBE[RepeatDetect]=1023-TristatePinADC;

			if(TristatePinADC<500){ //low base voltage is transistor
				//NPN transistor found
				PartType=PART_TRANSISTOR;
				PartMode=PART_MODE_NPN;
			}else{ //high base voltage is MOSFET
				//MOSFET and tests
				PartType=PART_FET;
				PartMode=PART_MODE_NPN;
				c=((1<<HighPin)<<3);
				DischargePin(TristatePin, LOW); //for MOSFETs
				R_470K(TristatePin, HIGH);
				while(R0_IN&c);	
				gthvoltage=ReadADC(TristatePin);			

			}
			c=HighPin;
			e=LowPin;
			b=TristatePin;
		}//adc<500
	}//adc < 200	
	//NON transistor tests
	//Diode and internal part diodes
/*	
	//Resistors
	//LowPin ground
	R_0(LowPin, LOW);
	//HighPin 680 high
	R_680(HighPin, HIGH);
	LowPinADC1=ReadADC(LowPin);
	HighPinADC1=ReadADC(HighPin)-LowPinADC1;
	//HighPin 470K high
	R_470K(HighPin, HIGH);	
	LowPinADC2=ReadADC(LowPin);
	HighPinADC2=ReadADC(HighPin)-LowPinADC2;

	//LowPin 680 ground
	R_680(LowPin, LOW);
	//highpin to Vcc
	R_0(HighPin, HIGH);
	LowPinADC1+=(1023-ReadADC(HighPin));
	//LowPin 470K ground
	R_470K(HighPin, HIGH);	
	LowPinADC2+=(1023-ReadADC(HighPin));

	if(((HighPinADC1 - LowPinADC1) < 900) && ((HighPinADC2 - LowPinADC2) > 20)) goto testend; //not a resistor
	if(((HighPinADC2 * 32) / 31) < HighPinADC1) {
		if((PartFound == PART_DIODE) || (PartFound == PART_NONE) || (PartFound == PART_RESISTOR)) {
			if((tmpPartFound == PART_RESISTOR) && (ra == LowPin) && (rb == HighPin)) {


				if(!((((adcv[0] + 100) * 6) >= ((rv[0] + 100) * 5)) && (((rv[0] + 100) * 6) >= ((adcv[0] + 100) * 5)) && (((adcv[1] + 100) * 6) >= ((rv[1] + 100) * 5)) && (((rv[1] + 100) * 6) >= ((adcv[1] + 100) * 5)))) {
					//min. 20% Abweichung => kein Widerstand
					tmpPartFound = PART_NONE;
					goto testend;
				}
				PartFound = PART_RESISTOR;
			}
			rv[0] = adcv[0];
			rv[1] = adcv[1];

			radcmax[0] = 1023 - adcv[2];	//Spannung am Low-Pin ist nicht ganz Null, sondern rund 0,1V (wird aber gemessen). Der dadurch entstehende Fehler wird hier kompenisert
			radcmax[1] = 1023 - adcv[3];
			ra = HighPin;
			rb = LowPin;
			tmpPartFound = PART_RESISTOR;
		}
	}
*/
testend:	
	HiZ(HighPin);
	HiZ(LowPin);
	HiZ(TristatePin);

}
Example #3
0
void ReadCapacity(uint8_t HighPin, uint8_t LowPin) {
  if(PartFound == PART_CAPACITOR) 
    goto end;								// Already a condenser found
  
  unsigned long gcval = 0;
  unsigned int tmpint = 0;
  uint8_t extcnt = 0;
  uint8_t tmpx = 0;

  tmpval2 = (2<<(2*HighPin));						// R_H
  tmpval = (1<<(2*HighPin));						// R_L
  ADC_PORT = 0x00;
  R_PORT = 0;
  R_DDR = 0;
  ADC_DDR = (1<<LowPin);					// Low pin to ground
  R_DDR = tmpval2;							// HighPin over R_H to ground
  _delay_ms(5);
  adcv[0] = ReadADC(HighPin);
  DischargePin(HighPin,1);
  adcv[2] = ReadADC(HighPin);
  _delay_ms(5);
  adcv[1] = ReadADC(HighPin);
  wdt_reset();

  if((adcv[1] > (adcv[0] + 1)) || (adcv[2] > (adcv[0] + 1))) {		// Voltage rose
    R_DDR = tmpval;							// High pin over R_L to gnd
    
    while(ReadADC(HighPin) > (ReadADC(LowPin) + 10)) {
      wdt_reset();
      tmpint++;

      if(tmpint == 0) {
	extcnt++;

	if(extcnt == 30) 
	  break;							// High pin over R_L to gnd
      }
    }
    
    tmpint = 0;
    extcnt = 0;
    R_PORT = tmpval;							// High pin over R_L on pluses
    _delay_ms(5);
    adcv[2] = ReadADC(HighPin);
    _delay_ms(80);
    adcv[3] = ReadADC(HighPin);
    
    if((adcv[3] < (adcv[2] + 3)) && (adcv[3] < 850)) 
      goto end;								// Voltage is not considerably increasedly => Abort
    
    if((NumOfDiodes > 0) && (adcv[3] > 950) && (PartFound != PART_FET)) 
      goto end; 							// in all probability (or several) a diode (n) in check direction, which is otherwise falsely recognized as condenser
    	
    R_PORT = 0;
    
    while(ReadADC(HighPin) > (ReadADC(LowPin) + 10)) {
      wdt_reset();
      tmpint++;
      if(tmpint == 0) {
	extcnt++;

	if(extcnt == 30) 
	  break;							// Timeout for unloading
      }
    }
    
    tmpint = 0;
    extcnt = 0;
    ADC_DDR = 7;						// all pins on exit and from gnd
    R_PORT = tmpval;							// HighPin over R_L on pluses
    tmpval=(1<<HighPin);
    _delay_ms(2);
    ADC_DDR = (1<<LowPin);					// Condenser over R_L slowly load
    
    while (!(ADC_PIN & tmpval)) {					// Control rooms, until HighPin goes on High; Loop lasts 7 cycles
      wdt_reset();
      tmpint++;
      
      if(tmpint == 0) {
	extcnt++;

	if(extcnt == 30) 
	  break;							// Timeout for load
      }
    }

    if((extcnt == 0) && (tmpint<256)) {					// Low capacity
      ADC_DDR = (1<<LowPin);	
									// with R_H measure again
      R_PORT = 0;
      tmpint = 0;
      extcnt = 0;

      while(ReadADC(HighPin) > (ReadADC(LowPin) + 10)) {
	wdt_reset();
	tmpint++;

	if(tmpint == 0) {
	  extcnt++;

	  if(extcnt == 30) 
	    break;							// Timeout for unloading
	}
      }

      tmpint = 0;
      extcnt = 0;
      ADC_DDR = 7;						// all pins on exit
      ADC_PORT = 0x00;						// all pins to ground
      R_DDR = tmpval2;							// HighPin over R_H on exit
      R_PORT = tmpval2;							// HighPin over R_H on pluses
      _delay_ms(2);

      if(PartFound == PART_FET) 
	ADC_DDR = (7 & ~tmpval);				// - Condenser over R_H slowly load, free pin (drain) for gate capacity measurement on gnd
      else 
	ADC_DDR = (1<<LowPin);				// Condenser over R_H slowly load

      while (!(ADC_PIN & tmpval)) {					// Control rooms, until HighPin goes on High; Loop lasts 7 cycles
	wdt_reset();
	tmpint++;

	if(tmpint == 0) {
	  extcnt++;

	  if(extcnt == 30) 
	    break;							// Timeout for capacity measurement
	}
      }

      tmpx = 1;
    }

    if(tmpx) {
      gcval = eeprom_read_word(&H_CAPACITY_FACTOR);

      if((extcnt == 0) && (tmpint < 5)) 
	goto end;							// Capacity too small

      cv = 1;
    } else {
	gcval = eeprom_read_word(&L_CAPACITY_FACTOR);
	cv = 1000;
      }

    gcval *= (unsigned long)(((unsigned long)extcnt * 65536) + (unsigned long)tmpint);	// Unrechnen worth and store
    gcval /= 100;
    cv *= gcval;

    PartFound = PART_CAPACITOR;						// Condenser found

    ca = HighPin;
    cb = LowPin;
									// Condenser again unload
    tmpint = 0;
    extcnt = 0;
    R_DDR = (1<<(2*HighPin));						// High pin over R_L to ground
    R_PORT = 0;

    while(ReadADC(HighPin) > (ReadADC(LowPin) + 10)) {
      wdt_reset();
      tmpint++;

      if(tmpint == 0) {
	extcnt++;

	if(extcnt == 30) break;						// Timeout for unloading
      }
    }

    ADC_DDR = 7;						// Timeout for unloading
    ADC_PORT = 7;
    _delay_ms(10);
									// Finished
  }
  
  end:
  ADC_DDR = 0x00;
  ADC_PORT = 0x00;
  R_DDR = 0;
  R_PORT = 0; 
}									// End of ReadCapacity()