# How SetPulse works?

I read some blogs but i am not able to understand the SetPulse Method.
Syntax: SetPulse(period,hightime)

Now i have the following requirement.

On Time: 2 milli second
Off Time: 800 micro second
Power: 2 volts (out of 3.3 volts max)

How do i write the Setpulse for the above example?

That would be:

SetPulse(2800 * 1000, 2000 * 1000);

So if i set the following

OnTime: 4 millisecond
OffTime: 0 millisecond
Power: 3.3 volts

then i should be getting a steady 3.3 volts for 4 milliseconds. If i put that in an infinite loop then i should be getting a 3.3 volt steady signal. is this assumption correct?

One more question.
If i want to send a signal for 100 or 200 microseconds just once then how would i use it. The timer or sleep methods only have milliseconds.

Use OutputCompare

I really don’t understand this.

[quote]So if i set the following

OnTime: 4 millisecond
OffTime: 0 millisecond
Power: 3.3 volts

then i should be getting a steady 3.3 volts for 4 milliseconds. If i put that in an infinite loop then i should be getting a 3.3 volt steady signal. is this assumption correct?
[/quote]

Why don’t you set the digital IO pin to TRUE and be done with it?

Also, I don’t think you understand what you’re seeing on the end of the earlier test you were doing. You can only set a digital IO pin to low or high, which means you get 3.3v or 0v. If you switch that really fast, and vary how long on and how long off, you get the appearance of a reduced voltage. You don’t get a reduced voltage, because at any point in time it is either 3v3 or 0v, there is no 2.2v for instance. What you see if you average it out is you end up with 2.2v. A typical multimeter will only show you 2.2v, not a quickly toggling 3v3 to 0v etc.

You only need to setup a PWM once, unless you want to change the period or the duty cycle. Don’t call SetPulse with the same values in a loop, it doesn’t make sence.

It looks idd like you are looking for OutputCompare instead.

Brett, my requirement is as follows

I need a signal with the following parameters

On Time (High time): 300 microseconds
Off Time (0 volts): 100 seconds

The High time power must be whatever i set (say 1 volt or 0.5 volt, etc…). i.e. during high time, the amplitude must not be more than the set 0.5 volt or 1 volt.

then you cannot use a digital output on a Fez. Digital IO outputs 3v3 or 0v. You’ll need to also involve analog electronics and do a voltage divider of some form to reduce that, but it’s going to mean you won’t get the nice sharp on/off steps.

What are you trying to achieve? What has this specific requirement?

You can have an analog output, f.e. from a DA converter and switch the analog voltage with a PWM signal. But it would be good to know why you need that custom voltage.

I need a pulse (on time and off time) ranging from 200 micro second to 20 milli seconds. This will power a laser diode. The power and time will be controlled from a 4.3 inch touch screen.
Please let me know the hardware i need to use to achieve the above requirement. I am a .NET Architect and new to embedded systems.

You can’t power you laser diode directly from an IO pin. You need an external power source. (And maybe even a constant current source). Once you have the source, you can switch it on or off with a FET controlled from a digital PWM output.

Sorry i missed to mention that. I have a DAC that will power the laser diode. All i have to do is to give it a signal with varying power (0-3.3 v) so that the power gets translated into 0-30 watts f.e…

All i need is to generate a pulse with a fixed power,ontime and offtime.

use a DA converter to generate the voltage. Then a PWM output will switch the DAC output on or off by using a FET or a transistor.

after i get the pwm signal from my board converting the voltage is not going to be a problem. my problem is with getting the pwm signal.

For
Ontime - 2 millisecond
offtime - 800 microsecond
power - 2 volt

is the below method usage correct?
SetPulse(Period,hightime)
SetPulse(28001000,20001000)

If this is right then I am not able to understand the 2000*1000 part in the above function

SetPulse(Period,hightime)
SetPulse(28001000,20001000)

You have a period of 2800*1000 nanoseconds = 2800 microseconds or 2.8 milliseconds.
The period defines the PWM frequency (Freq = 1 / Period), in this case PWM frequency will be 357Hz (1 / 0.0028).

The second number defines the ON time, you want 2 milliseconds, which is 2000 microseconds or 2000 * 1000 nanoseconds.

The OFF time is automatically the time (Period - Hightime). In this case 28001000 - 20001000 = 800*1000 nanoseconds or 800 microseconds as you requested.

Maybe this snippet makes it clear:

``````
int ontimeMicrosec = 2000;
int offtimeMicrosec = 800;
pwm.SetPulse((ontimeMicrosec + offtimeMicrosec ) * 1000, ontimeMicrosec * 1000);

``````

But what about the power of 2 V? Can i control it? I am able to control it by using the duty cycle in the Set method.

No, PWM is a digital IO, it switches between GND and Vcc.
But as you say:

[quote]after i get the pwm signal from my board converting the voltage is not going to be a problem[/quote]

To emphasise, you cannot control voltage from the pwm, other than to have it turn high (3.3V) or low (0V). No other Voltage values can be achieved.

With PWM you can only control duration of the pulses. If you also need to control the output voltage you need to have a separate circuit that outputs desired voltage, then control that constant voltage output with the PWM pulses.

In other words, the PWM can only give you timing control. The voltage level control must be separate.

Can you show us the datasheet of that DAC? Because I think you’re searching for the wrong solution.

Or the ‘DAC’ is just a current source, and you need to feed it a PWM signal. Or the ‘DAC’ is a translator from analog signal to PWM signal for the laser diode. I would be surprised if it really needs a mix of those two…