下面是我的代码:
想用 一块单片机 实现 spi1(主) 和spi2(从) 通信
现在 主机 能发出来 而且用示波器 测试 有时钟 但是从机 轮训收不到 ( 也进不去接受中断)
SPI_HandleTypeDef SPI_InitStructure;
SPI_HandleTypeDef SPI_InitStructure_2;
void SPI_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_SPI2_CLK_ENABLE();
// pb4_ mosi
/********************************òy½Å**************************************/
#if 1
//***3õê¼»ˉSPI1òy½Å
GPIO_InitStructure.Pin = GPIO_PIN_7;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP ;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
//GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_6 ;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP ;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
//GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
//***3õê¼»ˉSPI¿ØÖÆòy½Å
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP ;
// GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP ;
// GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1; //¸′óÃÎaspi1
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
/*******************************spi2òy½ÅÅäÖÃ******************************************/
GPIO_InitStructure.Pin = GPIO_PIN_15 | GPIO_PIN_14;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT; ; //íÆíìêä3ö
// GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB,&GPIO_InitStructure);
//***3õê¼»ˉSPI¿ØÖÆòy½Å
//PA4 OE_1
//PA6 DIR_1
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT; ; //
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB,&GPIO_InitStructure);
//PB12 OE_2
//PB14 DIR_2
GPIO_InitStructure.Pin = GPIO_PIN_13;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT; ; //
//GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
/********************************SPI2**************************************/
SPI_InitStructure.Instance=SPI2;
SPI_InitStructure.Init.Direction = SPI_DIRECTION_2LINES; //éèÖÃSPIμ¥Ïò»òÕßË«ÏòμÄêy¾YÄ£ê½:SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure.Init.Mode = SPI_MODE_SLAVE ; //éèÖÃSPI1¤×÷Ä£ê½:éèÖÃÎa′óSPI
SPI_InitStructure.Init.DataSize = SPI_DATASIZE_16BIT; //éèÖÃSPIμÄêy¾Y′óD¡:SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure.Init.CLKPolarity = SPI_POLARITY_HIGH ; //Ñ¡ÔñáË′®DDê±ÖóμÄÎèì¬:ê±ÖóDü¿Õ¸ß
SPI_InitStructure.Init.CLKPhase = SPI_PHASE_2EDGE; //êy¾Y2¶»ñóúμú¶t¸öê±ÖóÑØ
SPI_InitStructure.Init.NSS = SPI_NSS_SOFT; //NSSDÅoÅóéó2¼t£¨NSS1ü½Å£©»1êÇèí¼t£¨ê1óÃSSI룩1üàí:Äú2¿NSSDÅoÅóDSSIλ¿ØÖÆ
SPI_InitStructure.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; //¶¨òå2¨ìØÂêÔ¤·ÖÆμμÄÖμ:2¨ìØÂêÔ¤·ÖÆμÖμÎa256
SPI_InitStructure.Init.FirstBit = SPI_FIRSTBIT_MSB; //Ö¸¶¨êy¾Y′«êä′óMSBλ»1êÇLSBλ¿aê¼:êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
SPI_InitStructure.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
SPI_InitStructure.Init.TIMode = SPI_TIMODE_DISABLE;
SPI_InitStructure.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPI_InitStructure.Init.CRCPolynomial = 7; //CRCÖμ¼ÆËãμĶàÏîê½
if(HAL_OK==HAL_SPI_Init(&SPI_InitStructure)) //¸ù¾YSPI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèSPIx¼Ä′æÆ÷
{
printf("hello");
}
__HAL_SPI_ENABLE(&SPI_InitStructure);
//__HAL_SPI_ENABLE_IT(&SPI_InitStructure,SPI_IT_RXNE);
HAL_NVIC_EnableIRQ(SPI1_IRQn); //ê1ÄüspiÖD¶Ïí¨μà
HAL_NVIC_SetPriority(SPI1_IRQn,1,0);
/*********************************SPI1******************************************/
SPI_InitStructure_2.Instance=SPI1;
SPI_InitStructure_2.Init.Direction = SPI_DIRECTION_2LINES; //éèÖÃSPIμ¥Ïò»òÕßË«ÏòμÄêy¾YÄ£ê½:SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure_2.Init.Mode = SPI_MODE_MASTER ; //éèÖÃSPI1¤×÷Ä£ê½:éèÖÃÎa′óSPI
SPI_InitStructure_2.Init.DataSize = SPI_DATASIZE_16BIT; //éèÖÃSPIμÄêy¾Y′óD¡:SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure_2.Init.CLKPolarity = SPI_POLARITY_HIGH ; //Ñ¡ÔñáË′®DDê±ÖóμÄÎèì¬:ê±ÖóDü¿Õ¸ß
SPI_InitStructure_2.Init.CLKPhase = SPI_PHASE_2EDGE; //êy¾Y2¶»ñóúμú¶t¸öê±ÖóÑØ
SPI_InitStructure_2.Init.NSS = SPI_NSS_SOFT; //NSSDÅoÅóéó2¼t£¨NSS1ü½Å£©»1êÇèí¼t£¨ê1óÃSSI룩1üàí:Äú2¿NSSDÅoÅóDSSIλ¿ØÖÆ
SPI_InitStructure_2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; //¶¨òå2¨ìØÂêÔ¤·ÖÆμμÄÖμ:2¨ìØÂêÔ¤·ÖÆμÖμÎa256
SPI_InitStructure_2.Init.FirstBit = SPI_FIRSTBIT_MSB; //Ö¸¶¨êy¾Y′«êä′óMSBλ»1êÇLSBλ¿aê¼:êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure_2.Init.CRCPolynomial = 7; //CRCÖμ¼ÆËãμĶàÏîê½
SPI_InitStructure_2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
SPI_InitStructure_2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
SPI_InitStructure_2.Init.TIMode = SPI_TIMODE_DISABLE;
SPI_InitStructure_2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
// SPI_InitStructure_2.TxISR=SPI_TxISR_8BIT;
if(HAL_OK==HAL_SPI_Init(&SPI_InitStructure_2)) //¸ù¾YSPI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèSPIx¼Ä′æÆ÷
{
printf("hello");
}
__HAL_SPI_ENABLE(&SPI_InitStructure_2);
// __HAL_SPI_ENABLE_IT(&SPI_InitStructure_2,SPI_IT_RXNE);
HAL_NVIC_EnableIRQ(SPI2_IRQn);
HAL_NVIC_SetPriority(SPI2_IRQn,2,0);
}
中断函数
void SPI2_IRQHandler(void)
{
u8 buf_test[20]="hello love_STM";
u8 ch;
if(__HAL_SPI_GET_FLAG(&SPI_InitStructure,SPI_IT_RXNE)== SET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y
{
HAL_SPI_Receive_IT(&SPI_InitStructure,&ch,1);
SPI_data[data_count]=ch;
data_count++;
}
if(__HAL_SPI_GET_FLAG(&SPI_InitStructure,SPI_IT_TXE) == RESET)
{
if(HAL_SPI_Transmit_IT(&SPI_InitStructure,buf_test,14)==HAL_OK)
{
printf("hello");
}
}
HAL_SPI_IRQHandler(&SPI_InitStructure);
}
大神们给指正一下 哪里错了 用的是 原子 f7的板子
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>