想做一个ADE7758的SPI通信驱动程序,用的STVD软件库函数,模拟SPi读写ADE7758寄存器,程序如下:
void SPI_Write(unsigned char type,unsigned long int wdata,unsigned char databit)
{
unsigned char loop = 0;
type = type & 0x7F;
type = type | 0x80;
GPIO_WriteLow(SCLK); //时钟
GPIO_WriteLow(NCS); //片选信号
for(loop = 0; loop < 8; loop++)
{
GPIO_WriteHigh(SCLK);
nop();
if(type&0x80)
GPIO_WriteHigh(DIN);
else
GPIO_WriteLow(DIN);
nop();
GPIO_WriteLow(SCLK);
nop();
type = (type << 1);
nop();
}//end of for()
delayus(10);
for(loop = 0; loop < databit; loop++)
{
GPIO_WriteHigh(SCLK);
nop();
if(wdata&0x800000)//24位
GPIO_WriteHigh(DIN);
else
GPIO_WriteLow(DIN);
nop();
GPIO_WriteLow(SCLK);
nop();
wdata = (wdata << 1);
nop();
}//end of for()
GPIO_WriteHigh(NCS);
}//end of write7758()
unsigned long SPI_Read(unsigned char type,unsigned char databit)
{
unsigned char loop;
unsigned long rtdata = 0;
type = type & 0x7F;
type = type | 0x00;
GPIO_WriteLow(SCLK);
GPIO_WriteLow(NCS);
for(loop = 0;loop < 8;loop ++)
{
GPIO_WriteHigh(SCLK);
nop();
if(type&0x80)
GPIO_WriteHigh(DIN);
else
GPIO_WriteLow(DIN);
nop();
GPIO_WriteLow(SCLK);
nop();
type = (type << 1);
nop();
}
delayus(10);
for(loop = 0;loop < databit;loop ++)
{ rtdata = (rtdata << 1);
GPIO_WriteHigh(SCLK);
nop();
if(GPIO_ReadInputPin(DOUT))
rtdata |=0x01;
nop();
GPIO_WriteLow(SCLK);
nop();
}
GPIO_WriteHigh(NCS);
return(rtdata);
}//end of read7758()
同时读写3个电压增益寄存器,寄存器为有符号12位
SPI_Write(0x24,0x055500,16); //写相位A的电压增益寄存器
SPI_Write(0x25,0x055500,16); //写相位B的电压增益寄存器
SPI_Write(0x26,0x055500,16); //写相位C的电压增益寄存器
DATA[0]= SPI_Read(0x24,16);
DATA[1]= SPI_Read(0x25,16);
DATA[2]= SPI_Read(0x26,16);
仿真了一下得的结果不一样
DATA[0]=1365
DATA[1]=1195
DATA[2]=1365
A、C相数是对的,B相不对,读电流增益寄存器正好相反B相是对的,AC错了,是时序有问题还是其他原因,求帮助。
|