主机给从机发数据,从机接收采用中断,主机发数据收到的全是0,但是从机的中断进不了,看了很久不知道是哪儿的原因,下面是代码,望大神支招。
主机配置
——————————————————————————————————————
#include "spi_dma.h"
void SPI2_DMA_Configuration( void )
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13 | GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//SPI_Mode_Master SPI_Mode_Slave
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //SPI_NSS_Hard SPI_NSS_Soft
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
u8 SPI2_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while((SPI2->SR&1<<1)==0)
{
retry++;
if(retry>200)return 0;
}
SPI2->DR=TxData;
retry=0;
while((SPI2->SR&1<<0)==0)
{
retry++;
if(retry>200)return 0;
}
return SPI2->DR;
}
int main()
{
int i =0 ;
int j =0 ;
int q =0 ;
SPI2_DMA_Configuration();
while(1)
{
q = SPI2_ReadWriteByte((u8)(0x34));
i = SPI2_ReadWriteByte((u8)(0x22));
}
}
/****************************************/
/****************************************/
从机配置___________________________________________________
#include "spi_dma.h"
#include "string.h"
extern u8 Slave_Temp;
void SPI2_DMA_Configuration( void )
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13 | GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;//SPI_Mode_Master SPI_Mode_Slave
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; //SPI_NSS_Hard SPI_NSS_Soft
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_I2S_ITConfig(SPI2,SPI_I2S_IT_RXNE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=SPI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void SPI2_IRQHandler(void)
{
if(SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
{
Slave_Temp = SPI2_ReadByte(0x00);
SPI2_ReadWriteByte(0xba);
}
}
u8 SPI2_ReadByte(u8 TxData)
{
u8 retry=0;
while((SPI2->SR&1<<0)==0)
{
retry++;
if(retry>200)return 0;
}
return SPI2->DR;
}
u8 SPI2_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while((SPI2->SR&1<<1)==0)
{
retry++;
if(retry>200)return 0;
}
SPI2->DR=TxData;
retry=0;
while((SPI2->SR&1<<0)==0)
{
retry++;
if(retry>200)return 0;
}
return SPI2->DR;
}
int main()
{
int i = 0;
int t = 0;
int j =0xff;
SPI2_DMA_Configuration();
while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>