2.输出三角波与正弦波程序.
#include<AT89X52.H>
unsigned char flag; //波型输出标置变量
bit time;
unsigned char sin(unsigned char x)
{
unsigned char code sin_tab[]={125,128,131,134,138,141,144,147,150,153,156,159,
162,165,168,171,174,177,180,182,185,188,191,193,196,198,201,203,
206,208,211,213,215,217,219,221,223,225,227,229,231,232,234,235,
237,238,239,241,242,243,244,245,246,246,247,248,248,249,249,250,
250,250,250,250,250,250,250,249,249,248,248,247,246,246,245,244,
243,242,241,239,238,237,235,234,232,231,229,227,225,223,221,219,
217,215,213,211,208,206,203,201,198,196,193,191,188,185,182,180,
177,174,171,168,165,162,159,156,153,150,147,144,141,138,134,131,
128,125,122,119,116,112,109,106,103,100,97,94,91,88,85,82,79,76,73,
70,68,65,62,59,57,54,52,49,47,44,42,39,37,35,33,31,29,27,25,27,29,27,25,23,21,
19,18,16,15,13,12,11,9,8,7,6,5,4,4,3,2,2,1,1,0,0,0,0,0,0,0,0,1,1,2,
2,3,4,4,5,6,7,8,9,11,12,13,15,16,18,19,21,23,25,27,29,31,33,35,37,
39,42,44,47,49,52,54,57,59,62,65,68,70,73,76,79,82,85,88,97,94,97,
100,103,106,109,112,116,119,122};
return sin_tab[x];
}
void DAC0832(unsigned char x)
{
P2=x;
}
void main()
{
unsigned char i;
TMOD=0X02; //定时器0用于控制输出波的频率
TH0=256-40;
ET0=1; //按键接于外部中断0,与中断1
IT0=1;
IT1=1;
EX0=1;
EX1=1;
EA=1;
TR0=1;
flag=0; //开始时无输出
i=0;
while(1)
{
if(time==1)
{
time="0";
if(i>249)
i="0";
else
i++;
switch(flag) //当按键1的为输出三角波,按键2时输出正弦波
{
case 0:DAC0832(0);break;
case 1:
if(i>125)
DAC0832(250-i);
else
DAC0832(i);
break;
case 2:
DAC0832(sin(i));
break;
default: break;
}
}
}
}
void time0() interrupt 1
{
time="1";
}
void int0() interrupt 0
{ //按键1接于外部中断0
flag="1";
}
void int1() interrupt 2 //按键2接于外部中断1
{
flag="2";
}