Jump to content
43oh

Help with Frequency Meter, TivaC


Recommended Posts

Hello.

 

I need to measure a frequency from output VCO with a Timer, this frequency is between 1 MHz and 2 MHz.

 

Whit my other board i don't have any problem because i can use de library ''CounterLib'' for MSP430 with Energia.

 

Can i use any library for TivaC for use like a ''CounterLib'' with Energia?

 

Best Regard.

 

P.C.

 

Link to post
Share on other sites

Hello @@Jonoveve,

 

I'm not aware of a CounterLib for the ARM processors.  I have been meaning to look at it for the MSP432 but am still in the midst of learning the clocks and interrupts. Don't compare this too closely to the elegant CounterLib but if you just want to implement something in Energia that gets frequency the following might work:

 

*** PLEASE IGNORE THIS POST WHILE I DEBUG THE CODE ***  :D

// debugging
Link to post
Share on other sites

If you need to measure a frequency in the 1-2 Megahertz (MHz) range, I suspect that a hardware counter will be absolutely necessary. From the TM4C1294, it looks like the timers do support edge counting. You might also be able to use the quadrature input to measure counts over time too. 

Link to post
Share on other sites

 

*** PLEASE IGNORE THIS POST WHILE I DEBUG THE CODE ***  :D

hate it when that happens...

 

I have an interest in the limits of Energia so I played with this a bit this afternoon.  I don't have a frequency generator handy so I used another LaunchPad to generate a sloppy square wave that was 815 kHz on the oscilloscope  Using the Energia sketch below I was seeing error on the order of 4%.  At 1000 Hz it was more like a tenth of a percent.  Used a EK-TM4C123GXL LaunchPad and Energia V17.   I expect it only gets worse up to 2 MHz but if you try it I would be interested in how it works out...

const int counterPin = 10; // microcontroller pin for incoming pulse
volatile unsigned long counter = 0; // pulse counter incremented in ISR

void setup()
{
Serial.begin(115200);
Serial.println("Starting...");
pinMode(counterPin, INPUT);
attachInterrupt(counterPin, isr_Counter, RISING);
}

void loop()
{
Serial.print("Frequency = ");
Serial.println(getFrequency(1000));
}

// Count pulses for countMillis milliseconds and converts to frequency
long getFrequency(long countMillis) {
long frequency;
counter = 0; // reset counter
delay(countMillis); // counter incremented in the ISR during this period
frequency = counter; // grab counter as quickly as possible
frequency = (frequency * 1000) / countMillis;
return frequency;
}

void isr_Counter(void) { // interrupt service routine to count pulses
counter++;
}

UPDATE:  I was able to put something together that generated a faster signal - The sketch above hangs up somewhere before reaching 2 MHz.

Link to post
Share on other sites

Hi Fmilburn

 

I've tested your code and don't get a correct measurement of the frequency. I'm using a function generator as a source (square wave) and ranges from Hz to MHz, none of them measurements are correct. It would be helpful if resolved the problems. Thank you.

 

Regards

 

A.P.

 

Edit: I've tried again and I get to work it in a range from 0-500 kHz. But I need work in a (1-2) MHz range. Some ideas?? Thank you.

Link to post
Share on other sites

500 kHz isn't too bad for such simple code I suppose.  I don't have any more suggestions for going higher with Energia functions alone.  You will need to use the timers directly as was done in CounterLib for the MSP430.  It is quite doable but I have not worked my way through the ARM timers yet and it is not on my near term list of things to do so I won't be much help...

Link to post
Share on other sites

Hi,

 

Your code is very good to use in this range, thanks for your work.

I tried to make this code in Energia but that does not measure the frequency correctly. If someone could help me solve problems. Used  EK-TM4C123GXL LaunchPad and Energia V17. (Range  0-2 MHz).

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/tm4c123gh6pm.h" 
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/pin_map.h"

int timer;

void setup()
{

  Serial.begin(115200);

  unsigned long timer;
  void calc_freq();
  
  SysCtlClockSet (SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ | SYSCTL_USE_PLL | SYSCTL_SYSDIV_2_5);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);  
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

  //PinTypeTimer(T0CCP0);
  //GPIOPinTypeTimer(GPIOB,T0CCP0);

  //I want to use PIN-1 (T0CCP0 or PB6)

  GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_1);
  TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
  TimerConfigure(TIMER1_BASE, TIMER_CFG_A_CAP_COUNT);

  TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet() / 100);

  TimerIntRegister(TIMER0_BASE, TIMER_A, Timer0IntHandler);
  
  TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
  
  TimerEnable(TIMER0_BASE, TIMER_A);
  TimerEnable(TIMER1_BASE, TIMER_A);

   while(1)
    {
    	calc_freq();
    }
}

void loop()
{
 
  Serial.println(freq); 

}

void Timer0IntHandler() {
  TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
  timer = TimerValueGet(TIMER1_BASE,TIMER_A);
  TimerLoadSet(TIMER1_BASE, TIMER_A,10000);
}

void calc_freq()
{
	unsigned long count;
	char r[8];
	float time = 0.1;
	float freq;

    count = (10000 - timer) * 100;	// *100 converts kHz to Hz for easy displaying
    freq = time * count;		// Time is the time period the first timer is measuring, in this case it is 100mS
} 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Regard.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...