Hello "Cattledog",
Thank you very much for your suggestion, which I have incorporated in my code.
Hello All,
I have incorporated the capacitive measurment using two Digital pins instead of using one digital and an analog pin, which was for reading the voltage transition.
I try to use the digital pin with "attach interrupt" function in mode - FALLING. I am using Timer1 with prescaler =1 and starting the timer just when the discharging starts and stop when the voltage across the capacitor makes a transition from High to Low. But the timer counts across consecutive discharging cycles is varying erroneously.
Can anyone tell me what might be the Problem in the code as the timer counts are not the same (stable) or atleast with in a good tolerance.
circuit schematics : a resistor is connected between pin 4 and pin2, a capacitor is connected between pin2 and GND. Digital pin2 and Analog A0 are shorted (to measure the capacitance voltage - for debugging purpose).
I provide my code here.
float capVolt;
float capVolt_raw;
// unsigned long startvalue; // long int max value 4,294,967,295
unsigned long timer1_currentvalue;
unsigned long dischargeTime_ns;
unsigned long capacitance_pF;
int ISR_read =0;
int outputpin = 4;
int timer_running =0;
const byte interruptPin = 2;
void setup() {
// put your setup code here, to run once:
// two IO pins required for capacitance measurement
// Digi Pin 4 is configured as Output to charge the unknown capacitance.
pinMode(outputpin,OUTPUT);
digitalWrite(outputpin,LOW);
// Digi Pin 2 (an interrupt pin) is configured as Input to measure the unknown capacitance.
// digital pin as input provides high impedance state
pinMode(interruptPin,INPUT);
// while the Cap discharge, its voltage transition from High to Low at 2,42V triggers the ISR.
// attachInterrupt(digitalPinToInterrupt(interruptPin), ISR_state_high_to_low, FALLING);
attachInterrupt(0, ISR_state_high_to_low, FALLING);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
// discharge cap if any charge left
// digitalWrite(outputpin,LOW);
PORTD = PORTD & B11101111;
delay (500);
// R-C measurement, R = 1M ohm; Capacitance value eg., 10pF
// start charging capacitor ===================================================
// digitalWrite(outputpin,HIGH);
PORTD = PORTD | B0010000;
delay (1000);
// now capacitor is fully charged=================================================
// start discharging capacitor ===================================================
// digitalWrite(outputpin,LOW);
PORTD = PORTD & B11101111;
if (timer_running == 0)
{
// Congigure Timer1
TCCR1B = 0;//stop timer
TCNT1 = 0;//clear timer counts
TCCR1B = 1<<CS10;// start timer with prescaler = 1; 62.5ns per count
timer_running =1;
}
while (ISR_read == 0)
{
readVoltage();
}
if (ISR_read == 1)
{
ISR_read =0;
TCCR1B = 0;//stop timer
TCNT1 = 0;//clear timer counts
readVoltageISR();
Serial.println("timer1 counts ");
Serial.println(timer1_currentvalue);
/* dischargeTime_ns = timer1_currentvalue; // 1 count = 62.5ns
dischargeTime_ns = (dischargeTime_ns * 62.5);
Serial.println("Capacitor dischargeTime ns ");
Serial.println(dischargeTime_ns);
// ln (V1 / V tl) = ln (5V/2.42V) = 1,378
capacitance_pF = (dischargeTime_ns * 1378)/1000;
Serial.println("Capacitor value fF ");
Serial.println(capacitance_pF);
capacitance_pF = capacitance_pF/1000;
Serial.println("Capacitor value pF ");
Serial.println(capacitance_pF);*/
}
// to stop serial communication , user must input 'a'
if(Serial.available() > 0)
{ char ch = Serial.read();
if(ch == 'a')
{ while(1);
}
}
delay(1000);
}
void readVoltage()
{
capVolt_raw = analogRead(A0);
capVolt = capVolt_raw * ( 5.0 / 1023.0);
Serial.println("Cap current value (V)");
Serial.println(capVolt);
}
void ISR_state_high_to_low()
{
TCCR1B = 0;//stop timer
timer1_currentvalue = TCNT1;
ISR_read = 1;
timer_running =0;
}
void readVoltageISR()
{
Serial.println("ISR triggered");
/*capVolt_raw = analogRead(A0);
capVolt = capVolt_raw * ( 5.0 / 1023.0);
Serial.println("ISR triggered Cap voltage value");
Serial.println(capVolt);*/
}
Also I like to provide you with the code output (obtained through serial communication) with code tagging, so to avoid scrolling and not making it a big page..
Please look at the TImer1 counts value (which is not stable)
Cap current value (V)
4.80
Cap current value (V)
4.42
Cap current value (V)
4.07
Cap current value (V)
3.64
Cap current value (V)
2.63
ISR triggered
timer1 counts
32
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.23
Cap current value (V)
3.77
Cap current value (V)
2.72
ISR triggered
timer1 counts
226
Cap current value (V)
4.99
Cap current value (V)
4.58
Cap current value (V)
4.23
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
54
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
194
Cap current value (V)
4.99
Cap current value (V)
4.58
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
217
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
219
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.77
Cap current value (V)
2.72
ISR triggered
timer1 counts
27
Cap current value (V)
4.99
Cap current value (V)
4.58
Cap current value (V)
4.23
Cap current value (V)
3.77
Cap current value (V)
2.72
ISR triggered
timer1 counts
196
Cap current value (V)
4.99
Cap current value (V)
4.58
Cap current value (V)
4.22
Cap current value (V)
3.77
Cap current value (V)
2.72
ISR triggered
timer1 counts
19
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.23
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
130
Cap current value (V)
4.98
Cap current value (V)
4.58
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
67
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
171
Cap current value (V)
4.98
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
248
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.77
Cap current value (V)
2.73
ISR triggered
timer1 counts
5
Cap current value (V)
4.99
Cap current value (V)
4.58
Cap current value (V)
4.23
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
242
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.23
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
18
Cap current value (V)
4.99
Cap current value (V)
4.58
Cap current value (V)
4.22
Cap current value (V)
3.77
Cap current value (V)
2.72
ISR triggered
timer1 counts
226
Cap current value (V)
4.99
Cap current value (V)
4.59
Cap current value (V)
4.22
Cap current value (V)
3.78
Cap current value (V)
2.72
ISR triggered
timer1 counts
18
Could be problem with the timer configuration. I am doubtful in the way I read the timer counts. I just stopped the timer when the ISR hits and take this value to multiply with 62,5ns to get the time in ns.
Do my code is not optimised enough to make 62,5ns resolution measurement or the oscillator is not stable with prescalar "1".
Kindly give your suggestions.
Thanks in advance
muthu.