Ⅰ 用单片机怎么实现三角波等输出
根据要求三角波的参数,比如周期,幅度等,转化为固定周期的幅值表,也就是时间幅度数组。
然后让单片机根据数组,定时输出DAC就可以了。
Ⅱ 怎么用单片机测量三角波幅值
如果单片机有ADC,多次采样测量取最大值,
如果单片机有DA,DA输出接比较器输入,三角波分压接比较器另一个输入,比较器输出接单片机IO,边沿中断触发,DA升值直到没有触发。取DA值算峰值。
如果都没有,方案2的DA可以用IO输出加RC电路代替,没触发时计算时间,推导电容电压,计算峰值
自己想的,不知道是不是最佳方案,但实现是没问题的
Ⅲ 51单片机设计一个三角波发生器
需要两个模块,首先用51加一个8位DA转换器可以作一个方波输出,单片机输出的8位代码可以控制DA转换器的输出幅度,最后在加个积分电路,即可输出三角波了,很简单的
Ⅳ 单片机三角波跟矩形波转换怎么编写
单片机三态档角波跟矩形波转换需要编写两个代码。#include
#defineucharunsignedchar
#defineuintunsignedint
unsignedcharx=0,m=0,y=128。
sbitWR_DA=P2^1。
sbitCS_DA=P2^0。//DA与单片机的接口
sbitS0=P3^0。//波形选择,每次按下将产生不同的波形
sbitS1=P3^1。//频率减
sbitS2=P3^2。//频率加
sbitS3=P3^3。//调节方波帆宴乱的占空比
voidsanjiao()//三角波{uchari。CS_DA=0。WR_DA=0。for(i=0。i<255。i++){P0=i。delay(x)。}while(i--){P0=i。delay(x)。}}voidtixing()//梯形波{uchari=255,j,k。CS_DA=0。WR_DA=0。while(i--){P0=i。delay(x)。}for(j=0。j<100。j++){P0=0。delay(x)。}for(k=0。祥兄k<255。k++。
Ⅳ 利用MCS51单片机与DAC0832组成一个简易三角波发生器。
#include<reg52.h> //包含头文件
#include<intrins.h>
#define uchar unsigned char //宏定义
#define uint unsigned int
uchar pinlv=100;
uint a,b;
uchar code sanjiao[64]={
0,8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128,136,144,152,160,168,176,184,192,200,208,216,224,232,240,248,
248,240,232,224,216,208,200,192,184,176,168,160,152,144,136,128,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0
}; //三角波取码
void initclock() //定时器初始化函数
{
TMOD=0x01; //定时器的工作方式
TH0=a;
TL0=b; //定时器赋初值
EA=1; //打开中断总开关
ET0=1; //打开定时器允许中断开关
TR0=1; //打开定时器定时开关
}
void main() //主函数
{
m=65536-(15000/pinlv); //定时器初值
a=m/256;
b=m%256;
initclock(); //定时器初始化
while(1)
{
P1=sanjiao[u]; //三角波
}
}
void T0_time()interrupt 1 //定时器
{
TH0=a;
TL0=b;
u++; //变量加
if(u>=64) //一个周期采样64个点, 所以加到64就清零
u=0; //u清零
//根据不同的初值,定时器定时时间不同,达到不同频率的目的
}
Ⅵ 51单片机与DAC0832连接输出三角波
给你个参考
建三角波的斜线函数,设三角波的幅值=2V,采样10个点(红色斜线段),那么 K=2/10;
如此即可计算出Uo数列:0.2、0.4、0.6、。。。1.8、2.0,按顺序送到0832,下降线段就是反过来送。送数的快慢,反映在输出波形上的频率,采样点的多少,就决定了输出波形的相似度,也叫失真度;
正弦波的计算同理。
这里是交流平台,有问有答。你的问题没有唯一答案,也没有标准答案,我的回答算是一个思路,你有不同的想法可以提出来,才知道你想要什么;
你提出新要求并加分,可以,一声不吭直接加分的形式就不是个好方法
Ⅶ 单片机中用C怎样写产生三角波的程序请解释一下,谢谢
要产生怎样的三角波,如是简单的,Y=X即可,只是单片机要有D/A转换,或在外围电路进行转换。
Ⅷ 如何运用单片机原理制作智能信号发生器,要求产生方波、矩形波、三角波、锯齿波和正弦波。
#include<reg51.h>
#include<absacc.h>
#include<MAX72191.h>
#defineDAC XBYTE[0x7fff] //P2.7接CS
sbitkey0 = P3^2;// 增减切换键
sbitkey1 = P3^3;//个位,十位,百位,千位的控制切换
sbitkey2 = P3^4;// 调整位
sbitkey3 = P3^5;// 波形选择正弦、三角、矩形波,锯齿波
unsignedchar i,j;
unsignedint counter,step,flag;
typedefunsigned int uint;
//定时器0初始化
voidInit_Timer0(void)
{
TMOD = (TMOD & 0XF0) | 0X01;//设置工作方式和定时初始值
TH0 = 0xff;
TL0 = 0x00;
TR0 =1; //启动定时器
ET0 =1;
}
//定义输出波形的代码
unsignedchar code type[4][256]={
{ //正弦波代码
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x2, 0x3, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
0x9, 0xb,0xc, 0xd, 0xf, 0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x39,0x3b,0x3e,0x41,0x43,0x46,0x49,0x4c,
0x4f,0x52,0x55,0x58,0x5b,0x5e,0x61,0x64,0x67,0x6a,0x6d,0x70,0x73,0x76,0x7a,0x7d,
0x80,0x83,0x86,0x89,0x8c,0x8f,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb1,0xb4,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xeb,0xed,0xef,0xf0,0xf1,0xf3,0xf4,
0xf5,0xf6,0xf8,0xf9,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,
0xfe,0xfe,0xfe,0xfe,0xfe,0xfd,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf9,0xf8,0xf6,0xf5,
0xf4,0xf3,0xf1,0xf0,0xef,0xed,0xeb,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,0xda,
0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb4,0xb1,
0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99,0x96,0x93,0x8f,0x8c,0x89,0x86,0x83,0x80,
0x7d,0x7a,0x76,0x73,0x70,0x6d,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8, 0xe6, 0xe4,0xe2, 0xe0,
0xde,0xdc, 0xda, 0xd8, 0xd6, 0xd4, 0xd2, 0xd0,0xce, 0xcc, 0xca, 0xc8, 0xc6, 0xc4,0xc2, 0xc0,
0xbe,0xbc, 0xba, 0xb8, 0xb6, 0xb4, 0xb2, 0xb0,0xae, 0xac, 0xaa, 0xa8, 0xa6, 0xa4,0xa2, 0xa0,
0x9e, 0x9c, 0x9a, 0x98, 0x96, 0x94, 0x92, 0x90,0x8e, 0x8c, 0x8a, 0x88, 0x86, 0x84, 0x82, 0x80,
0x7e, 0x7c, 0x7a, 0x78, 0x76, 0x74, 0x72, 0x70,0x6e, 0x6c, 0x6a, 0x68, 0x66, 0x64, 0x62, 0x60,
0x5e, 0x5c, 0x5a, 0x58, 0x56, 0x54, 0x52, 0x50,0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x42, 0x40,
0x3e, 0x3c, 0x3a, 0x38, 0x36, 0x34, 0x32, 0x30,0x2e, 0x2c, 0x2a, 0x28, 0x26, 0x24, 0x22, 0x20,
0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x14, 0x12, 0x10,0xe, 0xc,0xa, 0x8, 0x6,0x4, 0x2, 0x00
},
{// 矩形脉冲波代码
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
},
{//锯齿波代码
0x00,0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,0x08,0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10,0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,0x18,0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f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xdf,
0xe0,0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,0xe8,0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0,0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,0xf8,0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}
};
//显示子函数
Disp7219(unsignedlong dat)
{
unsigned char i;
unsigned char led[8];
led[7]=dat%10;
led[6]=dat/10%10;
led[5]=dat/100%10;
led[4]=dat/1000%10;
led[3]=dat/10000%10;
led[2]=dat/100000%10;
led[1]=dat/1000000%10;
led[0]=dat/10000000%10;
for(i=0;i<8;i++)
{
max_7219(i+1, led[i]);
}
}
//延时约1m秒
voiddelay_ms(uint n)
{
uchar j;
while(n--)
for(j=0;j<120;j++);
}
//主函数
main()
{
unsigned int f,n,j;
delay_ms(500);
Init_Max7219();//初始化7219
Disp7219(000);
Init_Timer0();
step=18;
EA = 1;
while(1)
{
if(key0 == 0) n=n+1;
if(n==2)n=0;
if(key1==0) j=j+1;
if(j==4) j=0;
if(n==0 && j == 0 &&key2 == 0) if(step<180) step+=18; //个位增
if(n==1 && j == 0 &&key2 == 0) if(step>18) step-=18; //个位减
if(n==0 && j == 1 &&key2 == 0) if(step<1800) step+=180;//十位增
if(n==1 && j == 1 &&key2 == 0) if(step>180) step-=180; //十位减
if(n==0 && j == 2 &&key2 == 0) if(step<18000) step+=1800;//百位增
if(n==1 && j == 2 &&key2 == 0) if(step>1800) step-=1800;//百位减
if(n==0 && j == 3 &&key2 == 0) if(step<54000) step+=18000;//千位增
if(n==1 && j == 3 &&key2 == 0) if(step>18000) step-=18000;//千位减
if(key3==0)flag=flag+1;if(flag==4)flag=0;
while((!key0)||(!key1)||(!key2)||(!key3));
f=step/18;
Disp7219(f);}//显示频率
}
// 定时中断服务
voidTimer0(void) interrupt 1 using 2
{
TH0 = 0xff;
TL0 = 0x00;
counter = counter + step;
DAC=type[flag][(unsignedint)counter>>8];
}
Ⅸ 单片机输出三角波c程序
如果是普通的51单片机是做不到的,要想得到三角波通常都是在内部用程序周期的产生三角波形的数据,把数据送入DA中产生的。
Ⅹ 帮忙解决下:使用51单片机,产生方波、锯齿波、三角波等波形,频率可调。并且能实现在protues中的仿真
基本知识点:
1、定时器
2、PWM
方波、三角波的实现方法:
1、PWM固定占空比1:1,实现对称方波输出
2、方波后连接积分电路,可将方波变为同频率的三角波。
实现以上功能较容易,且可实现的频率范围较宽。
锯齿波实现方法:
锯齿波实现方法稍显麻烦,不过硬件电路原理与三角波类似。
区别在于锯齿波在一个周期内输出若干个占空比不同的方波,每个方波的占空比除了最后一个点之外,呈线性递增。这样出来的信号还不是锯齿波,需要经过积分电路才能转变为锯齿波。由于一个周期需要输出多个方波(个数越多,精度越高),因此,锯齿波的频率不能太高。
锯齿波个人建议采用AD或专用的信号发生器电路。