有没有写过AD9832的大神,来看看我程序问题在哪
时间:10-02
整理:3721RD
点击:
#include"AD9832.h"
/***********************************************************
函数名: WriteDataToAD9832
函数参数: uint uDATA
函数返回值: void
函数调用: WriteDataToAD9832()
函数说明: 写数据,出口数据 16 位
***********************************************************/
// unsigned int g_TempData=0x010624DF;
void ad9832_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
SCLK = 0;
// delay_ms(5);
FSYNC = 1;
SDATA = 1;
// Initialization();
// while(1)
// {
// void Init();
// // WriteNewF(g_TempData,0);
// // WriteDataToAD9832(0xA000); //SYNC = 1 SELSRC = 0
// // g_TempData++;
// delay_us(5);
// }
}
void WriteDataToAD9832(uint uDATA) //写数据
{
uchar i;
//P2 = 0xff;
FSYNC = 0; // 正在输入一个新字
// delay_ms(5);
for(i=0;i<16;i++)
{
SCLK = 0;
// delay_us(5);
if(uDATA & 0x8000)
{
SDATA=1;
}
else
{
SDATA=0;
}
// SCLK = 1;
// delay_ms(5);
uDATA <<= 1;
SCLK = 1;
// delay_us(5);
}
FSYNC = 1; //锁定,不做写操作
SDATA = 1;
// delay_ms(5);
}
/***********************************************************
函数名: Initialization
函数参数: void
函数返回值: void
函数调用: Initialization()
函数说明: Initialization
***********************************************************/
void Initialization(void)
{
// WriteDataToAD9832(0xD800); //reset 清 0
WriteDataToAD9832(0xF800); //reset 清 0
WriteDataToAD9832(0xB000); //SYNC = 1 SELSRC = 1
// WriteDataToAD9832(0xA000); //SYNC = 1 SELSRC = 0
WriteFrequencyDataToRegister(10,0); //初始化数据
WriteFrequencyDataToRegister(100000,1);
WritePhaseDataToRegister(0x0100,0);
WritePhaseDataToRegister(0x0100,1);
WritePhaseDataToRegister(0x0111,2);
WritePhaseDataToRegister(0x0101,3);
WriteDataToAD9832(0x6000); //选择频率寄存器0和相位寄存器0
WriteDataToAD9832(0xC000); //RESET = 0 CLR = 0 SET SLEEP = 0 命令开始输出
}
/***********************************************************
函数名: WriteFrequencyDataToRegister
函数参数: unsigned long fData,uchar Register
函数返回值: void
函数调用: WriteFrequencyDataToRegister()
函数说明: 写数据到频率寄存器 fData 单位是 度 (角度)
***********************************************************/
void WriteFrequencyDataToRegister(unsigned int fData,uchar Register) //Register = 0 or 1 fData is 32bits
{
uchar i;
uint DataAddr[4] = {0},reg[4] = {0};
unsigned long ulData = 0;
// ulData = fData * 130.3;
if(!Register)
{
DataAddr[0] = 0x2000;
DataAddr[1] = 0x3100;
DataAddr[2] = 0x2200;
DataAddr[3] = 0x3300;
}
else
{
DataAddr[0] = 0x3400;
DataAddr[1] = 0x2500;
DataAddr[2] = 0x3600;
DataAddr[3] = 0x2700;
}
for(i=0;i<4;i++)
{
reg[i] = ulData & 0x000000ff; //分4个字节写入寄存器
ulData >>= 8;
DataAddr[i] |= reg[i];
WriteDataToAD9832(DataAddr[i]);
}
}
/***********************************************************
函数名: WritePhaseDataToRegister
函数参数: uint fData,uchar Register
函数返回值: void
函数调用: WritePhaseDataToRegister()
函数说明: 写数据到相位寄存器
***********************************************************/
void WritePhaseDataToRegister(uint fData,uchar Register) //Register = 0 , 1, 2 ,3
{
uchar i;
uint DataAddr[2] = {0},reg[2] = {0};
// uint udata;
// udata = fData * 11.378;
switch(Register)
{
case 0 : DataAddr[0] = 0x1800;DataAddr[1] = 0x0900;break;
case 1 : DataAddr[0] = 0x1A00;DataAddr[1] = 0x0B00;break;
case 2 : DataAddr[0] = 0x1C00;DataAddr[1] = 0x0D00;break;
case 3 : DataAddr[0] = 0x1E00;DataAddr[1] = 0x0F00;break;
default: DataAddr[0] = 0x1800;DataAddr[1] = 0x0900;break;
}
for(i=0;i<2;i++)
{
reg[i] = fData & 0x00ff; //c0 c1 c2 c3 = 0 0 0 0
fData >>= 8;
DataAddr[i] |= reg[i];
WriteDataToAD9832(DataAddr[i]);
}
}
void WriteNewP(uint ulDATA,uchar Register)
{
WriteDataToAD9832(0xD000); //reset 清 0
WritePhaseDataToRegister(ulDATA,Register);
switch(Register)
{
case 0:WriteDataToAD9832(0x4000);break;//相位寄存器0
case 1:WriteDataToAD9832(0x4200);break;//相位寄存器1
case 2:WriteDataToAD9832(0x4400);break;//相位寄存器2
case 3:WriteDataToAD9832(0x4600);break;//相位寄存器3
default: break;
}
WriteDataToAD9832(0xC000); //reset 清 0 输出命令
}
void WriteNewF(unsigned int uiDATA,uchar Register)
{
WriteDataToAD9832(0xD000); //reset 清 0
WriteFrequencyDataToRegister(uiDATA,Register);
/*else*/ WriteDataToAD9832(0x5000); //选择频率寄存器0和相位寄存器0
WriteDataToAD9832(0xC000); //reset 置 0 输出命令
}
void Init()
{
Initialization();
}
/***********************************************************
函数名: WriteDataToAD9832
函数参数: uint uDATA
函数返回值: void
函数调用: WriteDataToAD9832()
函数说明: 写数据,出口数据 16 位
***********************************************************/
// unsigned int g_TempData=0x010624DF;
void ad9832_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); //使能PB,PE端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
SCLK = 0;
// delay_ms(5);
FSYNC = 1;
SDATA = 1;
// Initialization();
// while(1)
// {
// void Init();
// // WriteNewF(g_TempData,0);
// // WriteDataToAD9832(0xA000); //SYNC = 1 SELSRC = 0
// // g_TempData++;
// delay_us(5);
// }
}
void WriteDataToAD9832(uint uDATA) //写数据
{
uchar i;
//P2 = 0xff;
FSYNC = 0; // 正在输入一个新字
// delay_ms(5);
for(i=0;i<16;i++)
{
SCLK = 0;
// delay_us(5);
if(uDATA & 0x8000)
{
SDATA=1;
}
else
{
SDATA=0;
}
// SCLK = 1;
// delay_ms(5);
uDATA <<= 1;
SCLK = 1;
// delay_us(5);
}
FSYNC = 1; //锁定,不做写操作
SDATA = 1;
// delay_ms(5);
}
/***********************************************************
函数名: Initialization
函数参数: void
函数返回值: void
函数调用: Initialization()
函数说明: Initialization
***********************************************************/
void Initialization(void)
{
// WriteDataToAD9832(0xD800); //reset 清 0
WriteDataToAD9832(0xF800); //reset 清 0
WriteDataToAD9832(0xB000); //SYNC = 1 SELSRC = 1
// WriteDataToAD9832(0xA000); //SYNC = 1 SELSRC = 0
WriteFrequencyDataToRegister(10,0); //初始化数据
WriteFrequencyDataToRegister(100000,1);
WritePhaseDataToRegister(0x0100,0);
WritePhaseDataToRegister(0x0100,1);
WritePhaseDataToRegister(0x0111,2);
WritePhaseDataToRegister(0x0101,3);
WriteDataToAD9832(0x6000); //选择频率寄存器0和相位寄存器0
WriteDataToAD9832(0xC000); //RESET = 0 CLR = 0 SET SLEEP = 0 命令开始输出
}
/***********************************************************
函数名: WriteFrequencyDataToRegister
函数参数: unsigned long fData,uchar Register
函数返回值: void
函数调用: WriteFrequencyDataToRegister()
函数说明: 写数据到频率寄存器 fData 单位是 度 (角度)
***********************************************************/
void WriteFrequencyDataToRegister(unsigned int fData,uchar Register) //Register = 0 or 1 fData is 32bits
{
uchar i;
uint DataAddr[4] = {0},reg[4] = {0};
unsigned long ulData = 0;
// ulData = fData * 130.3;
if(!Register)
{
DataAddr[0] = 0x2000;
DataAddr[1] = 0x3100;
DataAddr[2] = 0x2200;
DataAddr[3] = 0x3300;
}
else
{
DataAddr[0] = 0x3400;
DataAddr[1] = 0x2500;
DataAddr[2] = 0x3600;
DataAddr[3] = 0x2700;
}
for(i=0;i<4;i++)
{
reg[i] = ulData & 0x000000ff; //分4个字节写入寄存器
ulData >>= 8;
DataAddr[i] |= reg[i];
WriteDataToAD9832(DataAddr[i]);
}
}
/***********************************************************
函数名: WritePhaseDataToRegister
函数参数: uint fData,uchar Register
函数返回值: void
函数调用: WritePhaseDataToRegister()
函数说明: 写数据到相位寄存器
***********************************************************/
void WritePhaseDataToRegister(uint fData,uchar Register) //Register = 0 , 1, 2 ,3
{
uchar i;
uint DataAddr[2] = {0},reg[2] = {0};
// uint udata;
// udata = fData * 11.378;
switch(Register)
{
case 0 : DataAddr[0] = 0x1800;DataAddr[1] = 0x0900;break;
case 1 : DataAddr[0] = 0x1A00;DataAddr[1] = 0x0B00;break;
case 2 : DataAddr[0] = 0x1C00;DataAddr[1] = 0x0D00;break;
case 3 : DataAddr[0] = 0x1E00;DataAddr[1] = 0x0F00;break;
default: DataAddr[0] = 0x1800;DataAddr[1] = 0x0900;break;
}
for(i=0;i<2;i++)
{
reg[i] = fData & 0x00ff; //c0 c1 c2 c3 = 0 0 0 0
fData >>= 8;
DataAddr[i] |= reg[i];
WriteDataToAD9832(DataAddr[i]);
}
}
void WriteNewP(uint ulDATA,uchar Register)
{
WriteDataToAD9832(0xD000); //reset 清 0
WritePhaseDataToRegister(ulDATA,Register);
switch(Register)
{
case 0:WriteDataToAD9832(0x4000);break;//相位寄存器0
case 1:WriteDataToAD9832(0x4200);break;//相位寄存器1
case 2:WriteDataToAD9832(0x4400);break;//相位寄存器2
case 3:WriteDataToAD9832(0x4600);break;//相位寄存器3
default: break;
}
WriteDataToAD9832(0xC000); //reset 清 0 输出命令
}
void WriteNewF(unsigned int uiDATA,uchar Register)
{
WriteDataToAD9832(0xD000); //reset 清 0
WriteFrequencyDataToRegister(uiDATA,Register);
/*else*/ WriteDataToAD9832(0x5000); //选择频率寄存器0和相位寄存器0
WriteDataToAD9832(0xC000); //reset 置 0 输出命令
}
void Init()
{
Initialization();
}