Problem with Pulses

Hey friends, I am trying to get as low time delay as possible to blink a laser. I am trying to get delay in nanoseconds and for that I tried using assembly language .The problem which I am facing is that I am not getting proper pulses ie the on time is around 350ns but the off time is more than 1us. Can anyone can tell me why this problem is arising and how can this problem be rectified ? Will using PWM be a better option ? Any other suggestions?
The code is as follows:-

int led = 10;
int laser =9;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
pinMode(laser,OUTPUT);  
}


void loop() {

  PORTB=B00000110;
  __asm__("nop\n\t");  


PORTB = B00000000;
  __asm__("nop\n\t");

}

Hi Kavish.

Could the differencs be caused by the Arduino performing other actions between calls to loop ()?

Try making your own loop inside loop () using while (True) { ... }

Paul

Hi , try reversing the order of the two command lines, if the high / low times swap then it supports PaulRB's suggestion.

Tom.. :slight_smile:

you can move all to setup()

const int led = 10;
const int laser =9;

void setup() 
{                
  pinMode(led, OUTPUT);
  pinMode(laser,OUTPUT);

  while(1)
  {
    PORTB = B00000110;
    __asm__("nop\n\t");  
    PORTB = B00000000;
    //  __asm__("nop\n\t");   // removed as the jump to begin of loop takes time too.
  }
}

void loop() 
{
}

you might need to use objdump.exe to see the code generated…

I will try the above mentioned suggestions and will inform if it works.
Thankyou

I tried above mentioned suggestions and I got somewhat good results with the method suggested by Robtillart , but I feel that the setup function runs only once , then how can I get such pulses ? And I an not getting pulses as a step function, instead it has peaks . How can I make it a step function ?
Thank you

Hi, put the commands in the loop.
I'm not sure that you may be having problems with slew rates of the output of the arduino IC, I had a quick look at the Atmel spec sheet, but couldn't find a figure, but there has to be one.

Tom..... :slight_smile:

I think it might be a good idea to explain what you actually want to do.

You are clearly going to be disappointed trying to implement these things in "C" as you have poor control over what is actually being assembled. You need to code the whole loop in assembler.

I must ask however, what is your ultimate expectation? Do you need repetitive pulses? How many? How are you going to determine when you have delivered sufficient? If it is a count, then that probably is doable in pure assembler. If it is a time, that will be substantially more difficult. What is certain, is that you cannot make decisions of any complexity whilst generating such pulses in software. And of course, if you only wanted to generate pulses forever, you probably would not be using a microprocessor though I suppose, an ATtiny 8-pin is a pretty neat way of doing so.

Thank you everyone . Finally I got the delay of 125 nano seconds with the code suggested by Robtillaart . Here are the results:

F0000TEK.JPG

Great.

There's a lot of ringing going on there, versus the change in output levels. Do you think that's caused by the Arduino, the 'scope, or stray capacitance in the breadboard or something?