51单片机波形发生器程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
sbit wr=P3^6; //P3.6为DAC0832输入寄存器的写信号WR1,低电平有效sbit cs=P3^0; //P3.0为DAC0832片选信号CS,低电平有效
unsigned char k,t;
sbit led1=P3^1; //波形指示灯
sbit led2=P3^3;
sbit led3=P3^4;
sbit led4=P3^5;
//正弦波数据
unsigned char code Table[]={ 0x7F,0x8A,0x96,0xA1,0xAC,0xB6,0xC0,0xCA,
0xD3,0xDB,0xE3,0xE9,0xEF,0xF4,0xF8,0xFB,
0xFD,0xFE,0xFE,0xFD,0xFB,0xF8,0xF4,0xEF,
0xE9,0xE3,0xDB,0xD3,0xCA,0xC0,0xB6,0xAC,
0xA1,0x96,0x8A,0x7F,0x74,0x68,0x5D,0x52,
0x48,0x3E,0x34,0x2B,0x23,0x1B,0x15,0x0F,
0x0A,0x06,0x03,0x01,0x00,0x00,0x01,0x03,
0x06,0x0A,0x0F,0x15,0x1B,0x23,0x2B,0x34,
0x3E,0x48,0x52,0x5D,0x68,0x74};
void Delay(int Time_ms);
void Int0();
void main()
{
unsigned char i;
k=0;
EX0=1;
IT0=1;
EA=1;
cs=0;
while(1)
{
if(k==0) //锯齿波
{
led1=0;led2=1;led3=1;led4=1;
t++;
wr=1;
P0=t;
wr=0;
Delay(1);
}
else if(k==1) //方波
{
led1=1;led2=0;led3=1;led4=1;
wr=1;
P0=0xff;
wr=0;
Delay(60);
wr=1;
P0=0x00;
wr=0;
Delay(60);
}
else if(k==2) //正弦波
{
led1=1;led2=1;led3=0;led4=1;
wr=1;
P0=Table[t+20];
wr=0;
Delay(2);
t++;
if(t==69) t=0;
else if(t==0xff) i=1;
}
else if(k==3) //三角波
{
led1=1;led2=1;led3=1;led4=0;
for(i=0;i<255;i++)
{
wr=1;
P0=i;
wr=0;---------------------
Delay(1);
}
while(i--)
{
wr=1;
P0=i;
wr=0;
Delay(1);
}
}
}
}
void Delay(int Time_ms)
{
int i;
unsigned char j;
for(i=0;i { for(j=0;j<150;j++) { } } } void Int0() interrupt 1 { k++; t=0; if(k==4)k=0; return; }