Counting 50 to 200 KHz pulses with TMPn on DZ60

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Counting 50 to 200 KHz pulses with TMPn on DZ60

831 Views
Francois
Contributor I

Hello,

 

I'm quite new with DZ60 and I need to measure the frequency of  a 50 to 200 kHz square wave signal.

 

My first idea was to use TPM2CH0, and to mesure the number of clock cycles between 2 rising edges

of my signal. It works but there are a lot of interrupts (at each rising edge) and it slows down significantely

my application.

 

My second idea is to measure the number of pulses in a 250 ms interval.

but I'm unable to configure the TPM2 timer in this way ?

For instance, I've tried to configure :

TPM2SC = 0x18;

TPM2C0SC = 0x04;

 

But the TPM2C0V counter doesn't increase ?

 

Could someone indicates me if it's possible to count automatiquely incomming pulses ?

What should I program for these 2 configuration registers ?

 

Thank you very much for your help

 

francois

Labels (1)
0 Kudos
2 Replies

333 Views
ThaManJD
Contributor III

Francois,

 

I think you should be able to do this but it's unclear from your comments which pin your square wave is ccoming in on.

 

I'll assume if your were capturing edges of your square wave that your signal went in the TPM2CH0 pin.

 

Maybe what you need to do is insert it into the square wave into TPM2CLK pin. (or TPM1CLK)

Setting the TPM2SC to external clock (TPM2SC=0x18) directs the clock source for timer 2 to come from that pin(TPM2CLK).  Have a look at the diagram in the datasheet at the beginning on the Timer/PWM module section. It shows the optional external clock inputs coming from those pins for each of the timer modules

 

This also means that your accumulation duration timer has to come from one of the other timers (presumably timer module 1 which I guess whos source would be one of the internal clocks)

 

Then it is the TPM2CNTH and TPM2CNTL (timer module 2 counters) and not the channel timer/counter that increment on your external clock input.

Check the datasheet about this. Your external input synchonises i think with the bus clock, so i'm guessing your scenario will be fine but if your input signal is faster than the bus clock then your count wont be accurate.

 

Writing to either TPM2CNTH or TPM2CNTL clears the whole 16bit timer word. Maybe set a timer overflow interrupt on timer 2 so you know you've hit 65535 counts (if you have to "carry" your overall counter)

 

 

I think doing that you should be able to count clock pulses over a time period without so many interrupts.

Technically you wouldn't have to use any timer 2 compare channels, just the main timer/counter for module 2.

Flipping the scenario on its head, you could additionally also use a timer 2 channel output compare to measure the time taken for a given number of clock pulses, marking time using timer module 1. You could also do this without the timer 2 channel by setting the modulo register and using the timer 2 module overflow flag/interrupt. But it sounds like you just want to discover the frequency of your quare wave.

 

JD

 

 

 

0 Kudos

333 Views
Francois
Contributor I

Hello JD,

 

Thank you very much for your reply.

My mistake was to put my square wave signal to TPM2CH0. Further to your indications, I've put it to TPM2CLK and now

I'm able to measure the number of pulses during a define interval with the TPM2CNTH and TPM2CNTL registers.

(TPM1 is the main clock) I've define this interval long enough to have a number of pulses < 65 k.

I prefer to let TPM2CLK free run, store the previous measure and make the difference with the new measure to get

the result. The 2 other comparators based on TPM2 are not used.

 

I've made a short test : I measure ~900 pulses during an interval of 8 ms this corresponds to a frequency of 

112500 Hz which is my result.

 

Thank you very much for you quick help

Best regards from France

francois

 

 

 

 

0 Kudos