基于STM32的DDS信号发生器源码
单片机源程序如下:
- #include "Init.h"
- #include "key.h"
- #define FCLK_KHZ 100.0
- double FOUT_KHZ = 1.0;
- unsigned char table_addr;
- unsigned int phase_inc;
- unsigned int phase_acc;
- 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};
- 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};
- 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};
- extern float SDS_OutData[4];
- extern PIT_InitTypeDef pit1_struct;
- uint8 flag=2;
- uint32 frequency=0;
- uint8 mod=0;//模式选择位
- int mVpp=2000;
- 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;
- void ShowMenu();
- void ShowMod1();
- void ShowMod2();
- void ShowMod3();
- void mod1()//设置pwm波的峰峰值,频率,和占空比
- {
- if(b==0)//每次进入进行清屏操作
- {
- b=1;
- LCD_CLS();
- ShowMod1();
- }
- uint8 DuliTemp=DuliScan(0);//扫描独立按键
- uint8 KeyTemp=KeyScan();//扫描矩阵按键
- if(KeyTemp!=0||DuliTemp==4||DuliTemp==2&&i<2)//矩阵键盘有键盘按下
- {
- if(i==0)//先设置频率
- {
- if(DuliTemp==2)
- frequency=frequency/10;//后退一格
- else if(DuliTemp==4)
- frequency=10*frequency;//前进一格
- else frequency=10*frequency+KeyTemp;
- b=0;
- }
- else if(i==1)
- {
- if(KeyTemp==1)
- mVpp-=50;
- else if(KeyTemp==2)
- mVpp+=50;
- else if(KeyTemp==4)
- mVpp-=1000;
- else if(KeyTemp==5)
- mVpp+=1000;
- b=0;
- }
- }
- if(DuliTemp==1)//S17按下设置下一个
- {
- i++;
- if(i==2)
- {
- FOUT_KHZ=frequency/1000.0;
- phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
- }
- }
- if(DuliTemp==3)//S17按下代表跳出本系统,显示主菜单、
- {
- a=0;//显示主菜单
- mod=0;//退出系统
- }
- }
- void mod2()//设置方波的频率和峰峰值
- {
- if(c==0)//每次进入进行清屏操作
- {
- c=1;
- LCD_CLS();
- ShowMod2();
- }
- uint8 DuliTemp=DuliScan(0);//扫描独立按键
- uint8 KeyTemp=KeyScan();//扫描矩阵按键
- if(KeyTemp!=0||DuliTemp==4||DuliTemp==2&&i<2)//矩阵键盘有键盘按下
- {
- if(i==0)//先设置频率
- {
- if(DuliTemp==2)
- frequency=frequency/10;//后退一格
- else if(DuliTemp==4)
- frequency=10*frequency;//前进一格
- else frequency=10*frequency+KeyTemp;
- c=0;
- }
- else if(i==1)
- {
- if(KeyTemp==1)
- mVpp-=50;
- else if(KeyTemp==2)
- mVpp+=50;
- else if(KeyTemp==4)
- mVpp-=1000;
- else if(KeyTemp==5)
- mVpp+=1000;
- c=0;
- }
- }
- if(DuliTemp==1)//S17按下设置下一个
- {
- i++;
- if(i==2)
- {
- FOUT_KHZ=frequency/1000.0;
- phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
- }
- }
- if(DuliTemp==3)//S17按下代表跳出本系统,显示主菜单、
- {
- a=0;//显示主菜单
- mod=0;//退出系统
- }
- }
- void mod3()//设置方波的频率和峰峰值
- {
- if(d==0)//每次进入进行清屏操作
- {
- d=1;
- LCD_CLS();
- ShowMod3();
- }
- uint8 DuliTemp=DuliScan(0);//扫描独立按键
- uint8 KeyTemp=KeyScan();//扫描矩阵按键
- if(KeyTemp!=0||DuliTemp==4||DuliTemp==2&&i<2)//矩阵键盘有键盘按下
- {
- if(i==0)//先设置频率
- {
- if(DuliTemp==2)
- frequency=frequency/10;//后退一格
- else if(DuliTemp==4)
- frequency=10*frequency;//前进一格
- else frequency=10*frequency+KeyTemp;
- d=0;
- }
- else if(i==1)
- {
- if(KeyTemp==1)
- mVpp-=50;
- else if(KeyTemp==2)
- mVpp+=50;
- else if(KeyTemp==4)
- mVpp-=1000;
- else if(KeyTemp==5)
- mVpp+=1000;
- d=0;
- }
- }
- if(DuliTemp==1)//S17按下设置下一个
- {
- i++;
- if(i==2)
- {
- FOUT_KHZ=frequency/1000.0;
- phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
- }
- }
- if(DuliTemp==3)//S17按下代表跳出本系统,显示主菜单、
- {
- a=0;//显示主菜单
- mod=0;//退出系统
- }
- }
- void ChangeMod()//通过按键切换模式(flag=2,4,6对应mod=1,2,3) a主菜单清屏
- {
- uint8 DuliTemp=DuliScan(0);//扫描独立键盘
- if(DuliTemp==4&&flag<6)//S20选择下移
- {
- flag+=2;
- a=0;
- }
- if(DuliTemp==2&&flag>2)//S20选择上移
- {
- flag-=2;
- a=0;
- }
- if(DuliTemp==1) //S17确定选择
- {
- mod=flag/2;
- }
- if(a==0)//进入主菜单
- {
- a=1;
- LCD_CLS();//清屏
- ShowMenu();
- }
- uint8 keytemp=KeyScan();
- if(keytemp==4)
- mod=4;
- }
- void main (void)
- {
- Init();//总的初始化函数
- KeyInit();
- while(1)
- {
- phase_inc=(FOUT_KHZ*256/FCLK_KHZ)*256+0.5;
- ChangeMod();//选择模式
- if(mod==1)
- {
- for(;;)
- {
- mod1();
- if(mod!=1) break;
- }
- }
- else if(mod==2)
- {
- for(;;)
- {
- mod2();
- if(mod!=2) break;
- }
- }
- else if(mod==3)
- {
- for(;;)
- {
- mod3();
- if(mod!=3) break;
- }
- }
- else
- {
- b=0;c=0;d=0;i=0;
- }
- }
- }
- void ShowMenu()//显示主菜单
- {
- LCD_P6x8Str(0,0,"Tell me your choice",0);
- LCD_P8x16Str(0,flag,"-->",0);//选择指示符
- LCD_P8x16Str(24,2,"Sin",0);
- LCD_P8x16Str(24,4,"Pwm",0);
- LCD_P8x16Str(24,6,"Triangle",0);
- }
- void ShowMod1()//显示设定正弦波界面
- {
- LCD_P6x8Str(0,0,"Setting PWM data!",0);
- LCD_P8x16Str(0,1,"f:",0);
- if(frequency>1000)
- { LCD_P8x16Str(100,1,"kHz",0);
- LCD_P8x16float(16,1,frequency*0.001,2,0);
- }
- else
- {
- LCD_P8x16Str(100,1,"Hz",0);
- LCD_P8x16s(16,1,frequency,0);
- }
- LCD_line_level(16,99,25);
- LCD_line_level(40,99,49);
- LCD_P8x16Str(0,4,"Vpp:",0);
- LCD_P8x16Str(100,4,"mV",0);
- LCD_P8x16s(35,4,mVpp,0);
- LCD_line_level(32,99,49);
- LCD_P6x8Str(0,7,"S17-->set over",0);
- }
- void ShowMod2()//显示设定方波界面
- {
- LCD_P6x8Str(0,0,"Setting fangbo!",0);
- LCD_P8x16Str(0,1,"f:",0);
- if(frequency>1000)
- { LCD_P8x16Str(100,1,"kHz",0);
- LCD_P8x16float(16,1,frequency*0.001,2,0);
- }
- else
- {
- LCD_P8x16Str(100,1,"Hz",0);
- LCD_P8x16s(16,1,frequency,0);
- }
- LCD_line_level(16,99,25);
- LCD_line_level(40,99,49);
- LCD_P8x16Str(0,4,"Vpp:",0);
- LCD_P8x16Str(100,4,"mV",0);
- LCD_P8x16s(35,4,mVpp,0);
- LCD_line_level(32,99,49);
- LCD_P6x8Str(0,7,"S17-->set over",0);
- }
- void ShowMod3()//显示设定三角波界面
- {
- LCD_P6x8Str(0,0,"Setting Triangle!",0);
- LCD_P8x16Str(0,1,"f:",0);
- if(frequency>1000)
- { LCD_P8x16Str(100,1,"kHz",0);
- LCD_P8x16float(16,1,frequency*0.001,2,0);
- }
- else
- {
- LCD_P8x16Str(100,1,"Hz",0);
- LCD_P8x16s(16,1,frequency,0);
- }
- LCD_line_level(16,99,25);
- LCD_line_level(40,99,49);
- LCD_P8x16Str(0,4,"Vpp:",0);
- LCD_P8x16Str(100,4,"mV",0);
- LCD_P8x16s(35,4,mVpp,0);
- LCD_line_level(32,99,49);
- LCD_P6x8Str(0,7,"S17-->set over",0);
- }
-
复制代码
所有资料51hei提供下载:
DDS.7z
(1.05 MB, 下载次数: 128)
|