您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 51单片机与0832波形发生器锯齿波、三角波、正弦波
//锯齿波#includereg51.h#includeabsacc.h//绝对地址访问头文件#defineDAC0832XBYTE[0x7fff]//DAC0832的地址为0x7fffvoiddelay()//定时器定时1ms{TH1=0xfc;TL1=0x18;//定时器初值设定TR1=1;//启动定时器while(!TF1);//查询是否溢出TF1=0;//将溢出标志位清零}voidmain(){unsignedchari;TMOD=0x10;//设置定时器工作方式while(1){for(i=0;i=255;i++)//形成锯齿波,最大值为255{DAC0832=i;//D/A转换输出delay();//延时}}}//三角波#includereg51.h#includeabsacc.h//绝对地址访问头文件#defineDAC0832XBYTE[0x7fff]//DAC0832的地址为0x7fffvoiddelay()//定时器定时1ms{TH1=0xfc;TL1=0x18;//定时器初值设定TR1=1;//启动定时器while(!TF1);//查询是否溢出TF1=0;//将溢出标志位清零}voidmain(){unsignedchari;TMOD=0x10;//设置定时器工作方式while(1){for(i=0;i255;i++)//形成三角波,i增加到最大值为255{DAC0832=i;//D/A转换增量输出delay();//延时}for(i=255;i0;i--)//形成三角波,i减小至最小值0{DAC0832=i;//D/A转换减量输出delay();//延时}}}//正弦波#includereg51.h#includeabsacc.h//绝对地址访问头文件#defineDAC0832XBYTE[0x7fff]//DAC0832的地址为0x7fffunsignedcharcodesin[]={//正弦波一周期内采样256次,把幅值按比例放大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,0x80};voiddelay()//定时器定时1ms{TH1=0xfc;TL1=0x18;//定时器初值设定TR1=1;//启动定时器while(!TF1);//查询是否溢出TF1=0;//将溢出标志位清零}voidmain(){unsignedchari;TMOD=0x10;//设置定时器工作方式while(1){for(i=0;i255;i++)//形成正弦波,i增加到最大值为255{DAC0832=sin[i];//D/A转换增量输出delay();//延时}}}//方波#includereg51.hsbitP2_0=P2^0;//定义位变量voidmain(){TMOD=0x02;//设置T0,工作方式2TH0=6;TL0=6;//设置初值TR0=1;//启动T0while(1){if(TF0==1)//查询是否溢出{TF0=0;//溢出标志清零P2_0=!P2_0;}//P2.0取反,输出方波}}
本文标题:51单片机与0832波形发生器锯齿波、三角波、正弦波
链接地址:https://www.777doc.com/doc-5662736 .html