单片机c语言函数发生器,单片机原理及应用c语言编程

单片机C语言版信号发生器

单片机信号发生器,一般是通过控制单片机的IO的高低电平来实现的,具体为

为崇阳等地区用户提供了全套网页设计制作服务,及崇阳网站建设行业解决方案。主营业务为成都网站建设、网站制作、崇阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

1,可以通过先配置一个管脚为输出,然后给这个管脚写0,输出就为低电平,然后写1,这个管脚就输出为高电平。如果你的单片机是3.3v供电的话,那么高电平一般为3.3V,低电平一般为0v。

2,如果要产生一个方波的话,需要先输出1,然后延迟一段时间,然后再输出0,然后再延迟一段时间,然后输出1.... 要如此反复的话,可以用while(1), 然后在while(1)里面写入你的置高,延迟,置低的代码,具体伪代码可以为

配置对应输出管脚为输出; //因为管脚可以配置为输出和输入,所以需要先配置为输出

while(1)

{

对对应输出管脚置高;

sleep(半个周期的时间);

对对应输出管脚置低;

sleep(半个周期的时间);

}

3, 如果你牵涉到高级应用的话,比如要输出一个正弦波,那么需要外加信号产生芯片,比如DDS芯片。

4,如果还需要更加复杂的信号,任意波形的,可以用一个片内DA按照你的意思输出对应的电平,或者片外DA来输出。

求一个正确的基于51单片机的函数信号发生器的C程序

#includereg51.h

#includeabsacc.h

#includeMAX72191.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]={

{ //正弦波代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,0x33,0x31,0x2e,0x2c,0x2a,0x27,0x25,

0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0xf,0xd, 0xc, 0xb, 0x9,

0x8,0x7, 0x6, 0x5, 0x4, 0x3, 0x3, 0x2, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

},

{ //三角波代码

0x2,0x4, 0x6, 0x8, 0xa, 0xc, 0xe, 0x10,0x12,0x14, 0x16, 0x18, 0x1a,0x1c, 0x1e, 0x20,

0x22,0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30,0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x40,

0x42,0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50,0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60,

0x62,0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70,0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, 0x80,

0x82,0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90,0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, 0xa0,

0xa2,0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0,0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc,0xbe, 0xc0,

0xc2,0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0,0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc,0xde, 0xe0,

0xe2,0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0,0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc,0xfe, 0xff,

0xfe,0xfc, 0xfa, 0xf8, 0xf6, 0xf4, 0xf2, 0xf0,0xee, 0xec, 0xea, 0xe8, 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,

0x20,0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,0x28,0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x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, 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;i8;i++)

{

max_7219(i+1, led[i]);

}

}

//延时约1m秒

voiddelay_ms(uint n)

{

uchar j;

while(n--)

for(j=0;j120;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(step180) step+=18; //个位增

if(n==1 j == 0 key2 == 0) if(step18) step-=18; //个位减

if(n==0 j == 1 key2 == 0) if(step1800) step+=180;//十位增

if(n==1 j == 1 key2 == 0) if(step180) step-=180; //十位减

if(n==0 j == 2 key2 == 0) if(step18000) step+=1800;//百位增

if(n==1 j == 2 key2 == 0) if(step1800) step-=1800;//百位减

if(n==0 j == 3 key2 == 0) if(step54000) step+=18000;//千位增

if(n==1 j == 3 key2 == 0) if(step18000) 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)counter8];

}

单片机C语言版正弦波信号发生器怎么做?

#includeat89x51.h

//unsigned char TIME0_H=0xec,TIME0_L=0x78; //定时器0的初值设置;全局变量

#includesinx.h

#include0832.h

void main()

{

TMOD=0X01;

TH0=0xff;

TL0=0xd9;

IT0=1; //设置中断触发方式,下降沿

EA=1;

EX0=1;

ET0=1;

IP=0X01; //键盘中断级别高

TR0=1;

while(1)

{

// square();

;

}

}

#ifndef __0832_h__

#define __0832_h__

//#define INPUT XBYTE[0xbfff] //即cs 与xfer 轮流低电平。

//#define DACR XBYTE[0x7fff] //单通道输出,单缓冲就行了。

unsigned char i,sqar_num=128; //最大值100,默认值50

unsigned char cho=0; //0:正弦波。1:方波。2:三角波。3:锯齿波。

unsigned char num=0;

unsigned char TIME0_H=0xff,TIME0_L=0xd9; //定时器0的初值设置;全局变量.对应正弦波,锯齿波50HZ

sbit chg= P1^0; //三角波100Hz.

sbit freq_u=P1^1;

sbit freq_d=P1^2;

sbit duty_u=P1^3;

sbit duty_d=P1^4;

sbit cs =P3^7;

bit flag=0;

unsigned int FREQ=50;//初始化频率,50HZ

//调节部分——频率

void freq_ud(void)

{

unsigned int temp;

if(freq_d==0)

{ FREQ--; }

else if(freq_u==0)

{ FREQ++; }

if(cho==1|cho==3) //锯齿波256次中断一周期,特殊处理下。否则他的频率是100(+\-)n*2Hz.

{

temp=0xffff-3906/FREQ; //方波,三角波默认为100hz,切换后频率也为50HZ 65336-10^6/(256*FREQ)

TIME0_H=temp/256;

TIME0_L=temp%256;

}

else if(cho==0|cho==3){ //正弦波 三角波默认周期50hz 65536-10^6//(512*FREQ)

temp=0xffff-1953/FREQ;

TIME0_H=temp/256;

TIME0_L=temp%256;

}

}

//调节部分——方波的占空比

void duty_ud(void) //方波也采用512次中断构成一个周期。

{

if(duty_d==0sqar_num0)

sqar_num--;

else if(duty_u==0sqar_num255)

sqar_num++;

}

//波形发生函数

void sint(void)

{

if(!flag)

{

cs=0;P2=sin_num[num++];cs=1;

if(num==0){num=255;flag=1;}

}

else if(flag)

{

cs=0;P2=sin_num[num--];cs=1;

if(num==255){num=0;flag=0;}

}

}

void square(void)

{

if(i++sqar_num) {cs=0;P2=0XFF;cs=1;}

else{cs=0;P2=0X00;cs=1;}

}

void triangle(void)

{

cs=0;P2=num++;cs=1;

}

void stw(void)

{

if(~flag)

{

cs=0;P2=num++;cs=1;

if(num==0){num=255;flag=1;}

}

else if(flag)

{

cs=0;P2=num--;cs=1;

if(num==255){num=1;flag=0;}

}

}

//按键中断处理程序。

void it0() interrupt 0

{

if(chg==0) { if(++cho==4) {cho=0;num=0;} } //num=0;所有数据从新开始,保证波形的完整性

else if(freq_u==0|freq_d==0)

{freq_ud();}

else if (cho==1(duty_d==0|duty_u==0))

{duty_ud();}

else ;

}

//定时器中断处理程序。

void intt0() interrupt 1

{

//TH0=0x00;TL0=0x00;sinx();

switch(cho)

{

case 0:{TH0=TIME0_H;TL0=TIME0_L;sint() ;break;} //正弦波//每半周期256取样。

case 1:{TH0=TIME0_H;TL0=TIME0_L;square(); break;} //方波 //为了提高方波的最高频率,只有牺牲占空比的最小可调值。分100份 每次1%。

case 2:{TH0=TIME0_H;TL0=TIME0_L;triangle();break;} //三角波

case 3:{TH0=TIME0_H;TL0=TIME0_L;stw(); break;} //锯齿波

default: ;

}

}

#endif

//正弦表;每半个周期256个取值,最大限度保证波形不失真。

//各个值通过MATLAB算出,并四设五如取整。具体程序如下

#ifndef __sinx_h__

#define __sinx_h__

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

};

#endif

//MATLAB程序:

//x=linspace(-pi/2,pi/2,255);%如果过采用1位采用,很多值是重的。虽然实际中并不会。

//y=(sin(x)+1)/2.0*255;

//%uint32(y)%强制类型转换。

//%fprintf('%.f\n',uint32(y));%控制输出类型

//round(y)%四舍五入函数

跪求单片机大神帮我解析下面函数信号发生器的代码(C语言)

你贴的代码里那几个数组没有用到啊,暂时不知道干什么的,初步猜测估计是液晶的点阵数据,main函数里也就是初始化了定时器和lcd液晶屏,不停的扫描按键键值,定时中断里应该还有很多东西,这里看不到。

需要问具体的话可以把程序发我邮箱吧:cxz1088cxz@vip.qq.com


本文名称:单片机c语言函数发生器,单片机原理及应用c语言编程
转载来源:http://azwzsj.com/article/dsgpcde.html