This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Capture pulses

Other Parts Discussed in Thread: MSP430G2553

I'm trying to capture pulses like a logic analyzer.
I hope the change of state of the leg and I time in microsgundos until it changes state.

 I've been reading you can do with the way TIMER_A 'CAP', but I do not understand how it works, and vector used.

I tried this, but not fast enough (it is a part of the code):
#######################################

#include  <msp430g2553.h>

#define MAXPULSE 25000
#define BUTTON BIT3

uint16_t pulses[100][2];  
uint8_t currentpulse = 0;

uint16_t highpulse, lowpulse;

DCOCTL = 0x00;                          // Set DCOCLK to 1MHz
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;


 
void capture(void) {
  

while(1){
  highpulse = lowpulse = 0;
 

  while ((BUTTON & P1IN))  { // pin is still HIGH
     highpulse++;
     __delay_cycles(1); 

     if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][1] = highpulse;
 

  while ((BUTTON & ~P1IN))  { // pin is still LOW
     lowpulse++;
     __delay_cycles(1); 

     if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][0] = lowpulse;
 
  currentpulse++;

}
}

####################################### 

 Regards

  • What your code does is 'sampling' the pin value. On each loop you get a value that is either '0' or '1'.
    The tierm capture feature works in a different way. It captures the 'moment' of a pulse. That means it freezes the value of the current timer count in the moment a pulse is deteted.

    So you only get one value per pulse, but it is a timestamp. Both has its advantages and disadvantages. And if done properly, both methods can be used to reconstruct the input signal.

    The disadvantage of the 'sampling' method are: the CPU is required to run in a deterministic way. So 100% CPU load are dedicated to the sampling process. And at least one bit storage is required per loop, which increases memory usage on high resolution. However, even high pulse frequencies can be sampled, as long as the loop is still faster than the input changes.

    The advantage of the timer method is: when no pulses are coming in, 100% of the CPU are free for other tasks. The resolution can be even higher than with the sampling loop, since the resolution depends ion the tiemr clock and not the code execution speed. Also, memory storage is only required when the input signal changes.
    However, there are disadvantages. While the resolution can be high (a mor eprecise determination of the exact moment), the maximum frequency is lower, since on every event an ISR needs to be called, which is slower than the maximum sample loop speed. To allow short pulses to be detected, it might be a good idea to use two CCR units, one for the rising and one for the falling edge of the input, so short pulses won't overflow the capture register with a second capture shortly aftert the first.
    Also, on faster pulse frequencies, the amoutn of memory required may be much larger than with the samplign method, since a timestamp is stored for each event rather than a single bit.

    About the 'vector' thing: The trick with most hardware in the MSP is, that when an event happens, an interrupt is triggered. If enabled, this causes the CPU to interrupt the current code execution (the main code) and exectute an ISR, an interrupt service routine, instead. Except for the hole in the timeline, the main code won't notice it.
    Each hardware module has its one (or even more than one) ISR vector in the vector table. This vector table holds the addresses of ISRs to be calle dwhen a certain interrupt happens.

    So in your case, you write a function that has to be executed when the timer CCR unit detects an input change. The funciton is marked as ISR, so the compier generates the proper code. It is also marked as tiemr ISR, so the compiler/linker can place the address of the funciton at the proper location in the vector table. That's all. Now every time the hardware detects an input change, the current code execution is interrupted as if there would just have been a function call to the timer ISR. If nothing happens, the main code continues unitnerrupted and does not need any CPU time for checking.

  • Hi Jens-Michael,
    Thank you very much for your interest.

    After reading your comment, I reflexiondo, and I modifcado my code, getting it to work with an error of + -10%, which is acceptable for my project.

    Modify the delay_cycles __ (1), by 3, and captured 20ums like, which do not understand if the CPU speed is 1Mhz.

    Pursuant to the TIMER-A, where I can find the information clear and easy?
    Could give an example, which told me of CCR?

    Again, thank you very much

    regards

  • On 1MHz, a delay of 1 cycle is 1µs. But each and every instructiont akes some µs too. So I guess your loop, and in addition the following code to store the pulse, takes some more cycles and therefore µs.

    The while loop with increment will probably compile to

    L1:
    TEST #BUTTON, &P1IN
    JZ L2
    INC &highpulse
    delay
    JMP L1
    L2:

    So while the while is true, this loop will take 3+2+4+delay+2 = 11 cycles + delay.
    If it is false, it takes 3+2 cycles, plus the execution time of the remaining code, until you enter the other while loop. So I'd say the minimum time required for detecting an edge is some 20µs (or even more) with a further granularity of 11+delay µs.

    I hope this answers your question, as I didn't exactly understood what you were wonderign about (I rather guessed).

    JavMainar said:
    Pursuant to the TIMER-A, where I can find the information clear and easy?


    The family users guide describes the complete working of the timers and the CCR modules in detail. It might not cover every expert trick but the basic functionailty is covered well. How to use this funcitonality is up to your imagination. :)

  • Hello
    Thanks for your time and help.
     I've been really helpful.

    I thank you again.

    regards

**Attention** This is a public forum