#include <reg52.h>
#include <intrins.h>
sbit TRIGGER =P2^0;
sbit SCLK =P2^1;
sbit SDIO =P2^2;
sbit CS =P2^3;
sbit RESET =P2^4;
void SET_CS()
{
CS=1; //P2.3->/CS, /CS=1
}
void CLR_CS()
{
CS=0; // /CS=0
}
void SET_SCLK()
{
SCLK=1; //P2.3->SCLK, SCLK=1
}
void CLR_SCLK()
{
SCLK=0; // SCLK=0
}
void SET_SDIO(void)
{
SDIO=1; //P2.2->SDIO, SDIO=1
}
void CLR_SDIO(void)
{
SDIO=0; //SDIO=0
}
void SET_Trigger(void)
{
TRIGGER=1; //P2.3->Trigger, Trigger=1
}
void CLR_Trigger(void)
{
TRIGGER=0; //Trigger=0
}
void SET_Reset(void)
{
RESET=1; //P2.3->Trigger, Trigger=1
}
void CLR_Reset(void)
{
TRIGGER=0; //Trigger=0
}
void delay (int length)
{
while (length>=0)
length--;
}
void spi_write_byte(unsigned char temp)
{
unsigned char i;
for(i=0;i<8;i++)//32
{
if(temp&0x80)
{
SET_SDIO(); //Send 1 to SDIO pin
}
else
{
CLR_SDIO(); //Send 0 to SDIO pin
}
delay(1);
SET_SCLK(); //SCLK Rising
delay(2);
CLR_SCLK(); //SCLK falling
delay(1);
temp<<= 1; //Rotate data
}
}
void WriteToAD9106(unsigned int Address, unsigned int Data)
{
CLR_CS();
CLR_SCLK();
delay(1);
spi_write_byte(Address >> 8);
spi_write_byte(Address);
spi_write_byte(Data >> 8);
spi_write_byte(Data);
SET_CS();
}
//void WriteToAD9106(unsigned int InstruAndData,unsigned int InstruAndData2)
//{
//unsigned int SendValue =0;
//unsigned int SendValue2 =0;
//unsigned int i = 0;
//SendValue = InstruAndData;
//SendValue2 = InstruAndData2;
//delay(1);
// //bring CS low
//CLR_SCLK();
//CLR_CS();
//delay(1);
//for(i=0;i<16;i++)//32
//{
// if(SendValue&0x8000)
// {
// SET_SDIO(); //Send 1 to SDIO pin
// }
// else
// {
// CLR_SDIO(); //Send 0 to SDIO pin
// }
// delay(1);
// SET_SCLK(); //SCLK Rising
// delay(2);
// CLR_SCLK(); //SCLK falling
// delay(1);
// SendValue <<= 1; //Rotate data
// }
//for(i=0;i<16;i++)//32
//{
// if(SendValue2&0x8000)
// {
// SET_SDIO(); //Send 1 to SDIO pin
// }
// else
// {
// CLR_SDIO(); //Send 0 to SDIO pin
// }
// delay(1);
// SET_SCLK(); //SCLK Rising
// delay(2);
// CLR_SCLK(); //SCLK falling
// delay(1);
// SendValue <<= 1; //Rotate data
// }
// SET_CS(); //bring CS high again
//}
void AD9106_Init()
{
SET_CS();
SET_Trigger();
CLR_Reset();
delay(14);
SET_Reset();
WriteToAD9106(0x001f,0x0000);//模式连续运行。
WriteToAD9106(0x000c,0x9f1f);
WriteToAD9106(0x0027,0x3131);//DDS输出,预存波形
WriteToAD9106(0x0037,0x00fc);//上斜坡锯齿波
WriteToAD9106(0x0035,0x4000);//Data=0x4000 Very important,the maximum value is 0x4000
WriteToAD9106(0x003e,0x006c);//Register 0x3E, Data=0x0E38
WriteToAD9106(0x003f,0xe600);//Register 0x3F, Data=0xE600 5MHz output,50MHz fsys
WriteToAD9106(0x001d,0x0001);//用新配置更新所有SPI设置(自清零)
WriteToAD9106(0x001e,0x0001);//PAT_STATUS 0x1E, run bit=1 Very important
WriteToAD9106(0x001d,0x0001);
delay(14);
CLR_Trigger();
WriteToAD9106(0x001d,0x0001);
}
void main()
{
//unsigned int InstruAndData_PatternType=0x001F0000; //Register 0x1F, Data=0x0000(Continuous)
//unsigned int InstruAndData_Wave4=0x00263131; //Register 0x26, Data=0x3131(wavefrom typ) DAC4 and DAC3 select the DDS prestore wave form.
//unsigned int InstruAndData_DGAIN4=0x00324000; //DAC4_DGAIN 0x32, Data=0x4000 Very important,the maximum value is 0x4000
//unsigned int InstruAndData_DGAIN3=0x00334000; //DAC3_DGAIN 0x33,
//unsigned int InstruAndData_DDSMSB=0x003E1999;//0x003E0E38; //Register 0x3E, Data=0x0E38
//unsigned int InstruAndData_DDSLSB=0x003F9A00;//0x003FE600; //Register 0x3F, Data=0xE600 10MHz output,180MHz fsys 10M=(FTW/2^24)*180M FTW=0XE38E6(932067.5555555556)
//unsigned int InstruAndData_DDS4_PW=0x00401300; //Register 0x40, DDS4 degree adjust the DDS4 and DDS3 to have the same phase
//unsigned int InstruAndData_Run=0x001E0001; //PAT_STATUS 0x1E, run bit=1 Very important
//unsigned int InstruAndData_update=0x001D3D70;//0x001D0001; //Register 0x1D, Data=0xE600
// AD9106_Init();
while(1)
{
// AD9106_Init();
WriteToAD9106(0x0003,0x003F);
//// WriteToAD9106(0x3203,0x982F);
//// WriteToAD9106(InstruAndData_PatternType);
// WriteToAD9106(0x001F,0x0000);
//// WriteToAD9106(InstruAndData_Wave4);
// WriteToAD9106(0x0026,0x3131);
//// WriteToAD9106(InstruAndData_DGAIN4);
// WriteToAD9106(0x0032,0x4000);
//// WriteToAD9106(InstruAndData_DGAIN3);
// WriteToAD9106(0x0033,0x4000);
//// WriteToAD9106(InstruAndData_DDSMSB);
// WriteToAD9106(0x003E,0x1999);
//// WriteToAD9106(InstruAndData_DDSLSB);
// WriteToAD9106(0x003F,0x9A00);
//// WriteToAD9106(InstruAndData_DDS4_PW);
// WriteToAD9106(0x0040,0x1300);
//// WriteToAD9106(InstruAndData_update);
// WriteToAD9106(0x001D,0x3D70);
//// WriteToAD9106(InstruAndData_Run);
// WriteToAD9106(0x001E,0x0001);
//// WriteToAD9106(InstruAndData_update);
// WriteToAD9106(0x001D,0x3D70);
// delay(10);
// CLR_Trigger();
//// WriteToAD9106(InstruAndData_update);
// WriteToAD9106(0x001D,0x3D70);
//// while(1);
//
}
}
|