有谁可以提供个中断主方式的SPI 1 程序让我参考一下最好不使用库,搞了几天了还是只能够设置完了后进一次中断,
//===================================================================
//
//===================================================================
void STM32_SPI_Configuration(void)
{
//SPI_InitTypeDef SPI_InitStructure;
//GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIOA clocks */
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
STM32_Rcc_Regs->apb2enr.all |= RCC_SPI1EN; // 时钟使能
/* Configure SPI1 pins: NSS, 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);
STM32_Gpioa_Regs->crl.bit.CNF5 = Output_Af_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE5 = Output_Mode_50mhz;
STM32_Gpioa_Regs->crl.bit.CNF6 = Output_Af_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE6 = Output_Mode_50mhz;
STM32_Gpioa_Regs->crl.bit.CNF7 = Output_Af_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE7 = Output_Mode_50mhz;
/* Configure PA.4 as Output push-pull, used as Flash Chip select */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//GPIO_Init(GPIOA, &GPIO_InitStructure);
STM32_Gpioa_Regs->crl.bit.CNF4 = Output_push_pull;
STM32_Gpioa_Regs->crl.bit.MODE4 = Output_Mode_50mhz;
/* Deselect the FLASH: Chip Select high */
//GPIOA->BSRR = GPIO_Pin_4;
STM32_Gpioa_Regs->bsrr.bit.BS4 = 1;// 置位
/* SPI2 configuration */
//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_High;
//SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
//SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
//SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
//SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//SPI_InitStructure.SPI_CRCPolynomial = 7;
//SPI_Init(SPI1, &SPI_InitStructure);
STM32_Spi1_Regs->cr1.bit.BR = 2; // 72mhz/8=9mhz 010=Fpclk/8
STM32_Spi1_Regs->cr1.bit.CPHA = 1; // 1: 数据采样从第二个时钟边沿开始
STM32_Spi1_Regs->cr1.bit.CPOL = 1; // 1: 空闲状态时,SCK保持高电平
STM32_Spi1_Regs->cr1.bit.DFF = 0; // 0:使用8位数据帧格式进行发送/接收
STM32_Spi1_Regs->cr1.bit.LSBFIRST = 0; // 0:先发送MSB
STM32_Spi1_Regs->cr1.bit.MSTR = 1; // 1:配置为主设备
STM32_Spi1_Regs->cr1.bit.SSM = 1; // 1:启用软件从设备管理。
STM32_Spi1_Regs->cr1.bit.BIDIMODE = 0; //0:选择“双线双向”模式
/* Enable SPI1 TXE interrupt */
//SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
STM32_Spi1_Regs->cr2.bit.TXEIE = 1;
/* Enable SPI1 RXNE interrupt */
//SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
//STM32_Spi1_Regs->cr2.bit.RNXEIE = 1;
/* Enable SPI1 */
//SPI_Cmd(SPI1, ENABLE);
STM32_Spi1_Regs->cr1.bit.SPE = 1; //1:开启SPI设备
/* Configure and enable SPI1 interrupt -------------------------------------*/
//NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQChannel;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure);
/* enabling interrupt 设置SPI1中断优先级,和关中断*/
STM32_Nvic_Regs->Priority[35].all = 0x50;
STM32_Nvic_Regs->Enable[1].bit.INT3 = 1; // 37 (35-32=3) 44 可设置 SPI1 SPI1全局中断 0x0000_00cc
}//end sub
/*******************************************************************************
* Function Name : SPI1_IRQHandler
* Description : This function handles SPI1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI1_IRQHandler(void)
{
// wwd mask
//if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET)
//{
// /* Send SPI1 data */
// SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
//
// /* Disable SPI1 TXE interrupt */
// if (TxIdx == BufferSize)
// {
// SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE,DISABLE);
// }
//}
// wwd mask end
// wwd mask
if( STM32_Spi1_Regs->sr.bit.TXE != 0)
{
/* Send SPI1 data */
//STM32_Spi1_Regs->dr.all = SPI1_Buffer_Tx[TxIdx++];
STM32_Spi1_Regs->dr.all = 0x55;
/* Disable SPI1 TXE interrupt */
//if (TxIdx == BufferSize)
//{
// //SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
// STM32_Spi1_Regs->cr2.bit.TXEIE = 0;
//}
}
// wwd mask end
}
问题解决了
问题解决了,加上下面一句就可以了
STM32_Spi1_Regs->cr2.bit.SSOE = 1;
还有个问题,NSS还是没有置低在SPI启动的时候
但是NSS还是不起作用,我使用了直接控制IO的方法
楼主加油!
"SPI 工作在主设备配置时,如果SSOE 位使能,则NSS 引脚用作输出,并输出低电平;此时,所有NSS 引脚连到该设备NSS 引脚的其他设备都将收到低电
平,当这些设备配置为NSS 硬件模式时,就被自动地配置成了从设备。"
直接控制IO的方法 就【自己操作】NSS的引脚电平高低啊!
/* Deselect the FLASH: Chip Select high */
//GPIOA->BSRR = GPIO_Pin_4;
STM32_Gpioa_Regs->bsrr.bit.BS4 = 1;// 置位
还有这个有点问题,应该改为如下吧
/* enabling interrupt 设置SPI1中断优先级,和关中断*/
STM32_Nvic_Regs->Priority[35].all = 0x50;
STM32_Nvic_Regs->Enable[1].bit.INT35 = 1; // 37 (35-32=3) 44 可设置 SPI1 SPI1全局中断 0x0000_00cc
上面搞错了不好意思! 你是对的!
/* enabling interrupt 设置SPI1中断优先级,和关中断*/
STM32_Nvic_Regs->Priority[35].all = 0x50;
STM32_Nvic_Regs->Enable[1].bit.INT3 = 1; // 37 (35-32=3) 44 可设置 SPI1 SPI1全局中断 0x0000_00cc
谢谢你提供的头文件
我是在你的头文件基础上写的,还需要多谢谢你.另外是否可以把你的完整的头文件给我一份,实在懒的敲那些字了,谢谢.
另外NSS还是没有输出,现在因为项目紧只好先直接控制IO的方式来写了