//file:dac0832
//锯齿,三角,正弦,方波的输出
#include"reg51.h"
#define uchar unsigned char
sbit cs=P1^0;
sbit wr=P3^6;
unsigned int i;
uchar k;
uchar discount=0;//外部中断计数
uchar code tab[]={128, 130, 132, 135, 137, 139, 141, 144, 146, 148,150, 152, 155, //正弦函数点数组
157, 159, 161, 163, 165, 168, 170,172, 174,
176, 178, 180, 182, 184, 186, 188, 190,
192, 194, 196, 198, 200, 201, 203, 205, 207, 209,
210, 212, 214, 215, 217, 219, 220, 222, 223, 225, 226,
227, 229, 230, 232, 233, 234, 235, 237, 238,
239, 240, 241, 242, 243, 244, 245, 246, 247, 247 ,
248, 249, 250, 250, 251, 252, 252, 253, 253, 254,
254, 254, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 254,
254, 254, 253, 253, 252, 252, 251, 250, 250, 249,
248, 247, 247, 246, 245, 244, 243, 242, 241, 240,
239, 238, 237, 235, 234, 233, 232, 230, 229, 227,
226, 225, 223, 222, 220, 219, 217, 215, 214, 212 ,
210, 209, 207, 205, 203, 201, 200, 198, 196, 194,
192, 190, 188, 186, 184, 182, 180, 178, 176, 174,
172, 170, 168, 165, 163, 161, 159, 157, 155, 152 ,
150, 148, 146, 144, 141, 139, 137, 135, 132, 130 ,
128, 126, 124, 121, 119, 117, 115, 112, 110, 108,
106, 104, 101, 99, 97, 95, 93, 91, 88, 86,
84, 82, 80, 78, 76, 74, 72, 70, 68, 66 ,
64, 62, 60, 58, 56, 55, 53, 51, 49, 47 ,
46, 44, 42, 41, 39, 37, 36, 34, 33, 31,
30, 29, 27, 26, 24, 23, 22, 21, 19, 18,
17, 16, 15, 14, 13, 12, 11, 10, 9, 9,
8, 7, 6, 6, 5, 4, 4, 3, 3, 2,
2, 2, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 2,
2, 2, 3, 3, 4, 4, 5, 6, 6, 7,
8, 9, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 21, 22, 23, 24, 26, 27, 29,
30, 31, 33, 34, 36, 37, 39, 41, 42, 44,
46, 47, 49, 51, 53, 55, 56, 58, 60, 62,
64, 66, 68, 70, 72, 74, 76, 78, 80, 82,
84, 86, 88, 91, 93, 95, 97, 99, 101, 104,
106, 108, 110, 112, 115, 117, 119, 121, 124, 126,
128};
//延时函数,可调整频率
void delay()
{
uchar b,c;
for(c=1;c>0;c--)
for(b=1;b>0;b--);
}
//锯齿波
void juchi()
{
for(i=0;i<256;i++)
{
P0=i;
delay();
}
}
//三角波
void sanjiaobo()
{
for(i=0;i<255;i++)
{P0=i;
delay();}
for(i=254;i>0;i--)
{P0=i;
delay();}
}
//方波
void plus()
{
for(i=0;i<500;i++)
{P0=255;
delay();
}
for(i=0;i<500;i++)
{P0=0;
delay();}
}
//正弦波
void sin()
{
for(i=0;i<360;i++)
{P0=tab[i];
delay();}
}
void ExtInt0() interrupt 0//使用外部中断0
{
discount++;
if(discount==4)
{discount=0;
}
}
void waveout() //波形选择输出
{
switch (discount)
{
case 0:juchi();break;
case 1:sanjiaobo();break;
case 2:sin();break;
case 3:plus();break;
}
}
//主函数
main()
{TCON=0X01; //下降沿有效
IE=0X81;
cs=0;wr=0;
while(1)
{
waveout();
}
}
你参考一下,很久没看过程序了····不好意思 |