Benutzer:Don 1414

Aus der Mikrocontroller.net Artikelsammlung, mit Beiträgen verschiedener Autoren (siehe Versionsgeschichte)
Wechseln zu: Navigation, Suche

Hello Everyone , I am new to programming in IDE. I am using built-in timer of arduino.I am using TDR method to capture reflection from fault location in cable and for that I am using timer.Timer should start as soon as output is send and stop at reflection. I have a reference code for it but I dont able to understand it, so if anyone know about it , it would be great.


void setup() {

 pinMode(stepPin, OUTPUT);
 pinMode(refPin, OUTPUT);
 pinMode(shutdownPin, OUTPUT);
 
 TCCR1A = 0;
 TCCR1B = (1 << ICNC1);   // input capture noise canceller enabled, capture on falling edge
 TIMSK1 = 0;              // timer 1 interrupts disabled
 ACSR = 0;                // input capture from ICP1 pin
 
 TCCR2B = (1 << CS20);    // change timer 2 PWM frequency to 31.25kHz because we're using pin 11 as a DAC
 
 Serial.begin(19200);

}

struct Step {

 unsigned int time;
 unsigned int amplitude;

};

// Take a single measurement, using either a positive or negative edge from the comparator. // The comparator reference voltage must have been set up and allowed to stablise before calling this. unsigned int takeMeasurement(bool posEdge) {

 byte reg1b = (posEdge) ? 0 : (1 << ICES1);    // input capture noise canceller csdisabled, set up input capture polarity, stop timer
 reg1b |= (1 << CS10);
 TCCR1B = reg1b;
 TCNT1H = 0;
 TCNT1L = 0;              // clear timer
 unsigned int capture = 0;
 unsigned long start = micros();  // get the time
 
 cli();
 TCNT1H = 0;
 TCNT1L = 0;              // clear timer
 TIFR1 = (1 << ICF1);     // clear timer 1 input capture bit
 PORTD |= (1 << 4);       // set output high
 sei();
 do
 {
   if ((TIFR1 & (1 << ICF1)) && capture == 0)
   {
     byte temp = ICR1L;
     capture = (ICR1H << 8) | temp;
   }
 } while (micros() - start < 100);
 
 PORTD &= ~(1 << 4);          // set output low
 return capture;

}

size_t findSteps(bool positive, struct Step *results, size_t maxResults) {

 byte amplitude = (positive) ? 5 : 250;
 analogWrite(refPin, amplitude);
 delay(100);      // wait 100ms for the output to stabilise
 unsigned int lastReading = 0;
 size_t numResults = 0;
 unsigned int stepSize = 0;        // 0 means not in a step
  1. ifdef DEBUG
 Serial.print((positive) ? "pos " : "neg ");
  1. endif
 for (int i = 0; i < 50; ++i)
 {
   analogWrite(refPin, amplitude);
   delay(10);
   unsigned int currentReading = takeMeasurement(positive);
   unsigned int currentDiff = currentReading - lastReading;    // diff since start of possible step
   if (stepSize == 0)
   {
     // Not currently in a step
     if (i != 0 && currentReading != 0 && currentDiff == 0)
     {
       // Found the start of a possible step
       ++stepSize;
     }
     lastReading = currentReading;
   }
   else
   {
     if (currentDiff > 2 || i + 1 == 50)
     {
       // Step has endeed, so record it if it is big enough
       if (stepSize >= 2)
       {
         results->time = lastReading;
         results->amplitude = amplitude - 5;
         ++results;
         ++numResults;
         if (numResults == maxResults) break;
       }
       stepSize = 0;
       lastReading = currentReading;
     }
     else if (currentDiff == 0)
     {
       ++stepSize;
     }
   }
  1. ifdef DEBUG
   if (i != 0) Serial.write(',');
   Serial.print(currentReading);
  1. endif
   if (positive)
   {
     amplitude += 5;
   }
   else
   {
     amplitude -= 5;
   }
 }
  1. ifdef DEBUG
 Serial.println();
  1. endif
 return numResults;

}

// Convert a number of clocks delay to a cable length in metres float clocksToMetres(unsigned int clocks) {

 float delayTime = (float)clocks/(float)F_CPU;    // delay in seconds
 float distance in metres= (delayTime * 3.0e8 * propagationFactor)/2.0;
 
 Serial.print("distance in metres: ");
 return (distance in metres);