SPI的SCK 、SO、SI选用B13 C2 C3 硬件上的连接已经与B3 B4 B5连接完成 软件代码如下SETSPEED和write基本没变() :
void SPI2_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);//ê1ÄüGPIOb/cê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);//ê1ÄüSPI2ê±Öó
//GPIOFC 2¡¢33õê¼»ˉéèÖÃ óÃà′ÅäÖÃSPI2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//PC2¡¢3¸′óÃ1|Äüêä3ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOC, &GPIO_InitStructure);//3õê¼»ˉ
//GPIOFB 13 3õê¼»ˉéèÖÃ óÃà′ÅäÖÃSPI2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;//PB13¸′óÃ1|Äüêä3ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉ
//¸′óÃÅäÖÃ óÃà′ÅäÖÃSPI2
GPIO_PinAFConfig(GPIOC,GPIO_PinSource2,GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2);
//SPI2μÄ3õê¼»ˉ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);//¸′λSPI2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,DISABLE);//í£Ö1¸′λSPI2
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //éèÖÃSPIμ¥Ïò»òÕßË«ÏòμÄêy¾YÄ£ê½:SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //éèÖÃSPI1¤×÷Ä£ê½:éèÖÃÎaÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //éèÖÃSPIμÄêy¾Y′óD¡:SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //′®DDí¬2½ê±ÖóμÄ¿ÕÏD×′ì¬Îa¸ßμçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //′®DDí¬2½ê±ÖóμÄμú¶t¸öìø±äÑØ£¨éÏéy»òϽ죩êy¾Y±»2éÑù
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSDÅoÅóéó2¼t£¨NSS1ü½Å£©»1êÇèí¼t£¨ê1óÃSSI룩1üàí:Äú2¿NSSDÅoÅóDSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨òå2¨ìØÂêÔ¤·ÖÆμμÄÖμ:2¨ìØÂêÔ¤·ÖÆμÖμÎa256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨êy¾Y′«êä′óMSBλ»1êÇLSBλ¿aê¼:êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖμ¼ÆËãμĶàÏîê½
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE); //ê1ÄüSPI2íaéè
SPI2_ReadWriteByte(0xff);
}
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//ÅD¶ÏóDD§DÔ
SPI1->CR1&=0XFFC7;//λ3-5Çåá㣬óÃà′éèÖÃ2¨ìØÂê
SPI1->CR1|=SPI_BaudRatePrescaler; //éèÖÃSPI1Ëù¶è
SPI_Cmd(SPI1,ENABLE); //ê1ÄüSPI1
}
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//ÅD¶ÏóDD§DÔ
SPI2->CR1&=0XFFC7;//λ3-5Çåá㣬óÃà′éèÖÃ2¨ìØÂê
SPI2->CR1|=SPI_BaudRatePrescaler; //éèÖÃSPI2Ëù¶è
SPI_Cmd(SPI2,ENABLE); //ê1ÄüSPI2
}
//SPI1 ¶áD′ò»¸ö×Ö½ú
//TxData:òaD′èëμÄ×Ö½ú
//·μ»ØÖμ:¶áè¡μ½μÄ×Ö½ú
u8 SPI1_ReadWriteByte(u8 TxData)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}//μè′y·¢ËíÇø¿Õ
SPI_I2S_SendData(SPI1, TxData); //í¨1yíaéèSPIx·¢Ëíò»¸öbyte êy¾Y
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} //μè′y½óêÕíêò»¸öbyte
return SPI_I2S_ReceiveData(SPI1); //·μ»Øí¨1ySPIx×î½ü½óêÕμÄêy¾Y
}
u8 SPI2_ReadWriteByte(u8 TxData)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}//μè′y·¢ËíÇø¿Õ
SPI_I2S_SendData(SPI2, TxData); //í¨1yíaéèSPIx·¢Ëíò»¸öbyte êy¾Y
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET){} //μè′y½óêÕíêò»¸öbyte
return SPI_I2S_ReceiveData(SPI2); //·μ»Øí¨1ySPIx×î½ü½óêÕμÄêy¾Y
}
然后在W2QXX的.c文件中将调用SPI1的函数换成SPI2的函数,程序 在运行过程中跑飞,设置断点后运行到 SPI2_ReadWriteByte中第一个while中,大神们先看下我软件或者硬件配置有木有问题
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>