|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include "stm32f10x.h"
#include "stdio.h"
#include "AD5235.h"
#include "delay.h"
void AD5235Initialization(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);
/*AD1-2片选引脚初始化*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_15);//AD1-2片选初始拉高
/*AD19-20片选引脚初始化*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_9);//AD19-20片选初始拉高
/*RDY引脚初始化*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_8);//RDY初始拉低
/* 模拟SPI1pins: AD5235_CLK(PA5), AD5235_SDO(PA7) and AD5235_DIN(PA6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
AD5235_CLK=1;
AD1_2=1;
AD5235_DIN=1;
// RDY=0;
}
void AD5235Command(unsigned int Command, u32 *Value)
{
unsigned char i;
u32 Register,Read;
Read=0x00000000;
Register=0x000000;
switch (Command)
{
case CMD_NOP:
Register=CMD_NOP;
break;
case WRITE_TO_RDAC1:
Register=WRITE_TO_RDAC1 | ((*Value)&0xFF3FFF); //将数据写入RDAC1 Register
break;
case WRITE_TO_RDAC2:
Register=WRITE_TO_RDAC2 | ((*Value)&0xFF3FFF); //将数据写入RDAC2 Register
break;
case Restore_EEMEM1_to_RDAC1:
Register=Restore_EEMEM1_to_RDAC1;
break;
case Restore_EEMEM2_to_RDAC2:
Register=Restore_EEMEM2_to_RDAC2 ;
break;
case RDAC1_Decrement_6dB:
Register=RDAC1_Decrement_6dB ;
break;
case RDAC2_Decrement_6dB:
Register=RDAC2_Decrement_6dB ;
break;
case ALL_RDAC_Decrement_6dB:
Register=ALL_RDAC_Decrement_6dB ;
break;
case RDAC1_Decrement_1:
Register=RDAC1_Decrement_1 ;
break;
case RDAC2_Decrement_1:
Register=RDAC2_Decrement_1 ;
break;
case ALL_RDAC_Decrement_1:
Register=ALL_RDAC_Decrement_1 ;
break;
case Refresh_RDAC:
Register=Refresh_RDAC ;
break;
case Read_RDAC1:
Register=Read_RDAC1 ;
break;
case Read_RDAC2:
Register=Read_RDAC2 ;
break;
case RDAC1_Increment_6dB:
Register=RDAC1_Increment_6dB ;
break;
case RDAC2_Increment_6dB:
Register=RDAC2_Increment_6dB ;
break;
case ALL_RDAC_Increment_6dB:
Register=ALL_RDAC_Increment_6dB ;
break;
case RDAC1_Increment_1:
Register=RDAC1_Increment_1 ;
break;
case RDAC2_Increment_1:
Register=RDAC2_Increment_1 ;
break;
case ALL_RDAC_Increment_1:
Register=ALL_RDAC_Increment_1 ;
break;
case Stores_RDAC2_into_EEMEM2:
Register=Stores_RDAC2_into_EEMEM2 ;
break;
case Stores_RDAC1_into_EEMEM1:
Register=Stores_RDAC1_into_EEMEM1 ;
break;
default:
break;
}
AD1_2=0;
AD5235_CLK=1;
delay_us(5);
for (i=0; i<24; i++)
{
// AD5235_DIN=(Register&0x800000);
if ((Register&0x800000) == 0x800000)
AD5235_DIN=1;
else
AD5235_DIN=0;
delay_us(10);
AD5235_CLK=0;
delay_us(10);
AD5235_CLK=1;
delay_us(10);
Read<<=1;
if (AD5235_SDO)
Read|=0x000001;
Register<<=1;
}
AD5235_CLK=1;
delay_us(20);
AD1_2=1;
delay_us(40);
AD1_2=0;
delay_us(40);
AD1_2=1;
delay_us(50);
}
void AD5235Cmd(unsigned int Cmd)
{
unsigned char i;
u32 Register;
Register=0x000000;
switch (Cmd)
{
case CMD_NOP:
Register=CMD_NOP;
break;
case Restore_EEMEM1_to_RDAC1:
Register=Restore_EEMEM1_to_RDAC1;
break;
case Restore_EEMEM2_to_RDAC2:
Register=Restore_EEMEM2_to_RDAC2 ;
break;
case RDAC1_Decrement_6dB:
Register=RDAC1_Decrement_6dB ;
break;
case RDAC2_Decrement_6dB:
Register=RDAC2_Decrement_6dB ;
break;
case ALL_RDAC_Decrement_6dB:
Register=ALL_RDAC_Decrement_6dB ;
break;
case RDAC1_Decrement_1:
Register=RDAC1_Decrement_1 ;
break;
case RDAC2_Decrement_1:
Register=RDAC2_Decrement_1 ;
break;
case ALL_RDAC_Decrement_1:
Register=ALL_RDAC_Decrement_1 ;
break;
case Refresh_RDAC:
Register=Refresh_RDAC ;
break;
case Read_RDAC1:
Register=Read_RDAC1 ;
break;
case Read_RDAC2:
Register=Read_RDAC2 ;
break;
case RDAC1_Increment_6dB:
Register=RDAC1_Increment_6dB ;
break;
case RDAC2_Increment_6dB:
Register=RDAC2_Increment_6dB ;
break;
case ALL_RDAC_Increment_6dB:
Register=ALL_RDAC_Increment_6dB ;
break;
case RDAC1_Increment_1:
Register=RDAC1_Increment_1 ;
break;
case RDAC2_Increment_1:
Register=RDAC2_Increment_1 ;
break;
case ALL_RDAC_Increment_1:
Register=ALL_RDAC_Increment_1 ;
break;
default:
break;
}
AD5235_CLK=1;
delay_us(1);
//RDY=1;
//delay_us(5);
AD1_2=0;
delay_us(1);
for (i=0; i<24; i++)
{
if ((Register&0x800000) == 0x800000)
AD5235_DIN=1;
else
AD5235_DIN=0;
delay_us(2);
AD5235_CLK=0;
delay_us(2);
AD5235_CLK=1;
Register<<=1;
}
AD5235_CLK=1;
delay_us(2);
AD1_2=1;
delay_us(2);
// RDY=0;
// delay_us(5);
AD1_2=0;
delay_us(2);
// RDY=1;
// delay_us(5);
AD1_2=1;
delay_us(10);
}
u32 AD5235_value_to_Hex(u32 Rwb)//输入的阻值转化为AD5235的10位16进制数据码
{
return (u32)((Rwb-300)*1024/250000);
}
void AD5235_DATA(u32 AD5235_zhi)
{
u32 AD5235HexValue;
AD5235HexValue = AD5235_value_to_Hex(AD5235_zhi);
AD5235Command(WRITE_TO_RDAC1,&AD5235HexValue);
delay_ms(3000);
//AD5235Command(Stores_RDAC1_into_EEMEM1,&AD5235HexValue);
//delay_ms(100);
AD5235Command(WRITE_TO_RDAC2,&AD5235HexValue);
//delay_us(50);
delay_us(100);
// AD5235Command(Stores_RDAC2_into_EEMEM2,&AD5235HexValue);
delay_us(100);
// AD5235Command(Read_RDAC1,&AD5235HexValue);
delay_us(10);
// AD5235Command(CMD_NOP,&AD5235HexValue);
} |
|