本帖最后由 jinyi7016 于 2016-4-3 17:43 编辑
使用SPI1.
GPIO使用的是PA5、6、7
配置如下:
void SPI1_GPIO_Configuration(void)
{
GPIO_InitPara GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_5 |GPIO_PIN_7;//|GPIO_PIN_6;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void SPI1_Configuration(void)
{
SPI_InitPara SPI_InitStructure;
RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB
| RCC_APB2PERIPH_GPIOC |RCC_APB2PERIPH_GPIOE|RCC_APB2PERIPH_AF,
ENABLE);
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_SPI1,ENABLE);
SPI_InitStructure.SPI_TransType = SPI_TRANSTYPE_FULLDUPLEX;
SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
SPI_InitStructure.SPI_FrameFormat = SPI_FRAMEFORMAT_8BIT;
SPI_InitStructure.SPI_SCKPL = SPI_SCKPL_LOW;
SPI_InitStructure.SPI_SCKPH = SPI_SCKPH_1EDGE;
SPI_InitStructure.SPI_SWNSSEN = SPI_SWNSS_SOFT;
SPI_InitStructure.SPI_PSC = SPI_PSC_64;
SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStructure.SPI_CRCPOL = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Enable(SPI1, ENABLE);
}
读取程序如下:
uint16_t SPI1_ReadWritebit(uint8_t data)
{
while(SPI_I2S_GetBitState(SPI1, SPI_FLAG_TBE) == RESET);
SPI_I2S_SendData(SPI1,data);
while (SPI_I2S_GetBitState(SPI1, SPI_FLAG_RBNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
但是用MOSI与SIMO短接后,不能收到数据。程序卡在了接收数据 的地方。
F207与F103的配置有好多不同,许多宏定义也不同,不知道对不对,是按例程写的。
有什么问题么?
查看写入DTR的值,无法写入,明明值不是0xff的啊。
|