基于51单片机的波形发生器(3种,调频)原理图、流程图、物料清单、仿真图、源代码
基于51单片机的波形发生器(3种,调频)
同时产生3种波形:正弦波、三角波、方波;
可分别调整各个波形的频率;
通过LCD显示频率。
【仿真时引脚会变黄(跟总线寻址的引脚重叠了),不影响正常功能】
#include<reg51.h>
#include"absacc.h"
#include"intrins.h"
#include "lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
#define sinn XBYTE[0xfeff]//正弦波
#define three XBYTE[0xfdff]//三角波
sbit fangbo=P3^5;//方波
sbit k1=P3^0;//按钮
sbit k2=P3^1;
sbit k3=P2^4;
sbit k4=P2^5;
sbit k5=P2^6;
sbit k6=P2^7;
uchar time1=3,time2=3;//计时变量
uint time3=50;
uchar sec1=0,sec2=0;
uint sec3=0;
uchar kk1=0,kk2=0;
uint freq1=10,freq2=10,freq3=10;
uchar hang1[]="z:00000 s:00000 ";//正弦、三角
uchar hang2[]="f:00000";//方波
uchar code sin_data[32]={//正弦编码
128,153,177,199,219,234,246,254,255,254,246,234,
219,199,177,153,128,103,79, 57, 37, 22, 10, 2,
0, 2, 10, 22, 37, 57, 79, 103};
void display()//显示
{
hang1[3]=freq1/1000+0x30;
hang1[4]=freq1%1000/100+0x30;
hang1[5]=freq1%100/10+0x30;
hang1[6]=freq1%10+0x30;
hang1[11]=freq2/1000+0x30;
hang1[12]=freq2%1000/100+0x30;
hang1[13]=freq2%100/10+0x30;
hang1[14]=freq2%10+0x30;
hang2[3]=freq3/1000+0x30;
hang2[4]=freq3%1000/100+0x30;
hang2[5]=freq3%100/10+0x30;
hang2[6]=freq3%10+0x30;
write_string(1,0,hang1);
write_string(2,0,hang2);
}
void main()
{
unsigned int i;
init_1602();//初始化LCD
display();
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
while(1)
{
if(!k1)//正弦波
{
if(freq1<31)
freq1++;
i=1000/32;
time1=i/freq1;
display();
while(!k1);
}
if(!k2)
{
if(freq1>1)
freq1--;
i=1000/32;
time1=i/freq1;
display();
while(!k2);
}
if(!k3)//三角波
{
if(freq2<30)
freq2++;
i=1000/32;
time2=i/freq2;
display();
while(!k3);
}
if(!k4)
{
if(freq2>1)
freq2--;
i=1000/32;
time2=i/freq2;
display();
while(!k4);
}
if(!k5)//方波
{
if(freq3<500)
freq3++;
time3=499/freq3;
display();
while(!k5);
}
if(!k6)
{
if(freq3>1)
freq3--;
time3=499/freq3;
display();
while(!k6);
}
}
}
//定时器中断
void Timer0() interrupt 1
{
if(sec1<time1)//正弦波输出
sec1++;
else
{
sec1=0;
if(kk1<31)
kk1++;
else
kk1=0;
sinn=sin_data[kk1];//正弦波
}
if(sec2<time2)//三角波输出
sec2++;
else
{
sec2=0;
if(kk2<32)
kk2++;
else
kk2=0;
if(kk2<17) //三角波
three=8*kk2;
else
three=8*(32-kk2);
}
if(sec3<time3)//方波输出
sec3++;
else
{
sec3=0;
fangbo=!fangbo;//方波
}
TH0=0XFC;
TL0=0X18;
}