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

📄 sci.c

📁 这是一个基于DSP2812的串口通讯下位机程序,通过该程序可以实现和上位机通讯,将上位机的数据读入,设置电机的转矩和转速.挺好的一个程序,供参考.
💻 C
字号:
/* 2812A板:
	使用直连的串口通信电缆;
	启动串口调试助手.exe;
	PC机发送一个"."为结束标志
*/
#include "DSP281x_Device.h"
#include "DSP281x_Examples.h"

// Prototype statements for functions found within this file.
void scia_loopback_init(void);
void scia_fifo_init(void);	
void scia_xmit(int a);
void error(int);
void wait(int nWait);
interrupt void scia_rx_isr(void);
interrupt void scia_tx_isr(void);

// Global counts used in this example
Uint16 LoopCount;
Uint16 ErrorCount; 
Uint16 Speed,motorspeed,Torque,motortorque,uSpeed[16],uTorque[16];
char cString[17]={ "Hello PC!,Over|" },cReceive,cBuffer[17],cAnswer[16]={"Ok"};
void main(void)
{    
    char ReceivedChar;   
    int i,k=0,second=0,nLen,bReceive=0;
// Step 1. Initialize System Control registers, PLL, WatchDog, Clocks to default state:
        // This function is found in the DSP281x_SysCtrl.c file.
	InitSysCtrl();

// Step 2. Select GPIO for the device or for the specific application:
       // This function is found in the DSP281x_Gpio.c file.
       // InitGpio(); skip this as this is example selects the I/O for SCI in this file itself
    EALLOW;
    GpioMuxRegs.GPFMUX.all=0x0030;	// Select GPIOs to be Sci pins	 
                                    // Port F MUX - x000 0000 0011 0000
    EDIS;

// Step 3. Initialize PIE vector table:
      // The PIE vector table is initialized with pointers to shell Interrupt 
      // Service Routines (ISR).  The shell routines are found in DSP281x_DefaultIsr.c.
      // Insert user specific ISR code in the appropriate shell ISR routine in 
      // the DSP28_DefaultIsr.c file.

      // Disable and clear all CPU interrupts:
	DINT;
	IER = 0x0000;
	IFR = 0x0000;

      // Initialize Pie Control Registers To Default State:
      // This function is found in the DSP281x_PieCtrl.c file.
	  // InitPieCtrl();  PIE is not used for this example

      // Initialize the PIE Vector Table To a Known State:
      // This function is found in DSP281x_PieVect.c.
      // This function populates the PIE vector table with pointers
      // to the shell ISR functions found in DSP281x_DefaultIsr.c.
	  InitPieVectTable();  

      // Enable CPU and PIE interrupts
      // This example function is found in the DSP281x_PieCtrl.c file.   
      EnableInterrupts();
	
// Step 4. Initialize all the Device Peripherals to a known state:
      // This function is found in DSP281x_InitPeripherals.c
      // InitPeripherals(); skip this for SCI tests
	
// Step 5. User specific functions, Reassign vectors (optional), Enable Interrupts:

    LoopCount = 0;
    ErrorCount = 0;
    
    scia_fifo_init();	   // Initialize the SCI FIFO
    scia_loopback_init();  // Initalize SCI for digital loop back 

    // Note: Autobaud lock is not required for this example
    
    // Send a character starting with 0 
 						

// Step 6. Send Characters forever starting with 0x00 and going through
// 0xFF.  After sending each, check the recieve buffer for the correct value
	for(;;)
    {        
    		k=0;
    		second=0;
    		Speed=0;
    		Torque=0;      
       		while(1)
       		{
            while(SciaRegs.SCIFFRX.bit.RXFIFST ==0) { } // 如果接受寄存器不为0则跳出
            ReceivedChar = SciaRegs.SCIRXBUF.all;
            cBuffer[k]=ReceivedChar; 
            if ( ReceivedChar==' ')
            second=k;
            
	        if ( ReceivedChar=='.')
			{
				cBuffer[k+1]='\0';
				nLen=k+1;
				bReceive=1;
				Speed=0;
				for(i=0;i<second;i++)
				{
					uSpeed[i]=cBuffer[i]-'0';
					Speed=Speed*10+uSpeed[i];
				}
				motorspeed=Speed;
				for(i=second+1;i<k;i++)
				{
					uTorque[i]=cBuffer[i]-'0';
					Torque=Torque*10+uTorque[i];
				}
				motortorque=Torque;
				for ( i=0;i<2;i++ )
				{			
				scia_xmit(cAnswer[i]);
		     	while(SciaRegs.SCIFFTX.bit.TXFFST !=0) { }
				}
				break;
			}
		    k++; k%=16;
		    }
		   
      
    }

} 	


// Step 7. Insert all local Interrupt Service Routines (ISRs) and functions here:	

void error(int ErrorFlag)
{
      ErrorCount++;

}

// Test 1,SCIA  DLB, 8-bit word, baud rate 0x000F, default, 1 STOP bit, no parity 
void scia_loopback_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 =0x0003; 
	SciaRegs.SCICTL2.bit.TXINTENA =1;
	SciaRegs.SCICTL2.bit.RXBKINTENA =1;
    SciaRegs.SCIHBAUD    =0x0001;
    SciaRegs.SCILBAUD    =0x00e7;
	SciaRegs.SCICCR.bit.LOOPBKENA =0; // disable loop back  
	SciaRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset 
}

// Transmit a character from the SCI'
void scia_xmit(int a)
{
    SciaRegs.SCITXBUF=a;
}    

// Initalize the SCI FIFO
void scia_fifo_init()										
{
    SciaRegs.SCIFFTX.all=0xE040;
    SciaRegs.SCIFFRX.all=0x204f;
    SciaRegs.SCIFFCT.all=0x0;
    
}  

void wait(int nWait)
{
	int i,j,k=0;
	for ( i=0;i<nWait;i++ )
		for ( j=0;j<64;j++ )
			k++;
}                							
 
    


//===========================================================================
// No more.
//===========================================================================

⌨️ 快捷键说明

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