Go Down

Topic: Timing problems with short 2...12 us pulses generation with PWM out (Read 5982 times) previous topic - next topic

doughboy

Right, I realized that hence I said I misread the original post.
Anyway, the OP posted in another thread and only knew to use analogWrite. I replied with instructions to use timer registers. Instead of following up in the same thread, he started this new thread and made it appear he knows how to use timer rrlegisters. Anyways, the answer was given in the other thread and one just need to apply a little creativity to get the right waveform.

rtrab


I would do it slightly differently. First of all, use timer 1 not timer 2. Timer 1 is a 16-bit timer, therefore with a prescaler of 1 (to give your 62ns pulse width resolution), you can generate pulse frequencies down to 244Hz directly. Second, for lower pulse frequencies, there is no need to use external gating, because this gating is already included on the chip. You just need to enable generation of an interrupt at the end of the pulse, and in the ISR, set the COM1Ax bits to 0 to suppress the next pulse at the OCR1A output pin. Similarly for the OCR2A output pin.

[EDIT: corrected timer numbers]

Dear dc42, thank you for answering. The use of timer 2 does decrease the pulse repetition frequency, but doesn't increase the resolution in pulse width adjustment. The use of the COM1Ax bits is new to me, and hopefully faster than the external gating I'm using now. Thanks again.
Regards,
Rtrab

rtrab


I think I misread what you need. I think you need a frequency of  2khz? or 10hz? which is it?

First of all, tanks for your help! I need all frequency values in between and preferably adjustable from outside, however that is one of the next steps.
Regards, Rtrab

rtrab


use my code in the other thread.
you need the max count of 4000 (for ICR1 register) to get 2khz
you will need to divide the clock further to get 10hz, as 16bits is not enough to get a count of 16000000 to get 10hz at div 1.

read up on mode 11 of waveform generation. that is what you need.

Doughboym thanks for your help.
1) If you devide the clock further than the resolution is to small to make 2us and no longer 62ns/bit. I agree that the repetition rate will decrease of course.
2) Mode 11 is a Timer1 mode, now I'm using Timer2 in mode 5.
Regards,
Rtrab

doughboy

I think for what you need, the simplest solution is to just use mode 0 (normal mode) and just control the pin output high low in the compare match ISR. You have total control of pulse width and frequency.. I don't think the suggestion to use the wave generation mode and setting com1a1 to 0 is not quite right, hence. might as well use normal mode.

if you div clock by 8, one count is 0.5us. to get a 2us pulse at 10hz,
set pin high at count=0
set pin low at count=4 (4*0.5=2us)
set pin high at count =200,000 (since this is higher than 8 or 16 bit, you have to count the overflows)
for 2000hz pulse
set pin high at count = 1000 (again if you are using 8bit timer, you need to count the overflows)

Go Up