微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于51单片机的波形发生器&DA转换(dac0832)

基于51单片机的波形发生器&DA转换(dac0832)

时间:11-17 来源:互联网 点击:
代码如下:

#include"reg51.h"
#define uchar unsigned char
sbit ksaw=P2^0; //锯齿波按键.
sbit ktran=P2^1; //三角波按键.
sbit ksquare=P2^2; //方波按键.
sbit ksin=P2^3; //正弦波按键.

uchar key1;
uchar keya;
uchar keyb;
uchar keyc;
uchar keyd;void delay( );
uchar code tab[128]={
64,67,70,73,76,79,82,85,88,91,94,96,99,102,104,106,
109,111,113,115,117,118,120,121,123,124,125,126,126,
127,127,127,127,127,127,127,126,126,125,124,123,121,
120,118,117,115,113,111,109,106,104,102,99,96,94,91,
88,85,82,79,76,73,70,67,64,60,57,54,51,48,45,42,39,
36,33,31,28,25,23,21,18,16,14,12,10,9,7,6,4,3,2,1,
1,0,0,0,0,0,0,0,1,1,2,3,4,6,7,9,10,12,14,16,18,21,23,
25,28,31,33,36,39,42,45,48,51,54,57,60};

void delay( )
{
uchar i;
for(i=0;i<255;i++);
}
void saw(void) //锯齿波.
{
uchar i;
while(1)
{
if(keya==1)
{
for(i=0;i<255;i++)
P0=i;
}
else break;
}
}
void tran(void) //三角波
{
uchar i;
while(1)
{
if(keyb==1)
{
for(i=0;i<255;i++)
P0=i;
for(i=255;i>0;i--)
P0=i;
}
else break;
}
}
void square(void) //方波
{
while(1)
{
if(keyc==1)
{
P0=0x00;
delay();
P0=0xff;
delay();
}
else break;
}

}

void sin( ) //正弦波
{
unsigned int i=0;
while(1)
{
if(keyd==1)
{
if(++i==128)i=0;
{P0=tab[i];}
}
else break;
}
}
void main(void) //主函数
{
EA=1;
EX0=1;
while(1)
{
if(key1)
{
switch(key1)
{
case 0x01:
//P0=0XC0;//
saw();
break;
case 0x02:
//P0=0XC0;//
tran();
break;
case 0x03:
//P0=0XBE;//
square();
break;
case 0x04:
//P0=0XB6;//
sin();
break;
}
}
}

}

void int0(void)interrupt0 using 1
{
if(ksaw==0)
{key1=0x01;keya=1;keyb=0;keyc=0;keyd=0;}
if(ktran==0)
{key1=0x02;keya=0;keyb=1;keyc=0;keyd=0;}
if(ksquare==0)
{ key1=0x03;keya=0;keyb=0;keyc=1;keyd=0;}
if(ksin==0)
{key1=0x04;keya=0;keyb=0;keyc=0;keyd=1;}
}

图片如下:

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top