资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

dac矩形波函数c语言 矩形波的时域表达式

at89c52产生三角波,锯齿波,方波,要求用c语言写,芯片是D0832

#includereg52.h

公司主营业务:网站建设、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出侯马免费做网站回馈大家。

#define uchar unsigned char

#define uint unsigned int

//#define Fosc 24000000/12000000 //12分频后的频率

#define DAdata P0//DA数据端口

sbit DA_S1= P2^0; // 控制DAC0832的8位输入寄存器,仅当都为0时,可以输出数据(处于直通状态),否则,输出将被锁存

sbit DA_S2= P2^1; // 控制DAC0832的8位DAC寄存器,仅当都为0时,可以输出数据(处于直通状态),否则,输出将被锁存

sbit key= P3^2;

uchar wavecount; //'抽点'计数

uchar THtemp,TLtemp;//传递频率的中间变量

//uint T_temp;

uchar judge=1; //在方波输出函数中用于简单判别作用

uchar waveform; //当其为0、1、2时,分别代表三种波

uchar code freq_unit[4]={10,50,200,10}; //三种波的频率单位 sawtooth

uchar idata wavefreq[4]={1,1,1,1}; //给每种波定义一个数组单元,用于存放单位频率的个数

uchar code lcd_hang1[]={"Sine Wave " "Triangle Wave " "Square Wave " "sawtooth Wave ""Select Wave: " "press No.1 key! "};

uchar idata lcd_hang2[16]={"f= Hz "};

/*uchar code wave_freq_adjust[]={ //频率调整中间值

0xff,0xb8,0x76,0x56,0x43,0x37,0x2e,0x26,0x20,0x1c, //正弦波频率调整中间值

0xff,0x8e,0x5a,0x41,0x32,0x28,0x20,0x1b,0x17,0x0e,//三角波频率调整中间值

0xff,0x8e,0x5a,0x41,0x32,0x28,0x20,0x1b,0x17,0x0e};

uint code wave_freq_adjust[]={ //频率调整中间值

380,184,118,86,67,55,46,28,38,32,

295,142, 90,65,50,40,32,27,23,14,

295,142, 90,65,50,40,32,27,23,14}; */

/*uchar code waveTH[]={

0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

uchar code waveTL[]={

0xf2,0x78,0xfb,0x3c,0x63,0x7d,0x8f,0x9d,0xa8,0xb1,

0x17,0x0b,0xb2,0x05,0x37,0x58,0x70,0x82,0x90,0x9b,

0x4d,0xa7,0xc4,0xd3,0xdc,0xe2,0xe6,0xea,0xec,0xee};*/

/***********这两组数组很重要,需要根据波形来调试,选择合适的值,使输出波形达到频率要求************/

uchar code waveTH[]={

0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

0xec,0xf6,0xf9,0xfb,0xfc,0xfc,0xfd,0xfd,0xfd,0xfe};

uchar code waveTL[]={

0x06,0x8a,0x10,0x4e,0x78,0x93,0xa8,0xb3,0xbe,0xc6, //正弦波频率调整中间值

0xac,0xde,0x48,0x7a,0x99,0xaf,0xbb,0xc8,0xd0,0xde,//三角波频率调整中间值

0x88,0x50,0x90,0x32,0x34,0xbe,0x4a,0xa3,0xe5,0x2c};

/*************************************************************************************************/

uchar code triangle_tab[]={ //每隔数字8,采取一次

0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78,

0x80,0x88,0x90,0x98,0xa0,0xa8,0xb0,0xb8,0xc0,0xc8,0xd0,0xd8,0xe0,0xe8,0xf0,0xf8,0xff,

0xf8,0xf0,0xe8,0xe0,0xd8,0xd0,0xc8,0xc0,0xb8,0xb0,0xa8,0xa0,0x98,0x90,0x88,0x80,

0x78,0x70,0x68,0x60,0x58,0x50,0x48,0x40,0x38,0x30,0x28,0x20,0x18,0x10,0x08,0x00};

uchar code sine_tab[256]={

//输出电压从0到最大值(正弦波1/4部分)

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,

//输出电压从最大值到0(正弦波1/4部分)

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,0xde,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,

//输出电压从0到最小值(正弦波1/4部分)

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,

//输出电压从最小值到0(正弦波1/4部分)

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,0x80};

uchar code sawtooth_tab[]={

0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,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};

void delay(uchar z)

{

uint x,y;

for(x=z;x0;x--)

for(y=110;y0;y--);

}

void triangle_out()//三角波输出

{

DAdata=triangle_tab[wavecount++];

if(wavecount64) wavecount=0;

DA_S1=0; //打开8位输入寄存器

DA_S1=1; //关闭8位输入寄存器

}

void sine_out() //正弦波输出

{

DAdata=sine_tab[wavecount++];

DA_S1=0; //打开8位输入寄存器

DA_S1=1; //关闭8位输入寄存器

}

void square_out() //方波输出

{

judge=~judge;

if(judge==1) DAdata=0xff;

else DAdata=0x00;

DA_S1=0; //打开8位输入寄存器

DA_S1=1; //关闭8位输入寄存器

}

void sawtooth_out() //锯齿波输出

{

DAdata=sawtooth_tab[wavecount++];

if(wavecount71) wavecount=0;

DA_S1=0; //打开8位输入寄存器

DA_S1=1; //关闭8位输入寄存器

}

/************1602液晶的相关函数*************/

#define lcd_ports P1

sbit rs=P2^2;

sbit rw=P2^3;

sbit lcden=P2^4;

void write_com(uchar com)

{

rs=0;//置零,表示写指令

lcden=0;

lcd_ports=com;

delay(5);

lcden=1;

delay(5);

lcden=0;

}

void write_date(uchar date)

{

rs=1;//置1,表示写数据(在指令所指的地方写数据)

lcden=0;

lcd_ports=date;

delay(5);

lcden=1;

delay(5);

lcden=0;

}

void disp_lcd(uchar addr,uchar *temp1)

{

uchar num;

write_com(addr);

delay(1); //延时一会儿???

for(num=0;num16;num++)

{

write_date(temp1[num]);//或者这样写write_date(*(temp1+num));

delay(1);

}

}

void init_lcd()

{

//uchar num;

lcden=0; //可有可无???

rw=0; //初始化一定要设置为零,表示写数据

write_com(0x38); //使液晶显示点阵,为下面做准备

write_com(0x0c); //初始设置

write_com(0x06); //初始设置

write_com(0x01); //清零

write_com(0x80); //使指针指向第一行第一格

disp_lcd(0x80,lcd_hang1[4*16]); //在第一行显示

disp_lcd(0xc0,lcd_hang1[5*16]); //在第二行显示

/*for(num=0;num16;num++)

{

write_date(table[num]);

delay(5);

}

write_com(0x80+0x40); //给指针重新赋值,使之指向第二行第一格

for(num=0;num16;num++)

{

write_date(table1[num]);

delay(5);

} */

/*TMOD=0x01; //选用定时方式1

TH0=(65536-50000)/256; //赋初值

TL0=(65536-50000)%256;//

EA=1;//开总中断

ET0=1; //开定时器中断

TR0=1; //启动定时器*/

}

/********************1602液晶函数声明结束*********************/

void main()

{

uchar i=0;

DA_S2=0; //使DAC寄存器处于直通状态

DAdata=0;

DA_S1=1; //关闭8位输入寄存器

init_lcd();

waveform=0;

TMOD=0x01; //设置定时器0为16位工作方式

IT0=1; //设置外部中断0为下降沿触发

ET0=1; //开定时器中断

EX0=1;

EA=1;

while(1)

{

//DAout(0xff); //可输出TTL波形

//DAout(0x80);

//T_temp=32;

}

}

void timer0() interrupt 1

{

TH0=THtemp;

TL0=TLtemp;

if(waveform==0) sine_out();

else if(waveform==1) triangle_out();

else if(waveform==2) square_out();

else if(waveform==3) sawtooth_out(); //tian jian

}

void key_int0() interrupt 0

{

uchar keytemp;

uint total_freq; //总频率

EA=0; TR0=0; //关总中断与定时器

delay(5); //延时够吗???

if(key==0) //确实有按键按下而引发中断

{

keytemp=P30xf0; //获取P3口高四位的值

switch(keytemp)

{

case 0xe0://选择波形

waveform++;

if(waveform3) waveform=0; //jiang 2 gai wei 3

break;

case 0xd0: //频率按规定单位依次增加

wavefreq[waveform]++;

if(wavefreq[waveform]10) wavefreq[waveform]=1; // /*这边要用“10”,因为它比“=11”可靠

break; // 性更高,使加数有个上限,不会一直加下去*/

case 0xb0: //频率按规定单位依次衰减

wavefreq[waveform]--;

if(wavefreq[waveform]1) wavefreq[waveform]=10; //这边要用“1”,因为它比“=0”可靠性更高

break;

case 0x70: //TTL输出

DA_S2=1; //使DAC寄存器关闭

break;

}

THtemp=waveTH[waveform*10+(wavefreq[waveform]-1)]; //方括号中选取第几个数后,并把该值赋给T_temp

TLtemp=waveTL[waveform*10+(wavefreq[waveform]-1)];

total_freq= wavefreq[waveform] * freq_unit[waveform]; //求输出频率(个数*单位)

lcd_hang2[5]=total_freq%10+0x30; //在液晶中显示个位,(0x30 在液晶显示中表示数字0)

total_freq/=10; lcd_hang2[4]=total_freq%10+0x30; //在液晶中显示时十位

total_freq/=10; lcd_hang2[3]=total_freq%10+0x30; //在液晶中显示时百位

total_freq/=10; lcd_hang2[2]=total_freq%10+0x30; //在液晶中显示时千位

disp_lcd(0x80,lcd_hang1[waveform*16]); //在第一行显示

disp_lcd(0xc0,lcd_hang2); //在第二行显示

}

wavecount=0; //'抽点'计数清零

while(!key);

EA=1; TR0=1; //开启总中断与定时器

51 单片机和DAC0832输出方波、矩形波和正弦波由液晶1602显示的C语言程序

显示频率,幅度可调,可产生四种波形,正弦波,方波,锯齿波,三角波,希望你能喜欢,给你发了一张效果图,喜欢的话别忘了采纳我的回答啊

#includereg52.h

#define  uchar unsigned char

#define  uint  unsigned int

#define DAdata  P0 //DA数据端口

sbit DA_S1= P2^0;   // 控制DAC0832的8位输入寄存器,仅当都为0时,可以输出数据(处于直通状态),否则,输出将被锁存

sbit DA_S2= P2^1;   // 控制DAC0832的8位DAC寄存器,仅当都为0时,可以输出数据(处于直通状态),否则,输出将被锁存

sbit key=   P3^2;

uchar wavecount;    //'抽点'计数

uchar THtemp,TLtemp;//传递频率的中间变量

uchar judge=1;      //在方波输出函数中用于简单判别作用

uchar waveform;     //当其为0、1、2时,分别代表三种波

uchar code freq_unit[3]={10,50,200}; //三种波的频率单位

uchar idata wavefreq[3]={1,1,1};    //给每种波定义一个数组单元,用于存放单位频率的个数

uchar code  lcd_hang1[]={"Sine Wave       " "Triangle Wave   " "Square Wave     " "Select Wave:    " "press No.1 key! "};

uchar idata lcd_hang2[16]={"f=    Hz        "};

uchar code waveTH[]={

0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,

0xec,0xf6,0xf9,0xfb,0xfc,0xfc,0xfd,0xfd,0xfd,0xfe};

uchar code waveTL[]={

0x06,0x8a,0x10,0x4e,0x78,0x93,0xa8,0xb3,0xbe,0xc6, //正弦波频率调整中间值

0xac,0xde,0x48,0x7a,0x99,0xaf,0xbb,0xc8,0xd0,0xde, //三角波频率调整中间值

0x88,0x50,0x90,0x32,0x34,0xbe,0x4a,0xa3,0xe5,0x2c};

/*************************************************************************************************/

uchar code triangle_tab[]={  //每隔数字8,采取一次

0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78,

0x80,0x88,0x90,0x98,0xa0,0xa8,0xb0,0xb8,0xc0,0xc8,0xd0,0xd8,0xe0,0xe8,0xf0,0xf8,0xff,

0xf8,0xf0,0xe8,0xe0,0xd8,0xd0,0xc8,0xc0,0xb8,0xb0,0xa8,0xa0,0x98,0x90,0x88,0x80,

0x78,0x70,0x68,0x60,0x58,0x50,0x48,0x40,0x38,0x30,0x28,0x20,0x18,0x10,0x08,0x00};

uchar code sine_tab[256]={

//输出电压从0到最大值(正弦波1/4部分)

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,

//输出电压从最大值到0(正弦波1/4部分)

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,0xde,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,

//输出电压从0到最小值(正弦波1/4部分)

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,

//输出电压从最小值到0(正弦波1/4部分)

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,0x80};

void delay(uchar z)

{

uint x,y;

for(x=z;x0;x--)

for(y=110;y0;y--);

}

void triangle_out() //三角波输出

{

DAdata=triangle_tab[wavecount++];

if(wavecount64) wavecount=0;

DA_S1=0;  //打开8位输入寄存器

DA_S1=1;  //关闭8位输入寄存器

}

void sine_out()  //正弦波输出

{

DAdata=sine_tab[wavecount++];

DA_S1=0;  //打开8位输入寄存器

DA_S1=1;  //关闭8位输入寄存器

}

void square_out()  //方波输出

{

judge=~judge;

if(judge==1) DAdata=0xff;

else  DAdata=0x00;

DA_S1=0;  //打开8位输入寄存器

DA_S1=1;  //关闭8位输入寄存器

}

/************1602液晶的相关函数*************/

#define lcd_ports P1

sbit rs=P2^2;

sbit rw=P2^3;

sbit lcden=P2^4;

void write_com(uchar com)

{

rs=0; //置零,表示写指令

lcden=0;

lcd_ports=com;

delay(5);

lcden=1;

delay(5);

lcden=0;

}

void write_date(uchar date)

{

rs=1; //置1,表示写数据(在指令所指的地方写数据)

lcden=0;

lcd_ports=date;

delay(5);

lcden=1;

delay(5);

lcden=0;

}

void disp_lcd(uchar addr,uchar *temp1)

{

uchar num;

write_com(addr);

delay(1);   //延时一会儿???

for(num=0;num16;num++)

{

write_date(temp1[num]);//或者这样写write_date(*(temp1+num));

delay(1);

}

}

void init_lcd()

{

//uchar num;

lcden=0;  //可有可无???

rw=0;  //初始化一定要设置为零,表示写数据

write_com(0x38);  //使液晶显示点阵,为下面做准备

write_com(0x0c);  //初始设置

write_com(0x06);  //初始设置

write_com(0x01);  //清零

write_com(0x80);  //使指针指向第一行第一格

disp_lcd(0x80,lcd_hang1[3*16]);  //在第一行显示

disp_lcd(0xc0,lcd_hang1[4*16]);  //在第二行显示

}

/********************1602液晶函数声明结束*********************/

void main()

{

uchar i=0;

DA_S2=0;    //使DAC寄存器处于直通状态

DAdata=0;

DA_S1=1;    //关闭8位输入寄存器

init_lcd();

waveform=0;

TMOD=0x01;  //设置定时器0为16位工作方式

IT0=1;      //设置外部中断0为下降沿触发

ET0=1;      //开定时器中断

EX0=1;

EA=1;

while(1)

{

//DAout(0xff);    //可输出TTL波形

//DAout(0x80);

//T_temp=32;

}

}

void timer0() interrupt 1

{

TH0=THtemp;

TL0=TLtemp;

if(waveform==0)      sine_out();

else if(waveform==1) triangle_out();

else if(waveform==2) square_out();

}

void key_int0() interrupt 0

{

uchar keytemp;

uint total_freq; //总频率

EA=0; TR0=0;     //关总中断与定时器

delay(5);       //延时够吗???

if(key==0)       //确实有按键按下而引发中断

{

keytemp=P30xf0; //获取P3口高四位的值

switch(keytemp)

{

case 0xe0: //选择波形

 waveform++;

 if(waveform2) waveform=0;

 break;

case 0xd0:  //频率按规定单位依次增加

 wavefreq[waveform]++;

 if(wavefreq[waveform]10) wavefreq[waveform]=1; // /*这边要用“10”,因为它比“=11”可靠

 break;  //   性更高,使加数有个上限,不会一直加下去*/

case 0xb0:  //频率按规定单位依次衰减

 wavefreq[waveform]--;

 if(wavefreq[waveform]1) wavefreq[waveform]=10; //这边要用“1”,因为它比“=0”可靠性更高

 break;

case 0x70:    //TTL输出

 DA_S2=1; //使DAC寄存器关闭

 break;

}

THtemp=waveTH[waveform*10+(wavefreq[waveform]-1)]; //方括号中选取第几个数后,并把该值赋给T_temp

TLtemp=waveTL[waveform*10+(wavefreq[waveform]-1)];

total_freq= wavefreq[waveform] * freq_unit[waveform]; //求输出频率(个数*单位)

lcd_hang2[5]=total_freq%10+0x30;                 //在液晶中显示个位,(0x30 在液晶显示中表示数字0)

total_freq/=10; lcd_hang2[4]=total_freq%10+0x30; //在液晶中显示时十位

total_freq/=10; lcd_hang2[3]=total_freq%10+0x30; //在液晶中显示时百位

total_freq/=10; lcd_hang2[2]=total_freq%10+0x30; //在液晶中显示时千位

disp_lcd(0x80,lcd_hang1[waveform*16]);  //在第一行显示

disp_lcd(0xc0,lcd_hang2);  //在第二行显示

}

wavecount=0; //'抽点'计数清零

while(!key);

EA=1; TR0=1;     //开启总中断与定时器

}

用C语言编写一段程序,使用查表的方式使单片机控制DAC0832产生一个电压值在0~5V之间的方波。

#includereg52.h //包含头文件

#includeintrins.h

#define uchar unsigned char //宏定义

#define uint unsigned int

sbit s1=P3^5; //定义按键的接口

sbit s2=P3^6;

sbit s3=P3^7;

sbit s4=P3^4;

sbit s5=P2^3;

sbit led0=P3^0; //定义四个LED,分别表示不同的波形

sbit led1=P3^1;

sbit led2=P3^2;

sbit led3=P3^3;

sbit lcdrs=P2^7; //液晶控制引脚,还有一个控制脚是RW,因为我们只需要向液晶里写数据系那是就好了,所以,我们直接将RW引脚接地

sbit lcden=P2^6;

char num,boxing,u; //定义全局变量

uchar pinlv=100,bujin=1,bujin1=1; //频率初始值是10Hz,步进值默认是0.1,显示步进值变量

uchar code table[]="0123456789"; //定义显示的数组

uchar code table1[]="Fout= Wave form:"; //初始化显示字符

unsigned int m,pwm=50; //定义变量 m

int a,b,h,num1; //定义全局变量

//自定义字符

uchar code zifu[]={ //此数组内数据为液晶上显示波形符号的自定义字符

0x0e,0x11,0x11,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x11,0x11,0x0e,0x00, //正弦波 0 1

0x00,0x07,0x04,0x04,0x04,0x04,0x1c,0x00,

0x00,0x1c,0x04,0x04,0x04,0x04,0x07,0x00, //矩形波 2 3

0x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00,

0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00, //三角波 4 5

0x00,0x01,0x03,0x05,0x09,0x11,0x00,0x00, //锯齿波 6

};

uchar code sin[64]={ //此数组内的数据为,da输出对应电压值对应的数字量,0是0V,255是5V

135,145,158,167,176,188,199,209,218,226,234,240,245,249,252,254,254,253,251,247,243,237,230,222,213,204,193,182,170,158,

146,133,121,108,96,84,72,61,50,41,32,24,17,11,7,3,1,0,0,2,5,9,14,20,28,36,45,55,66,78,90,102,114,128

}; //正弦波取码

uchar code juxing[64]={ //一个周期是采样64个点, 所以数组内是64个数据

255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,

255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

}; //矩形波取码

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

}; //三角波取码

uchar code juchi[64]={

0,4,8,12,16,20,24,28,32,36,40,45,49,53,57,61,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125,130,134,138,142,

146,150,154,158,162,166,170,174,178,182,186,190,194,198,202,206,210,215,219,223,227,231,235,239,243,247,251,255

}; //锯齿波取码

void delay(uint xms) //延时函数

{

int a,b;

for(a=xms;a0;a--)

for(b=110;b0;b--);

}

void write_com(uchar com) //写命令函数

{

lcdrs=0;

P0=com;

delay(1);

lcden=0;

delay(1);

lcden=1;

}

void write_date(uchar date) //写数据函数

{

lcdrs=0;

P0=date;

delay(1);

lcden=0;

delay(1);

lcden=1;

}

//自定义字符集

void Lcd_ram()

{

uint i,j,k=0,temp=0x04;

for(i=0;i7;i++)

{

for(j=0;j8;j++)

{

write_com(temp+j);

write_date(zifu[k]);

k++;

}

temp=temp+8;

}

}

void init_lcd() //初始化函数

{

uchar i;

lcden=0; //默认开始状态为关使能端,见时序图

Lcd_ram();

write_com(0x01); //显示清屏,将上次的内容清除,默认为0x01.

write_com(0x0f);

write_com(0x38); //显示模式设置,默认为0x38,不用变。

write_com(0x0c); //显示功能设置0x0f为开显示,显示光标,光标闪烁;0x0c为开显示,不显光标,光标不闪

write_com(0x06); //设置光标状态默认0x06,为读一个字符光标加1.

write_com(0x80); //设置初始化数据指针,是在读指令的操作里进行的

for(i=10;i20;i++) //显示初始化

{

write_date(table1[i]); //显示第一行字符

}

write_com(0x80+40); //选择第二行

for(i=0;i9;i++)

{

write_date(table1[i]); //显示第二行字符

}

write_com(0x80+10); //选择第一行第十个位置

write_date(0);

write_date(1);

write_date(0);

write_date(1);

write_date(0);

write_date(1); //显示自定义的波形图案

write_com(0x80+40+9); //选择第二行第九个位置

write_date(' ');

write_date('1');

write_date('0');

write_date('.');

write_date('0');

write_date('H');

write_date('z'); //显示初始的频率值

}

void initclock() //定时器初始化函数

{

TMOD=0x01; //定时器的工作方式

TH0=a;

TL0=b; //定时器赋初值

EA=1; //打开中断总开关

ET0=1; //打开定时器允许中断开关

TR0=1; //打开定时器定时开关

}

void display() //显示函数

{

uchar qian,bai,shi,ge; //定义变量用于显示

qian=pinlv/1000; //将频率值拆成一位的数据,将数据除以1000,得到的商是一位数,赋值给qian

bai=pinlv%1000/100; //将频率除以1000的余数再除以100就得到了频率的百位,赋值给bai

shi=pinlv%1000%100/10; //同上,得到频率的十位

ge=pinlv%1000/100%10;

write_com(0x80+40+9); //选中第二行第九个位置

if(qian==0) //千位如果为0

write_date(' '); //不显示

else //千位不为0

write_date(table[qian]); //正常显示千位

if(qian==0bai==0) //千位和百位都为0

write_date(' '); //百位不显示

else //不都为0

write_date(table[bai]); //百位正常显示

write_date(table[shi]); //显示十位数

write_date('.'); //显示小数点

write_date(table[ge]); //显示个位

write_date('H'); //显示频率的单位Hz

write_date('z');

if(boxing==0) //判断波形为正弦波

{

write_com(0x80+10); //选中一行频率图案位置

write_date(0); //显示正弦波图案

write_date(1);

write_date(0);

write_date(1);

write_date(0);

write_date(1);

led3=1;

led0=0; //点亮正弦波指示灯

}

if(boxing==1) //注释同上

{

write_com(0x80+10);

write_date(2);

write_date(3);

write_date(2);

write_date(3);

write_date(2);

write_date(3);

led0=1;

led1=0;

}

if(boxing==2)

{

write_com(0x80+10);

write_date(4);

write_date(5);

write_date(4);

write_date(5);

write_date(4);

write_date(5);

led1=1;

led2=0;

}

if(boxing==3)

{

write_com(0x80+10);

write_date(6);

write_date(6);

write_date(6);

write_date(6);

write_date(6);

write_date(6);

led2=1;

led3=0;

}

}

void keyscan() //频率调节键盘检测函数

{

if(s1==0) //加按键是否按下

{

EA=0; //关闭中断

while(!s1); //按键松开

pinlv+=bujin; //频率以步进值加

if(pinlv1000) //最大加到100Hz

{

pinlv=100; //100Hz

}

display(); //显示函数

m=65536-(15000/pinlv);//计算频率

/*频率值最小是10Hz,pinlv的值是100(因为要显示小数点后一位),150000/100=1500,这个1500就是定时器需要计时的,单位是us,65536-1500得到的是定时器的初值,

先不管初值,先看定时时间,1500us,一个波形的周期是由64个定时组成的,所以,一个波形周期就是64*1500us=96000,也就是96ms,约等

于100ms,也就是10Hz的频率*/

a=m/256; //将定时器的初值赋值给变量

b=m%256;

EA=1; //打开中断总开关

}

if(s2==0) //减按键按下

{

EA=0;

while(!s2);

pinlv-=bujin; //频率以步进值减

if(pinlv100)

{

pinlv=100;

}

display();

m=65536-(15000/pinlv);

a=m/256;

b=m%256;

EA=1;

}

if(s3==0) //波形切换按键

{

EA=0;

while(!s3);

boxing++; //波形切换

if(boxing=4) //4种波形

{

boxing=0;

}

display();

EA=1;

}

if(s5==0) //PWM切换按键

{

EA=0;

while(!s5);

pwm+=10;

if(pwm90)

{

pwm=10;

}

// display();

EA=1;

}

}

void bujindisplay() //步进值设置界面显示程序

{

uint bai,shi,ge; //定义步进值 百十个位

bai=bujin1/100; //将步进值除以100得到百位,也就是频率值的十位,因为有一个小数位

shi=bujin1%100/10; //将步进值除以100的余数除以十得到十位

ge=bujin1%100%10; //取余10后得到个位,也就是频率步进值的小数点后一位

write_com(0x80+11); //选中液晶第一行第十一列

if(bai==0) //百位是否为0

write_date(' '); //百位不显示

else //百位不为0

write_date(table[bai]); //显示百位数据

write_date(table[shi]); //显示十位数据

write_date('.'); //显示小数点

write_date(table[ge]); //显示个位,也就是小数点后一位

}

void bujinjiance() //步进值设置键盘程序

{

if(s4==0) //步进设置按键按下

{

delay(5); //延时去抖

if(s4==0) //再次判断按键

{

while(!s4); //按键释放,按键松开才继续向下执行

h++; //变量加

if(h==1) //进入设置状态时

{

write_com(0x01); //清屏

write_com(0x80); //初始化显示步进设置界面

write_date('S');delay(1); //step value

write_date('t');delay(1);

write_date('e');delay(1);

write_date('p');delay(1);

write_date(' ');delay(1);

write_date('v');delay(1);

write_date('a');delay(1);

write_date('l');delay(1);

write_date('u');delay(1);

write_date('e');delay(1);

write_date(':');delay(1);

bujin1=bujin; //步进值赋值给临时变量

bujindisplay(); //显示步进值

}

if(h==2) //退出设置

{

h=0; //清零

bujin=bujin1; //设置好的临时步进值赋值给步进变量

init_lcd(); //初始化液晶显示

initclock(); //定时器初始化

display(); //调用显示程序

}

}

}

if(h==1) //设置步进值时

{

if(s1==0) //加按键按下

{

delay(5); //延时去抖

if(s1==0) //再次判断

{

while(!s1); //按键释放

bujin1++; //步进值加1

if(bujin1=101) //步进值最大100,也就是10.0Hz

{

bujin1=1; //超过最大值就恢复到0.1Hz

}

bujindisplay(); //步进显示

}

}

if(s2==0) //减按键,注释同上

{

delay(5);

if(s2==0)

{

while(!s2);

bujin1--; //步进减

if(bujin1=0)

{

bujin1=100;

}

bujindisplay();

}

}

}

}

void main() //主函数

{

init_lcd(); //调用初始化程序

m=65536-(15000/pinlv); //定时器初值

a=m/256;

b=m%256;

initclock(); //定时器初始化

led0=0; //点亮第一个波形指示灯

while(1) //进入while循环,括号内为1,一直成立,所以也叫死循环,程序不会跳出,一直在内执行

{

if(h==0) //正常模式不是步进调节

{

keyscan(); //扫描按键

// display();

}

bujinjiance(); //扫描步进调节程序

switch(boxing) //选择波形

{

case 0 : P1=sin[u]; break; //正弦波

case 1 : //矩形波

if(upwm*64/100)P1=255;

else

P1=0;

break;

case 2 : P1=sanjiao[u]; break; //三角波

case 3 : P1=juchi[u]; break; //锯齿波

}

}

}

void T0_time()interrupt 1 //定时器

{

TH0=a;

TL0=b;

u++; //变量加

if(u=64) //一个周期采样64个点, 所以加到64就清零

u=0; //u清零

//根据不同的初值,定时器定时时间不同,达到不同频率的目的

}

急求用DAC接口电路,编写产生负向锯齿波、三角波、梯形波的程序

sbit wr=P3^6;

sbit rd=P3^2;

sbit k0=P1^0;

sbit k1=P1^1;

sbit k2=P1^2;

sbit k3=P1^3;

sbit k4=P1^4;

unsigned char flag;//当flag为0、1、2、3、4时分别产生正弦波、方波、三角波、梯形波、锯齿波

unsigned char const code sin_code[256]={

0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,

0xa5,0xa8,0xab,0xae,0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,

0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,0xda,0xdc,0xde,0xe0,

0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf4,

0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,

0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,

0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf3,0xf2,

0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe3,0xe1,0xde,0xdc,

0xda,0xd8,0xd6,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,

0xbc,0xb9,0xb6,0xb4,0xb1,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,

0x99,0x96,0x92,0x8f,0x8c,0x89,0x86,0x83,0x80,0x7d,0x79,0x76,

0x73,0x70,0x6d,0x6a,0x67,0x64,0x61,0x5e,0x5b,0x58,0x55,0x52,

0x4f,0x4c,0x49,0x46,0x43,0x41,0x3e,0x3b,0x39,0x36,0x33,0x31,

0x2e,0x2c,0x2a,0x27,0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,

0x15,0x14,0x12,0x10,0xf,0xd,0xc,0xb,0x9,0x8,0x7,0x6,0x5,0x4,

0x3,0x3,0x2,0x1,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,

0x0,0x1,0x1,0x2,0x3,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xc,0xd,

0xe,0x10,0x12,0x13,0x15,0x17,0x18,0x1a,0x1c,0x1e,0x20,0x23,

0x25,0x27,0x29,0x2c,0x2e,0x30,0x33,0x35,0x38,0x3b,0x3d,0x40,

0x43,0x46,0x48,0x4b,0x4e,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,

0x66,0x69,0x6c,0x6f,0x73,0x76,0x79,0x7c

}; //用于生成正弦

unsigned char kscan() //键盘扫描函数

{

unsigned char kscannum,t;

P1=0xff;

t=P1;

if(~(t0xff))

{

if(k0==0)

{

kscannum=1;

}

else if(k1==0)

{

kscannum=2;

}

else if(k2==0)

{

kscannum=3;

}

else if(k3==0)

{

kscannum=4;

}

else if(k4==0)

{

kscannum=5;

}

else

{

kscannum=0;

}

return kscannum;

}

}

void init_DA0832()//DA0832初始化

{

rd=0;

wr=0;

}

void fangbo()//产生方波函数

{

EA=1; //开总中断

ET0=1; //计时器T0工作

TMOD=1;

TR0=1; //开启T0

TH0=0xff; //装入初值

TL0=0x83;

}

void sin()//产生正弦波函数

{

unsigned inti;

do{

P2=sin_code[i];

i=i+1;

}while(i256);

}

void juchibo()//产生锯齿波函数

{

P2=0x00;

do{

P2=P2+1;

}while(P2=0xff);

}

void sanjiaobo()//产生三角波函数

{

P2=0x00;

do{

P2=P2+1;

}while(P20xff);

P2=0xff;

do{

P2=P2-1;

}while(P20x00);

P2=0x00;

}

void tixingbo()//产生梯形波函数

{

unsigned char i;

P2=0x00;

do{

P2=P2+1;

}while(P20xff);

P2=0xff;

for(i=255;i0;i--)

{

P2=0xff;

}

do{

P2=P2-1;

}while(P20x00);

P2=0x00;

}

void timer0(void) interrupt 1//中断函数

{

P2=~P2; //取反

TH0=0xff; //重新装入初值

TL0=0x83;

TR0=1;

}

/*主函数*/

void main()

{

init_DA0832();//初始化

do

{

flag=kscan();

}while(!flag);//等待按键按下

while(1)

{

switch(flag)

{

case 1:

do{

flag=kscan();

sin();

}while(flag==1);//产生正弦波

break;

case 2:

fangbo();

do{

flag=kscan();

}while(flag==2);//产生方波

TR0=0;

break;

case 3:

do{

flag=kscan();

sanjiaobo();

}while(flag==3);//产生三角波

break;

case 4:

do{

flag=kscan();

tixingbo();

}while(flag==4);//产生梯形波

break;

case 5:

do{

flag=kscan();

juchibo();

}while(flag==5);//产生锯齿波

break;

default:

flag=kscan();

break;

}

}

}


名称栏目:dac矩形波函数c语言 矩形波的时域表达式
转载注明:http://cdkjz.cn/article/dogcsop.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220