Jump to content
43oh

[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.

Link to post
Share on other sites
  • Replies 46
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

From time to time, threads pop up where someone tries to count very fast pulses in the hundreds of kHz or even MHz range. There is a solution for the hardcore C-coders among us, but to my surprise the

When thinking about what Energia project to bring to Makerfaire, it occurred to me, that the counter library can count slower things than just wave forms in the MHz. You could say, the penny dropped.

Hello @@chicken   Very elegant and nicely documented.  I did a quick and dirty test to see what I could see...   Staying with the Energia theme, I wrote this little sketch and ran it on a EK-TM4C1

Posted Images

  • 1 month later...

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?

Link to post
Share on other sites
  • 2 months later...

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

Link to post
Share on other sites
  • 3 weeks later...

@@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.

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

Link to post
Share on other sites
  • 1 year later...

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...