void AD7793_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
AD7793_CS_SET();
}
void WriteToReg(unsigned char ByteData)
{
unsigned char temp;
unsigned char i;
AD7793_CS_CLR();
temp=0x80;
for(i=0;i<8;i++)
{
if((temp & ByteData)==0)
{
AD7793_DIN_CLR();
}
else
{
AD7793_DIN_SET();
}
AD7793_SCLK_CLR();
Delay(200);
AD7793_SCLK_SET();
Delay(200);
temp=temp>>1;
}
AD7793_CS_SET();
}
void AD7793_Reset(void) //复位AD7793
{
unsigned int ResetTime;
ResetTime=32;
AD7793_SCLK_SET();
AD7793_CS_CLR();
AD7793_DIN_SET();
while(ResetTime--)
{
WriteToReg(0xff);
Delay(100);
AD7793_SCLK_CLR();
Delay(100);
AD7793_SCLK_SET();
}
AD7793_CS_SET();
}
unsigned char AD7793_ReadStatusRegister(void) //读状态寄存器
{
unsigned char j;
unsigned char temp;
WriteToReg(0x40);
AD7793_DIN_SET();
AD7793_CS_CLR();
temp=0;
AD7793_DOUT_SET()
for(j=0; j<8; j++)
{
AD7793_SCLK_CLR();
AD7793_DOUT_SET()
if(AD7793_DOUT_GET()==0)
{
temp=temp<<1;
}else
{
temp=temp<<1;
temp=temp+0x01;
}
Delay(200);
AD7793_SCLK_SET();
Delay(200);
}
AD7793_CS_SET();
return temp;
}
void Ad7793_WriteModeRegister(unsigned char ModeRegisterH,unsigned char ModeRegisterL) //写模式寄存器
{
WriteToReg(0x08);
WriteToReg(ModeRegisterH);
WriteToReg(ModeRegisterL);
}
void Ad7793_WriteConfigRegister(unsigned char ConfigRegisterH,unsigned char ConfigRegisterL) //写配置寄存器
{
WriteToReg(0x10);
WriteToReg(ConfigRegisterH);
WriteToReg(ConfigRegisterL);
}
void Ad7793_WriteIORegister(unsigned char IORegister) //写IO寄存器
{
WriteToReg(0x28);
WriteToReg(IORegister);
}
long AD7793_ReadDataRegister(void) //读数据寄存器
{
union long_4uchar AD7793Result;
unsigned char i,j;
unsigned char temp;
temp=AD7793_ReadStatusRegister();
while((temp&0x80)==0x80)
{
temp=AD7793_ReadStatusRegister();
}
WriteToReg(0x58);
AD7793_DIN_SET();
AD7793_CS_CLR();
AD7793_DOUT_SET()
for(i=0; i<3; i++)
{
for(j=0; j<8; j++)
{
AD7793_SCLK_CLR();
AD7793_DOUT_SET()
if(AD7793_DOUT_GET()==0)
{
temp=temp<<1;
}else
{
temp=temp<<1;
temp=temp+0x01;
}
Delay(200);
AD7793_SCLK_SET();
Delay(200);
}
AD7793Result._4byte._uchar[3-i]=temp;
}
AD7793_CS_SET();
AD7793Result._long=AD7793Result._long>>17;
return AD7793Result._long;
}
void Init_AD7793(void)
{
AD7793_GPIO_Config();
AD7793_Reset();
Ad7793_WriteModeRegister(0x00,0x0a);
Ad7793_WriteConfigRegister(0x1A,0x10);
Ad7793_WriteIORegister(0x03);
}
...查看更多详情
|