51proteus仿真:生成正弦波
仿真電路圖和生成鋸齒波的電路圖一樣。
程序用中斷來(lái)定時(shí)。
C程序如下:
#include
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; //計(jì)數(shù)器T0,自啟動(dòng),定時(shí),方式1
TH0=0xff; //選擇計(jì)數(shù)器T0
TL0=0x00;
IT0=1; //設(shè)置中斷觸發(fā)方式,下降沿
EA=1; //開(kāi)中斷
EX0=1;//外部中斷INT0的,來(lái)擴(kuò)展用可以按鍵調(diào)整波形
ET0=1; //外部中斷INT0的,來(lái)擴(kuò)展用可以按鍵調(diào)整波形
IP=0X01; //鍵盤(pán)中斷級(jí)別高
TR0=1;
CS0=0;//dac0832為直通方式
WR0=0;
while(1)
{
}
}