完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
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); 大神现身吧 |
|
相关推荐
1个回答
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1621 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1546 浏览 1 评论
980 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
686 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1599 浏览 2 评论
1867浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
650浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
518浏览 3评论
536浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
506浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 02:44 , Processed in 0.669620 second(s), Total 47, Slave 41 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号