标题:
数模转换,基于单片机的正弦波产生
[打印本页]
作者:
阿五五
时间:
2018-8-30 19:55
标题:
数模转换,基于单片机的正弦波产生
软件编程使
DAC0832转换模块循环输出锯齿波
软件编程使
DAC0832转换模块循环输出正弦波。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit WR1=P3^0;
sbit WR2=P3^1;
sbit choose=P1^0; //锯齿波,正弦波选择
uchar x=0x00;
uchar y=0;
uchar code sin_table[]={0x80,0x83,0x85,0x88,0x8a,0x8d,0x8f,0x92,0x94,0x97,
0x99,0x9b,0x9e,0xa0,0xa3,0xa5,0xa7,0xaa,0xac,0xae,
0xb1,0xb3,0xb5,0xb7,0xb9,0xbb,0xbd,0xbf,0xc1,0xc3,
0xc5,0xc7,0xc9,0xcb,0xcc,0xce,0xd0,0xd1,0xd3,0xd4,
0xd6,0xd7,0xd8,0xda,0xdb,0xdc,0xdd,0xde,0xdf,0xe0,
0xe1,0xe2,0xe3,0xe3,0xe4,0xe4,0xe5,0xe5,0xe6,0xe6,
0xe7,0xe7,0xe7,0xe7, //输出电压从0到最大值,正弦波1/4部分
0xe7,0xe7,0xe7,0xe7,0xe6,0xe6,0xe5,0xe5,0xe4,0xe4,
0xe3,0xe3,0xe2,0xe1,0xe0,0xdf,0xde,0xdd,0xdc,0xdb,
0xda,0xd8,0xd7,0xd6,0xd4,0xd3,0xd1,0xd0,0xce,0xcc,
0xcb,0xc9,0xc7,0xc5,0xc3,0xc1,0xbf,0xbd,0xbb,0xb9,
0xb7,0xb5,0xb3,0xb1,0xae,0xac,0xaa,0xa7,0xa5,0xa3,
0xa0,0x9e,0x9b,0x99,0x97,0x94,0x92,0x8f,0x8d,0x8a,
0x88,0x85,0x83,0x80, //输出电压从最大值到0,正弦波1/2部分
0x7d,0x7b,0x78,0x76,0x73,0x71,0x6e,0x6c,0x69,0x67,
0x65,0x62,0x60,0x5d,0x5b,0x59,0x56,0x54,0x52,0x4f,
0x4d,0x4b,0x49,0x47,0x45,0x43,0x41,0x3f,0x3d,0x3b,
0x39,0x37,0x35,0x34,0x32,0x30,0x2f,0x2d,0x2c,0x2a,
0x29,0x28,0x26,0x25,0x24,0x23,0x22,0x21,0x20,0x1f,
0x1e,0x1d,0x1d,0x1c,0x1c,0x1b,0x1b,0x1a,0x1a,0x1a,
0x19,0x19,0x19,0x19, //输出电压从0到最小值,正弦波3/4部分
0x19,0x19,0x19,0x19,0x1a,0x1a,0x1a,0x1b,0x1b,0x1c,
0x1c,0x1d,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,
0x25,0x26,0x28,0x29,0x2a,0x2c,0x2d,0x2f,0x30,0x32,
0x34,0x35,0x37,0x39,0x3b,0x3d,0x3f,0x41,0x43,0x45,
0x47,0x49,0x4b,0x4d,0x4f,0x52,0x54,0x56,0x59,0x5b,
0x5d,0x60,0x62,0x65,0x67,0x69,0x6c,0x6e,0x71,0x73,
0x76,0x78,0x7b,0x7d}; //输出电压从最小值到0,完整正弦波
void delay(uchar z)
{
uchar i,j;
for(i=0;i<=z;i++);
for(j=0;j<=10;j++);
} //延时程序
void init()
{
WR1=0;
WR2=0;
} //DA芯片启用位操作
void timer0(void) interrupt 1 using 3
{
if(choose==1) //产生锯齿波
{
delay(50);
P0=x;
delay(20);
x=x+1;
if(x>255)
x=0;
}
if(choose==0) //产生正弦波
{
delay(50);
if(y<=255)
{
P0=sin_table[y];
delay(5);
y++;
}
if(y==256)
y=0;
}
}
void main()
{
init();
TMOD=0x02; // 定时器T0,方式二
TL0=0XE2; //30us发送一个数据
TH0=0XE2;
ET0=1;
TR0=1;
EA=1;
}
复制代码
DA.doc
2018-8-30 19:54 上传
点击文件名下载附件
下载积分: 黑币 -5
18 KB, 下载次数: 5, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1