10-20k方波三角波生成器 50-100kPWM方波
Posted T4b3_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10-20k方波三角波生成器 50-100kPWM方波相关的知识,希望对你有一定的参考价值。
10-20k方波三角波生成器 50-100kPWM方波
总体框架
CUBEMX初始化
DAC,DMA输出任意波形
这里给出8位宽8位深的方波三角波数组,我这dac升到37khz左右就上不去了,想进一步提高频率可以在数组内包含更多周期波形数据。
const uint8_t square_8bit[256]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232,232;
const uint8_t triangle_8bit[256] = 117,119,121,123,125,126,128,130,132,134,135,137,139,141,143,145,146,148,150,152,154,155,157,159,161,163,164,166,168,170,172,174,175,177,179,181,183,184,186,188,190,192,193,195,197,199,201,203,204,206,208,210,212,213,215,217,219,221,222,224,226,228,230,232,230,228,226,224,222,221,219,217,215,213,212,210,208,206,204,203,201,199,197,195,193,192,190,188,186,184,183,181,179,177,175,174,172,170,168,166,164,163,161,159,157,155,154,152,150,148,146,145,143,141,139,137,135,134,132,130,128,126,125,123,121,119,117,116,114,112,110,108,106,105,103,101,99,97,96,94,92,90,88,87,85,83,81,79,77,76,74,72,70,68,67,65,63,61,59,58,56,54,52,50,48,47,45,43,41,39,38,36,34,32,30,29,27,25,23,21,19,18,16,14,12,10,9,7,5,3,1,0,1,3,5,7,9,10,12,14,16,18,19,21,23,25,27,29,30,32,34,36,38,39,41,43,45,47,48,50,52,54,56,58,59,61,63,65,67,68,70,72,74,76,77,79,81,83,85,87,88,90,92,94,96,97,99,101,103,105,106,108,110,112,114,116;
HAL_TIM_Base_Start(&htim6);
HAL_TIM_Base_Start(&htim7);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)square_8bit, 256, DAC_ALIGN_8B_R);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_2, (uint32_t*)triangle_8bit, 256, DAC_ALIGN_8B_R);
同时也给出matlab波形数据生成代码,可自调深度宽度。
这里是1V的方波,三角波,正弦波,锯齿波
depth=256; %数据深度,存储单元数
width=12; %数据宽度
fdic=fopen('E:\\T4b3_works\\sawtooth_256.txt','wt');
%fprintf(fdic,'depth=%d;\\n',depth);
%fprintf(fdic,'width=%d;\\n',width);
%fprintf(fdic,'address_radix=uns;\\n');
%fprintf(fdic,'data_radix=uns;\\n');
%fprintf(fdic,'Content Begin\\n');
for(x=1:depth/4);
%fprintf(fdic,'%d,',round((2047*sin(pi*(x-1)/128)+2048)/3.3));
%fprintf(fdic,'%d,',fix((x*2047/64+2048)/3.3));
%fprintf(fdic,'%d,',0);
fprintf(fdic,'%d,',fix(1241*x/256));
end
for(x=depth/4+1:depth/2);
%fprintf(fdic,'%d,',round((2047*sin(pi*(x-1)/128)+2048)/3.3));
%fprintf(fdic,'%d,',fix((6144-x*4096/128)/3.3));
%fprintf(fdic,'%d,',0);
fprintf(fdic,'%d,',fix(1241*x/256));
end
for(x=depth/2+1:depth*3/4);
%fprintf(fdic,'%d,',round((2047*sin(pi*(x-1)/128)+2048)/3.3));
%fprintf(fdic,'%d,',fix((6144-x*4096/128)/3.3));
%fprintf(fdic,'%d,',1241);
fprintf(fdic,'%d,',fix(1241*x/256));
end
for(x=depth*3/4+1:depth);
%fprintf(fdic,'%d,',round((2047*sin(pi*(x-1)/128)+2048)/3.3));
%fprintf(fdic,'%d,',fix((x*2047/64-6144)/3.3));
%fprintf(fdic,'%d,',1241);
fprintf(fdic,'%d,',fix(1241*x/256));
end
%fprintf(fdic,'end');
fclose(fdic);
PWM输出高频方波
HAL_TIM_Base_Start(&htim10);
HAL_TIM_PWM_Start(&htim10,TIM_CHANNEL_1);
PWM很简单,设置好时钟,启动即可
外部中断控制频率
主函数给出频率变量
int freq_square=10000,freq_triangle=10000,freq_high=50000;
在gpio.c加入Callback外部中断函数
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
extern int freq_square;
extern int freq_triangle;
extern int freq_high;
char freq[20];
switch(GPIO_Pin)
case KEY_UP_Pin: //判断KEY_UP
delay_ms(10); //消抖
if(HAL_GPIO_ReadPin(KEY_UP_GPIO_Port,KEY_UP_Pin)==1)
LCD_Clear(WHITE);
LCD_ShowString(100,40,500,24,24,"KEY_UP PRESSED!");
break;
case KEY0_Pin:
delay_ms(10); //消抖
if(HAL_GPIO_ReadPin(KEY_UP_GPIO_Port,KEY_UP_Pin)==0)
以上是关于10-20k方波三角波生成器 50-100kPWM方波的主要内容,如果未能解决你的问题,请参考以下文章
怎么把1K~10K的方波信号变换成三角波、锯齿波、正弦波?可以提供电路图的最好。