chipKIT® Development Platform

Inspired by Arduino™

Counting pulse using MPIDE w MX7

Created Mon, 18 Aug 2014 19:13:18 +0000 by marco


marco

Mon, 18 Aug 2014 19:13:18 +0000

Hi all,

In arduino, I can count the number of pulse occurring on a pin completely in hardware without any software processing time being consumed.

The code is written like this (as shown in Arduino cookbook, session 18.7) :[url]https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-18/recipe-18-7[/url]

/*
* HardwareCounting sketch
*
* uses pin 5 on 168/328
*/
const int hardwareCounterPin = 5; // input pin fixed to internal Timer
const int ledPin = 13;
const int samplePeriod = 1000; // the sample period in milliseconds
unsigned int count;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  // hardware counter setup (see ATmega data sheet for details)
  TCCR1A=0; // reset timer/counter control register A
}
void loop()
{
  digitalWrite(ledPin, LOW);
  delay(samplePeriod);
  digitalWrite(ledPin, HIGH);
  // start the counting
  bitSet(TCCR1B ,CS12); // Counter Clock source is external pin
  bitSet(TCCR1B ,CS11); // Clock on rising edge
  delay(samplePeriod);
  // stop the counting
  TCCR1B = 0;
  count = TCNT1;
  TCNT1 = 0; // reset the hardware counter
  if(count > 0)
  Serial.println(count);
}

Does chipkit provide the same implementation in MPIDE? If not, what would be the best implementation in MPIDE for chipkit with minimal software processing time?


JesseN

Thu, 21 Aug 2014 23:04:38 +0000

Hi,

What is your chipkit target board? An implementation would be as follows:

Note: I used ChipKit WF32.

#inlcude <peripheral/timer.h>>

In the setup: OpenTimer2(T2_SOURCE_EXT, Period); //tells the timer to expect an external source

In the loop: To start counting OpenTimer2(T2_ON, Period) ;

To stop counting CloseTimer2();

to read the timer value ReadTimer2();

to reset the timer WriteTimer2(0); // this would achieve your reset hardware counter.

Please note, that 'Period' is the sampling period before the timer resets its count.

Regards, Jesse N


marco

Mon, 25 Aug 2014 17:05:47 +0000

Hi Jesse,

Thank you for your reply.

I am using Cerebot Mx7cK.

Today I tried your suggestion by monitoring the count with serial port:

int Period = 5000;
void setup()
{
    // start serial port at 9600 bps:
  Serial.begin(9600);
  OpenTimer2(T2_SOURCE_EXT, Period); //tells the timer to expect an external source
  OpenTimer2(T2_ON, Period) ;
}

void loop()
{
  delay(1000);
  Serial.println(ReadTimer2());
  if (Serial.available() &gt; 0)
    WriteTimer2(0); // this would achieve your reset hardware counter.
}

The code compiled successfully, even excluding the #include line. Next I follow the manual and found the external source for Timer 2 is JP-C1. I short it to ground to ensure input slient. I then open the serial monitor and the results is shown like this:

3788 2805 1794 801 4809 3825 2815 1831 813 4821 3841 2831 1847 829 4837 3857 2847 1863 845 4853 3873 2863 1879 861 4869 3889 2879 1895 877 4885 3905 2895 1911 893 4901 3921 2911 1927 909 4917 3937 2927 1943 925 4933 3953 2943 1959 941 4949 3969 2959 1975 957 4965 3985 2975 1991 973 4981 4001 2991 2007

Apparently there is something wrong, the count should stay at 0.

Attached my sketch as well


JesseN

Thu, 28 Aug 2014 21:46:13 +0000

Hi,

Unfortunately just realized that Chipkit does not currently support external source timers. The timer libraries currently support internal timers. The solution provided earlier on would work in MPLAB X with the inclusion of PLIB.h which is not open source.

Best Regards, Jesse N