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

📄 singlebytefinal.c

📁 自己编译和注解的2812的SCI的FIFO收发程序
💻 C
字号:
#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File

#define BAUDSTEP 100            // Amount BRR will be incremented between each
                                // autobaud lock
#define frame_length 10
#define rs485_node_address0 0x30
#define rs485_node_address1 0x31
#define rs485_node_address2 0x32
#define rs485_node_address3 0x33
#define rpara_num 23
#define tpara_num 20
#define data_frame_type  0
#define request_frame_type  1
#define heart_beat_frame_type  2
#define answer_frame_type  3
#define times 1
#define q_value 23
#define rs485_rw GpioDataRegs.GPEDAT.bit.GPIOE2
#define receive 0
#define transmit 1
// Prototype statements for functions found within this file.
void scia_init(void);
void scia_xmit(int a);
interrupt void rxaint_isr(void); 
interrupt void txaint_isr(void); 
void rs485_tdata_deal(Uint16, Uint16, Uint32, Uint16);
void rs485_rdata_deal(void);
// Global counts used in this example
Uint16 LoopCount;
//RS485 reception related variables:
Uint16 RECEIVEDCOUNT=0;
Uint16 rs485_ready_to_serve; //rs485 ready signal
Uint16 rs485_can=0;         //the symbol to symbolize an effective SOF reception.

Uint16 ErrorCount; 
Uint16 SendChar;
Uint16 ReceivedAChar;   // scia received character
Uint16 ReceivedBChar;   // scib received character
Uint16 frame_count;     // the data transmitted/received
Uint16 rs485_rdata[frame_length]; //rs485 receive data buffer area
Uint16 rs485_tdata[frame_length]; //rs485 transmit data buffer area
Uint32 Err_Flag,Instruction_word;

typedef struct a0
       { 
	     Uint32 Value;
		 Uint32 Max;
		 Uint32 Min;
		 Uint32 Default;
		 Uint16 RW;
       }rec_item;    //data received from the setter
typedef struct a1
       { 
	     Uint32 Value;
		 Uint16 RW;
       }trans_item;  //data transmitted to the setter

rec_item     rparameter[rpara_num]; //rparameter array
trans_item   tparameter[tpara_num]; //tparameter array

void main(void)
{
   int i;
   int j;
   Uint32 temp;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO: 
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();  // Skipped for this example  

   EALLOW;
   GpioMuxRegs.GPFMUX.all=0x0030;	// Select GPIOs to be Scia pins	 
                                    // Port F MUX - x000 0000 0011 0000
   GpioMuxRegs.GPGMUX.all=0x0000;	
   GpioMuxRegs.GPEMUX.all=0x0000;
   GpioMuxRegs.GPEDIR.bit.GPIOE2=1;
   EDIS;

   InitPieCtrl();
   
// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

   InitPieVectTable();

   scia_init();       // Initalize SCIA
//为了调试,将各接收和发送数据区进行临时赋值。
   Err_Flag=0x01020304;
   for(i=0;i<tpara_num;i++)
    {
	  temp=i;
	  for(j=0;j<q_value;j++)
	    temp*=2;
      tparameter[i].Value=temp;
    }
  
for(;;)
{
  if(SciaRegs.SCIRXST.bit.RXRDY==1)
  {
   if((RECEIVEDCOUNT==0)&&SciaRegs.SCIRXBUF.all==0xaa) rs485_can=1;
   if((RECEIVEDCOUNT==0)&&SciaRegs.SCIRXBUF.all!=0xaa) rs485_can=0;
   if(rs485_can==1)
     {
      rs485_rdata[RECEIVEDCOUNT]=SciaRegs.SCIRXBUF.all; //
      RECEIVEDCOUNT++;
      if(RECEIVEDCOUNT==frame_length)
        {
	      RECEIVEDCOUNT=0;
		  if(rs485_rdata[frame_length-1]==0x55)
              rs485_rdata_deal();
        }//if
	  }//if
   }//if
}//for

}//main	

// SCIA  8-bit word, baud rate 0x000F, default, 1 STOP bit, no parity 
void scia_init()
{    
    // Note: Clocks were turned on to the SCIA peripheral
    // in the InitSysCtrl() function
    SciaRegs.SCICCR.all =0x0007;   // 1 stop bit,  No loopback 
                                   // No parity,8 char bits,
                                   // async mode, idle-line protocol
    SciaRegs.SCICTL1.all =0x0003;  // enable TX, RX, internal SCICLK, 
                                   // Disable RX ERR, SLEEP, TXWAKE
    SciaRegs.SCICTL2.all =0x0000; 
    SciaRegs.SCICTL2.bit.RXBKINTENA =0;
	SciaRegs.SCICTL2.bit.TXINTENA =0;
    SciaRegs.SCIHBAUD = 0x01;
    SciaRegs.SCILBAUD = 0xE7;      //To set the sci baud rate to 9600 bps
	                               //The LSPCLK has been set to 37.5M by default.
    SciaRegs.SCICTL1.all =0x0023;  // Relinquish SCI from Reset                                 
	EALLOW;
    SysCtrlRegs.PCLKCR.bit.SCIAENCLK = 1;
    EDIS;
	GpioDataRegs.GPEDAT.bit.GPIOE2=receive;
}

// Transmit a character from the SCI-A'
void scia_xmit(int a)
{
    GpioDataRegs.GPEDAT.bit.GPIOE2=transmit;
    SciaRegs.SCITXBUF=a;
	//.TXRDY
	while(SciaRegs.SCICTL2.bit.TXEMPTY==0) {;}  //To wait until transmission is completed.
    GpioDataRegs.GPEDAT.bit.GPIOE2=receive;
}
/* --------------------------------------------------- */
/* To deal with the rdata of 485                       */
/* The data received are stored in array rs485_rdata[frame_length]*/
/* The dealing of data abides by the BJTULFV08- NPS 008*/
/* --------------------------------------------------- */
void rs485_rdata_deal(void)
{
   Uint16 i,j;
   Uint32 temp;
//To check if the frame is transmitted correctly by the setter
   for(j=0;j<frame_length;j++)
     rs485_rdata[j]&=0x00ff;
   if(rs485_rdata[0]!=0x00aa)                      
           return;  // SOF error
   if(rs485_rdata[frame_length-1]!=0x0055)         
           return;  // EOF error
   if((rs485_rdata[1]&0x003f)!=rs485_node_address1)
           return;  // Node address unmatched 
   if((rs485_rdata[3])!=((~(rs485_rdata[2]))&0x00ff)) 
           return;  // op_code check failed
   if(rs485_rdata[8]!=(rs485_rdata[4]^rs485_rdata[5]^rs485_rdata[6]^rs485_rdata[7]))
           return;  // Data byte check failed

//To deal with the frames according to their types
   if((rs485_rdata[1]&0x00c0)==0)        //Data frame
     {
	   if((rs485_rdata[2]<0x00fd)&&(rs485_rdata[2]>0x00f0))   
	                                     // Working instructions
	    {
          Instruction_word=rs485_rdata[4];
	    }
	   else if(rs485_rdata[2]<rpara_num) // Working parameter settings
	    {
		  temp=0; 
	      temp|=((Uint32)(rs485_rdata[4])<<24);
		  temp|=((Uint32)(rs485_rdata[5])<<16);
		  temp|=((Uint32)(rs485_rdata[6])<<8);
		  temp|=((Uint32)(rs485_rdata[7]));
          if((temp<rparameter[rs485_rdata[2]].Max)&&(temp>rparameter[rs485_rdata[2]].Min))
            rparameter[rs485_rdata[2]].Value=temp;

	    }
     }
   else if((rs485_rdata[1]&0x00c0)==0x0040)   //Request frame
     {
       if(rs485_rdata[2]==0x00FD)
        {
		  // Error type
		  for(i=0;i<times;i++)
            rs485_tdata_deal(rs485_node_address0, rs485_rdata[2], Err_Flag, data_frame_type);          

        }
	   else if(rs485_rdata[2]<rpara_num+tpara_num)
	    {
		  // data parameters
		  for(i=0;i<times;i++)
            rs485_tdata_deal(rs485_node_address0, rs485_rdata[2], tparameter[rs485_rdata[2]-rpara_num].Value, data_frame_type);
	    }         
     }
   else if((rs485_rdata[1]&0x00c0)==0x0080)   //Heartbeat frame
     {
    
     }
   else if((rs485_rdata[1]&0x00c0)==0x00c0)   //Answer frame
     {
    
     }


}
/* --------------------------------------------------- */
/* To deal with the tdata of 485                       */
/* The deal of data abides by BJTULFV08- NPS 008*/
/* --------------------------------------------------- */
void rs485_tdata_deal(Uint16 node_address, Uint16 op_code, Uint32 tdata, Uint16 frame_type)
{
//node_address: the receive terminal of the frame
//op_code:      the operating code
//tdata:        the data to be transmitted
//frame_type:   the frame type 
   Uint16 i;
   for(i=0;i<frame_length;i++) rs485_tdata[i]=0; 
   rs485_tdata[0]=0xaa;               //SOF
   rs485_tdata[frame_length-1]=0x55;  //EOF
   rs485_tdata[1]|=node_address;
   if(frame_type==data_frame_type) //Data frame
     {
       rs485_tdata[1]|=0x0000;       
     }
   else if(frame_type==request_frame_type)
     {
       rs485_tdata[1]|=0x0040;      
     }
   else if(frame_type==heart_beat_frame_type)
     {
       rs485_tdata[1]|=0x0080;      
     }
   else if(frame_type==answer_frame_type)
     {
       rs485_tdata[1]|=0x00c0;      
     }
   rs485_tdata[2]=op_code;                     //op_code
   rs485_tdata[3]=(~(rs485_tdata[2]))&0x00ff;  //check byte of op_code
   rs485_tdata[7]=((Uint16)(tdata>>24)&0x00ff);//Data 0
   rs485_tdata[6]=((Uint16)(tdata>>16)&0x00ff);//Data 1
   rs485_tdata[5]=((Uint16)(tdata>>8)&0x00ff); //Data 2
   rs485_tdata[4]=((Uint16)(tdata)&0x00ff);    //Data 3
   rs485_tdata[8]=rs485_tdata[4]^rs485_tdata[5]^rs485_tdata[6]^rs485_tdata[7]; //Check byte
   rs485_tdata[8]&=0x00ff;
   for(i=0;i<frame_length;i++)   //To transmit data
     {
       scia_xmit(rs485_tdata[i]);
     }

}




⌨️ 快捷键说明

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