51单片机控制DAC0832产生正弦波,如何用定时器改变频率,求代码!

#include<reg51.h>unsignedinti;codeunsignedcharSin[128]={64,67,70,73,76,79,82,85,88,91... #include <reg51.h>
unsigned int i;
code unsigned char Sin[128]={
64,67,70,73,76,79,82,85,88,91,94,96,99,102,104,106,
109,111,113,115,117,118,120,121,123,124,125,126,126,
127,127,127,127,127,127,127,126,126,125,124,123,121,
120,118,117,115,113,111,109,106,104,102,99,96,94,91,
88,85,82,79,76,73,70,67,64,60,57,54,51,48,45,42,39,
36,33,31,28,25,23,21,18,16,14,12,10,9,7,6,4,3,2,1,
1,0,0,0,0,0,0,0,1,1,2,3,4,6,7,9,10,12,14,16,18,21,23,
25,28,31,33,36,39,42,45,48,51,54,57,60};
main()
{
while(1)
{
i++;
if(i==128)i=0;
P1=Sin[i];
}
}
展开
 我来答
cmingguo
推荐于2018-03-08 · TA获得超过119个赞
知道答主
回答量:123
采纳率:0%
帮助的人:120万
展开全部
可以用一个定时器中断,在中断服务程序里面,直接把对应的正弦采样数据置入P1口,通过修改定时器的中断间隔,达到调整输出频率的目的;
假设你的采样数据的原始采样率为1KHz, 被采样正弦波频率为100Hz,当Timer中断间隔为1ms时,DAC那里输出处理后输出即为100Hz, 如果中断间隔为500us,输出就是200Hz

static unsigned char byIndex=0;
中断函数里面包含如下两句

P1 = Sin[byIndex++];
byIndex &= 0x7f;
N_0_1
高粉答主

推荐于2016-01-19 · 关注我不会让你失望
知道大有可为答主
回答量:8628
采纳率:47%
帮助的人:846万
展开全部
DAC0832转换时间为1uS,一个正弦周期输出32个点的话,最高输出信号频率可达31.25kHz。
如果采用单片机产生,具体还与单片机的速度有关。采用RISC指令集的单片机,指令周期与晶振周期相同,采用16MHZ晶振的话,可以达到上述要求。
先确定每个周期输出的点数,假设是32个点。
DAC0832输出分辨率为8位,DAC0832输出以半电压上下对称的正弦波,峰值对应±128,那么,分别计算出32个点的正弦值,制作一个表格存储在单片机的程序空间或EEPROM空间。
根据信号频率设置定时器的溢出周期,定时器溢出周期为信号周期的1/32,定时器溢出时,依次输出32个点的正弦值至DAC0832的数字量输入端口。
最后,DAC0832的输出经运放电路转变为正负对称的正弦波,再经一个积分器或低通滤波器可输出平滑的正弦波。
#include<reg52.h>
unsigned char i;
unsigned char code sin_tab[] = //正弦波输出表
{
0x80,0x83,0x86,0x89,0x8D,0x90,0x93,0x96,0x99,0x9C,0x9F,0xA2,0xA5,0xA8,0xAB,0xAE,
0xB1,0xB4,0xB7,0xBA,0xBC,0xBF,0xC2,0xC5,0xC7,0xCA,0xCC,0xCF,0xD1,0xD4,0xD6,0xD8,
0xDA,0xDD,0xDF,0xE1,0xE3,0xE5,0xE7,0xE9,0xEA,0xEC,0xEE,0xEF,0xF1,0xF2,0xF4,0xF5,
0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFD,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFD,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6,
0xF5,0xF4,0xF2,0xF1,0xEF,0xEE,0xEC,0xEA,0xE9,0xE7,0xE5,0xE3,0xE1,0xDF,0xDD,0xDA,
0xD8,0xD6,0xD4,0xD1,0xCF,0xCC,0xCA,0xC7,0xC5,0xC2,0xBF,0xBC,0xBA,0xB7,0xB4,0xB1,
0xAE,0xAB,0xA8,0xA5,0xA2,0x9F,0x9C,0x99,0x96,0x93,0x90,0x8D,0x89,0x86,0x83,0x80,
0x80,0x7C,0x79,0x76,0x72,0x6F,0x6C,0x69,0x66,0x63,0x60,0x5D,0x5A,0x57,0x55,0x51,
0x4E,0x4C,0x48,0x45,0x43,0x40,0x3D,0x3A,0x38,0x35,0x33,0x30,0x2E,0x2B,0x29,0x27,
0x25,0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x15,0x13,0x11,0x10,0x0E,0x0D,0x0B,0x0A,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0A,0x0B,0x0D,0x0E,0x10,0x11,0x13,0x15,0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x25,
0x27,0x29,0x2B,0x2E,0x30,0x33,0x35,0x38,0x3A,0x3D,0x40,0x43,0x45,0x48,0x4C,0x4E,
0x51,0x55,0x57,0x5A,0x5D,0x60,0x63,0x66,0x69,0x6C,0x6F,0x72,0x76,0x79,0x7C,0x7E
};
sbit W=P2^0;
sbit CS=P2^1;
void delay(x)
{
int i,j;
for(j=x;j>0;j--)
for(i=1111;i>0;i--);
}
void main()
{
W=0;
CS=0;
while(1)
{
/*for(i=0;i<256;i++)
{
P1=sin_tab[i];
if(i==256)
i=0;
} */
P1=0xff;
while(1);
//delay(10);
//P1=0x00;
//delay(10);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式