I am using Timer3 as code posted below. I call startCounting() before a task
executes and I call stopCounting after task executes.

But, I am getting variable result. Is there any mistake in my code or
somebody can provide code performing similar job.

#include "printf.h"

uint16_t coarseValues;
uint16_t fineValues;
uint16_t COARSECOMPARE = 0x7FFF;
uint16_t fineOffset = 0x000E;
uint16_t coarseOffset = 0x0021;
uint16_t coarseWasCalled = 0;

module ClockCount
{
   provides
   {
      interface TaskCount as TC;
   }

   uses interface HplAtm128Compare<uint16_t>;
}

implementation
{

   async event void HplAtm128Compare.fired()
   {
      TCCR3B = 0x00;
      atomic coarseWasCalled++;
      atomic coarseValues++;
      TCNT3 = 0x0000; //Counter3 clear
      TCCR3B = 0x09;
   }

   async command void TC.startCounting()
   {
      atomic coarseValues = 0;
      atomic fineValues = 0;

      //Actual Timer/Counter
      TCNT3 = 0x0000;

      //Set counter clear on match (Couter Control Registers)
      TCCR3A = 0x80;
      TCCR3B = 0x09;
      //Interupt mask set (Extended Timer Interrupt Mask Register)
      ETIMSK = 0x10;

      //Compare values (Output Compare Register)
      OCR3A = COARSECOMPARE;

   }

   void calculateFine()
   {
      uint16_t callOffset = (coarseWasCalled * coarseOffset)/COARSECOMPARE;
      //Was Commented............
      atomic coarseValues = coarseValues + callOffset;

      if (coarseWasCalled)
      {
         atomic fineValues = TCNT3 - coarseOffset;
         atomic coarseWasCalled = 0;
      }
      else
      {
         //there is a 13 overhead on running the counter
         atomic fineValues = TCNT3 - fineOffset;
      }
   }

   async command void TC.stopCounting()
   {
      TCCR3B = 0x00;
      calculateFine();
   }

   command unsigned int TC.getCoarseValue() {atomic return coarseValues;}
   command unsigned int TC.getFineValue() {atomic return fineValues; }

   command void TC.setCoarseCompare(uint16_t newCompare)
   {
     COARSECOMPARE = newCompare;
     OCR3A = newCompare;
   }

   command void TC.setFineOffset(uint16_t off)
   {
      atomic fineOffset = off;
   }

   command void TC.setCoarseOffset(uint16_t off)
   {
      atomic coarseOffset = off;
   }

   command void TC.sendValuesToScreen()
   {
      printf("Fine Value is %u:\n", fineValues);
      printf("Coarse Value is %u:\n", coarseValues);
      printfflush();
   }
}
_______________________________________________
Tinyos-help mailing list
Tinyos-help@millennium.berkeley.edu
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to