1.实现功能(设计概述):
1.2个通道同时输出正弦波、三角波、方波或锯齿波。
2.使用点阵屏显示波形,通过按钮切换各通道波形。
3.输出波形频率固定为7.5HZ。
2.仿真全图
3.元器件清单
型号 | 型号*数量 |
---|---|
单片机 | AT89C51*1 |
电容 | 10uf*1 |
电容 | 30pf*2 |
晶振 | 12MHZ*1 |
电阻 | 10K |
按钮 | *4 |
数模芯片 | DAC0832*2 |
集成运放 | LM358 |
串转并芯片 | 74LS595*2 |
点阵 | 884 |
非门芯片 | 74LS595*2 |
译码器 | 74C154 |
4.软件流程图
5.仿真运行图
6.相关代码
#include<reg51.h>
#include"absacc.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define out1 XBYTE[0xfeff]
#define out2 XBYTE[0xfdff]
#define uchar unsigned char
#define uint unsigned int
sbit SRCLK=P3^3;//595引脚
sbit RCLK=P3^4;
sbit SER=P3^5;
sbit k1=P3^0;//按钮
sbit k2=P3^1;
sbit k3=P3^2;
uchar boxing1=0,boxing2=0;//波形。正弦、三角、方波
uchar time=0;//计时
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};
uchar tongdao=0;//通道
uchar str1[8]={//正弦
0x30,0x08,0x0C,0x0C,0x18,0x30,0x30,0x18
};
uchar str2[8]={//三角波
0x20,0x10,0x08,0x04,0x04,0x08,0x10,0x20
};
uchar str3[8]={//方波
0x3C,0x04,0x04,0x3C,0x20,0x20,0x3C,0x00
};
uchar str4[8]={//Hz
0x7E,0x08,0x08,0x7E,0x48,0x68,0x58,0x48
};
uchar str5[8]={//7.5
0x62,0x12,0x0E,0x40,0x0E,0x4A,0x7A,0x00
};
uchar str6[]={0x3C,0x08,0x10,0x3C,0x08,0x10,0x20,0x00};//锯齿波
void delay(uint xms)//延时函数
{
while(xms--);
}
//595发送函数
void Hc595SendByte(uchar i)
{
uchar a,dat1;
dat1=i;
SRCLK = 1;
RCLK = 1;
for(a=0;a<8;a++) //发送8位数
{
if(dat1 & 0x80)
SER =1; //从最高位开始发送
else
SER=0;
dat1 <<= 1;
SRCLK = 0; //发送时序
_nop_();
_nop_();
SRCLK = 1;
}
/*RCLK = 0;
_nop_();
_nop_();
RCLK = 1;*/
}
void lock()//锁定
{
RCLK = 0;
_nop_();
_nop_();
RCLK = 1;
}
void display()//显示
{
uchar i;
for(i=0;i<8;i++)//通道1
{
Hc595SendByte(0xff);
Hc595SendByte(0xff);
lock();
P1=i;
Hc595SendByte(~str5[i]);//频率
switch(boxing1)
{
case 0: Hc595SendByte(~str1[i]);break;//正弦
case 1: Hc595SendByte(~str2[i]);break;//三角波
case 2: Hc595SendByte(~str3[i]);break;//方波
case 3: Hc595SendByte(~str6[i]);//锯齿波
}
lock();
delay(100);
}
}
void main()
{
//设置定时器
TMOD|=0X01;
TH0=0xfc;//给定时器赋初值
TL0=0x18;
ET0=1;//打开定时器0中断允许
TR0=1;//打开定时器
EA=1;//打开总中断
while(1)
{
display();
if(!k1)//通道1
tongdao=0;
while(!k3);
}
}
}
void Timer0() interrupt 1//定时器中断
{
//通道1输出
if(boxing1==0)//正弦波
{
out1=zhengx[time];
time++;
}
if(boxing1==1)//三角波
{
if(time<128)
{
out1=time;
}
else
{
out1=255-time;
}
time++;
}
if(boxing1==2)//方波
{
if(time<128)
out1=0;
else
{
out1=0xff;
}
time++;
}
if(boxing1==3)//锯齿波
{
out1=time;
time++;
}
//通道2输出
if(boxing2==0)//正弦波
{
out2=zhengx[time];
time++;
}
if(boxing2==1)//三角波
{
if(time<128)
{
out2=time;
}
else
{
out2=255-time;
}
time++;
}
if(boxing2==2)//方波
{
if(time<128)
out2=0;
else
{
out2=0xff;
}
time++;
}
if(boxing2==3)//锯齿波
{
out2=time;
time++;
}
TH0=0xfc;//给定时器赋初值
TL0=0x18;
}