基于51单片机的波形发生器(5种,调频)
设计一个单片机控制的信号发生器。
用处理器系统的控制可用于生成各种波形,例如方波,三角波,锯齿波,正弦波等。 可以调整信号发生器产生的波形的频率。 信号波形可以通过软件更改。
基本要求:
(1)产生三种以上波形。如正弦波、三角波、矩形波等。
(2)最大频率不低于 500Hz。并且频率可按一定规律调节,如周期按1T,2T,3T,4T或1T,2T,4T,8T变化。
(3)扩展要求:产生更多的频率和波形。
//周期1252 2504 3756 5007 6261 7513 8756 10020
//
#include<reg51.h>
#include"absacc.h"
#include"intrins.h"
#include "lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
#define out XBYTE[0xfeff]
uchar boxing=0;//波形。正弦、三角、方波、锯齿、梯形
uchar zhouqi=0;//周期1T2T3T4T5T6T7T8T
uchar time=0;//计时
uchar first[]="wave:0";//波形编号
uchar two[]="1T";//周期
uchar code zhengx[]={//正弦编码
128,131,134,137,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,188,191,194,
196,199,202,204,207,209,212,214,216,219,221,223,225,227,229,231,233,234,236,238,239,241,242,244,245,246,247,249,
250,250,251,252,253,254,254,255,255,255,255,255,255,255,255,255,255,255,255,255,254,254,253,252,251,250,250,249,247,
246,245,244,242,241,239,238,236,234,233,231,229,227,225,223,221,219,216,214,212,209,207,204,202,199,196,194,191,
188,186,183,180,177,174,171,168,165,162,159,156,153,150,147,144,141,137,134,131,128,125,122,119,115,112,109,106,
103,100,97,94,91,88,85,82,79,76,73,70,68,65,62,60,57,54,52,49,47,44,42,40,37,35,33,31,29,27,25,23,22,20,18,17,15,
14,12,11,10,9,7,6,6,5,4,3,2,2,1,1,1,1,0,0,0,0,0,0,1,1,1,2,2,3,4,5,6,6,7,9,10,11,12,14,15,17,18,20,22,23,25,27,29,
31,33,35,37,40,42,44,47,49,52,54,57,60,62,65,68,70,73,76,79,82,85,88,91,94,97,100,103,106,109,112,115,119,122,125,128,128,128};
void delay(uint i) //延时函数
{
while(i--);
}
void num0()//正弦
{
switch(zhouqi)
{
case 0:
for(time=0;time<255;time++)
out=zhengx[time];
break;
case 1:
for(time=0;time<255;time++)
{
out=zhengx[time];
_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 2:
for(time=0;time<255;time++)
{
out=zhengx[time];
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 3:
for(time=0;time<255;time++)
{
out=zhengx[time];
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 4:
for(time=0;time<255;time++)
{
out=zhengx[time];
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 5:
for(time=0;time<255;time++)
{
out=zhengx[time];
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();
}
break;
case 6:
for(time=0;time<255;time++)
{
out=zhengx[time];
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 7:
for(time=0;time<255;time++)
{
out=zhengx[time];
delay(1);
}
}
}
void num1()//三角波
{
switch(zhouqi)
{
case 0:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
}
break;
case 1:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 2:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
delay(2);
}
break;
case 3:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
delay(4);
_nop_();_nop_();_nop_();_nop_();
}
break;
case 4:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
delay(5);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 5:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
delay(7);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 6:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
delay(12);
}
break;
case 7:
for(time=0;time<60;time++)
{
if(time<30)
out=8*time;
else
out=8*(60-time);
delay(16);
}
}
}
void num2()//方波
{
switch(zhouqi)
{
case 0:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
}
break;
case 1:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 2:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
delay(1);
_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 3:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
delay(3);
_nop_();_nop_();
}
break;
case 4:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
delay(4);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 5:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
delay(6);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 6:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
delay(9);
}
break;
case 7:
for(time=0;time<80;time++)
{
if(time<40)
out=0;
else
out=0xff;
delay(11);
}
}
}
void num3()//锯齿波
{
switch(zhouqi)
{
case 0:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
}
break;
case 1:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(1);
}
break;
case 2:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(3);
_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 3:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(6);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 4:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(8);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 5:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(10);
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
break;
case 6:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(14);
}
break;
case 7:
for(time=0;time<100;time++)
{
if(time<50)
out=256-5*time;
else
out=256-5*(time-50);
delay(20);
}
}
}
void num4()//梯形波
{
switch(zhouqi)
{
case 0:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
}
break;
case 1:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(1);
}
break;
case 2:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(3);
}
break;
case 3:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(5);
}
break;
case 4:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(8);
}
break;
case 5:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(11);
}
break;
case 6:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(14);
}
break;
case 7:
for(time=0;time<50;time++)
{
if(time<15)
out=255-17*time;
else if(time<35)
out=0;
else
out=255-17*(50-time);
delay(20);
}
}
}
void main()
{
//设置INT0
IT0=1;//跳变沿出发方式(下降沿)
EX0=1;//打开INT0的中断允许。
//设置INT1
IT1=1;//跳变沿出发方式(下降沿)
EX1=1;//打开INT0的中断允许。
//设置定时器
/* TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0=0XFF;//给定时器赋初值
TL0=0X18;
ET0=1;//打开定时器0中断允许
TR0=0;//打开定时器*/
EA=1;//打开总中断
init_1602();//初始化LCD
write_string(1,4,first);
write_string(2,8,two);
while(1)
{
time++;//计时加一
switch(boxing)
{
case 0://正弦波
num0();
break;
case 1://三角波
num1();
break;
case 2: //方波
num2();
break;
case 3: //锯齿波
num3();
break;
case 4: //梯形波
num4();
}
}
}
void intt0() interrupt 0//INT0中断
{
if(boxing<4)//波形选择
boxing++;
else
boxing=0;
first[5]=boxing+0x30;
write_string(1,4,first);
}
void intt1() interrupt 2//INT1中断
{
if(zhouqi<7)//周期选择
zhouqi++;
else
zhouqi=0;
two[0]=zhouqi+0x31;
write_string(2,8,two);
}
/*void Timer0() interrupt 1//定时器中断
{
TH0=0Xff; //给定时器赋初值
TL0=init[zhouqi];
}*/
标签:include,波形,uchar,原理图,波形发生器,128,250,源代码,255
From: https://blog.csdn.net/cqtianxingkeji/article/details/136814552