Jump to content
chicken

[Energia Library] Hardware Counter Library for MSP430

Recommended Posts

@@altineller

 

The timer/counters can be fed from various clocks. From the MSP430x2xx Family User's Guide:

post-9974-0-98454800-1444704696_thumb.png

 

The library currently only uses the external clock (TACLK), but with a small change this can be changed to an internal source. The relevant bits are TASSELx (9 and 8) in the register TACTL.

 

From the MSP430x2xx Family User's Guide:

 

TASSELx Bits 9-8 Timer_A clock source select

  • 00 TACLK
  • 01 ACLK
  • 10 SMCLK
  • 11 INCLK (INCLK is device-specific and is often assigned to the inverted TBCLK) (see the devicespecific data sheet)

Simply add those bits when my library starts the timer, e.g. around line 115 for the MSP430G2553:

 

// configure timer A0 to count upwards based on TA0CLK input
// halt timer, disable interrupts, TACLK clock source, reset counter
TACTL = TACLR | divider1_bits;  // clears MCx, TAIE, TASSEL
TACTL |= BIT8;  // set ACLK as timer input

 

I don't know what speeds ACLK and SMCLK are setup by Energia, so you will probably have to play with with dividers and/or adjust your original code a bit.

 

Officially supporting internal clocks as input is actually a good idea and I might add it in the future.

Share this post


Link to post
Share on other sites

RE clock speed in Energia: Looking at the source (wiring.c), SMCLK seems to be running at CPU clock.

 

Not quite sure what ACLK is set to. I think it's the external crystal, or internal LFO (8KHz or thereabouts) if there's no crystal.

Share this post


Link to post
Share on other sites

Hi @@StevenSegal

 

@@chicken will surely have a better answer than me but here are some thoughts.  First, CounterLib is a digital counter.  It will count every time the signal cycles LOW to HIGH.  So, if  that  is what you are looking for it might work for you.  Can  you supply additional information on the signal and what you are trying to do with it?  Is it a 0-3 V signal?  Are you only interested in when it goes above a threshold?

Share this post


Link to post
Share on other sites

Hello.

I have a quartz oscillator with fo=1.6 MHz and I have changed it features and now it band width is around 20 kHz. For this, i need to a good precision.

I am using the board msp430fr6989.

 

My code is the next:

#include <CounterLib_t.h>

Counter<CL_TimerB0> MyCounter; //pin P2.0

void setup() {
  MyCounter.start(CL_Div2); // 2 pulses 
  Serial.begin(9600);
}

void loop()
{
    MyCounter.reset();
    delay(1000);
    Serial.print((MyCounter.read()+32767.5*23)*2+32800);  //32765.5 = 65535/2          **CL_Div2**
                                                          //(MyCounter.read()+...)*2   **CL_Div2**
                                                          //32800 manual calibrate 
    Serial.println(" Hz");
    Serial.println(cont);
    delay(1000);
   }

With this code i get:

- From frequency generator: F=1.58 MHz     -> Measure with the board: F=1579589 Hz     -> Error: 0.026%

- From frequency generator: F=1.6 MHz       -> Measure with the board: F=1600093 Hz     -> Error: 0.0058%

- From frequency generator: F=1.62 MHz     -> Measure with the board: F=1620377 Hz     -> Error: 0.023%

 

The total BW is 127000 Hz

- From 1,542 MHz with error=0.077% to 1,669 MHz with error=0,074

 

For my proyect is a good precision.

 

I put images like a sample.

DSC_00021_zpseyic7dwj.jpg

 

Captura_zps7pm5b1j7.jpg

Share this post


Link to post
Share on other sites

@@erharishkrs

 

I don't have a FR4133 so can't test the code but try this:

#elif defined(__MSP430FR4133__)

  enum CL_TIMER_t
  {
    CL_TimerA0,    // FR4133 P1.5, pin 11 on LaunchPad
    CL_TimerA1,    // FR4133 P8.2, pin 9 on LaunchPad
  };
  
  // clk pin setup for each supported timer
  #define CL_TA0CLK_PIN_SETUP  { P1DIR &= ~BIT5; P1SEL0 &= ~BIT5; P1SEL1 |= BIT5; }
  #define CL_TA1CLK_PIN_SETUP  { P8DIR &= ~BIT2; P8SEL0 &= ~BIT2; P8SEL1 |= BIT2; }

Insert it into CounterLib_t.h just before the section at line 72 with the following code

#else
  #error 1) This microcontroller is not supported by CounterLib
#endif

If, in my haste, I did this correctly it should work with the examples on Pin 11 of the FR4133.  Please report back whether or not this works for you.

Share this post


Link to post
Share on other sites

I noticed that the library attached to the first post was a bit out of date. I replaced the ZIP file with a link to GitHub and also updated the table of supported devices.

 

I will include the FR4133 once someone confirms that it's working.

Share this post


Link to post
Share on other sites

@@Fmilburn After editing for msp430fr4133 in the counterLib it is showing compilation error-

In member function 'void Counter<timer>::start(CL_DIVIDER_t, uint8_t)': 'P1SEL1' & 'P8SEL1' were not declared in this scope

Share this post


Link to post
Share on other sites

@@erharishkrs

 

I don't have a FR4133 so can't test the code but try this:

#elif defined(__MSP430FR4133__)

  enum CL_TIMER_t
  {
    CL_TimerA0,    // FR4133 P1.5, pin 11 on LaunchPad
    CL_TimerA1,    // FR4133 P8.2, pin 9 on LaunchPad
  };
  
  // clk pin setup for each supported timer
  #define CL_TA0CLK_PIN_SETUP  { P1DIR &= ~BIT5; P1SEL0 &= ~BIT5; P1SEL1 |= BIT5; }
  #define CL_TA1CLK_PIN_SETUP  { P8DIR &= ~BIT2; P8SEL0 &= ~BIT2; P8SEL1 |= BIT2; }

Insert it into CounterLib_t.h just before the section at line 72 with the following code

#else
  #error 1) This microcontroller is not supported by CounterLib
#endif

If, in my haste, I did this correctly it should work with the examples on Pin 11 of the FR4133.  Please report back whether or not this works for you.

After editing for msp430fr4133 in the counterLib it is showing compilation error-

In member function 'void Counter<timer>::start(CL_DIVIDER_t, uint8_t)': 'P1SEL1' & 'P8SEL1' were not declared in this scope

Share this post


Link to post
Share on other sites

@@erharishkrs

 

OK, I just tried it and got the error as well.  I don't use the FR4133 and don't know what is going on.  The family guide has PxSEL1 but they aren't in the header file.  So, not sure whether I am misinterpreting things or not.  Maybe someone else familiar with it can have a look?

Share this post


Link to post
Share on other sites

Hello, can anyone help me to configure this library for TM4C123G launchpads ?? they have 80MHz clock speed. I want to measure upto 20MHz frequency( simple square wave with 50% duty cycle). 

Share this post


Link to post
Share on other sites

Hello @Anik96

The TM4C123G is an ARM processor and differs in both timers and registers from the MSP430 but read through chicken’s posts here:  

He describes clearly how to do it but you will need to account for the difference in processors. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×