I am new here and do not know much about programming, YET.
I am looking to control a gate two switch a HV supply to a transducer/receiver.
I need to control the gate with very short 5V pulses. Approximately 1.2microseconds. I hooked my arduino to a scope using the microsecond command, but the shortest pulse duration I can get is about 4 microseconds.
Does anyone know how to reduce this pulse duration down to 1.2 microseconds.
For my program:
Loop
30 microsecond (Low)
1.2 microsecond (High)
About a 10 second Delay
End Loop
If anyone can help me out with this short program I would very much appreciate it.
All the output pins are grouped in PORTs: PORTB, PORTC: PORTD. Each port has a corresponding PIN register: PINB, PINC, PIND. If you write a 1 to a bit in the PIN register the output pin will flip:
PIND = 0x10; // Toggle Pin 4 (Port D bit 4)
PIND = 0x10; // Toggle Pin 4 back (Port D bit 4)
Maybe two direct port manipulations in full speed is too fast for the OP. If you need the 1.2us, you can use assembler commands to let the processor wait:
johnwasser:
All the output pins are grouped in PORTs: PORTB, PORTC: PORTD. Each port has a corresponding PIN register: PINB, PINC, PIND. If you write a 1 to a bit in the PIN register the output pin will flip:
PIND = 0x10; // Toggle Pin 4 (Port D bit 4)
PIND = 0x10; // Toggle Pin 4 back (Port D bit 4)
This should cause a very short pulse on Pin 4.
Thanks, I will try this today.
What exactly is this code saying? I like to know what I am doing so I can learn from this.
PIND = 0x10; // Toggle Pin 4 (Port D bit 4)
PIND = 0x10; // Toggle Pin 4 back (Port D bit 4)
from johnwasser uses the feature of the PINx register to toggle the value of the selected bits (XOR, documented only in the datasheet 13.2.2).
The usual code for this is direct port manipulation:
PORTD |= 0x10; // Switch on pin 4
PORTD &= 0xEF; // Switch off pin4
It's almost the same as the code from RIDDICK but without affecting all other pins of the same port (pins 0-7).
Both code snippets just tries to switch on and off a single pin as fast as possible, probably faster than you want. That's why you may have to insert the NOP assembler statements to wait some time before toggling the pin back.
What did you expect? Rectangular pulses? That would mean you have absolutely no capacity on the line. How much of that is coming from your measurement equipment (a scope I guess)? Do you have the resolution to even see a rectangular signal if it was there?
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.
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 () ;
}
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:
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]
You always get ringing on an unterminated logic signal into a oscilloscope probe - add 100 ohm resistor in series with the probe to vastly reduce this. The inductance and capacitance are stray.
Which Arduino was used here?
I found out that the Due is faster than the Uno. Would this make a difference in this case?
I have a similar problem with producing microsecond pulses. Please consider checking out the new topic I made for this problem.