请问有人用过STM32F405RGTX芯片的GPIOB12~15作为SPI通讯的吗?
项目中用这四个口分别作为四线SPI的CS,SCK,MISO和MOSI,程序也是参见开发板SPI例程中的例子改写的,其中:
spi.c:
void SPI2_Init(void)
{
u16 tempreg=0;
RCC->AHB1ENR|=1<<0;
//RCC->APB2ENR|=1<<12; //SPI1
---------> RCC->APB1ENR|=1<<14; //SPI2
//GPIO_Set(GPIOB,0x7<<3,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU); //PB3~5
//GPIO_AF_Set(GPIOB,3,5); //PB3,AF5
//GPIO_AF_Set(GPIOB,4,5); //PB4,AF5
//GPIO_AF_Set(GPIOB,5,5); //PB5,AF5
------->GPIO_Set(GPIOB,0x7<<13,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU); //PB13~15
------->GPIO_AF_Set(GPIOB,13,5); //PB13,AF5
-------> GPIO_AF_Set(GPIOB,14,5); //PB14,AF5
-------> GPIO_AF_Set(GPIOB,15,5); //PB15,AF5
//RCC->APB2RSTR|=1<<12; //
//RCC->APB2RSTR&=~(1<<12);//
-------> RCC->APB1RSTR|=1<<14; //
------>RCC->APB1RSTR&=~(1<<14);//
tempreg|=0<<10; //
tempreg|=1<<9; //
tempreg|=1<<8;
tempreg|=1<<2; //
tempreg|=0<<11; //
tempreg|=1<<1; //
tempreg|=1<<0; //
//
tempreg|=7<<3; //Fsck=Fpclk1/256
tempreg|=0<<7; //MSB First
tempreg|=1<<6; //
//SPI1->CR1=tempreg; //
//SPI1>I2SCFGR&=~(1<<11);//
//SPI1_ReadWriteByte(0xff);//
-------> SPI2->CR1=tempreg; //
-------> SPI2->I2SCFGR&=~(1<<11);//
-------> SPI2_ReadWriteByte(0xff);//
}
/*
void SPI1_SetSpeed(u8 SpeedSet)
{
SpeedSet&=0X07;
SPI1->CR1&=0XFFC7;
SPI1->CR1|=SpeedSet<<3;
SPI1->CR1|=1<<6;
} */
void SPI2_SetSpeed(u8 SpeedSet)
{
SpeedSet&=0X07;
SPI2->CR1&=0XFFC7;
SPI2->CR1|=SpeedSet<<3;
SPI2->CR1|=1<<6;
}
/*
u8 SPI1_ReadWriteByte(u8 TxData)
{
while((SPI1->SR&1<<1)==0);
SPI1->DR=TxData;
while((SPI1->SR&1<<0)==0);
return SPI1->DR;
}*/
u8 SPI2_ReadWriteByte(u8 TxData)
{
while((SPI2->SR&1<<1)==0);
SPI2->DR=TxData;
while((SPI2->SR&1<<0)==0);
return SPI2->DR;
}
PB12(SPICS)配置如下:
RCC->AHB1ENR |= 1<<1;
GPIO_Set(GPIOB,PIN12,GPIO_MODE_OUT,GPIO_OTYPE_OD,GPIO_SPEED_50M,GPIO_PUPD_PU); //PB12(SPI_CS)
#define MAX21100_CS PBout(12)
读芯片ID函数:
u8 MAX21100_ReadID(void)
{
u8 temp = 0;
u8 temp1 = -2;
MAX21100_CS = 0; //SPI_CS=0, select chip
temp1 = SPI2_ReadWriteByte(MAX21100_WHO_AM_I);
temp = SPI2_ReadWriteByte(0xFF);
MAX21100_CS = 1; //SPI_CS=1, deselect chip
return temp;
}
个人感觉这样应该可以正常通信了,配置应无错误。但是实际情况是总是读出0xFF。不知是哪里有没理解到的地方。 |