本帖最后由 ppp_1978 于 2019-10-29 22:30 编辑
pa15 - CS,pb3 - pb5设置为SPI0读取外部设备,三个IO上用示波器都能看到数据,但是的spi0_data读出来始终为0xFF,硬件没有问题,同样使用这几个IO模拟SPI读写都没有问题,求帮助!
void RCU_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_SPI0);
}
void GPIO_Init(void)
{
gpio_mode_set(GPIOA,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,iCS_PinA15);
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP,GPIO_OSPEED_10MHZ,iCS_PinA15);
gpio_af_set(GPIOB,GPIO_AF_0,iSCK_PinB3|iMISO_PinB4|iMOSI_PinB5); gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,iSCK_PinB3|iMISO_PinB4|iMOSI_PinB5); gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,iSCK_PinB3|iMISO_PinB4|iMOSI_PinB5);
}
void SPI0_Init(void)
{
spi_parameter_struct spi_init_struct;
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_8;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI0,&spi_init_struct);
spi_enable(SPI0);
}
#define uchar unsigned char
#define SPI0_Enable() gpio_bit_reset(GPIOA,iCS_PinA15)
#define SPI0_Disable() gpio_bit_set(GPIOA,iCS_PinA15)
void SPI0_RegByte_Write(uchar addr_reg,uchar data_reg)
{
SPI0_Enable();
while(spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) == 0);
spi_i2s_data_transmit(SPI0,addr_reg);
while(spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) == 0);
spi_i2s_data_receive(SPI0);
while(spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) == 0);
spi_i2s_data_transmit(SPI0,data_reg);
while(spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) == 0);
spi_i2s_data_receive(SPI0);
SPI0_Disable();
}
uchar SPI0_RegByte_Read(uchar addr_reg)
{
uchar mSPI0_Temp;
SPI0_Enable();
while(spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) == 0);
spi_i2s_data_transmit(SPI0,addr_reg|0x80);
while(spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) == 0);
mSPI0_Temp = spi_i2s_data_receive(SPI0);
while(spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) == 0);
spi_i2s_data_transmit(SPI0);
while(spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) == 0);
mSPI0_Temp = spi_i2s_data_receive(SPI0);
ISPI0_Disable();
return mSPI0_Temp;
}
|