⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 master_spi_isr.c

📁 2812SPI双机通信程序.rar
💻 C
字号:

//修改时间2008.4.21
//版本号:0.1

#include "DSP281x_Device.h"     
#include "DSP281x_Examples.h" 
/
//===========读A、B、C相电压==============//  在主程序中运行,向下位机请求 三相电压数据
//电压命令:0x0003 0x1004 0x0004  前两个为功能码,0004为变量个数
	Initspi();
	SCheckSum = SPI_Schecksum(0x0003,0x100A,0x0004);  //求得发送验证码		
	Sendcommand(0x0003,0x100A,0x0004,SCheckSum);         //向从机发送电压数据的命令
		
	if(SPI_Sflag == 1)                       //上位机接收到正确的数据
	{
    	UXa = store_32(spi_storedata[0],spi_storedata[1]);  //存入Ua、Ub、Uc,显示程序将显示在液晶屏上
    	UXb = store_32(spi_storedata[2],spi_storedata[3]);
    	UXc = store_32(spi_storedata[4],spi_storedata[5]); 
    	UXd = store_32(spi_storedata[6],spi_storedata[7]);
    	SPI_Sflag = 0;		
	}      



int16 spi_p=0;
Uint16 spi_storedata[62];
Uint16 RCheckSum = 0;
Uint16 SPI_Rcount = 0;
Uint16 SPI_ISRcount = 0;
Uint16 SPI_number = 0;
Uint16 SPI_rdata[100];
Uint16 spi_flag=0 ;


void Sendcommand(Uint16 a,Uint16 b,Uint16 c,Uint16 d)    //主机发送命令子程序
{	
	Uint16 i;
	
	spi_p = (int16)c;       //先读出将要发送数据的字数 如电压4个变量 是8个字

	  
	if(SpiaRegs.SPIFFTX.bit.TXFFST==0)    //发送3个控制字
	{  	

        SpiaRegs.SPITXBUF=a; 
		SpiaRegs.SPITXBUF=b; 
		SpiaRegs.SPITXBUF=c;
		SpiaRegs.SPITXBUF=d;
			
	}
	

	
    for(i = 0; i < 20; i++)   //延时
    {
    	DELAY_US(10);
    	KeyScan();
    	KickDog();	
    }

                            				
    for(i=0;i<(spi_p + 3);i++)                    //向从机发送一组0,为从机提供时钟                    
    {
    	SpiaRegs.SPITXBUF=0x0000;					
        
    }

 	for(i = 0; i < 20; i++)   //延时
    {
    	DELAY_US(10);
    	KeyScan();
    	KickDog();	
    }
    	
    
    
}


Uint32 store_32 (Uint16 a,Uint16 b)
{
	Uint32 temp;
	temp = (Uint32)a;
	temp=temp<<16;
	temp=temp+b;
	return(temp);	
}

Uint16 SPI_Rchecksum(Uint16 array[])   //接收验证码计算子程序
{	
	Uint16 i;
	RCheckSum = 0;
	for	(i=0;i<SPI_Rcount;i++)
	{
		RCheckSum +=array[i]; 
	}
	return(RCheckSum);			
}


Uint16 SPI_Schecksum(Uint16 a,Uint16 b,Uint16 c)  //发送验证码计算子程序
{
	SCheckSum =0;
	SCheckSum +=a;
	SCheckSum +=b;
	SCheckSum +=c;	
	return(SCheckSum);
}



interrupt void Spirxfifo_isr(void)            //SPI接收中断 用于接收从机向主机发送的数据
{
	
	Uint16 i = 0;
	Uint16 j = 0;

	
	SPI_rdata[SPI_ISRcount] = SpiaRegs.SPIRXBUF; 
	SPI_ISRcount++;    //接收数据长度位
	

	if(SPI_ISRcount >= (spi_p+7))   //当中断次数和接收数据长度一致时
	{
		if((SPI_rdata[4] == 3) && (SPI_rdata[5] == spi_p)) //判断功能吗和数据长度是否正确
		{		
				SPI_Rcount = spi_p;
    		
    			j = 0;
   	 			for(i=0;i<(spi_p+1);i++)     //如功能吗和数据长度正确,开始接收数据和校验码
   	 			{
   	 				spi_storedata[i] = 	SPI_rdata[j+6];
   	 				j++;
    	   		}
    	   	
    	   		RCheckSum = SPI_Rchecksum(spi_storedata);  
    	   	
    	   		if(RCheckSum == spi_storedata[spi_p] )
    	   		{
    	   			SPI_Sflag = 1;   //接收验证码正确 置接收标志位 	   		
    	   		}
    	   	    	   		   	   	    	   	
		}
		SPI_ISRcount = 0; 
	}	
	
	KickDog();
	
 	SpiaRegs.SPIFFRX.bit.RXFFINTCLR=1;
	PieCtrlRegs.PIEACK.all|=0x0020;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -