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.

Pulse counting

Other Parts Discussed in Thread: MSP430F2252

Hello guys, 

 

I am trying to get my head around on counting pulses. I am using photo interrupter (GP1A51HRJ00F) and need to count pulses on one of the MSP430F2252 pin. The idea is to count pulses ( low = 0V and High=5V ) and then calculate a wind speed using calibration chart. I would appreciate if you give me idea or small program code. I am newbee so need help. 

  • Counting pulses is basically done using a Timer and the Capture capability. There are many threads on this topic, and many of them very good. Please use the search function to find them.

    One example thread is here:

    http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/p/18804/72582.aspx?sms_ss=email&at_xt=4d920c8ead9fff38%2C0#72582

     

    Good Luck,

    Gustavo

  • Gustavo, 

    The link you gave me did not give me any detail idea and I am still confuse. 

    I am trying to do the similar thing ... could you please write a simple program for me. 

     

     

    thanks. 

     

     

     

  • Hi, Please refer to the following link which has code that illustrates the counting of pulse. Here the interrupt mechanism of the port is used for doing this job. http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/p/102716/361162.aspx#361162 Here is the c code for quick reference: ----------------------------------------------------------------------------------------- /* Assuming the pulse is fed into the P1.4 port pin */ /* Include required header file here */ /* Variable to measure the number of interrupts on P1.4 */ unsigned int count = 0; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1IE |= 0x10; // P1.4 interrupt enabled P1IES |= 0x10; // P1.4 Hi/lo edge P1IFG &= ~0x10; // P1.4 IFG cleared _BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { count++; // Incrementing the counter when ever an interrupt comes P1IFG &= ~0x10; // P1.4 IFG cleared } ----------------------------------------------------------------------------------------- Regards, Nag (If your question is answered, please click the "Verify Answer" button below)
  • The server maintenance yesterday swallowed my lengthy answer (After pressing the 'post' button, I got the message 'the server is down for maintenance' and all my text was gone.)

    Okay, second try.

    The simplest solution is to poll teh content of a port pin and count if you see it changing from low to high. Of oyurse oyu also need to somehow count the time.

    So let's start there: to count the time, you need to set up a timer. Feed it with a known input frequency and you can configure it to trigger an interrupt every so often.
    You can count the interrupts in a global variable (volatile!) and your main loop that is observing the por tpin can check whether the variable has been incremented a certain value.

    Personally, I have a timer running on 1MHz and triggering an interrupt every 1000th count, so I get an interrupt every millisecond. SO I can simply check for delays of up to 65 seconds with 1ms resolution. (I have a second, independent seconds counter for long delays which is incremented every 100th interrupt))

    The second way to count pulses is to use the port interrupt. On every pin of port 1 or 2, you can trigger an interrupt when a positive or negative (configurabel) edge occurs. In the port interrupt funciton you incremente the pulse counter, and in the timer interrupt function you just read the current pulse counter value and compare it with the (stored) previous value.

    Next step is to let the hardware count. You can configure a second timer so it acts as a counter. Just switch it from using an internal clock (liek the timer for the delay) to an external clock (your pulse source). When the delay timer triggers an interrupt, read the value of the counter timer register.

    The last and most complex, yet most precise solution is to trigger the counter by the timer. To do so, you configure one of the delay timer's CCR registers to output a pulse on each interval. THis pulse is output on a port pin. Route this pulse to one of the coutner timers CCR units and configure this unit to capture the current count of the counter timer when a pulse comes. and trigger an interrupt. This way, you only have one interrupt when a time interval has passed, and the CCR register contains the counte rtimer value in the moment of the delay timer pulse. Build the difference to the last value and you're done.
    The advantage of this approach is that you only have one small interrupt function and the MSP can sleep in low powe rmode all the othe rtime (if you don't have something else to do). Also, the timing is exact, since the counter value is read and captured in hardware, no matter how long the latency of your code is, or whether it is executing a different ISR at just this moment.

  • Jens-Michael Gross said:
    The server maintenance yesterday swallowed my lengthy answer (After pressing the 'post' button, I got the message 'the server is down for maintenance' and all my text was gone.)

    I really dislike when that happens.  Where's the auto-save feature and how do I enable it!

    Jim Noxon

     

  • Jim,

     

    I've learned to have a notepad window open to type in. It's happened to me a few times as well and is quite frustrating, especially when I wrote such a nice answer the first time :D

     

    Gustavo

  • Gustavo L said:
    It's happened to me a few times as well and is quite frustrating, especially when I wrote such a nice answer the first time :D

    Years ago in an online game, the message editor had the habit of losing characters and the focus for special keys when one of the banner ads reloaded.

    It gained byck teh focus for normal letters, but when you noticed that your last word was mistyped (because of a few missing chars) and pressed the backspace key without preciously clicking into the editor, you were routed byck to the previous page and all input was gone. It was a simple text editor with no formatting, so I learned to wiote my texts in notepad and copy them when finished.

    Here it is different. The editor offers formatting, quoting etc, so It's better to write the answer here. And usually, I copy the final text into clipboard.
    Unfortunately, on tuesday I was about to leave the office when this happened. I 'just' wanted to send this last message and forgot to copy the text. Well, sh*t always happens when you don't have the time to clean up the mess. :(

     

    The 'correct' way to handle things like this editor would be sending the text to the server, and only forward to the thread bage when the server has properly accepted the data.
    There's so much unnecessary server traffic for this and that 'feature' (popups, menues etc.), but where it really counts, a plain old HTTP-post mechanism is used :(

  • Hi Jens-Michael,

     

     

    I really appreciate for such a detailed answer, which in theory makes sense. Since I am new to handle timer and related interrupts. I was wondering if you can make a small program and send it to me? This will be grateful of you. 

     

     

    Thank again. 

     

    Best Regards, 

     

    Mo.

  • Muhammad Imran Khan said:
    I was wondering if you can make a small program and send it to me?

    Sorry, If I'd do that for you, I had to do it for all the others too, and my day is no longer than yours. :)

    But there are some (older) threads in this forum where code has been posted and discussed.

    If I ever make it to write my book about MSP, then I will perhaps include some code examples. But in the meantime I can barely keep up with the forum in my spare minutes.

**Attention** This is a public forum