📄 main.c
字号:
#define IN_MAIN
#include "config.h"
#include "SPI.h"
#include "SSP.h"
#include "ISR.h"
#include "UART.h"
#pragma import(__use_no_semihosting_swi) //don't delete this line
#define SPI_Slave 0 //SPI从模式
#define SPI_Fspi 100000 //通信速率为100K
#define SPI_INT 1 //中断允许
#define SPI_slot 0 //SPI中断通道为0
#define SSP_SPI 0 //SPI格式
#define SSP_Master 1 //主模式
#define SSP_Fssp 100000 //通信速率为100K
#define SSP_INTEn 0 //中断禁止
volatile uint8 rcv_data = 0;
/*********************************************************************************************************
** 函数名称:IRQ_SPI()
** 函数功能:SPI中断服务函数
** 入口参数:无
** 出口参数:无
********************************************************************************************************/
void IRQ_SPI(void)
{
rcv_data = SPI_SPSR;
rcv_data = SPI_SPDR;
UARTn_SendByte(0,rcv_data); //将接收到的数据通过UART发送到PC机上
SPI_SPINT = 0x01; //清除中断标志位
VICVectAddr = 0x00;
}
/*********************************************************************************************************
** Function name: IRQ_Exception
**
** Descriptions: interrupt exceptional handler , change it as needed
** don't delete this function
********************************************************************************************************/
void IRQ_Exception(void)
{
}
/*********************************************************************************************************
** 函数名称:DelayNS()
** 函数功能:延时子程序。
** 入口参数:dly 延时参数
** 出口参数:无
********************************************************************************************************/
void DelayNS(uint8 dly)
{
uint32 i;
for(;dly > 0;dly--)
for(i = 0;i < 50000;i++);
}
/*********************************************************************************************************
** 函数名称:Main()
** 函数功能:SPI从机实验程序。实验中,将SSP设置成SPI的通信格式,然后发送数据。
** SSP循环发送数据'0'~'9',SPI收到数据后,再将收到的数据通过UART发送到PC机上。
** 入口参数:无
** 出口参数:无
** 说明:P0.4连接到P0.14,P0.5连接到P0.19,P0.6连接到P0.20,P0.7连接到P0.21。
********************************************************************************************************/
void Main(void)
{
uint8 i;
TargetInit(VPBDIV_DATA, PLLCFG_DATA, MAMTIM_DATA); // don't delete
while((PLLSTAT & (1 << 10)) == 0); // can delete
SPI_Init(SPI_Slave,SPI_Fspi,SPI_INT); //初始化SPI为从机模式
SSP_Init(SSP_SPI,SSP_Master,SSP_Fssp,SSP_INTEn); //初始化SSP为SPI主机模式
//UART0,波特率115200,8位数据位,1位停止位,无奇偶校验位,中断禁止
UARTn_Init(0,115200,8,1,0,0);
SetISR(SPI0_INT,SPI_slot,(uint32)IRQ_SPI); //初始化SPI中断
IRQEnable();
while(1)
{
for(i = 0;i < 10;i++)
{
SSP_SendByte(i + '0');
DelayNS(100);
}
}
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -