PWM DAC原理
PWM本质上是周期一定,高低电平占空比可调的方波
函数表示: 展开傅里叶级数: N:PWM波一周期中高电平的计数脉冲个数,即STM32ARR-1的值 n:PWM波一周期中高电平的计数脉冲个数,即STM32CCRx的值 VH和VL分别是PWM波的高低电平电压值,k为谐波次数,t为时间
将②式中的谐波过滤掉,则可以得到从PWM波到电压输出DAC的转换:PWM波解调(滤波器) ②式变成: 若VL=0,则f(t)=[n/N Vh];
分辨率:log2(N)
PWM DAC硬件
二阶RC滤波截止频率计算公式:f=1/2 πRC
实验程序
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "adc.h"
#include "key.h"
#include "pwmdac.h"
int main(void)
{
u16 adcx;
float temp;
u8 t=0;
u16 pwmval =0;
u8 key;
NVI_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
LED_Init();
LCD_Init();
Adc_Init();
KEY_Init();
TIM9_CH2_PWM_Init(255,0);
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"PWM DAC TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2014/5/6");
LCD_ShowString(30,130,200,16,16,"WK_UP:+ KEY1:-");
POINT_COLOR=BLUE;
LCD_ShowString(30,150,200,16,16,"DAC VAL:");
LCD_ShowString(30,170,200,16,16,"DAC VOL:0.000V");
LCD_ShowString(30,190,200,16,16,"ADC VOL:0.000V");
TIM_SetCompare2(TIM9,pwmval);
while(1)
{
t++;
key=KEY_Scan(0);
if(key==4)
{
if(pwmval<250)pwmval+=10;
TIM_SetCompare2(TIM9,pwmval);
}else if(key==2)
{
if(pwmval>10)pwmval-=10;
else pwmval=0;
TIM_SetCompare2(TIM9,pwmval);
}
if(t==10||key==2||key==4)
{
adcx=TIM_GetCapture2(TIM9);;
LCD_ShowxNum(94,150,adcx,3,16,0);
temp=(float)adcx*(3.3/256);;
adcx=temp;
LCD_ShowxNum(94,170,temp,1,16,0);
temp-=adcx;
temp*=1000;
LCD_ShowxNum(110,170,temp,3,16,0x80);
adcx=Get_Adc_Average(ADC_Channel_5,20);
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_ShowxNum(94,190,temp,1,16,0);
temp-=adcx;
temp*=1000;
LCD_ShowxNum(110,190,temp,3,16,0x80);
t=0;
LED0=!LED0;
}
delay_ms(10);
}
}
|