cancel
Showing results for 
Search instead for 
Did you mean: 

How to use PWM as DAC?

mani
Associate II
Posted on August 04, 2016 at 12:49

STM32F207VE Custom Board. I want to generate a PWM of 50-50 duty cycle with varying voltages (zero-to-maximum) like a DAC which gives output based on count. I do not have the schematic now. I'm directly viewing the waveform on scope via breakout pins. The amplitude of the waveform must be in my control. I am successful in generating PWM wave but with a constant amplitude <3V>. How do I change the voltage now? 

I'm using Timer 4.

#adc #stm32 #pwm #dma #dac #stm32f2xx
9 REPLIES 9
Posted on August 04, 2016 at 12:57

> How do I change the voltage now? 

You don't. Why would you think it should be possible?

JW

mani
Associate II
Posted on August 04, 2016 at 13:14

I see people using pwm as dac? How it is done then?

This is a noob question but I just need some guidance.

Posted on August 04, 2016 at 13:22

By changing the duty cycle and then filtering. The output is DC, not PWM.

Look at AN4453, chapter 3.

JW
mani
Associate II
Posted on August 04, 2016 at 13:36

I'm looking into it. Could you post a code snippet for configuring TIM4 as PWM o/p with 50-50 duty cycle and having 10 kHz freq. ?

AvaTar
Lead
Posted on August 04, 2016 at 14:01

> I'm looking into it. Could you post a code snippet for configuring TIM4 as PWM o/p with 50-50 duty cycle and having 10 kHz freq. ?

 

A fixed duty cycle with a fixed PWM does not create any change in (average) amplitude.

And for filtering, keep the PWM frequency as high as possible - or, more exactly, the gap between PWM frequency and desired output frequency / change rate of the DAC.

I suggest rather 100 .. 500kHz.

AvaTar
Lead
Posted on August 04, 2016 at 14:09

To comment myself:

You need to change the PWM duty cycle to change the (filtered) output amplitude.

And you will need an amplifier stage, because filtering will reduce your amplitude drastically.

An external DAC seems a viable alternative, perhaps even cheaper than the required filter/buffer stage.

mani
Associate II
Posted on August 04, 2016 at 14:24

Actually we are using stm's internal dac till now. We ran into problems when FETs are continuously loaded (they are getting burned due to heavy load up to 100A). So we want to shift to pwm o/p so that FETs can be turned off/on by 50-50 times so that they are turned on for only half of full time specified. 

AvaTar
Lead
Posted on August 04, 2016 at 15:18

This suggests this is not an application for analog output at all.

Most power FETs are designed for switching purposes, to have a very low RDS(on).

Driving them analog (sinusoid) means spending considerable time in the linear range, with extreme power dissipation.

Better consult a hardware expert, and the FET datasheets.

Posted on August 04, 2016 at 15:26

Could you post a code snippet for configuring TIM4 as PWM o/p with 50-50 duty cycle and having 10 kHz freq?

This is a close example that should be easy to modify for the F2 @ 10 KHz, saves me from constantly repeating myself.

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/PWM%20Output%20on%20STM32F2XX&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=12830

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..