I am trying to generate sub 2us width pulse. I am using Timer1 for generating the off width and the sub 2us on-width is generated using instruction cycle delay within Timer1 ISR. To generate fine delay, I was hoping to use nop instruction. However, the minimum I can get is 2us. I would love to cut this down further to 0.5us atleast. I am not sure what is causing the pulse broadening. I have disabled the interrupts within the ISR as well to avoid Timer0 etc. interrupting within the ISR. Yet no respite. Can someone suggest what the cause could be?
Thanks for your time
void setup()
{
// some initialization code that I am avoiding
ISR(TIMER1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{
cli();
PORTD=PORTD^B100;
for (int i=0;i<timeon;i++) asm("nop\n\t");
TCNT1 = 65536-timeoff; // preload timer 65536-16MHz/8/2MHz
PORTD=PORTD^B100;
sei();
//digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
}
Make a local copy of timeon and make sure its a char
Use a while loop and count down to 0
Make i a char instead of int
Thats without looking at your logic.
There is a recent on my blog rcarduino.blogspot.com tht will show you how to view the assembly output of your sketch, you should see you loop get smaller after these changes
Did I get something wrong or could you just use a PWM timer?
Like CTC or Fast PWM mode (see datasheet page 136)
You can get frequencies up to 16Mhz/2 = 8Mhz, means pulses with 0.125µs and you have no software that has to be executed, cause the timers are hardware-implemented.
So I didnt know about FAST-PWM. It wasnt one-shot but periodic pulses but the off time could be orders of magnitude greater than on time. Time to do some reading thouhg. Thanks a lot guys.