《函数信号发生器源程序.doc》由会员分享,可在线阅读,更多相关《函数信号发生器源程序.doc(6页珍藏版)》请在三一办公上搜索。
1、#include#include#define DAC0832 XBYTE0x0fffint pinlv=256; /改变频率的时延自变量sbit p20=P20;sbit p21=P21;sbit p22=P22;sbit p23=P23;sbit RS=P30;sbit RW=P31; /液晶显示的端口sbit E=P34;int j=0;unsigned char code TAB=0123456789msTIME:;unsigned char code TAB1=zhengxianbofangbo sanjiaobo jvchibo ;unsigned char table4;floa
2、t code table2= /正弦波信号采点值 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,0x
3、E4,0xE4,0xE5,0xE5,0xE6,0xE6,0xE7,0xE7,0xE7,0xE7, 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,0xA
4、3,0xA0,0x9E,0x9B,0x99,0x97,0x94,0x92,0x8F,0x8D,0x8A,0x88,0x85,0x83,0x80, 0x7D,0x7B,0x78,0x76,0x73,0x71,0x6E,0x6C,0x69,0x67,0x65,0x62,0x60,0x5D,0x5B,0x59, 0x56,0x54,0x52,0x4F,0x4D,0x4B,0x48,0x47,0x45,0x43,0x41,0x3F,0x3D,0x3B,0x39,0x37, 0x35,0x34,0x32,0x30,0x2F,0x2D,0x2C,0x2A,0x29,0x28,0x26,0x25,0x24,
5、0x23,0x22,0x21, 0x20,0x1F,0x1E,0x1D,0x1D,0x1C,0x1C,0x1B,0x1B,0x1A,0x1A,0x1A,0x19,0x19,0x19,0x19, 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,0
6、x47,0x49,0x4B,0x4D,0x4F,0x52,0x54,0x56, 0x59,0x5B,0x5D,0x60,0x62,0x65,0x67,0x69,0x6C,0x6E,0x71,0x73,0x76,0x78,0x7B,0x7D;void delay(unsigned char ms); /延时函数void write_com(unsigned char com); /液晶写指令void write_data(unsigned char dat); /液晶写数据void LCD1602_init(void) /液晶初始化 delay(15); write_com(0x38); del
7、ay(5); write_com(0x38); delay(5); write_com(0x38); write_com(0x38); write_com(0x08); write_com(0x01); write_com(0x06); write_com(0x0C);void write_com(unsigned char com) E=0; /复位初始为高电平 RS=0; RW=0; P1=com; delay(1); E=1; delay(1); E=0;void write_data(unsigned char dat) E=0; /复位初始为高电平 RS=1; RW=0; P1=da
8、t; delay(1); E=1; delay(1); E=0;void delay(unsigned char ms) /晶振12KHz unsigned char i; while(ms-) for(i=0;i=120;i+); void fang() /方波 DAC0832=0; delay(pinlv/2); DAC0832=0xff; delay(pinlv/2);void jvchi() /锯齿波 unsigned char i; for(i=0;i255;i+) DAC0832=i; delay(pinlv/256); void tran() /三角波 unsigned char
9、 i; for(i=0;i0;i-) DAC0832=i; delay(pinlv/256); void sin() /正弦波 unsigned int i; for(i=0;i255;i+) DAC0832=table2i; delay(pinlv/256); void main(void) /主函数 IE=0x85; /外部中断设定 改变频率 TCON=0x05; LCD1602_init(); table0=pinlv/1000; /计算周期作为输出 table1=pinlv%1000/100; table2=pinlv%100/10; table3=pinlv%10; write_co
10、m(0x80+0x40); /设置待写入数据的地址,写LCD1602的第2行 写出产生波形的周期 for(j=12;j17;j+) write_data(TABj); delay(1); for(j=0;j4;j+) write_data(TABtablej); delay(1); write_data(TAB10); /在周期后面加上“MS” write_data(TAB11); while(1) if(p20=0) /方波 write_com(0x80); /写LCD1602的第1行,写明产生的波形 for(j=11;j22;j+) write_data(TAB1j); while(p20
11、=0) fang(); if(p21=0) /锯齿波 write_com(0x80); /写LCD1602的第2行 for(j=33;j44;j+) write_data(TAB1j); while(p21=0) jvchi(); if(p22=0) /三角波 write_com(0x80); /写LCD1602的第2行 for(j=22;j33;j+) write_data(TAB1j); while(p22=0) tran(); if(p23=0) /正弦波 write_com(0x80); /写LCD1602的第2行 for(j=0;j11;j+) write_data(TAB1j);
12、while(p23=0) sin(); void int0(void) interrupt 0 /中断0 降频 pinlv=pinlv*2; /频率减半 table0=pinlv/1000; table1=pinlv%1000/100; table2=pinlv%100/10; table3=pinlv%10; write_com(0x80+0x40); /设置代写入数据的地址,写LCD1602的第2行 for(j=12;j17;j+) write_data(TABj); delay(1); for(j=0;j4;j+) write_data(TABtablej); delay(1); wri
13、te_data(TAB10); write_data(TAB11);void int1(void) interrupt 2 /中断2 升频 pinlv=pinlv/2; table0=pinlv/1000; table1=pinlv%1000/100; table2=pinlv%100/10; table3=pinlv%10; write_com(0x80+0x40); /设置代写入数据的地址,写LCD1602的第1行 for(j=12;j17;j+) write_data(TABj); delay(1); for(j=0;j4;j+) write_data(TABtablej); delay(1); write_data(TAB10); write_data(TAB11);