51proteus仿真:生成正弦波
51proteus仿真:生成正弦波
仿真电路图和生成锯齿波的电路图一样。
程序用中断来定时。
C程序如下:
#include <reg51.h>
sbit CS0=P3^0;
sbit WR0=P3^1;
bit flag=0;
unsigned char i=0;
unsigned char code sin_num[]={
0,0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2,
2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 18, 18, 19, 20, 21,
22, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37,
38, 39, 40, 41, 42, 44, 45, 46, 47, 49, 50, 51, 52, 54, 55, 56,
57, 59, 60, 61, 63, 64, 66, 67, 68, 70, 71, 73, 74, 75, 77, 78,
80, 81, 83, 84, 86, 87, 89, 90, 92, 93, 95, 96, 98, 99, 101,102,
104, 106, 107, 109, 110, 112, 113, 115, 116, 118, 120, 121, 123, 124, 126, 128,
129, 131, 132, 134, 135, 137, 139, 140, 142, 143, 145, 146, 148, 149, 151, 153,
154, 156, 157, 159, 160, 162, 163, 165, 166, 168, 169, 171, 172, 174, 175, 177,
178, 180, 181, 182, 184, 185, 187, 188, 189, 191, 192, 194, 195, 196, 198, 199,
200, 201, 203, 204, 205, 206, 208, 209, 210, 211, 213, 214, 215, 216, 217, 218,
219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 230, 230, 231, 232, 233, 234,
235, 236, 237, 237, 238, 239, 240, 240, 241, 242, 243, 243, 244, 245, 245, 246,
246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 251, 252, 252, 253, 253,
253, 253, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255
}; //这是正弦波上升半周期的采样
void sinn() interrupt 1
{
if(flag==0)
{
P2=sin_num[i++];
if(i==0){flag=1;i=255;}
}
else
{
P2=sin_num[i--];
if(i==255){flag=0;i=0;}
}
}
main()
{
TMOD=0X01; //计数器T0,自启动,定时,方式1
TH0=0xff; //选择计数器T0
TL0=0x00;
IT0=1; //设置中断触发方式,下降沿
EA=1; //开中断
EX0=1; //外部中断INT0的,来扩展用可以按键调整波形
ET0=1; //外部中断INT0的,来扩展用可以按键调整波形
IP=0X01; //键盘中断级别高
TR0=1;
CS0=0;//dac0832为直通方式
WR0=0;
while(1)
{
}
}