本帖最后由 woshilee 于 2014-12-17 14:33 编辑 void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure PA5£¬PA7 in output nopull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource4, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0); } void RCC_Configuration(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA |RCC_AHBPeriph_GPIOC , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); } void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge; SPI_InitStructure.SPI_CPOL=SPI_CPOL_High; SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8 ; SPI_InitStructure.SPI_CRCPolynomial=7; SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b; SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; SPI_InitStructure.SPI_Mode=SPI_Mode_Master; SPI_InitStructure.SPI_NSS=SPI_NSS_Soft ; SPI_Init(SPI1,&SPI_InitStructure); SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Set); SPI_SSOutputCmd(SPI1, ENABLE); SPI_Cmd(SPI1, ENABLE); } 基本的配置就是这样,下面是我写的读写函数 uint8_t SPI2_ReadWriteByte(uint8_t TxData) { uint8_t RxData = 0; while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_SendData8(SPI1, TxData); while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); RxData = SPI_ReceiveData8(SPI1);//SPI2 return (uint8_t)RxData; } 每次都会死在红色的地方,这究竟是为什么呢? 我看了一下深入浅出STM32-NUCLEO-072RB的资料,有点问题,里边这样配置: void SPI1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_AHBPeriphClockCmd(SPI_GPIO_CLK,ENABLE); RCC_APB2PeriphClockCmd(SPI_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN|SPI_MISO_PIN|SPI_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SPI_GPIO_PORT , &GPIO_InitStructure); GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_SCK_SOURCE , SPI_SCK_AF); GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MISO_SOURCE, SPI_MISO_AF); GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MOSI_SOURCE, SPI_MOSI_AF); //SPI CONFIGURATION SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPI1, &SPI_InitStructure); SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set); SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF); SPI_Cmd(SPI1, ENABLE); /* SPI enable */ } 这是里边的定义: #define SPI_CLK RCC_APB2Periph_SPI1 #define SPI_GPIO_CLK RCC_AHBPeriph_GPIOB #define SPI_GPIO_PORT GPIOB /* GPIOB*/ #define SPI_SCK_PIN GPIO_Pin_3 /* PB.3 */ #define SPI_SCK_SOURCE GPIO_PinSource3 #define SPI_SCK_AF GPIO_AF_0 #define SPI_MISO_PIN GPIO_Pin_4 /* PB.4 */ #define SPI_MISO_SOURCE GPIO_PinSource4 #define SPI_MISO_AF GPIO_AF_0 #define SPI_MOSI_PIN GPIO_Pin_5 /* PB.5 */ #define SPI_MOSI_SOURCE GPIO_PinSource5 #define SPI_MOSI_AF GPIO_AF_0 SPI1对应的端口不是PB而是PA,只有重映射了才会这样,但是没找到是在哪里重映射的。 且不说这个问题,大家怎么看卡死在 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); 大神现身吧 |
这个函数是决定RXNE标志位产生条件的,详细的可以看我发的帖子
视频是枫叶老师做的,他也在社区发了ppt课件https://www.stmcu.org.cn/module/ ... 5%E6%B5%85%E5%87%BA