如图我现在用单片机直接读取KXR94的值,但是读出的结果不对,程序如下,此外我LTC1865有没有朋友用过,能否给我一个驱动程序,我使用AD试试也行
UCHAR8 KXR94_read_write(UCHAR8 cnt)
{
UCHAR8 sum = ZERO;
UCHAR8 i;
for(i=0; i<8; i++)
{
//CLR_SPI_SCK;
CLR_KXR94_SCK;
//准备好写入的数据
if(cnt&(1<<(7-i))) SET_KXR94_DO;
else CLR_KXR94_DO;
delay_us(NINE);
SET_KXR94_SCK;
//SET_SPI_SCK;//上升沿写入,同时miso在上升沿已经输出数据
//读入数据
if(Read_KXR94_DI) sum |= (1<<(7-i));
delay_us(NINE);
//CLR_SPI_SCK;
CLR_KXR94_SCK;
}
return sum;
}
void spi_write(unsigned char spi_dat)
{
unsigned char i;
for (i=0;i<8;i++)
{
CLR_KXR94_SCK;
if((spi_dat & 0x80)==0x80)SET_KXR94_DO;
else CLR_KXR94_DO;
SET_KXR94_SCK;
spi_dat=(spi_dat<<1);
}
}
/********************************
从SPI器件读出一个字节数据
********************************/
unsigned char spi_read()
{
unsigned char i,spi_dat;
for (i=0;i<8;i++)
{
CLR_KXR94_SCK;
spi_dat=(spi_dat<<1);
SET_KXR94_SCK;
if(Read_KXR94_DI)spi_dat|=0x01;
else spi_dat &= ~0x01;
}
return spi_dat;
}
void KXR94_CHANGE_chanel(UCHAR8 aix)
{
CLR_KXR94_CS;
delay_us(5);
KXR94_read_write(0x04);//command to write to the control register
delay_us(40);
KXR94_read_write(0x04);//enable bit in the internal register
SET_KXR94_CS;
delay_us(100);
CLR_KXR94_CS;
KXR94_read_write(aix);//the command to convert theX-channel
delay_us(100);
if(aix == 0x00)
{
KXR94.DATA_AXIS_X[1] = KXR94_read_write(0x00);//placeholders
delay_us(100);
KXR94.DATA_AXIS_X[0] = KXR94_read_write(0x00);
}
else if(aix == 0x01)
{
KXR94.DATA_AXIS_Y[1] = KXR94_read_write(0x00);//placeholders
delay_us(100);
KXR94.DATA_AXIS_Y[0] = KXR94_read_write(0x00);
}
else if(aix == 0x02)
{
KXR94.DATA_AXIS_Z[1] = KXR94_read_write(0x00);//placeholders
delay_us(100);
KXR94.DATA_AXIS_Z[0] = KXR94_read_write(0x00);
}
SET_KXR94_CS;
}
void READ_AIXS_CYCLE(void)
{
UCHAR8 REC_CONTS;
KXR94_CHANGE_chanel(0x00); // aixs_x
KXR94_CHANGE_chanel(0x01); // aixs_Y
KXR94_CHANGE_chanel(0x02); // aixs_Z
}转载
|