|
使用硬件SPI的方式,和oz3717通信,用逻辑分析仪挂出来数据正常,但读取SPDAT寄存器一直读到0xFF;
硬件上外部有上拉电阻
IO口初始化部分
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.Mode = GPIO_PullUp;
GPIO_Inilize(GPIO_P2,&GPIO_InitStructure);
SPI初始化部分
SPI_InitTypeDef SPI_InitStructure;
SPI_SW(SPI_P22_P23_P24_P25); //SPI_P54_P13_P14_P15,SPI_P22_P23_P24_P25,SPI_P54_P40_P41_P43,SPI_P35_P34_P33_P32
SPI_InitStructure.SPI_Enable = ENABLE; //SPI启动 ENABLE, DISABLE
SPI_InitStructure.SPI_SSIG = ENABLE; //片选位 ENABLE, DISABLE
SPI_InitStructure.SPI_FirstBit = SPI_MSB; //移位方向 SPI_MSB, SPI_LSB
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主从选择 SPI_Mode_Master, SPI_Mode_Slave
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //时钟相位 SPI_CPOL_High, SPI_CPOL_Low
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据边沿 SPI_CPHA_1Edge, SPI_CPHA_2Edge
SPI_InitStructure.SPI_Speed = SPI_Speed_16; //SPI速度 SPI_Speed_4, SPI_Speed_8, SPI_Speed_16, SPI_Speed_2
SPI_Init(&SPI_InitStructure);
//NVIC_SPI_Init(ENABLE,Priority_3); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
SPI_MISO_2 = 1;
SPI_MOSI_2 = 1;
SPI_SCLK_2 = 1;
SPI_SS_2 = 1;
SPIF = 1; //清SPIF标志
WCOL = 1; //清WCOL标志
u8 SPI_WriteReadByte(u8 dat) //SPI发送和读取一个字节数据
{
SPDAT = dat;
while(SPIF == 0); //查询模式
SPIF = 1;
WCOL = 1;
return (SPDAT);
}
u8 spi_access(u8 number_bytes,u8 * dataout,u8 * datain){
u8 i,ret=0;
SPI_SS_2 = 0;
for(i=0;i<number_bytes;i++){
test_data = SPI_WriteByte(dataout);
datain=test_data;
}
SPI_SS_2 = 1;
return ret;
}
u8 spi_read_cmd(u8 cmd, u8 index, u8 read_number,u16 *buf){
u8 ret=0;
u8 btmp=0;
u8 pec_cal = 0;
u8 bdata_buf[6];
if(read_number > 16)
return 0;
if(read_number == 0)
return 0;
bdata_buf[0] = cmd | (read_number-1);
bdata_buf[1] = index;
bdata_buf[2] = 0xff;
bdata_buf[3] = 0xff;
ret = spi_access((u8)(read_number*2+1+2),bdata_buf,SPI_RxBuffer); //卡断点在此处读到全是0xff
pec_cal = sCall_CRC8(bdata_buf_read, (u8)(read_number*2+2));
if(pec_cal != bdata_buf_read[read_number*2+2]){
return 0;
}
if(buf != NULL)
{
for(btmp=0;btmp<read_number;btmp++)
{
//Index 0 store command, index 1 store index
buf[btmp] = bdata_buf_read[btmp*2+2] << 8;
buf[btmp] |= bdata_buf_read[btmp*2+1+2];
}
}
return 1;
}
u8 afe_register_read(u8 index, u8 read_number,u16 *buf)
{
u8 ret;
ret = spi_read_cmd(RD_REG_CMD, index, read_number, buf);
return ret;
}
最外层调用afe_register_read(0x00, 1,&wData)读取0x00寄存器,如下波形没有问题,SPI_WriteReadByte(u8 dat)这个函数返回值一直不正常,发啥读到的都是FF.
|
-
-
|