Go Down

Topic: 1 Microsecond pulses (Read 3 times) previous topic - next topic

MarkT


I am getting a sawtooth pattern decay with this pulse?




You have set it as an OUTPUT?
[ I won't respond to messages, use the forum please ]

MarkT

This gives about 1.2us pulses, and disables interrupts during the pulse output to stop them occasionally stretching.

Disabling interrupts also means that any calls to digitalWrite () for pins in the D port in interrupt routines aren't broken by our direct port manipulation.

Code: [Select]
void setup ()
{
  pinMode (4, OUTPUT) ;
  digitalWrite (4, LOW) ;  // or set to HIGH for low-going pulses.
}

void loop ()
{
  pulse () ;
  delayMicroseconds (5) ;
}

void pulse ()
{
  cli () ;
  PIND = 0x10 ;
  __asm__("nop\n\t");
  __asm__("nop\n\t");
  __asm__("nop\n\t");
  __asm__("nop\n\t");
  delayMicroseconds (1) ;
  PIND = 0x10 ;
  sei () ;
}
[ I won't respond to messages, use the forum please ]

pylon

@MarkT: This gives you 4.2us pulses, try it out. delayMicroseconds() waits at least 4us even if the parameter is less than 4.

But you can change the code to:

Code: [Select]
void pulse ()
{
  cli () ;
  PIND = 0x10 ;
  __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
  __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
  __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
  __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
  PIND = 0x10 ;
  sei () ;
}


This should result in a pulse of 1.2us.

Lithanial

Thanks,

I finally got it.  There is a bit of overshooting at the top and bottom of the pulsing, but I can work with this.  Thanks.

MarkT

#14
Oct 05, 2012, 12:30 am Last Edit: Oct 05, 2012, 12:38 am by MarkT Reason: 1

@MarkT: This gives you 4.2us pulses, try it out. delayMicroseconds() waits at least 4us even if the parameter is less than 4.

But you can change the code to:

Code: [Select]
void pulse ()
{
 cli () ;
 PIND = 0x10 ;
 __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
 __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
 __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
 __asm__("nop\n\tnop\n\tnop\n\tnop\n\tnop\n\t");
 PIND = 0x10 ;
 sei () ;
}


This should result in a pulse of 1.2us.


Try it out - I tested it with 100MHz scope and get 1.19us pulses.  Am using the Arduino 1.0 software and avr-gcc 4.3.5
The code for delayMicroseconds has a comment saying that it is close to 1us when called with an argument of 1.

What version gives 4us then?

[edit:  Arduino 1.0.1 and arduino-0022 give me 1.19us as well]
[ I won't respond to messages, use the forum please ]

Go Up