采用帶內(nèi)部程序存儲器的MCS51單片機(jī),采用10位精度D/A(TLC5615),采用運算放大器LM358調(diào)節(jié)輸出信號幅度。使輸出最大幅度為0.1V--3V可調(diào)。輸出的函數(shù)信號為正弦波,方波,鋸齒波,頻率為0.1Hz--100Hz。用數(shù)碼管顯示輸出信號類型和頻率,擴(kuò)展按鍵用于輸出功能選擇。
說明:頻率越高,誤差越大,因為51單片機(jī)速度太慢了
nclude"TLC5615.h"
#include "74hc595.h"
#define uchar unsigned char
#define uint unsigned int
sbit smg1=P2^0;//數(shù)碼管引腳
sbit smg2=P2^1;
sbit smg3=P2^2;
sbit smg4=P2^3;
sbit smg5=P2^4;
sbit smg6=P2^5;
sbit smg7=P2^6;
sbit smg8=P2^7;
uchar code smgduan0[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//顯示0~9,無小數(shù)點
uchar code smgduan1[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//顯示0~9,有小數(shù)點
uchar boxing=0;//波形。正弦、鋸齒、方波
uint step=0;//步進(jìn)數(shù)
uchar bu=0;
uchar sec=0,flag=0;//顯示計時
uint freq=1000; //頻率/10
uint amp=30;//振幅
uint time=0;//計時
uchar code zhengx[256]={//正弦編碼
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,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,0,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
};
uchar key=0;
uchar key_scan()//按鍵檢測
{
uchar i,j;
i=0;
j=0;
P1=0x0f;
if(P1!=0x0f) //檢測有無按下
{
switch(P1)//檢測行
{
case 0x0e:i=1;break;
case 0x0d:i=5;break;
case 0x0b:i=9;break;
case 0x07:i=13;
}
P1=0xf0;
switch(P1)//檢測列
{
case 0xe0:j=0;break;
case 0xd0:j=1;break;
case 0xb0:j=2;break;
case 0x70:j=3;
}
}
if(key!=i+j)
{
key=i+j;
return key;
}
else
return 0;
}
//主函數(shù)
void main()
{
uchar i;
Hc595SendByte(~amp);
TMOD=0X02; //設(shè)置計數(shù)器工作方式2
//設(shè)置定時器
TH0=217;//給定時器賦初值
TL0=217;
ET0=1;//打開定時器0中斷允許
TR0=1;//打開定時器
EA=1;//打開總中斷
while(1)
{
i=key_scan();
if(i==1)//正弦波
{
boxing=0;
}
if(i==2)//方波
{
boxing=1;
}
if(i==3)//鋸齒波
{
boxing=2;
}
if(i==5)//頻率加
{
if(freq<1000)
freq++;
step=1000/freq-1;//計算步進(jìn)數(shù)
}
if(i==6)//頻率減
{
if(freq>1)
freq--;
step=1000/freq-1;//計算步進(jìn)數(shù)
}
if(i==9)//頻率加10
{
if(freq<1000)
freq+=10;
step=1000/freq-1;//計算步進(jìn)數(shù)
}
if(i==10)//頻率減10
{
if(freq>10)
freq-=10;
step=1000/freq-1;//計算步進(jìn)數(shù)
}
if(i==13)//頻率加100
{
if(freq<1000)
freq+=100;
step=1000/freq-1;//計算步進(jìn)數(shù)
}
if(i==14)//頻率減100
{
if(freq>100)
資料借鑒于此紛傳
閱讀全文