❶ 如何用c语言编写一段方波,频率为1M。
如果是模拟出来的话,频率是1Mhz,首先你要有一个1Mx2的定时,这个2Mhz的定时每次计数到达的时候将对应的io口翻转,频率为1M周期为1us的方波就出来了。
大部分单片机都有pwm功能直接配置寄存器使能就可以输出对应的频率方波。
❷ 51 单片机和DAC0832输出方波、矩形波和正弦波由液晶1602显示的C语言程序
显示频率,幅度可调,可产生四种波形,正弦波,方波,锯齿波,三角波,希望你能喜欢,给你发了一张效果图,喜欢的话别忘了采纳我的回答啊
#include<reg52.h>
#defineucharunsignedchar
#defineuintunsignedint
#defineDAdataP0 //DA数据端口
sbitDA_S1=P2^0;//控制DAC0832的8位输入寄存器,仅当都为0时,可以输出数据(处于直通状态),否则,输出将被锁存
sbitDA_S2=P2^1;//控制DAC0832的8位DAC寄存器,仅当都为0时,可以输出数据(处于直通状态),否则,输出将被锁存
sbitkey=P3^2;
ucharwavecount;//'抽点'计数
ucharTHtemp,TLtemp;//传递频率的中间变量
ucharjudge=1;//在方波输出函数中用于简单判别作用
ucharwaveform; //当其为0、1、2时,分别代表三种波
ucharcodefreq_unit[3]={10,50,200};//三种波的频率单位
ucharidatawavefreq[3]={1,1,1}; //给每种波定义一个数组单元,用于存放单位频率的个数
ucharcodelcd_hang1[]={"SineWave""TriangleWave""SquareWave""SelectWave:""pressNo.1key!"};
ucharidatalcd_hang2[16]={"f=Hz"};
ucharcodewaveTH[]={
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};
ucharcodewaveTL[]={
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};
/*************************************************************************************************/
ucharcodetriangle_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};
ucharcodesine_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};
voiddelay(ucharz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidtriangle_out() //三角波输出
{
DAdata=triangle_tab[wavecount++];
if(wavecount>64)wavecount=0;
DA_S1=0;//打开8位输入寄存器
DA_S1=1;//关闭8位输入寄存器
}
voidsine_out() //正弦波输出
{
DAdata=sine_tab[wavecount++];
DA_S1=0;//打开8位输入寄存器
DA_S1=1;//关闭8位输入寄存器
}
voidsquare_out()//方波输出
{
judge=~judge;
if(judge==1)DAdata=0xff;
elseDAdata=0x00;
DA_S1=0;//打开8位输入寄存器
DA_S1=1;//关闭8位输入寄存器
}
/************1602液晶的相关函数*************/
#definelcd_portsP1
sbitrs=P2^2;
sbitrw=P2^3;
sbitlcden=P2^4;
voidwrite_com(ucharcom)
{
rs=0; //置零,表示写指令
lcden=0;
lcd_ports=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
voidwrite_date(uchardate)
{
rs=1; //置1,表示写数据(在指令所指的地方写数据)
lcden=0;
lcd_ports=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
voiddisp_lcd(ucharaddr,uchar*temp1)
{
ucharnum;
write_com(addr);
delay(1);//延时一会儿???
for(num=0;num<16;num++)
{
write_date(temp1[num]);//或者这样写write_date(*(temp1+num));
delay(1);
}
}
voidinit_lcd()
{
//ucharnum;
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液晶函数声明结束*********************/
voidmain()
{
uchari=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;
}
}
voidtimer0()interrupt1
{
TH0=THtemp;
TL0=TLtemp;
if(waveform==0)sine_out();
elseif(waveform==1)triangle_out();
elseif(waveform==2)square_out();
}
voidkey_int0()interrupt0
{
ucharkeytemp;
uinttotal_freq;//总频率
EA=0;TR0=0;//关总中断与定时器
delay(5);//延时够吗???
if(key==0)//确实有按键按下而引发中断
{
keytemp=P3&0xf0;//获取P3口高四位的值
switch(keytemp)
{
case0xe0: //选择波形
waveform++;
if(waveform>2)waveform=0;
break;
case0xd0://频率按规定单位依次增加
wavefreq[waveform]++;
if(wavefreq[waveform]>10)wavefreq[waveform]=1;///*这边要用“>10”,因为它比“=11”可靠
break; //性更高,使加数有个上限,不会一直加下去*/
case0xb0://频率按规定单位依次衰减
wavefreq[waveform]--;
if(wavefreq[waveform]<1)wavefreq[waveform]=10;//这边要用“<1”,因为它比“=0”可靠性更高
break;
case0x70://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;//开启总中断与定时器
}