51单片机制作的函数信号发生器设计 Proteus仿真
Proteus仿真和51单片机C源码.zip
(108.81 KB, 售价: 3 E币)
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
部分源码:完整源码见附件
- #include <reg52.h>
- #include <math.h>
- #include <intrins.h>
- #include <lcd12864.h>
- #define DAC_OUT P2
- sbit change_wave=P3^2; //改变波形按键
- sbit change_rate=P3^3; //改变频率按键
- uchar mode=0,rate=0,delay_time=0,k,p; //为波形发生模块提供中间变量
- uchar *which_wave,*which_wave2;
- uint rate_num;
- uchar code Sine_wave[64]= //DA输出对应电压值对应的数字量,正弦波
- {
- 128,114,102,90,78,66,55,45,36,28,20,14,9,5,2,1,1,1,
- 3,7,11,17,24,32,41,50,61,72,84,96,108,121,133,146,
- 158,170,182,193,204,213,222,230,237,243,247,251,253,
- 254,254,252,249,245,240,234,226,218,209,199,188,176,
- 167,158,145,135
- };
- uchar code Sawtooth_Wave[64]= //锯齿波
- {
- 255,251,247,243,239,235,231,227,223,219,215,210,206,202,
- 198,194,190,186,182,178,174,170,166,162,158,154,150,146,
- 142,138,134,130,125,121,117,113,109,105,101,97,93,89,85,
- 81,77,73,69,61,57,53,49,45,40,36,32,28,24,20,16,12,8,4,0
- };
- uchar code Square_wave[64]= //方波
- {
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,255,255,255,255,255,255,255,255,
- 255,255,255,255,255,255,
- };
- uchar code Triangular_Wave[64]= //三角波
- {
- 0,8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128,136,
- 144,152,160,168,176,184,192,200,208,216,224,232,240,248,
- 248,240,232,224,216,208,200,192,184,176,168,160,152,144,
- 136,128,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0
- };
- void wave_delay() //波形延时函数
- {
- int a,b;
- for(a=1; a>0; a--)
- for(b=122; b>0; b--);
- }
- void wave_init() //波形发生模块的初始化(外部中断0、1)
- {
- EA=1;
- IT0=1; //下降沿触发
- EX0=1;
- IT1=1;
- EX1=1;
- }
- void disp_wave(uchar *wave) //显示波形函数
- {
- uchar page,i;
- uint date;
- select(1); //选择左屏
- for(i=32; i<64; i++)
- {
- page=7-(wave[i]/4)/8;
- date=7-(wave[i]/4)%8;
- date=pow(2,date);
- setpos(page,i); //选择行列
- lcd_wdat(date);
- }
- select(2); //选择右屏
- for(i=0; i<64; i++)
- {
- page=7-(wave[i]/4)/8;
- date=7-(wave[i]/4)%8;
- date=pow(2,date);
- setpos(page,i); //选择行列
- lcd_wdat(date);
- }
- }
- void main()
- {
- lcd_init();
- clr_screen();
- which_wave=&Sine_wave[0];
- disp_chinese();
- disp_function(chinese_sine);
- disp_rate(400);
- disp_xy();
- disp_wave(which_wave);
- wave_init();
- while(1)
- {
- delay_time=rate; //rate=0时,12mhz下,为400hz的波形。循环一次的时间为0.00025ms
- DAC_OUT=*(which_wave+k);
- k++;
- if(k==64)
- k=0;
- while(delay_time)
- delay_time--;
- }
- }
- void int0() interrupt 0 //波形选择中断服务程序
- {
- EX0=0;
- wave_delay();
- mode++;
- if(mode==4)
- mode=0;
- switch(mode)
- {
- case 0 : //显示正弦波
- which_wave=&Sine_wave[0];
- which_wave2=&chinese_sine[0];
- break;
- case 1 : //显示三角波
- which_wave=&Triangular_Wave[0];
- which_wave2=&chinese_triangular[0];
- break;
- case 2 : //显示方波
- which_wave=&Square_wave[0];
- which_wave2=&chinese_square[0];
- break;
- case 3 : //显示锯齿波
- which_wave=&Sawtooth_Wave[0];
- which_wave2=&chinese_sawtooth[0];
- break;
- }
- wave_delay();
- clr_screen();
- disp_chinese();
- disp_rate(400/rate);
- disp_function(which_wave2);
- disp_xy();
- disp_wave(which_wave);
- while(!change_wave);
- EX0=1;
- }
- void int1() interrupt 2 //频率选择
- {
- EX1=0;
- wave_delay();
- p++;
- if(p==50)
- p=0;
- rate=p;
- wave_delay();
- rate_num=1/((0.000036+0.000006*rate)*64);
- clc_rate();
- disp_rate(rate_num);
- while(!change_rate);
- EX1=1;
- }
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|
|