#includereg52.h
公司主營業(yè)務:網(wǎng)站建設、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出侯馬免費做網(wǎng)站回饋大家。
#define uchar unsigned char
#define uint unsigned int
//#define Fosc 24000000/12000000 //12分頻后的頻率
#define DAdata P0//DA數(shù)據(jù)端口
sbit DA_S1= P2^0; // 控制DAC0832的8位輸入寄存器,僅當都為0時,可以輸出數(shù)據(jù)(處于直通狀態(tài)),否則,輸出將被鎖存
sbit DA_S2= P2^1; // 控制DAC0832的8位DAC寄存器,僅當都為0時,可以輸出數(shù)據(jù)(處于直通狀態(tài)),否則,輸出將被鎖存
sbit key= P3^2;
uchar wavecount; //'抽點'計數(shù)
uchar THtemp,TLtemp;//傳遞頻率的中間變量
//uint T_temp;
uchar judge=1; //在方波輸出函數(shù)中用于簡單判別作用
uchar waveform; //當其為0、1、2時,分別代表三種波
uchar code freq_unit[4]={10,50,200,10}; //三種波的頻率單位 sawtooth
uchar idata wavefreq[4]={1,1,1,1}; //給每種波定義一個數(shù)組單元,用于存放單位頻率的個數(shù)
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[]={ //頻率調(diào)整中間值
0xff,0xb8,0x76,0x56,0x43,0x37,0x2e,0x26,0x20,0x1c, //正弦波頻率調(diào)整中間值
0xff,0x8e,0x5a,0x41,0x32,0x28,0x20,0x1b,0x17,0x0e,//三角波頻率調(diào)整中間值
0xff,0x8e,0x5a,0x41,0x32,0x28,0x20,0x1b,0x17,0x0e};
uint code wave_freq_adjust[]={ //頻率調(diào)整中間值
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};*/
/***********這兩組數(shù)組很重要,需要根據(jù)波形來調(diào)試,選擇合適的值,使輸出波形達到頻率要求************/
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, //正弦波頻率調(diào)整中間值
0xac,0xde,0x48,0x7a,0x99,0xaf,0xbb,0xc8,0xd0,0xde,//三角波頻率調(diào)整中間值
0x88,0x50,0x90,0x32,0x34,0xbe,0x4a,0xa3,0xe5,0x2c};
/*************************************************************************************************/
uchar code triangle_tab[]={ //每隔數(shù)字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液晶的相關函數(shù)*************/
#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,表示寫數(shù)據(jù)(在指令所指的地方寫數(shù)據(jù))
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; //初始化一定要設置為零,表示寫數(shù)據(jù)
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液晶函數(shù)聲明結(jié)束*********************/
void main()
{
uchar i=0;
DA_S2=0; //使DAC寄存器處于直通狀態(tài)
DAdata=0;
DA_S1=1; //關閉8位輸入寄存器
init_lcd();
waveform=0;
TMOD=0x01; //設置定時器0為16位工作方式
IT0=1; //設置外部中斷0為下降沿觸發(fā)
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) //確實有按鍵按下而引發(fā)中斷
{
keytemp=P30xf0; //獲取P3口高四位的值
switch(keytemp)
{
case 0xe0://選擇波形
waveform++;
if(waveform3) waveform=0; //jiang 2 gai wei 3
break;
case 0xd0: //頻率按規(guī)定單位依次增加
wavefreq[waveform]++;
if(wavefreq[waveform]10) wavefreq[waveform]=1; // /*這邊要用“10”,因為它比“=11”可靠
break; // 性更高,使加數(shù)有個上限,不會一直加下去*/
case 0xb0: //頻率按規(guī)定單位依次衰減
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)]; //方括號中選取第幾個數(shù)后,并把該值賦給T_temp
TLtemp=waveTL[waveform*10+(wavefreq[waveform]-1)];
total_freq= wavefreq[waveform] * freq_unit[waveform]; //求輸出頻率(個數(shù)*單位)
lcd_hang2[5]=total_freq%10+0x30; //在液晶中顯示個位,(0x30 在液晶顯示中表示數(shù)字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; //'抽點'計數(shù)清零
while(!key);
EA=1; TR0=1; //開啟總中斷與定時器
顯示頻率,幅度可調(diào),可產(chǎn)生四種波形,正弦波,方波,鋸齒波,三角波,希望你能喜歡,給你發(fā)了一張效果圖,喜歡的話別忘了采納我的回答啊
#includereg52.h
#define??uchar?unsigned?char
#define??uint??unsigned?int
#define?DAdata??P0 //DA數(shù)據(jù)端口
sbit?DA_S1=?P2^0;???//?控制DAC0832的8位輸入寄存器,僅當都為0時,可以輸出數(shù)據(jù)(處于直通狀態(tài)),否則,輸出將被鎖存
sbit?DA_S2=?P2^1;???//?控制DAC0832的8位DAC寄存器,僅當都為0時,可以輸出數(shù)據(jù)(處于直通狀態(tài)),否則,輸出將被鎖存
sbit?key=???P3^2;
uchar?wavecount;????//'抽點'計數(shù)
uchar?THtemp,TLtemp;//傳遞頻率的中間變量
uchar?judge=1;??????//在方波輸出函數(shù)中用于簡單判別作用
uchar?waveform; ????//當其為0、1、2時,分別代表三種波
uchar?code?freq_unit[3]={10,50,200};?//三種波的頻率單位
uchar?idata?wavefreq[3]={1,1,1};? ??//給每種波定義一個數(shù)組單元,用于存放單位頻率的個數(shù)
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,?//正弦波頻率調(diào)整中間值
0xac,0xde,0x48,0x7a,0x99,0xaf,0xbb,0xc8,0xd0,0xde, //三角波頻率調(diào)整中間值
0x88,0x50,0x90,0x32,0x34,0xbe,0x4a,0xa3,0xe5,0x2c};
/*************************************************************************************************/
uchar?code?triangle_tab[]={ ?//每隔數(shù)字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液晶的相關函數(shù)*************/
#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,表示寫數(shù)據(jù)(在指令所指的地方寫數(shù)據(jù))
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;??//初始化一定要設置為零,表示寫數(shù)據(jù)
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液晶函數(shù)聲明結(jié)束*********************/
void?main()
{
uchar?i=0;
DA_S2=0;????//使DAC寄存器處于直通狀態(tài)
DAdata=0;
DA_S1=1;????//關閉8位輸入寄存器
init_lcd();
waveform=0;
TMOD=0x01;??//設置定時器0為16位工作方式
IT0=1;??????//設置外部中斷0為下降沿觸發(fā)
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)???????//確實有按鍵按下而引發(fā)中斷
{
keytemp=P30xf0;?//獲取P3口高四位的值
switch(keytemp)
{
case?0xe0: //選擇波形
?waveform++;
?if(waveform2)?waveform=0;
?break;
case?0xd0:??//頻率按規(guī)定單位依次增加
?wavefreq[waveform]++;
?if(wavefreq[waveform]10)?wavefreq[waveform]=1;?//?/*這邊要用“10”,因為它比“=11”可靠
?break; ?//???性更高,使加數(shù)有個上限,不會一直加下去*/
case?0xb0:??//頻率按規(guī)定單位依次衰減
?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)];?//方括號中選取第幾個數(shù)后,并把該值賦給T_temp
TLtemp=waveTL[waveform*10+(wavefreq[waveform]-1)];
total_freq=?wavefreq[waveform]?*?freq_unit[waveform];?//求輸出頻率(個數(shù)*單位)
lcd_hang2[5]=total_freq%10+0x30;?????????????????//在液晶中顯示個位,(0x30?在液晶顯示中表示數(shù)字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;?//'抽點'計數(shù)清零
while(!key);
EA=1;?TR0=1;?????//開啟總中斷與定時器
}
#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,因為我們只需要向液晶里寫數(shù)據(jù)系那是就好了,所以,我們直接將RW引腳接地
sbit lcden=P2^6;
char num,boxing,u; //定義全局變量
uchar pinlv=100,bujin=1,bujin1=1; //頻率初始值是10Hz,步進值默認是0.1,顯示步進值變量
uchar code table[]="0123456789"; //定義顯示的數(shù)組
uchar code table1[]="Fout= Wave form:"; //初始化顯示字符
unsigned int m,pwm=50; //定義變量 m
int a,b,h,num1; //定義全局變量
//自定義字符
uchar code zifu[]={ //此數(shù)組內(nèi)數(shù)據(jù)為液晶上顯示波形符號的自定義字符
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]={ //此數(shù)組內(nèi)的數(shù)據(jù)為,da輸出對應電壓值對應的數(shù)字量,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個點, 所以數(shù)組內(nèi)是64個數(shù)據(jù)
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) //延時函數(shù)
{
int a,b;
for(a=xms;a0;a--)
for(b=110;b0;b--);
}
void write_com(uchar com) //寫命令函數(shù)
{
lcdrs=0;
P0=com;
delay(1);
lcden=0;
delay(1);
lcden=1;
}
void write_date(uchar date) //寫數(shù)據(jù)函數(shù)
{
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() //初始化函數(shù)
{
uchar i;
lcden=0; //默認開始狀態(tài)為關使能端,見時序圖
Lcd_ram();
write_com(0x01); //顯示清屏,將上次的內(nèi)容清除,默認為0x01.
write_com(0x0f);
write_com(0x38); //顯示模式設置,默認為0x38,不用變。
write_com(0x0c); //顯示功能設置0x0f為開顯示,顯示光標,光標閃爍;0x0c為開顯示,不顯光標,光標不閃
write_com(0x06); //設置光標狀態(tài)默認0x06,為讀一個字符光標加1.
write_com(0x80); //設置初始化數(shù)據(jù)指針,是在讀指令的操作里進行的
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() //定時器初始化函數(shù)
{
TMOD=0x01; //定時器的工作方式
TH0=a;
TL0=b; //定時器賦初值
EA=1; //打開中斷總開關
ET0=1; //打開定時器允許中斷開關
TR0=1; //打開定時器定時開關
}
void display() //顯示函數(shù)
{
uchar qian,bai,shi,ge; //定義變量用于顯示
qian=pinlv/1000; //將頻率值拆成一位的數(shù)據(jù),將數(shù)據(jù)除以1000,得到的商是一位數(shù),賦值給qian
bai=pinlv%1000/100; //將頻率除以1000的余數(shù)再除以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]); //顯示十位數(shù)
write_date('.'); //顯示小數(shù)點
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() //頻率調(diào)節(jié)鍵盤檢測函數(shù)
{
if(s1==0) //加按鍵是否按下
{
EA=0; //關閉中斷
while(!s1); //按鍵松開
pinlv+=bujin; //頻率以步進值加
if(pinlv1000) //最大加到100Hz
{
pinlv=100; //100Hz
}
display(); //顯示函數(shù)
m=65536-(15000/pinlv);//計算頻率
/*頻率值最小是10Hz,pinlv的值是100(因為要顯示小數(shù)點后一位),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得到百位,也就是頻率值的十位,因為有一個小數(shù)位
shi=bujin1%100/10; //將步進值除以100的余數(shù)除以十得到十位
ge=bujin1%100%10; //取余10后得到個位,也就是頻率步進值的小數(shù)點后一位
write_com(0x80+11); //選中液晶第一行第十一列
if(bai==0) //百位是否為0
write_date(' '); //百位不顯示
else //百位不為0
write_date(table[bai]); //顯示百位數(shù)據(jù)
write_date(table[shi]); //顯示十位數(shù)據(jù)
write_date('.'); //顯示小數(shù)點
write_date(table[ge]); //顯示個位,也就是小數(shù)點后一位
}
void bujinjiance() //步進值設置鍵盤程序
{
if(s4==0) //步進設置按鍵按下
{
delay(5); //延時去抖
if(s4==0) //再次判斷按鍵
{
while(!s4); //按鍵釋放,按鍵松開才繼續(xù)向下執(zhí)行
h++; //變量加
if(h==1) //進入設置狀態(tài)時
{
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(); //調(diào)用顯示程序
}
}
}
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() //主函數(shù)
{
init_lcd(); //調(diào)用初始化程序
m=65536-(15000/pinlv); //定時器初值
a=m/256;
b=m%256;
initclock(); //定時器初始化
led0=0; //點亮第一個波形指示燈
while(1) //進入while循環(huán),括號內(nèi)為1,一直成立,所以也叫死循環(huán),程序不會跳出,一直在內(nèi)執(zhí)行
{
if(h==0) //正常模式不是步進調(diào)節(jié)
{
keyscan(); //掃描按鍵
// display();
}
bujinjiance(); //掃描步進調(diào)節(jié)程序
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清零
//根據(jù)不同的初值,定時器定時時間不同,達到不同頻率的目的
}
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時分別產(chǎn)生正弦波、方波、三角波、梯形波、鋸齒波
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() //鍵盤掃描函數(shù)
{
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()//產(chǎn)生方波函數(shù)
{
EA=1; //開總中斷
ET0=1; //計時器T0工作
TMOD=1;
TR0=1; //開啟T0
TH0=0xff; //裝入初值
TL0=0x83;
}
void sin()//產(chǎn)生正弦波函數(shù)
{
unsigned inti;
do{
P2=sin_code[i];
i=i+1;
}while(i256);
}
void juchibo()//產(chǎn)生鋸齒波函數(shù)
{
P2=0x00;
do{
P2=P2+1;
}while(P2=0xff);
}
void sanjiaobo()//產(chǎn)生三角波函數(shù)
{
P2=0x00;
do{
P2=P2+1;
}while(P20xff);
P2=0xff;
do{
P2=P2-1;
}while(P20x00);
P2=0x00;
}
void tixingbo()//產(chǎn)生梯形波函數(shù)
{
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//中斷函數(shù)
{
P2=~P2; //取反
TH0=0xff; //重新裝入初值
TL0=0x83;
TR0=1;
}
/*主函數(shù)*/
void main()
{
init_DA0832();//初始化
do
{
flag=kscan();
}while(!flag);//等待按鍵按下
while(1)
{
switch(flag)
{
case 1:
do{
flag=kscan();
sin();
}while(flag==1);//產(chǎn)生正弦波
break;
case 2:
fangbo();
do{
flag=kscan();
}while(flag==2);//產(chǎn)生方波
TR0=0;
break;
case 3:
do{
flag=kscan();
sanjiaobo();
}while(flag==3);//產(chǎn)生三角波
break;
case 4:
do{
flag=kscan();
tixingbo();
}while(flag==4);//產(chǎn)生梯形波
break;
case 5:
do{
flag=kscan();
juchibo();
}while(flag==5);//產(chǎn)生鋸齒波
break;
default:
flag=kscan();
break;
}
}
}