打印

两STM32F105通过SPI通信,从机不产生中断,大家帮忙

[复制链接]
6371|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
03024003|  楼主 | 2010-9-20 08:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查询方式通信可以。

硬件连接:
主机                              从机
MOSI         ----                 MOSI
MISO         ----                 MISO
SCLK         ----                 SCLK

主机初始化及主程序:
void SPI_Master(void)
{
//时钟初始化
//Enable SPI1 clock and GPIO clock for SPI1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);

//IO初始化
GPIO_InitTypeDef GPIO_InitStructure;
//Configure SPIy pins: SCK, MISO and MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);


//1st phase: SPI1 Master
//SPIy Config
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_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);

//Enable SPI1
SPI_Cmd(SPI1, ENABLE);
}
main()
{
while(1)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,0x55);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);

DelayMs(1000);
}
}


//------------------------------------------------------------------------

//从机初始化及SPI从机中断
void SPI_slave(void)
{
//Enable SPI1 clock and GPIO clock for SPI1 and SPI
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);

//IO初始化
GPIO_InitTypeDef GPIO_InitStructure;
//Configure SPIy pins: SCK, MISO and MOSI
GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);


//1st phase: SPI1 Master
//SPI1 Config
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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);

   NVIC_InitTypeDef   NVIC_InitStructure;
   NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
   NVIC_Init(&NVIC_InitStructure);
   
SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);

//Enable SPI1
SPI_Cmd(SPI1, ENABLE);
}
void SPI1_IRQHandler(void)
{
      USART1->DR = 0x99;//串口打印信息

}
main()
{
while(1)
{
}
}
沙发
barboon| | 2010-9-20 09:32 | 只看该作者
NVIC_InitTypeDef   NVIC_InitStructure;
   NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
   NVIC_Init(&NVIC_InitStructure);

这段代码不完整吧,应该再加一句
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

使用特权

评论回复
板凳
03024003|  楼主 | 2010-9-20 10:27 | 只看该作者
2# barboon


谢谢 我这个错误真不应该  现在好了

使用特权

评论回复
地板
sunshinecry| | 2013-10-18 20:04 | 只看该作者
void SPI1_IRQHandler(void)  这个函数只写这些就行么?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:--->

21

主题

125

帖子

0

粉丝