![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
2个回答
展开全部
这个网上资料很多的,你可以参考下的,我前几天也做了个,proteus仿真的,程序如下,希望能帮到你的,有问题可以交流下的
#include<reg51.h>
unsigned char i,sqar_num=128; //最大值100,默认值50
unsigned char cho=0; //0:正弦波。1:方波。2:三角波。3:锯齿波。
unsigned char num=0;
unsigned char TIME0_H=0xff,TIME0_L=0xdc; //定时器0的初值设置;全局变量.对应正弦波,锯齿波50HZ
sbit chg= P1^0;
sbit freq_u=P1^1;
sbit freq_d=P1^2;
sbit duty_u=P1^3;
sbit duty_d=P1^4;
sbit cs =P3^7;
bit flag=0;
unsigned int FREQ=50;//初始化频率,30HZ
unsigned char flag1;
sbit rs=P1^5;
sbit lcdcs=P1^7;
sbit cd=P3^0;
unsigned char TempBuffer[7];
unsigned char value1[]={"Frequency:"};
void delay(unsigned int ms)
{
unsigned int i,j;
for (j=0;j<ms;j++)
for (i=0;i<120;i++);
}
unsigned char code sin_num[]={
0x80,0x82,0x85,0x88,0x8b,0x8e,0x91,0x94,0x97,0x9a,0x9d,0xa0,0xa3,0xa6,
0xa9,0xac,0xaf,0xb2,0xb6,0xb9,0xbc,0xbf,0xc2,0xc5,0xc7,0xca,0xcc,0xcf,
0xd1,0xd4,0xd6,0xd8,0xda,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,
0xee,0xef,0xf1,0xf2,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,
0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfe,0xfd,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,
0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,0xde,0xdd,0xda,0xd8,0xd6,
0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,0xbf,0xbc,0xba,0xb7,0xb4,0xb1,
0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,0x93,0x90,0x8d,0x89,0x86,
0x83,0x80,
0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,0x66,0x63,0x60,0x5d,
0x5a,0x57,0x55,0x51,0x4e,0x4c,0x48,0x45,0x43,0x40,0x3d,0x3a,0x38,0x35,
0x33,0x30,0x2e,0x2b,0x29,0x27,0x25,0x22,0x20,0x1e,0x1c,0x1a,0x18,0x16,
0x15,0x13,0x11,0x10,0x0e,0x0d,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,
0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,
0x0d,0x0e,0x10,0x11,0x13,0x15,0x16,0x18,0x1a,0x1c,0x1e,0x20,0x22,0x25,
0x27,0x29,0x2b,0x2e,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,0x48,
0x4c,0x4e,0x51,0x55,0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x72,
0x76,0x79,0x7c,0x80
/* 135,145,158,167,176,188,199,209,218,226,234,240,245,249,252,254,254,253,251,247,243,237,230,222,213,204,193,182,170,158,
146,133,121,108,96,84,72,61,50,41,32,24,17,11,7,3,1,0,0,2,5,9,14,20,28,36,45,55,66,78,90,102,114,128
*/
};
unsigned code sanjiao_num[]=
{
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,
36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,
70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,
104,106,108,110,112,114,116,118,120,122,124,126,128,130,
132,134,136,138,140,142,144,146,148,150,152,154,158,160,162,164,166,168,170,
172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,
212,214,216,218,220,222,224,226,228,230,232,234,236,238,240,242,244,246,248,250,
252,254,255,254,252,250,248,246,244,242,240,238,236,234,232,230,228,226,224,222,220,
218,216,214,212,210,208,206,204,202,200,198,196,194,192,190,188,186,184,182,180,178,
176,174,172,170,168,166,164,162,160,158,154,152,150,148,146,144, 142,140,138,136,134,
132,130,128,126,124,122,120,118,116,114,112,110,108,106,104,102,100,98,96,94,92,90,88,
86,84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,
34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0,
};
//调节部分——频率
void freq_ud(void)
{
unsigned int temp;
if(freq_d==0)
{ FREQ--; }
else if(freq_u==0)
{ FREQ++; }
temp=0xffff-3906/FREQ; //切换后频率为50HZ 65336-10^6/(256*FREQ)
// temp=0xffff-1953/FREQ;
TIME0_H=temp/256;
TIME0_L=temp%256;
}
//调节部分——方波的占空比
void duty_ud(void) //方波也采用512次中断构成一个周期。
{
if(duty_d==0&sqar_num>0)
sqar_num--;
else if(duty_u==0&sqar_num<255)
sqar_num++;
}
//波形发生函数
void sint(void)
{
// char num=0;
// if(!flag)
{
cs=0;P2=sin_num[num++];cs=1;
if(num==255){num=0;}
}
/* else if(flag)
{
cs=0;P2=sin_num[num--];cs=1;
if(num==0){num=0;flag=0;}
} */
}
void square(void)
{
if(i++<sqar_num) {cs=0;P2=0XFF;cs=1;}
else{cs=0;P2=0X00;cs=1;}
}
void triangle(void)
{
cs=0;P2=num++;cs=1;
}
void stw(void)
{
cs=0;P2=sanjiao_num[num++];cs=1;
if(num==255){num=0;flag=1;}
}
//1602显示开始
void write_command(unsigned char command)
{
rs=0;
P0=command;
lcdcs=1;
lcdcs=0;
}
void write_data(unsigned char data0)
{
rs=1;
P0=data0;
lcdcs=1;
lcdcs=0;
}
void init_1602()
{
delay(1);
write_command(0x38);
delay(1);
write_command(0x0c);
delay(1);
write_command(0x06);
delay(1);
}
//1602显示结束
/*******************频率值转换为字符串**********************/
void temp_to_str()
{
TempBuffer[0]=FREQ/1000+'0'; //千位
TempBuffer[1]=FREQ%1000/100+'0'; //百位
TempBuffer[2]=FREQ%1000%100/10+'0';//十位
TempBuffer[3]=FREQ%1000%100%10+'0';//个位
TempBuffer[4]='H';
TempBuffer[5]='Z';
TempBuffer[6]='\0';
}
void main()
{
TMOD=0X01;
TH0=0xff;
TL0=0xd9;
IT0=1; //设置中断触发方式,下降沿
EA=1;
EX0=1;
ET0=1;
IP=0X01; //键盘中断级别高
init_1602();//初始化lcd
write_command(0x80);//液晶显示位置
delay(1);
for (i=0;i<sizeof(value1)-1;i++)
{
write_data(value1[i]);
delay(1);
}
TR0=1;
while(1)
{
show_frequency();
}
}
#include<reg51.h>
unsigned char i,sqar_num=128; //最大值100,默认值50
unsigned char cho=0; //0:正弦波。1:方波。2:三角波。3:锯齿波。
unsigned char num=0;
unsigned char TIME0_H=0xff,TIME0_L=0xdc; //定时器0的初值设置;全局变量.对应正弦波,锯齿波50HZ
sbit chg= P1^0;
sbit freq_u=P1^1;
sbit freq_d=P1^2;
sbit duty_u=P1^3;
sbit duty_d=P1^4;
sbit cs =P3^7;
bit flag=0;
unsigned int FREQ=50;//初始化频率,30HZ
unsigned char flag1;
sbit rs=P1^5;
sbit lcdcs=P1^7;
sbit cd=P3^0;
unsigned char TempBuffer[7];
unsigned char value1[]={"Frequency:"};
void delay(unsigned int ms)
{
unsigned int i,j;
for (j=0;j<ms;j++)
for (i=0;i<120;i++);
}
unsigned char code sin_num[]={
0x80,0x82,0x85,0x88,0x8b,0x8e,0x91,0x94,0x97,0x9a,0x9d,0xa0,0xa3,0xa6,
0xa9,0xac,0xaf,0xb2,0xb6,0xb9,0xbc,0xbf,0xc2,0xc5,0xc7,0xca,0xcc,0xcf,
0xd1,0xd4,0xd6,0xd8,0xda,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,
0xee,0xef,0xf1,0xf2,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,
0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfe,0xfd,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,
0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,0xde,0xdd,0xda,0xd8,0xd6,
0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,0xbf,0xbc,0xba,0xb7,0xb4,0xb1,
0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,0x93,0x90,0x8d,0x89,0x86,
0x83,0x80,
0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,0x66,0x63,0x60,0x5d,
0x5a,0x57,0x55,0x51,0x4e,0x4c,0x48,0x45,0x43,0x40,0x3d,0x3a,0x38,0x35,
0x33,0x30,0x2e,0x2b,0x29,0x27,0x25,0x22,0x20,0x1e,0x1c,0x1a,0x18,0x16,
0x15,0x13,0x11,0x10,0x0e,0x0d,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,
0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,
0x0d,0x0e,0x10,0x11,0x13,0x15,0x16,0x18,0x1a,0x1c,0x1e,0x20,0x22,0x25,
0x27,0x29,0x2b,0x2e,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,0x48,
0x4c,0x4e,0x51,0x55,0x57,0x5a,0x5d,0x60,0x63,0x66,0x69,0x6c,0x6f,0x72,
0x76,0x79,0x7c,0x80
/* 135,145,158,167,176,188,199,209,218,226,234,240,245,249,252,254,254,253,251,247,243,237,230,222,213,204,193,182,170,158,
146,133,121,108,96,84,72,61,50,41,32,24,17,11,7,3,1,0,0,2,5,9,14,20,28,36,45,55,66,78,90,102,114,128
*/
};
unsigned code sanjiao_num[]=
{
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,
36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,
70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,
104,106,108,110,112,114,116,118,120,122,124,126,128,130,
132,134,136,138,140,142,144,146,148,150,152,154,158,160,162,164,166,168,170,
172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,
212,214,216,218,220,222,224,226,228,230,232,234,236,238,240,242,244,246,248,250,
252,254,255,254,252,250,248,246,244,242,240,238,236,234,232,230,228,226,224,222,220,
218,216,214,212,210,208,206,204,202,200,198,196,194,192,190,188,186,184,182,180,178,
176,174,172,170,168,166,164,162,160,158,154,152,150,148,146,144, 142,140,138,136,134,
132,130,128,126,124,122,120,118,116,114,112,110,108,106,104,102,100,98,96,94,92,90,88,
86,84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,
34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0,
};
//调节部分——频率
void freq_ud(void)
{
unsigned int temp;
if(freq_d==0)
{ FREQ--; }
else if(freq_u==0)
{ FREQ++; }
temp=0xffff-3906/FREQ; //切换后频率为50HZ 65336-10^6/(256*FREQ)
// temp=0xffff-1953/FREQ;
TIME0_H=temp/256;
TIME0_L=temp%256;
}
//调节部分——方波的占空比
void duty_ud(void) //方波也采用512次中断构成一个周期。
{
if(duty_d==0&sqar_num>0)
sqar_num--;
else if(duty_u==0&sqar_num<255)
sqar_num++;
}
//波形发生函数
void sint(void)
{
// char num=0;
// if(!flag)
{
cs=0;P2=sin_num[num++];cs=1;
if(num==255){num=0;}
}
/* else if(flag)
{
cs=0;P2=sin_num[num--];cs=1;
if(num==0){num=0;flag=0;}
} */
}
void square(void)
{
if(i++<sqar_num) {cs=0;P2=0XFF;cs=1;}
else{cs=0;P2=0X00;cs=1;}
}
void triangle(void)
{
cs=0;P2=num++;cs=1;
}
void stw(void)
{
cs=0;P2=sanjiao_num[num++];cs=1;
if(num==255){num=0;flag=1;}
}
//1602显示开始
void write_command(unsigned char command)
{
rs=0;
P0=command;
lcdcs=1;
lcdcs=0;
}
void write_data(unsigned char data0)
{
rs=1;
P0=data0;
lcdcs=1;
lcdcs=0;
}
void init_1602()
{
delay(1);
write_command(0x38);
delay(1);
write_command(0x0c);
delay(1);
write_command(0x06);
delay(1);
}
//1602显示结束
/*******************频率值转换为字符串**********************/
void temp_to_str()
{
TempBuffer[0]=FREQ/1000+'0'; //千位
TempBuffer[1]=FREQ%1000/100+'0'; //百位
TempBuffer[2]=FREQ%1000%100/10+'0';//十位
TempBuffer[3]=FREQ%1000%100%10+'0';//个位
TempBuffer[4]='H';
TempBuffer[5]='Z';
TempBuffer[6]='\0';
}
void main()
{
TMOD=0X01;
TH0=0xff;
TL0=0xd9;
IT0=1; //设置中断触发方式,下降沿
EA=1;
EX0=1;
ET0=1;
IP=0X01; //键盘中断级别高
init_1602();//初始化lcd
write_command(0x80);//液晶显示位置
delay(1);
for (i=0;i<sizeof(value1)-1;i++)
{
write_data(value1[i]);
delay(1);
}
TR0=1;
while(1)
{
show_frequency();
}
}
展开全部
基于51单片机的信号发生器-完整电路、程序
http://wenku.baidu.com/view/effec6f2ba0d4a7302763a13.html 免费的。
可以产生弦波、方波、三角波、三角波、梯形波及其他任意波形,波形的频率(最大790HZ)和幅度在一定范围内可任意改变。波形和频率的改变通过软件控制,幅度的改变通过硬件实现。这是本人课程设计得到优秀的作品,有完整的电路图,程序。不是光仿真那种。
如果你觉得790HZ的频率不够的话,可以用FPGA+AD,频率几十M都没问题,也不是很难。多看看资料,
http://wenku.baidu.com/view/effec6f2ba0d4a7302763a13.html 免费的。
可以产生弦波、方波、三角波、三角波、梯形波及其他任意波形,波形的频率(最大790HZ)和幅度在一定范围内可任意改变。波形和频率的改变通过软件控制,幅度的改变通过硬件实现。这是本人课程设计得到优秀的作品,有完整的电路图,程序。不是光仿真那种。
如果你觉得790HZ的频率不够的话,可以用FPGA+AD,频率几十M都没问题,也不是很难。多看看资料,
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询