http://www.edn.com/design/analog/4337128/Make-a-DAC-with-a-microcontroller-s-PWM-timer

Many embedded-microcontroller applications require generation of analog signals. An integrated or stand-alone DAC fills the role. However, you can often use PWM signals for generating the required analog signals. You can use PWM signals to create both dc and ac analog signals. This Design Idea shows how to use a PWM timer to simultaneously create a sinusoid, a ramp, and a dc voltage. A PWM signal is a digital signal with fixed frequency but varying duty cycle. If the duty cycle of the PWM signal varies with time and you filter the PWM signal, the output of the filter is an analog signal (Figure 1).

If you build a PWM DAC in this manner, its resolution is equivalent to the resolution of the PWM signal you use to create the DAC. The PWM output signal requires a frequency that is equivalent to the update rate of the DAC, because each change in PWM duty cycle is the equivalent of one DAC sample. The frequency the PWM timer requires depends on the required PWM signal frequency and the desired resolution. The required frequency is FCLOCK=FPWM×2n, where FCLOCK is the required PWM-timer frequency, FPWM is the PWM-signal frequency, and n is the desired DAC resolution in bits.

Figure 2

depicts a circuit that delivers a 250-Hz sine wave, a 125-Hz ramp, and a dc signal. The desired sampling rate is 8 kHz (32 samples for each sine-wave cycle (16× oversampled), and 64 samples for each ramp cycle (32× oversampled)). These figures result in a required PWM-signal frequency of 8 kHz and a required PWM clock frequency of 2.048 MHz. It is usually best for the PWM signal frequency to be much higher than the desired bandwidth of the signals to be produced. Generally, the higher the PWM frequency, the lower the order of filter required and the easier it is to build a suitable filter. This design uses Timer B of the MSP430 in 16-bit mode and in "up" mode, in which the counter counts up to the contents of capture/compare register 0 (CCR0) and then restarts at zero. CCR0 is loaded with 255, thereby giving the counter an effective 8-bit length. You can find this register and others in a DAC demonstration program for the MSP430 microcontroller. Click here to download the program.

CCR1 and output TB1 produce the sine wave. CCR2 and TB2 generate the ramp, and CCR3 and TB3 yield the dc value. For each output, the output mode is the reset/set mode. In this mode, each output resets when the counter reaches the respective CCRx value and sets when the counter reaches the CCR0 value. This scheme provides positive pulses equivalent to the value in CCRx on each respective output. If you use the timer in 8-bit mode, the reset/set output mode is unavailable for the PWM outputs because the reset/set mode requires CCR0. The timer's clock rate is 2.048 MHz. Figure 3

shows the sine and ramp waveforms. The sine wave in this example uses 32 samples per cycle. The sample values are in a table at the beginning of the program. A pointer points to the next value in the sine table, so that, at the end of each PWM cycle, the new value of the sine wave is written to the capture/compare register of the PWM timer.

The ramp in this example does not require a table of data values. Rather, the ramp simply increments the duty cycle for each cycle of the PWM signal until it reaches the maximum and then starts over at the minimum duty cycle. This gradual increase in PWM-signal duty cycle results in a ramp voltage when the signal passes through a filter. You control the dc level by simply setting and not changing the value of the PWM-signal duty cycle. The dc level is directly proportional to the duty cycle of the PWM signal. Figure 2 shows the reconstruction filters used for each signal in this example. The filter for the ac signals is a simple two-pole, stacked-RC filter, which is simple and has no active components. This type of filter necessitates a higher sampling rate than would be required if the filter had a higher order. With the type of filter shown in Figure 2, you should use at least a 16× oversampling rate.

The filter yields its best response when R2>>R1. Also, setting the cutoff frequency too close to the bandwidth edge causes a fair amount of attenuation. To reduce the amount of attenuation in the filter, set the cutoff frequency above the bandwidth edge but much lower than the frequency of the PWM signal. The filter for the dc value serves for charge storage rather than ac-signal filtering. Therefore, it uses a simple, single-pole RC filter. Figure 4

shows the software flow for the DAC. After a reset, the routine stops the watchdog timer, configures the output ports, and sets up the clock system. Next, the software calls a delay to allow the 32,768-Hz crystal to stabilize to calibrate the DCO (digitally controlled oscillator).

Next, the routine calls the calibration routine to set the operating frequency to 2.048 MHz. After the DCO calibration, the program sets up Timer_B, CCR1 and CCR2 for PWM generation and then starts the timer. Finally, the MSP430 goes into low-power mode 0 (LPM0) to conserve power. The CPU wakes up to handle each CCIFG0 interrupt from the PWM timer and then re-enters LPM0. (See references 1, 2, and 3 for more information on the DCO and the MSP430 family.)

Make a DAC with a microcontroller's PWM timer的更多相关文章

  1. 【STM32】PWM DAC基本原理(实验:PWM实现DAC)

    虽然STM32F103ZET6具有内部DAC,但是也仅仅只有两条DAC通道,并且STM32还有其他的很多型号是没有DAC的.通常情况下,采用专用的D/A芯片来实现,但是这样就会带来成本的增加. 不过S ...

  2. Create a DAC from a microcontroller's ADC

    Few microcontrollers include a DAC. Although you can easily find an inexpensive DAC to control from ...

  3. Cortex-A9 PWM Timer

    PWM定时器        4412时钟为我们提供了PWM定时器,在4412中共有5个32位的定时器,这些定时器可发送中断信号给ARM子系统.另外,定时器0.1.2.3包含了脉冲宽度调制(PWM),并 ...

  4. PWM DAC vs. Standalone

    http://analogtalk.com/?p=534 http://analogtalk.com/?p=551 Posted by AnalogAdvocate on April 09, 2010 ...

  5. PWM DAC Low Pass Filtering

    [TI博客大赛][原创]LM3S811之基于PWM的DAC http://bbs.ednchina.com/BLOG_ARTICLE_3005301.HTM http://www.fpga4fun.c ...

  6. how to generate an analog output from a in-built pwm of Atmega 32AVR microcontrloller?

    how to generate an analog output from a in-built pwm of Atmega 32AVR microcontrloller? you need a re ...

  7. M451 PWM对照数据手册分析

    PWM_T Struct Reference Control Register » Pulse Width Modulation Controller(PWM)   typedef struct { ...

  8. 说说M451例程之PWM的寄存器讲解

    M451提供了两路PWM发生器.每路PWM支持6通道PWM输出或输入捕捉.有一个12位的预分频器把时钟源分频后输入给16位的计数器,另外还有一个16位的比较器.PWM计数器支持向上,向下,上下计数方式 ...

  9. 说说M451例程之PWM

    /**************************************************************************//** * @file main.c * @ve ...

随机推荐

  1. 洛谷P1120 小木棍(升级版)

    传送门啦 一道经典的搜索剪枝题,不废话,步入正题. 分析: 一.输入时手动过滤不合法的情况 二.很明显我们要枚举把哪些棍子拼接成原来的长棍,而原始长度(原来的长棍的长度)都相等,因此我们可以在 $ d ...

  2. MinGW-MSYS Bundle Win32编译ffmpeg 生成DLL并加入X264模块

    组件资源站点 1)MinGW-MSYS Bundle http://sourceforge.net/projects/mingwbundle/files/ 2)yasm汇编器 http://yasm. ...

  3. 关于Fuzz——peach的学习

    最近在搞漏洞挖掘,之前写过一个文件格式漏洞挖掘的博文,使用的是光刃牛写的Alpha Fuzz工具.感觉样本生成的质量不是很好,这次打算使用一下老牌的Fuzz工具peach.学长介绍了一下说peach的 ...

  4. s12-day01-work01用户登录接口

    README # README.md # day001-work-1 @南非波波 功能实现:登录接口 流程图: ![](http://i.imgur.com/61eFd0u.jpg) 程序实现: 1. ...

  5. 【LOJ】#2062. 「HAOI2016」地图

    题解 我对莫队真是一无所知 这个东西显然可以用圆方树转成一个dfs序列 然后呢,用莫队计算每个询问区间的每个数出现的次数,从而顺带计算每个数字的奇偶性 但是我们要查的数字也用一个范围,可以直接用分块维 ...

  6. 【LOJ】#2027. 「SHOI2016」黑暗前的幻想乡

    题解 我一开始写的最小表示法写的插头dp,愉快地TLE成60分 然后我觉得我就去看正解了! 发现是容斥 + 矩阵树定理 矩阵树定理对于有重边的图只要邻接矩阵的边数设置a[u][v]表示u,v之间有几条 ...

  7. 灵活的按键处理程序 FlexibleButton

    前言 正好工作中用到按键处理,需要处理单击.长按等按键事件,然后就造了这么一个轮子,为了以后更方便地加入其它的项目中使用,遂将其开源到 GitHub 中. 后面发现 RT-Thread 软件包里也有一 ...

  8. c语言程序与设计第三版-苏小红--第一轮学习笔记、难点整理

    ---恢复内容开始--- 1> 编程:需求分析.设计.编写程序(编码.编辑.链接.运行).调试程序 2> 指数形式:e的左边是数值部分(有效数字),不能省略,但可以表示成 .e-4:等等: ...

  9. OptParse选项工具模块

    OptParse是一个从Python2.3版本起引入的一个编写命令行工具模块,示例如下 ######example.py###### import optparse if __name__ == &q ...

  10. Win10 重装后,必须修改的设置

    作为一个程序猿,系统易用性是相当重要,每次重装WIN10 都会遇到一头包的问题,比如不能远程,打开文件各种提示需要管理员权限(mlgb很想骂人,我明明是管理员权限) ,然后开了管理员权限,结果又不能用 ...