詳細說明:51單片機控制AD9851產(chǎn)生20HZ-1MHZ的正弦波,并且實現(xiàn)了按不同步進頻率掃屏-51 single-chip AD9851 produced control 20HZ-1MHZ the sine wave, and realize a different step frequency sweep screen
#include "reg51.h"
#include"stdio.h"
#define SELECT_LCD() CS0=0; CS1=1; CS2=0
#define SELECT_NONE() CS0=1; CS1=1; CS2=1
#define Set0(x) x=0
#define Set1(x) x=1
sbit CS0 = P2^5 ;
sbit CS1 = P2^6 ;
sbit CS2 = P2^7 ;
sbit Lcd_RW = P2^4 ;
sbit Lcd_RS = P2^3 ;
sbit DDS_FQ=P1^0;
sbit DDS_CLK=P1^1;
sbit DDS_DATA=P1^2;
unsigned long ddsseg[15]={1432,2147,2863,7158,14317,21475,71583,143166,214748,715829,1431656,2147484,42949672,57266231,71582788};
/********************** * 20 * 30 * 40 *100 * 200 * 300 * 1 K * 2 K * * 3 K * 10 K * 20 K * 30 K * 600 K * 800 K * 1 M *****/
/*****************************函數(shù)聲明************************************/
void LcdWrite(unsigned char value , bit RS);
unsigned char LcdRead(bit RS);
void LcdWriteCmd(unsigned char value , unsigned char attrib);
void LcdWriteData(unsigned char value);
unsigned char LcdReadData();
void LcdInit(void);
void LocateXY(char x , char y);
void PutChar(char x , char y , char value);
void PutStr(char x , char y , char* str);
/*--------------------------------------------------------------------------*/
/*------------------------------通用延時程序--------------------------------*/
void delay(unsigned int time) // time*0.5ms延時
{
unsigned int i,j;
for(j=0;j<time;j++)
{
for(i=0;i<60;i++)
{;}
}
}
/*--------------------------------------------------------------------*/
/*-----------------------------液晶顯示模塊---------------------------*/
void LcdWrite(unsigned char value , bit RS)
{
/* 1. 設(shè)置RS信號線 */
Lcd_RS = RS ;
/* 2. 設(shè)置RW信號線為讀 */
Lcd_RW = 0 ;
/* 3. 設(shè)置LCD片選 */
SELECT_LCD();
/* 4. 通過P0端口寫值 */
P0 = value ;
/* 5. 鎖存 */
SELECT_NONE();
}
/***************************************
說明: LCD讀函數(shù),完成LCD讀操作的物理時序
RS: 0 - 寫指令
1 - 寫數(shù)據(jù)
return: 讀出的具體值
*****************************************/
unsigned char LcdRead(bit RS)
{
unsigned char rt ;
/* 1. 設(shè)置RS信號線 */
Lcd_RS = RS ;
/* 2. 設(shè)置RW信號線為讀 */
Lcd_RW = 1 ;
/* 3. 釋放P0口 */
P0 = 0xff ;
/* 4. 設(shè)置LCD片選 */
SELECT_LCD();
SELECT_LCD();
SELECT_LCD();
SELECT_LCD();
/* 5. 通過P0端口讀值 */
rt = P0 ;
/* 6. 鎖存 */
SELECT_NONE();
return rt ;
}
/***************************************
說明: LCD寫指令
value: 寫的具體值
attrib: 0 - 不檢測忙信號
1 - 監(jiān)測忙信號
*****************************************/
void LcdWriteCmd(unsigned char value , unsigned char attrib)
{
/* 監(jiān)測忙狀態(tài) */
if(attrib)
while( LcdRead(0)&0x80 ) ;
LcdWrite(value , 0);
}
/***************************************
說明: LCD寫數(shù)據(jù)
*****************************************/
void LcdWriteData(unsigned char value)
{
/* 監(jiān)測忙狀態(tài) */
while( LcdRead(0)&0x80 ) ;
LcdWrite(value , 1);
}
/***************************************
說明: LCD讀數(shù)據(jù)
*****************************************/
unsigned char LcdReadData()
{
/* 監(jiān)測忙狀態(tài) */
while( LcdRead(0)&0x80 ) ;
return LcdRead(1);
}
/***************************************
說明: LCD初始化函數(shù),需要按照產(chǎn)品
說明書完成
*****************************************/
void LcdInit(void)
{
delay(800);
/* 寫指令0x38連續(xù)3次, 不檢測忙信號*/
LcdWriteCmd(0x38 , 0 );
delay(10);
LcdWriteCmd(0x38 , 0 );
delay(10);
LcdWriteCmd(0x38 , 0 );
delay(10);
/* 以后的讀寫操作均要判斷忙信號 */
LcdWriteCmd(0x38 , 1 );
/* 先設(shè)置關(guān)屏 */
LcdWriteCmd(0x08 , 1 );
/* 清屏 */
LcdWriteCmd(0x01 , 1 );
/* 設(shè)置光標顯示模式 */
LcdWriteCmd(0x06 , 1 );
/* 開屏并顯示光標 */
LcdWriteCmd(0x0c , 1 );
}
/***************************************
說明: 設(shè)置光標定位
*****************************************/
void LocateXY(char x , char y)
{
unsigned char temp;
temp = x & 0xf;
y &= 0x1;
if ( y )temp |= 0x40;
temp |= 0x80;
LcdWriteCmd(temp,1);
}
/***************************************
說明: 顯示字符
x: 橫坐標 y: 縱坐標
value: 顯示的字符
*****************************************/
void PutChar(char x , char y , char value)
{
LocateXY(x,y);
LcdWriteData(value);
}
/***************************************
說明: 顯示字符串
*****************************************/
void PutStr(char x , char y , char* str)
{
unsigned char i,len=0;
/* 計算字符串長度 */
while (str[len] >31)
len++;
/* 顯示字符串 */
for (i=0;i<len;i++)
{
PutChar(x++,y,str[i]);
if ( x == 16 )
{
x = 0; y ^= 1;
}
}
}
/*---------------------------------------------------------------------*/
/*--------------------------AD9851控制模塊-----------------------------*/
void Send(unsigned int control,unsigned long value)
{
unsigned char t;
DDS_FQ=0;
for(t=0;t<32;t++)//控制頻率
{
DDS_CLK=0;
if(value&0x01)
Set1(DDS_DATA);
else
Set0(DDS_DATA);
DDS_CLK=1;
value=value>>1;
}
for(t=0;t<8;t++)//控制狀態(tài)
{
DDS_CLK=0;
if(control&0x01)
Set1(DDS_DATA);
else
Set0(DDS_DATA);
DDS_CLK=1;
control=control>>1;
}
DDS_FQ=1;
}
void Init_DDS()
{
Send(0x01,0x00000000);
delay(10);
}
void main()
{
unsigned char i=0;
Init_DDS();
LcdInit();
PutStr(0,0,"DDS");
delay(2000);
while(1)
{
if(i==16)
i=0;
Send(0x01,ddsseg[i]);
switch(i)
{
case 0:
PutStr(0,0,"DDS +10HZ");
PutStr(0,1," 20 HZ");
break;
case 1:
PutStr(0,0,"DDS +10HZ");
PutStr(0,1," 30 HZ");
break;
case 2:
PutStr(0,0,"DDS +10HZ");
PutStr(0,1," 40 HZ");
break;
case 3:
PutStr(0,0,"DDS +100HZ");
PutStr(0,1," 100 HZ");
break;
case 4:
PutStr(0,0,"DDS +100HZ");
PutStr(0,1," 200 HZ");
break;
case 5:
PutStr(0,0,"DDS +100HZ");
PutStr(0,1," 300 HZ");
break;
case 6:
PutStr(0,0,"DDS +1KHZ");
PutStr(0,1," 1 KHZ");
break;
case 7:
PutStr(0,0,"DDS +1KHZ");
PutStr(0,1," 2 KHZ");
break;
case 8:
PutStr(0,0,"DDS +1KHZ");
PutStr(0,1," 3 KHZ");
break;
case 9:
PutStr(0,0,"DDS +10KHZ");
PutStr(0,1," 10 KHZ");
break;
case 10:
PutStr(0,0,"DDS +10KHZ");
PutStr(0,1," 20 KHZ");
break;
case 11:
PutStr(0,0,"DDS +10KHZ");
PutStr(0,1," 30 KHZ");
break;
case 12:
PutStr(0,0,"DDS +200KHZ");
PutStr(0,1," 600 KHZ");
break;
case 13:
PutStr(0,0,"DDS +200KHZ");
PutStr(0,1," 800 KHZ");
break;
case 14:
PutStr(0,0,"DDS +200KHZ");
PutStr(0,1," 1 MHZ");
break;
default: break;
}
i++;
delay(6000);
}
}