新手入门
- 积分
- 6
- 金钱
- 6
- 注册时间
- 2020-8-1
- 在线时间
- 1 小时
|
问题:SDO和SDI在示波器上波形显示正确,但是读SBUF的时候一直是0!急急急!求助!跪谢大佬!
SPI时序:ADXL345要求CPOL=1,CPHA=1,我在PIC单片机设置SSP1STATbits.CKE = 0;SSP1STATbits.SMP = 0;SSP1CON1 = 0x32;代码:
往从机寄存器里写值,第一个字节发地址,第二个字节发要写的值:
void adxl345_set_register_value(uint8_t register_address,uint8_t register_value)
{
uint8_t data_buffer[2] = {0, 0};
data_buffer[0] = ADXL345_SPI_WRITE & register_address;
data_buffer[1] = register_value;
SPI_Write(DEVID,data_buffer,2);
}
读从机寄存器的值,第一个字节先发地址,第二个字节发寄存器地址,读回来第一个字节无效,第二个字节是需求的寄存器值:
uint8_t adxl345_get_register_value(uint8_t register_address)
{
uint8_t data_buffer[2] = {0,0};
uint8_t register_value = 0;
data_buffer[0] = (ADXL345_SPI_READ |register_address) ;
data_buffer[1] = 0xFF;
register_value = SPI_Read(DEVID,data_buffer,2);
return register_value;
}
SPI写函数:
unsigned char SPI_Write(unsigned char slaveDeviceId,
unsigned char* data,
unsigned char bytesNumber)
{
unsigned char byte = 0;
unsigned char tempByte = 0;
if(slaveDeviceId == 1)
{
SPI_CS_LOW;
}
for(byte = 0; byte < bytesNumber; byte++)
{
SSP1BUF = data[byte];
while(SSPSTATbits.BF ==0);
while(PIR1bits.SSP1IF == 0);
tempByte = SSP1BUF;
SSP1IF = 0;
}
if(slaveDeviceId == 1)
{
SPI_CS_HIGH;
}
return bytesNumber;
}
SPI读函数:
unsigned char SPI_Read(unsigned char slaveDeviceId,
unsigned char* data,
unsigned char bytesNumber)
{
unsigned char byte = 0;
unsigned char writeBuffer[2] = {0, 0};
unsigned char receiveBuffer[2] = {0 ,0};
unsigned char reg_value;
for(byte = 0; byte < bytesNumber; byte++)
{
writeBuffer[byte] = data[byte];
}
if(slaveDeviceId == 1)
{
SPI_CS_LOW;
}
for(byte = 0; byte < bytesNumber; byte++)
{
SSP1BUF = writeBuffer[byte];
while(SSPSTATbits.BF ==0);
while(PIR1bits.SSP1IF == 0);
receiveBuffer[byte] = SSP1BUF;
SSP1IF = 0;
}
reg_value = receiveBuffer[1];
if(slaveDeviceId == 1)
{
SPI_CS_HIGH;
}
return reg_value;
}
主函数:
void main(void)
{
delayMs(100); //上电延时
Init_CPU();
SPI_Init();
Init_ADXL345(); //给ADXL345_OFSY寄存器写0x05
int8_t aa = 1;
while(1)
{
RA5_PIN_OUT;
RA5_PIN_LOW;
while(aa!=0x05)
{
aa = adxl345_get_register_value(ADXL345_OFSY);
}
RA5_PIN_HIGH;
}
}
SDO上可以看到ADXL345_OFSY这个寄存器的地址,SDI可以看到返回来的值0x05!但是aa的值一直是0!!!跳不出循环,RA5这个引脚无法拉高,这是为什么啊!!
|
|