找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6857|回复: 5
收起左侧

基于STM32的DDS信号发生器源码

[复制链接]
ID:292924 发表于 2018-3-16 21:19 | 显示全部楼层 |阅读模式
基于STM32的DDS信号发生器源码

单片机源程序如下:
  1. #include "Init.h"
  2. #include "key.h"
  3. #define FCLK_KHZ 100.0
  4. double FOUT_KHZ = 1.0;
  5. unsigned char table_addr;
  6. unsigned int phase_inc;
  7. unsigned int phase_acc;
  8. const int fangbo[256]={4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,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};
  9. const unsigned char sin_table[256]= {128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220,218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179,176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,103,100,97,93,90,88,85,82,79,76,73,70,67,65,62,59,57,54,52,49,47,44,42,40,37,35,33,31,29,27,25,23,21,20,18,17,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,17,18,20,21,23,25,27,29,31,33,35,37,40,42,44,47,49,52,54,57,59,62,65,67,70,73,76,79,82,85,88,90,93,97,100,103,106,109,112,115,118,121,124};
  10. const int Triangle[256]={0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,480,512,544,576,608,640,672,704,736,768,800,832,864,896,928,960,992,1024,1056,1088,1120,1152,1184,1216,1248,1280,1312,1344,1376,1408,1440,1472,1504,1536,1568,1600,1632,1664,1696,1728,1760,1792,1824,1856,1888,1920,1952,1984,2016,2048,2080,2112,2144,2176,2208,2240,2272,2304,2336,2368,2400,2432,2464,2496,2528,2560,2592,2624,2656,2688,2720,2752,2784,2816,2848,2880,2912,2944,2976,3008,3040,3072,3104,3136,3168,3200,3232,3264,3296,3328,3360,3392,3424,3456,3488,3520,3552,3584,3616,3648,3680,3712,3744,3776,3808,3840,3872,3904,3936,3968,4000,4032,4064,4095,4064,4032,4000,3968,3936,3904,3872,3840,3808,3776,3744,3712,3680,3648,3616,3584,3552,3520,3488,3456,3424,3392,3360,3328,3296,3264,3232,3200,3168,3136,3104,3072,3040,3008,2976,2944,2912,2880,2848,2816,2784,2752,2720,2688,2656,2624,2592,2560,2528,2496,2464,2432,2400,2368,2336,2304,2272,2240,2208,2176,2144,2112,2080,2048,2016,1984,1952,1920,1888,1856,1824,1792,1760,1728,1696,1664,1632,1600,1568,1536,1504,1472,1440,1408,1376,1344,1312,1280,1248,1216,1184,1152,1120,1088,1056,1024,992,960,928,896,864,832,800,768,736,704,672,640,608,576,544,512,480,448,416,384,352,320,288,256,224,192,160,128,96,64,32};
  11. extern float SDS_OutData[4];
  12. extern PIT_InitTypeDef pit1_struct;
  13. uint8 flag=2;
  14. uint32 frequency=0;
  15. uint8 mod=0;//模式选择位
  16. int mVpp=2000;
  17. uint8 a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
  18. void ShowMenu();
  19. void ShowMod1();
  20. void ShowMod2();
  21. void ShowMod3();
  22. void mod1()//设置pwm波的峰峰值,频率,和占空比
  23. {
  24.   if(b==0)//每次进入进行清屏操作
  25.   {
  26.     b=1;
  27.     LCD_CLS();
  28.     ShowMod1();
  29.   }
  30.   uint8 DuliTemp=DuliScan(0);//扫描独立按键
  31.   uint8 KeyTemp=KeyScan();//扫描矩阵按键
  32.   if(KeyTemp!=0||DuliTemp==4||DuliTemp==2&&i<2)//矩阵键盘有键盘按下
  33.   {
  34.     if(i==0)//先设置频率
  35.     {
  36.       if(DuliTemp==2)
  37.         frequency=frequency/10;//后退一格
  38.       else if(DuliTemp==4)
  39.         frequency=10*frequency;//前进一格
  40.       else frequency=10*frequency+KeyTemp;
  41.       b=0;
  42.     }
  43.     else if(i==1)
  44.     {
  45.       if(KeyTemp==1)
  46.         mVpp-=50;
  47.       else if(KeyTemp==2)
  48.         mVpp+=50;
  49.       else if(KeyTemp==4)
  50.         mVpp-=1000;
  51.       else if(KeyTemp==5)
  52.         mVpp+=1000;
  53.       b=0;
  54.     }
  55.   }
  56.   if(DuliTemp==1)//S17按下设置下一个
  57.   {
  58.     i++;
  59.     if(i==2)
  60.     {
  61.       FOUT_KHZ=frequency/1000.0;
  62.       phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
  63.     }
  64.   }
  65.   if(DuliTemp==3)//S17按下代表跳出本系统,显示主菜单、
  66.   {
  67.     a=0;//显示主菜单
  68.     mod=0;//退出系统
  69.   }
  70. }
  71. void mod2()//设置方波的频率和峰峰值
  72. {
  73.   if(c==0)//每次进入进行清屏操作
  74.   {
  75.     c=1;
  76.     LCD_CLS();
  77.     ShowMod2();
  78.   }
  79.   uint8 DuliTemp=DuliScan(0);//扫描独立按键
  80.   uint8 KeyTemp=KeyScan();//扫描矩阵按键
  81.   if(KeyTemp!=0||DuliTemp==4||DuliTemp==2&&i<2)//矩阵键盘有键盘按下
  82.   {
  83.     if(i==0)//先设置频率
  84.     {
  85.       if(DuliTemp==2)
  86.         frequency=frequency/10;//后退一格
  87.       else if(DuliTemp==4)
  88.         frequency=10*frequency;//前进一格
  89.       else frequency=10*frequency+KeyTemp;
  90.       c=0;
  91.     }
  92.     else if(i==1)
  93.     {
  94.       if(KeyTemp==1)
  95.         mVpp-=50;
  96.       else if(KeyTemp==2)
  97.         mVpp+=50;
  98.       else if(KeyTemp==4)
  99.         mVpp-=1000;
  100.       else if(KeyTemp==5)
  101.         mVpp+=1000;
  102.       c=0;
  103.     }
  104.   }
  105.   if(DuliTemp==1)//S17按下设置下一个
  106.   {
  107.     i++;
  108.     if(i==2)
  109.     {
  110.       FOUT_KHZ=frequency/1000.0;
  111.       phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
  112.     }
  113.   }
  114.   if(DuliTemp==3)//S17按下代表跳出本系统,显示主菜单、
  115.   {
  116.     a=0;//显示主菜单
  117.     mod=0;//退出系统
  118.   }
  119. }
  120. void mod3()//设置方波的频率和峰峰值
  121. {
  122.   if(d==0)//每次进入进行清屏操作
  123.   {
  124.     d=1;
  125.     LCD_CLS();
  126.     ShowMod3();
  127.   }
  128.   uint8 DuliTemp=DuliScan(0);//扫描独立按键
  129.   uint8 KeyTemp=KeyScan();//扫描矩阵按键
  130.   if(KeyTemp!=0||DuliTemp==4||DuliTemp==2&&i<2)//矩阵键盘有键盘按下
  131.   {
  132.     if(i==0)//先设置频率
  133.     {
  134.       if(DuliTemp==2)
  135.         frequency=frequency/10;//后退一格
  136.       else if(DuliTemp==4)
  137.         frequency=10*frequency;//前进一格
  138.       else frequency=10*frequency+KeyTemp;
  139.       d=0;
  140.     }
  141.     else if(i==1)
  142.     {
  143.       if(KeyTemp==1)
  144.         mVpp-=50;
  145.       else if(KeyTemp==2)
  146.         mVpp+=50;
  147.       else if(KeyTemp==4)
  148.         mVpp-=1000;
  149.       else if(KeyTemp==5)
  150.         mVpp+=1000;
  151.       d=0;
  152.     }
  153.   }
  154.   if(DuliTemp==1)//S17按下设置下一个
  155.   {
  156.     i++;
  157.     if(i==2)
  158.     {
  159.       FOUT_KHZ=frequency/1000.0;
  160.       phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
  161.     }
  162.   }
  163.   if(DuliTemp==3)//S17按下代表跳出本系统,显示主菜单、
  164.   {
  165.     a=0;//显示主菜单
  166.     mod=0;//退出系统
  167.   }
  168. }

  169. void ChangeMod()//通过按键切换模式(flag=2,4,6对应mod=1,2,3)  a主菜单清屏
  170. {
  171.   uint8 DuliTemp=DuliScan(0);//扫描独立键盘
  172.   if(DuliTemp==4&&flag<6)//S20选择下移
  173.   {
  174.     flag+=2;
  175.     a=0;
  176.   }
  177.   if(DuliTemp==2&&flag>2)//S20选择上移
  178.   {
  179.     flag-=2;
  180.     a=0;
  181.   }
  182.   if(DuliTemp==1) //S17确定选择
  183.   {
  184.      mod=flag/2;
  185.   }
  186.   if(a==0)//进入主菜单
  187.   {
  188.     a=1;
  189.     LCD_CLS();//清屏
  190.     ShowMenu();
  191.   }
  192.   uint8 keytemp=KeyScan();
  193.   if(keytemp==4)
  194.     mod=4;
  195. }
  196. void main (void)
  197. {
  198.   Init();//总的初始化函数
  199.   KeyInit();  
  200.   while(1)
  201.   {
  202.     phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
  203.     ChangeMod();//选择模式
  204.     if(mod==1)
  205.     {
  206.       for(;;)
  207.       {
  208.         mod1();
  209.         if(mod!=1) break;
  210.       }
  211.     }
  212.     else if(mod==2)
  213.     {
  214.       for(;;)
  215.       {
  216.         mod2();
  217.         if(mod!=2) break;
  218.       }
  219.     }
  220.     else if(mod==3)
  221.     {
  222.       for(;;)
  223.       {
  224.         mod3();
  225.         if(mod!=3) break;
  226.       }
  227.     }
  228.     else
  229.     {
  230.       b=0;c=0;d=0;i=0;
  231.     }
  232.   }
  233. }
  234. void ShowMenu()//显示主菜单
  235. {
  236.   LCD_P6x8Str(0,0,"Tell me your choice",0);
  237.   LCD_P8x16Str(0,flag,"-->",0);//选择指示符
  238.   LCD_P8x16Str(24,2,"Sin",0);
  239.   LCD_P8x16Str(24,4,"Pwm",0);
  240.   LCD_P8x16Str(24,6,"Triangle",0);
  241. }
  242. void ShowMod1()//显示设定正弦波界面
  243. {
  244.   LCD_P6x8Str(0,0,"Setting PWM data!",0);
  245.   LCD_P8x16Str(0,1,"f:",0);
  246.   if(frequency>1000)
  247.   { LCD_P8x16Str(100,1,"kHz",0);
  248.     LCD_P8x16float(16,1,frequency*0.001,2,0);
  249.   }
  250.   else
  251.   {
  252.     LCD_P8x16Str(100,1,"Hz",0);
  253.     LCD_P8x16s(16,1,frequency,0);
  254.   }
  255.   LCD_line_level(16,99,25);
  256.   LCD_line_level(40,99,49);
  257.   LCD_P8x16Str(0,4,"Vpp:",0);
  258.   LCD_P8x16Str(100,4,"mV",0);
  259.   LCD_P8x16s(35,4,mVpp,0);
  260.   LCD_line_level(32,99,49);
  261.   LCD_P6x8Str(0,7,"S17-->set over",0);
  262. }
  263. void ShowMod2()//显示设定方波界面
  264. {
  265.   LCD_P6x8Str(0,0,"Setting fangbo!",0);
  266.   LCD_P8x16Str(0,1,"f:",0);
  267.   if(frequency>1000)
  268.   { LCD_P8x16Str(100,1,"kHz",0);
  269.     LCD_P8x16float(16,1,frequency*0.001,2,0);
  270.   }
  271.   else
  272.   {
  273.     LCD_P8x16Str(100,1,"Hz",0);
  274.     LCD_P8x16s(16,1,frequency,0);
  275.   }
  276.   LCD_line_level(16,99,25);
  277.   LCD_line_level(40,99,49);
  278.   LCD_P8x16Str(0,4,"Vpp:",0);
  279.   LCD_P8x16Str(100,4,"mV",0);
  280.   LCD_P8x16s(35,4,mVpp,0);
  281.   LCD_line_level(32,99,49);
  282.   LCD_P6x8Str(0,7,"S17-->set over",0);
  283. }
  284. void ShowMod3()//显示设定三角波界面
  285. {
  286.   LCD_P6x8Str(0,0,"Setting Triangle!",0);
  287.   LCD_P8x16Str(0,1,"f:",0);
  288.   if(frequency>1000)
  289.   { LCD_P8x16Str(100,1,"kHz",0);
  290.     LCD_P8x16float(16,1,frequency*0.001,2,0);
  291.   }
  292.   else
  293.   {
  294.     LCD_P8x16Str(100,1,"Hz",0);
  295.     LCD_P8x16s(16,1,frequency,0);
  296.   }
  297.   LCD_line_level(16,99,25);
  298.   LCD_line_level(40,99,49);
  299.   LCD_P8x16Str(0,4,"Vpp:",0);
  300.   LCD_P8x16Str(100,4,"mV",0);
  301.   LCD_P8x16s(35,4,mVpp,0);
  302.   LCD_line_level(32,99,49);
  303.   LCD_P6x8Str(0,7,"S17-->set over",0);
  304. }
复制代码

所有资料51hei提供下载:
DDS.7z (1.05 MB, 下载次数: 128)
回复

使用道具 举报

ID:298900 发表于 2018-3-29 16:11 | 显示全部楼层
我想问一下这个文件夹是怎么用的
回复

使用道具 举报

ID:427706 发表于 2018-11-16 15:02 | 显示全部楼层
没用工程,差评
回复

使用道具 举报

ID:307825 发表于 2018-11-16 15:32 | 显示全部楼层
么样用也没个说明
回复

使用道具 举报

ID:673201 发表于 2019-12-25 10:06 | 显示全部楼层
文件已损坏啊
回复

使用道具 举报

ID:729512 发表于 2021-4-26 14:44 | 显示全部楼层
用不了呀 损坏了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表