#include "Ad7793.h"
void Dly(unsigned int di)
{
unsigned int di1;
for(di1=0;di1<di;di1++);
}
void Ad7793Reset(void) //Ad7793复位
{
unsigned char i;
SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST);
PRT1DR&=~0x80; //cs=0
for(i=0;i<5;i++)
{
while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
SPIM_SendTxData(0xff);
}
PRT1DR|=0x80; //cs=1
Dly(10);
}
void Ad7793Write(unsigned char iReg,unsigned char *iTxbuff,unsigned char iLen)//将iTxbuff数据写入iAddress地址开始iLen字节
{
unsigned char i;
SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST);
PRT1DR&=~0x80; //cs=0
Dly(500);
while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
SPIM_SendTxData(iReg);
Dly(100);
for(i=0;i<iLen;i++)
{
while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
SPIM_SendTxData(iTxbuff);
}
Dly(500);
PRT1DR|=0x80; //cs=1
}
void Ad7793Read(unsigned char iReg1,unsigned char *iRxbuff,unsigned char iLen1)
{
unsigned char i;
unsigned char i1;
//PRT1GS=0x4c;
i1=iReg1|0x40;
Dly(500);
SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST);
PRT1DR&=~0x80; //cs=0
while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
SPIM_SendTxData(i1);
Dly(100);
SPIM_Start(SPIM_SPIM_MODE_3 | SPIM_SPIM_MSB_FIRST);
for(i=0;i<iLen1;i++)
{
while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
SPIM_SendTxData(0);
while( ! (SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ) );
iRxbuff=SPIM_bReadRxData();
}
Dly(100);
PRT1DR|=0x80; //cs=1
//PRT1GS=0x0c; //hz dout
}
void InisAd7793(void)
{
unsigned char iTx[3];
iTx[0]=0x03;
Ad7793Write(IoReg,iTx,1); //配置1Ma电流输出
iTx[0]=0x00;
iTx[1]=0x0A;
Ad7793Write(ModReg,iTx,2); //设置连续转换模式,16.7HZ
iTx[0]=0x30;
iTx[1]=0x07;
Ad7793Write(ConfigReg,iTx,2);
PRT1GS=0x0c; //hz out
}
用户377235 2013-7-30 13:07
用户1168699 2012-9-29 18:34
用户1360014 2009-11-11 09:39
bjshion_407728712 2008-11-14 18:11