【此贴中包含了SPI转串的说明及工程文件】
我在使用PSoc4的SCB模块SPI时不知道如何配置接收中断,还有就是全局中断的设置,请各位大神指点一二,目前根据资料只是完成了SPI及串口等的发送功能,接收及中断都还没有完成。
答案:总体来说与51单片机差不多(全局中断加对应硬件中断都要使能),但更方便了,直接使用API即可,而且中断函数框架也是搭建好了,只需要添加应用代码即可,以下以分别写了全局中断及SPI中断的设置。
答案:
1. 全局中断的设置在CyLib.h文件中有宏定义如下:
#if defined(__ARMCC_VERSION)
#define CyGlobalIntEnable do \
{ \
__enable_irq(); \
} while ( 0 )
#define CyGlobalIntDisable do \
{ \
__disable_irq(); \
} while ( 0 )
#elif defined(__GNUC__)
#define CyGlobalIntEnable do \
{ \
__asm("CPSIE i"); \
} while ( 0 )
#define CyGlobalIntDisable do \
{ \
__asm("CPSID i"); \
} while ( 0 )
#endif /* (__ARMCC_VERSION) */
所以可以在程序中以语句 CyGlobalIntDisable; 来完成全局中断的禁用。
使用语句CyGlobalIntEnable;来完成全局中断的使能。
答案2,以SPI通信的中断设置及服务函数为例:
先来SPI相关的配置截图:

先来主程序的代码:
/* ========================================
*
* Copyright YOUR COMPANY, THE YEAR
* All Rights Reserved
* UNPUBLISHED, LICENSED SOFTWARE.
*
* CONFIDENTIAL AND PROPRIETARY INFORMATION
* WHICH IS THE PROPERTY OF your company.
*
* 说明:该程序用于测试串口模块,使用PSoc 4
* 下载器自带的串口通信引脚与PC进行通信
* P12.6 -- PCTX | 此为与PC机相连的串口
* P12.7 -- PCRX
* 本例程中实际PSoc 4的串口相关引脚如下:
* P4.0 -- PSoc_RX
* P4.1 -- PSOC_TX
* 如果要将PSoc 4串口与PC相连,还需要使用跳线
* 将PC串口线与PSoc串口线交叉相连
*
* 注意:并不是所有PSoc 4端口都支持串口,所以
* 在选择串口引脚是要注意选择支持串口对应发送
* 或接收功能的引脚
* 2014-1-16
* Veiko
* -----------------------------------------
* SPI引脚连接(仅限本例)
* MISO -- P4.1
* MOSI -- P4.0
* SCLK -- P4.2
* SS -- P4.3
* 注意:本机是SPI从机模式
* ========================================
*/
#include
/*
#include "SPI_Slave_PVT.h"
#include "SPI_Slave_SPI_UART_PVT.h"
#include "UART_SPI_UART.h"
#include "SPI_Slave_SCB_IRQ.h"
#include "SPI_Slave.h"
*/
unsigned char SPI_Rec_Data = 0;
unsigned char CounterRec = 0;
int main()
{
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
UART_Start();
SPI_Slave_Start(); /* 使能SPI模块工作 */
SPI_Slave_SCB_IRQ_Start(); /* 使能SPI中断 */
CyGlobalIntEnable; /* 使能全局中断 */
/* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
for(;;)
{
/* Place your application code here. */
UART_UartPutString("Hellow!/r/n");
/*
CounterRec = SPI_Slave_SpiUartGetRxBufferSize();
if(CounterRec > 0)
{
SPI_Rec_Data = SPI_Slave_mosi_s_ReadDataReg();
UART_UartPutChar(SPI_Rec_Data);
SPI_Slave_SpiUartClearRxBuffer();
}
*/
}
}
/* [] END OF FILE */
再来SPI中断函数的代码,这个函数在SCB_SCB_IRQ.c 文件当中
/*******************************************************************************
* Function Name: SPI_Slave_SCB_IRQ_Interrupt
********************************************************************************
*
* Summary:
* The default Interrupt Service Routine for SPI_Slave_SCB_IRQ.
*
* Add custom code between the coments to keep the next version of this file
* from over writting your code.
*
* Parameters:
* None
*
* Return:
* None
*
*******************************************************************************/
CY_ISR(SPI_Slave_SCB_IRQ_Interrupt)
{
unsigned char SPI_Rec_Data_spi = 0;
unsigned char SPI_Rec_Counter = 0;
/* Place your Interrupt code here. */
/* `#START SPI_Slave_SCB_IRQ_Interrupt` */
if(SPI_Slave_SpiUartGetRxBufferSize() >=1)
{
SPI_Rec_Counter=SPI_Slave_SpiUartGetRxBufferSize();
SPI_Rec_Data_spi = (unsigned char)SPI_Slave_SpiUartReadRxData();
UART_UartPutChar(SPI_Rec_Data_spi);
}
SPI_Slave_SCB_IRQ_ClearPending();
/* `#END` */
}
最后,传上我的代码,main.c中的代码注释可能有误,SCB的具体参数可直接双击SCB模块查看:
——回复可见内容——
我要赚赏金
