完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
主机代码:
//GPIO配置: void SPI_Master_Init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; SPI_APBxClock_FUN ( SPI_CLK, ENABLE ); SPI_CS_APBxClock_FUN ( SPI_CS_CLK|SPI_SCK_CLK| SPI_MISO_PIN|SPI_MOSI_PIN, ENABLE ); GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SPI_CS_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(SPI_SCK_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(SPI_MISO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStructure); SPI_CS_HIGH(); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b ; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; 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_Init(SPIx , &SPI_InitStructure); SPI_Cmd(SPIx , ENABLE); } // 其中 CS PA4、 SCK PA5、MISO PA6、MOSI PA7 |
|
|
|
主机发送数据函数:
u8 SPI_SendByte(u8 byte) { SPITimeout = SPIT_FLAG_TIMEOUT; while (SPI_I2S_GetFlagStatus(SPIx , SPI_I2S_FLAG_TXE) == RESET) { if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0); } SPI_I2S_SendData(SPIx , byte); SPITimeout = SPIT_FLAG_TIMEOUT; while (SPI_I2S_GetFlagStatus(SPIx , SPI_I2S_FLAG_RXNE) == RESET) { if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(1); } return SPI_I2S_ReceiveData(SPIx ); } 同时,该函数也是从机的发送接收数据函数 |
|
|
|
从机配置:
void SPI_Slave_Init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; SPI_APBxClock_FUN ( SPI_CLK, ENABLE ); SPI_CS_APBxClock_FUN ( SPI_CS_CLK|SPI_SCK_CLK| SPI_MISO_PIN|SPI_MOSI_PIN, ENABLE ); GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(SPI_SCK_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(SPI_MISO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b ; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; 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_Init(SPIx , &SPI_InitStructure); SPI_Cmd(SPIx , ENABLE); } |
|
|
|
|
|
|
|
从机采用外部中断接受数据的方式,即主机片选端产生低电平后,从机进入中断,从机的中断配置如下:static void SPI_NVIC_Configuration(void)
{ NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void SPI_EXTI_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE ); GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(SPI_CS_PORT, &GPIO_InitStructure); //SPI_CS_HIGH(); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4); EXTI_InitStructure.EXTI_Line = EXTI_Line4 ; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); / SPI_NVIC_Configuration(); } |
|
|
|
从机的中断函数:
void EXTI4_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line4) != RESET) { Rec_data=SPI_SendByte(Send_data); printf("Slave_data=%x Send_data=%xrn",Rec_data,Send_data); } EXTI_ClearITPendingBit(EXTI_Line4); } |
|
|
|
主机在主程序中的发送函数:int main()
{ unsigned char value=0; u8 rec=0; LED_GPIO_Config(); USART1_Config(); Key_GPIO_Config(); SPI_Master_Init(); while(1) { value=KEY_Scan(); if(value==2) { rec= SPI_send_data(0xAC); printf("Rec_data=%xrn",rec); } if(value==3) { rec= SPI_send_data(0xBD); printf("Rec_data=%xrn",rec); } } } //其中SPI_send_data函数包括了cs线先拉低,然后发送并接受一个字节,然后CS线拉低 |
|
|
|
|
|
|
|
|
|
|
|
反正就是从机接收数据完全OK,主机接受的数据都却不对,是自己发出去的数据。求大神解答啊!
|
|
|
|
好复杂,帮顶
|
|
|
|
你好 看到你关于SPI双机通信的问题 请问你解决了么。。我现在也在做双机通讯 有一些问题 方便交流一下么 谢谢 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
991 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
970 浏览 2 评论
2080 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1177 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 18:53 , Processed in 0.684330 second(s), Total 63, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号