完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
好久没有再碰STM32的程序,今天出于需求又把程序捡起来调试了一下,发现问题居多,以下就归纳总结以下:
1.我是用两块STM32F103C8T6进行SPI通讯,一块做主机,一块作为从机; 主机部分代码: void SPI2_Config(void)//SPI2的传输配置 { SPI_InitTypeDef SPI_InitStructure; SPI_Cmd(SPI2, DISABLE); 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_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); } void Spi2_Master_Fun(void)//SPI2端口配置 { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能spi2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽复用 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15;//sck mosi GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//miso GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//cs GPIO_Init(GPIOB,&GPIO_InitStructure); SPI2_Config(); printf("MasterFunInitOver!rn"); } //SPI读写数据: unsigned char SPIWriteByte(unsigned char Byte) { while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送区空 SPI_I2S_SendData(SPI2, Byte); //发送一个byte //delayms(100); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接收完一个byte return SPI_I2S_ReceiveData(SPI2); //返回收到的数据 } void USART1_IRQHandler(void)//将串口的传过来的数据发送给从机 { u8 k; if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//检查指定的USART中断发生与否 { // k=USART_ReceiveData(USART1); // printf("Send_Data:%xrn",k); GPIO_ResetBits(GPIOB,GPIO_Pin_12); SPIWriteByte(USART_ReceiveData(USART1)); GPIO_SetBits(GPIOB,GPIO_Pin_12); } } 从机部分代码: void SPI2_Slaver_Config(void)//从机配置 { SPI_InitTypeDef SPI_InitStructure; SPI_Cmd(SPI2, DISABLE); 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_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); /*Enable SPI1.NSS as a GPIO*/ //SPI_SSOutputCmd(SPI1, ENABLE); SPI_Cmd(SPI2, ENABLE); SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE); //spi1_cs_low; } /* 中断嵌套控制器配置 */ void NVIC_config(void) { NVIC_InitTypeDef NVIC_InitStructure; /* 选择中断分组2 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); /* 设置SPI1中断 */ NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void Spi2_Slaver_Fun(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能spi2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //推挽复用 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15;//sck mosi GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//miso GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//cs GPIO_Init(GPIOB,&GPIO_InitStructure); NVIC_config(); SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE); SPI2_Slaver_Config(); printf("SlaverFunInitOver!rn"); } void SPI2_IRQHandler(void)//接受数据并且打印到串口 { u8 i; if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET) { SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE); while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); i = SPI_I2S_ReceiveData(SPI2); printf("Receive_Data:%xrn",i); while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI2, i); } 延迟函数: void delayms(u32 i) { u32 temp; SysTick->CTRL=0X01; SysTick->LOAD=9000*i; SysTick->VAL=0X00; do { temp=SysTick->CTRL; } while((temp&0x01)&&(!(temp&(1<<16)))); SysTick->CTRL=0X00; SysTick->VAL=0X00; } main函数: #include"stm32f10x.h" #include "../APP/SPI/SPI.h" #include "../APP/Usart/Uart.h" #define Spi_Master 1 #define Spi_Slaver 0 int main(void) { Uart1_Init_Config(); #if Spi_Master Spi2_Master_Fun(); printf("Please Input:rn"); while(1) { delayms(100); }; #endif #if Spi_Slaver Spi2_Slaver_Fun(); while(1); #endif } 接线图: 单片机1:NSS -> 单片机2:NSS 单片机1:SCK-> 单片机2:SCK 单片机1:MISO-> 单片机2:MISO 单片机1:MOSI-> 单片机2:MOSI 实验结果: 虽然进行慢速发没问题,但是将速度加到10ms后就会发生接收数据偏移错误,目前不知道是什么原因,希望有大侠可以告知根号的解决方案。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1771 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1619 浏览 1 评论
1070 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
724 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1673 浏览 2 评论
1936浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
729浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
569浏览 3评论
594浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
552浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 18:03 , Processed in 0.838548 second(s), Total 76, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号