主要函数读写寄存器的模拟时序
void write_register(int data)
{
int i=0;
cs_high;
cs_low;
for(i=0;i<16;i++)
{
sclk_high;
sdi_write((data & 0x8000)>0);//data & 0x8000取最高位
data<<=1;//数据左移一位
// Delay(3);
sclk_low;
// Delay(3);
}
for(i=0;i<32;i++)//还在配置寄存器后面的输出数据直接丢掉
{
sclk_high;
// Delay(3);
sclk_low;
// Delay(3);
}
cs_high;
Delay(5);
}
读寄存器
int read_register(int data)
{
int i=0;
int result=0;
data &=0xF000;
cs_low;
sdi_low;
Delay(3);
for(i=0;i<48;i++)//F+1
{
sclk_high;
// Delay(3);
sclk_low;
// Delay(3);
}
cs_high;
Delay(5);
cs_low;
for(i=0;i<48;i++)//F+2 configuration for F+3
{
sclk_high;
sdi_write((data & 0x8000)>0);//最高
data<<=1;
// Delay(3);
sclk_low;
// Delay(3);
}
sdi_low;
cs_high;
Delay(5);
cs_low;
F+2前16位读寄存器的配置后面32位没有用
for(i=0;i<16;i++)
{
sclk_high;
result|=sdoa_read;
result<<=1;
// Delay(3);
sclk_low;
// Delay(3);
}
for(i=0;i<32;i++)
{
sclk_high;
// Delay(5);
sclk_low;
}
cs_high;
Delay(5);
cs_low;
sdi_low;
for(i=0;i<48;i++)//F+3一个延时稳定的作用
{
sclk_high;
// Delay(3);
sclk_low;
// Delay(3);
}
cs_high;
Delay(5);
return (result>>1);
}
以及数据读取
void read_data(void)
{
int i=0;
unsigned char temp = 0;
cs_low;
sdi_low;
Delay(3);
for(i=0;i<16;i++)
{
sclk_high;//直接拉高拉低把数据丢掉
sclk_low;
}
temp = 0;//前16位不要
for(i=0;i<8;i++)
{
sclk_high;
temp<<=1;
temp|=sdoa_read;
sclk_low;
}
result_temp[0] = temp;//读了8位一个字节的数据
temp = 0;
for(i=0;i<8;i++)
{
sclk_high;
temp<<=1;
temp|=sdoa_read;
sclk_low;
}
result_temp[1] = temp;
temp = 0;
for(i=0;i<8;i++)
{
sclk_high;
temp<<=1;
temp|=sdoa_read;
sclk_low;
}
result_temp[2] = temp;
temp = 0;
for(i=0;i<8;i++)
{
sclk_high;
temp<<=1;
temp|=sdoa_read;
sclk_low;
}
result_temp[3] = temp;
cs_high;
Delay(3);
}
//16位数据读完了
下面是拼数部分:
把4包数据拼成一包发出来,并转化为偏移码
read_data();
uartTxData[timer_sample*4+0] = result_temp[0]^0x80;//最高位取反求偏移码
uartTxData[timer_sample*4+1] = result_temp[1];
uartTxData[timer_sample*4+2] = result_temp[2]^0x80;
uartTxData[timer_sample*4+3] = result_temp[3];
timer_sample++;
if(timer_sample>3)//0 1 2 3共4包数据
{
flag = 1;
timer_sample = 0;
led ^= LED_ON;
PORT1_Write(led);
}