完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
芯片是STM32F030C8T6,代码是根据F103可以通讯的代码移植过来的,接线也没有问题,但是寄存器就是没有数据,希望大神帮忙看看是哪里有什么问题吗?谢谢大家!
```SPI1(主机模式)初始化代码: void SPI1_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE); // SPI_Cmd(SPI1, DISABLE); SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = SPI1_SCK_PIN | SPI1_MOSI_PIN | SPI1_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_MISO_PORT, GPIO_InitStructure); GPIO_PinAFConfig(SPI1_SCK_PORT, SPI1_SCK_SOURCE, GPIO_AF_0); GPIO_PinAFConfig(SPI1_MISO_PORT, SPI1_MISO_SOURCE, GPIO_AF_0); GPIO_PinAFConfig(SPI1_MOSI_PORT, SPI1_MOSI_SOURCE, GPIO_AF_0); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_Init(SPI1, SPI_InitStructure); SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF); SPI_Cmd(SPI1, ENABLE); } ``` 主机发送数据代码: ```uint8_t SPI1_WriteRead8Byte(uint8_t TxData) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_SendData8(SPI1, TxData); // *(uint8_t*) SPI1->DR = TxData; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return SPI_ReceiveData8(SPI1); } ``` SPI2(从机模式)初始化代码: void SPI2_Init(void) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN | SPI2_MOSI_PIN | SPI2_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI2_MISO_PORT, GPIO_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_Init(SPI2, SPI_InitStructure); SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF); SPI_Cmd(SPI2, ENABLE); SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE); SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_ERR, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( NVIC_InitStructure); } 从机中断函数: ```void SPI2_IRQHandler(void) { if (SET == SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE)) { ucSPI2_RxBuf[ucSPI2_RxCount] = *(uint8_t*) SPI2->DR; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) ; *(uint8_t*) SPI2->DR = ucSPI2_RxBuf[ucSPI2_RxCount] + 0x05; ucSPI2_RxCount++; if (ucSPI2_RxCount > SPI_BUF_LEN - 1) { ucSPI2_RxCount = 0; } SPI_I2S_ClearFlag(SPI2, SPI_I2S_IT_RXNE); //SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE); } SPI_I2S_ClearFlag(SPI2, SPI_I2S_IT_RXNE); //SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE); } } ``` 主函数: ```int main(void) { int i = 0; System_Initializes(); while (1) { for (i = 0; i < SPI_BUF_LEN; i++) { ucSPI1_RxBuf[i] = SPI1_WriteRead8Byte(ucSPI1_TxBuf[i]); Delay_ms(10); } } } ``` |
|
相关推荐
1个回答
|
|
F0 标准外设库里有SPI 例程,可以参考对照一下。stm32f0_stdperiph_libv1.5STM32F0xx_StdPeriph_Lib_V1.5.0ProjectsSTM32F0xx_StdPeriph_ExamplesSPISPI_TwoBoardsDataExchangeInterrupt
另外标准库的方式已经落伍了,而且厂家也不更新维护了,尽快使用CUBE库吧。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1658 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1568 浏览 1 评论
996 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
695 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1616 浏览 2 评论
1876浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
662浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
528浏览 3评论
547浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
517浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 06:14 , Processed in 0.741051 second(s), Total 79, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号