完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
总体框架
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_workssawtooth_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 Beginn’); 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){ if(HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)==1) //低频kEY0降频 { if(freq_square》10000)freq_square-=1000; int reload_square=84000000/256/freq_square-1; __HAL_TIM_SET_AUTORELOAD(&htim6,reload_square); __HAL_TIM_SET_AUTORELOAD(&htim7,reload_square); sprintf((char*)freq,“FREQUENCE:%5d Hz”,84000000/256/(reload_square+1)); LCD_Clear(WHITE); LCD_ShowString(100,40,500,24,24,freq); } } else{ if(HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)==1){ //高频KEY0降频 if(freq_high》50000)freq_high-=5000; int reload=84000000/freq_high-1; __HAL_TIM_SET_AUTORELOAD(&htim10,reload); __HAL_TIM_SET_COMPARE(&htim10,TIM_CHANNEL_1,(reload+1)/2-1); sprintf((char*)freq,“FREQUENCE:%6d Hz”,84000000/(reload+1)); LCD_Clear(WHITE); LCD_ShowString(100,40,500,24,24,freq); } } break; case KEY1_Pin: delay_ms(10); //消抖 if(HAL_GPIO_ReadPin(KEY_UP_GPIO_Port,KEY_UP_Pin)==0){ if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==1) //低频KEY1升频 { if(freq_square《20000)freq_square+=1000; int reload_square=84000000/256/freq_square-1; __HAL_TIM_SET_AUTORELOAD(&htim6,reload_square); __HAL_TIM_SET_AUTORELOAD(&htim7,reload_square); sprintf((char*)freq,“FREQUENCE:%5d Hz”,84000000/256/(reload_square+1)); LCD_Clear(WHITE); LCD_ShowString(100,40,500,24,24,freq); } } else{ if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==1){ //高频KEY1升频 if(freq_high《100000)freq_high+=5000; int reload=84000000/freq_high-1; __HAL_TIM_SET_AUTORELOAD(&htim10,reload); __HAL_TIM_SET_COMPARE(&htim10,TIM_CHANNEL_1,(reload+1)/2-1); sprintf((char*)freq,“FREQUENCE:%6d Hz”,84000000/(reload+1)); LCD_Clear(WHITE); LCD_ShowString(100,40,500,24,24,freq); } } break; default : break; } return ; } 总结 这是给本次电设校赛写的代码,博主是32新手,写这篇主要给自己记录,以供未来查阅,不喜勿喷。(顺便吐槽这次给的100k也太高了,哪里是lm324这种负担得起的啊) 工程文件已在资源中上传,其中delay模块和lcd模块都直接用的正点原子的代码。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1801 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1629 浏览 1 评论
1096 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
735 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1684 浏览 2 评论
1944浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
745浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
580浏览 3评论
602浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
565浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-27 17:43 , Processed in 0.654145 second(s), Total 46, Slave 40 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号