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

📄 dsp28_sci.c

📁 使用芯片为TI公司的DSP2812。该程序用以完成对直流电机的控制
💻 C
字号:
//
//      TMDX ALPHA RELEASE
//      Intended for product evaluation purposes
//
//###########################################################################
//
// FILE:	DSP28_Sci.c
//
// TITLE:	DSP28 SCI Initialization & Support Functions.
//
//###########################################################################
//
//  Ver | dd mmm yyyy | Who  | Description of changes
// =====|=============|======|===============================================
//  0.55| 06 May 2002 | L.H. | EzDSP Alpha Release
//  0.56| 20 May 2002 | L.H. | No change
//  0.57| 27 May 2002 | L.H. | No change
//###########################################################################

#include "DSP28_Device.h"

#include "Sci.h"

unsigned int * UART_MODE = (unsigned int *) 0x4010;
unsigned char SciaInbuf[BUFLEN];
int velocity = 0;
int Temperature = 0;
//---------------------------------------------------------------------------
// InitSPI: 
//---------------------------------------------------------------------------
// This function initializes the SPI(s) to a known state.
//

void InitSci(void)
{
	// Initialize SCI-A:
	*UART_MODE = 0x44; // Rs232,全双工,高速转换率	
	
	EALLOW;
//   SysCtrlRegs.PCLKCR.bit.SCIENCLKA=1;
// SysCtrlRegs.PCLKCR.bit.SCIENCLKB=1;	
	GpioMuxRegs.GPFMUX.all |= 0x0030;   
//	GpioMuxRegs.GPGMUX.all = 0x0030;
	EDIS;
	/* loopback   8 bit data */
	SciaRegs.SCICCR.all = 0x07;  // one stop bit, enable odd parity, idle mode, 8 bits character length, idle_line mode.
	
//	SciaRegs.SCICTRL1.all = 0x20 //  reset SCIA status bit
	SciaRegs.SCICTL1.all = 0x03; // disable RxERR INT, enable Tx&Rx, disable Sleep,
								// transmit feature is not selected, reset SCI.
	
	SciaRegs.SCICTL2.all = 0x0;  // disable Rx/BRK INT & TxINT
	
	SciaRegs.SCIHBAUD = 0x01;     // 9600 bps
	SciaRegs.SCILBAUD = 0x0E7;
//	SciaRegs.SCIHBAUD = 0x00;     // 19200 bps
//	SciaRegs.SCILBAUD = 0xF3;

	
	SciaRegs.SCICTL1.all = 0x23; // reenable SCI
	SciaRegs.SCIFFTX.all = 0xE040; // enable scia FIFO, TX FIFO interrupt disabled;
									// TXFFIVL is 0;
	SciaRegs.SCIFFTX.all &= 0x5FFF;	// reset TX FIFO
	SciaRegs.SCIFFTX.all |= 0xA000; // resume TX FIFO
	SciaRegs.SCIFFRX.all = 0x6045; // disable RX FIFO interrupt based on RXFFIVL( the value is 2);
	SciaRegs.SCIFFRX.all &= 0xDFFF; // reset RX FIFO
	SciaRegs.SCIFFRX.all |= 0x2000; // reenable RX FIFO
	SciaRegs.SCIFFCT.bit.FFTXDLY = 0; // the delay time of FIFO data shifting to TXSHFT register	

	// Initialize SCI-B:
	//tbd...
}	

int Receive(void)
{
    int i = 0;
	unsigned char checksum;	
	
/*	if(SciaRegs.SCIRXST.bit.RXERR|SciaRegs.SCIFFRX.bit.RXFFOVF)
		{
		i++;
		}*/
	checksum = 0x007F & SciaRegs.SCIRXBUF.all;
	for(i=0;i<BUFLEN;i++)
		{
		SciaInbuf[i] = 0x00FF & SciaRegs.SCIRXBUF.all; 
		checksum ^= SciaInbuf[i];
		}
	 
	if(!(checksum&0x7F))
		{
        //ScibRegs.SCITXBUF = 'O';
        SendChar('O');
        return 1;
        }
    else
    	{ 
        //ScibRegs.SCITXBUF = 'E';		
        SendChar('E');
        return 0;
		}
}

void Communicate(void)
{
	Uint16 tmp,spd;
//	float f;
//	_iq iq;

 	if(SciaRegs.SCIFFRX.bit.RXFFINT)
	 	{
		SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1;		 	
       	if(Receive())
       		{
       		tmp = Interpreter(SciaInbuf);
       		switch(tmp)
       			{
           		case MODIFY:
//               		spd = Pack(SciaInbuf);
  //             		fVelocityReference = (float)BCD2BIN(spd);
/*  						spd = BCD2BIN(Pack(SciaInbuf));
  						if(!iModifyType)
  							{
  							if((spd>=1) && (spd <=4))
  								{
  								iModifyType = spd;
  								}
  							else
  								{
  								fVelocityReference = (float)spd;
  								}  							
  							}
  						else
  							{
  							f = ((float)spd)/10000.0;
  							iq = _IQ(f);
  							switch(iModifyType)
  								{
  								case KP:
								//	oVelocityPI.Kp_reg3 = iq;	
  									break;
  								case KI:
  								//	oVelocityPI.Ki_reg3 = iq;
  									break;
  								case KD:
  								//	oVelocityPI.Kd_reg3 = iq;
  									break;
  								case KC:
  								//	oVelocityPI.Kc_reg3 = iq;
  									break;
  								default:
  									break;  								
  								}
  							iModifyType = 0;
  							}
*/  							
               		break;

           		case SPEED:
               		spd = BIN2BCD((unsigned int) velocity);
               		Unpack(spd, SciaInbuf);
               		SendString(SciaInbuf,BUFLEN);
               		break;
           	    case TEMP:
               		tmp = BIN2BCD(Temperature);
               		Unpack(tmp, SciaInbuf);
               		SendString(SciaInbuf,BUFLEN);
               		break;
               	default:
               		break;
       			}
       		}
       	}
}	
	
//===========================================================================
// No more.
//===========================================================================


/////////***************///////////////////

Uint16 Interpreter(unsigned char *buf)
{
  	unsigned char ch;
  	
	ch = *buf;
  	if(ch>='0'&&ch<='9')
  		{
  		return(MODIFY);
  		}	
    if(ch=='R')
  		{
      	return(SPEED);
      	}    
   if(ch=='T')
  		{
      	return(TEMP);
  		}
   return(0);
}

/*************************************************/
/*                                               */
/* The function changes a compact BCD code into  */
/* a binary                                      */
/*                                               */
/* Designed by K.Shuang                          */
/* Date 2005-9-30                                */
/* Modified by                                   */
/* Date                                          */
/*                                               */
/*************************************************/
Uint16 BCD2BIN(Uint16 bcd)
{
    int val;
    val = ((bcd&0xF000)>>12)*1000;
    val += ((bcd&0x0F00)>>8)*100;
    val += ((bcd&0x00F0)>>4)*10;
    val += bcd&0x000F;
    return(val);
}

/*************************************************/
/*                                               */
/* The function changes a binary into a compact  */
/* BCD code                                      */
/*                                               */
/* Designed by Dr. K.Shuang                      */
/* Date 2005-9-15                                */
/* Modified by N.Xiao                            */
/* Date  2005-9-15                               */
/*                                               */
/*************************************************/
Uint16 BIN2BCD(unsigned int bin)
{
    unsigned int a, v;
    a = bin%10;
    v = a;
    bin /= 10;
    a = bin%10;
    v |= a<<4;
    bin /= 10;
    a = bin%10;
    v |= a<<8;
    bin /= 10;
    a = bin%10;
    v |= a<<12;
    return(v);
}



/*************************************************/
/*                                               */
/* The function changes a compact BCD code into  */
/* a ASC code                                    */
/*                                               */
/* Designed by K.Shuang                          */
/* Date 2005-9-30                                */
/* Modified by                                   */
/* Date                                          */
/*                                               */
/*************************************************/
void Unpack(Uint16 spd,unsigned char *buf)
{
   buf[0] = ((spd&0xF000)>>12) | 0x30;
   buf[1] = ((spd&0x0F00)>>8) | 0x30;
   buf[2] = ((spd&0x00F0)>>4) | 0x30;
   buf[3] = (spd&0x000F) | 0x30;
}


/*************************************************/
/*                                               */
/* The function changes a ASC code into a compact*/
/* BCD code                                      */
/*                                               */
/* Designed by K.Shuang                          */
/* Date 2005-9-30                                */
/* Modified by                                   */
/* Date                                          */
/*                                               */
/*************************************************/
Uint16 Pack(unsigned char *buf)
{
    Uint16 tmp;
    tmp = (buf[0]&0x0F)<<12;
    tmp |= (buf[1]&0x0F)<<8;
    tmp |= (buf[2]&0x0F)<<4;
    tmp |= buf[3]&0x0F;
    return(tmp);
}


void SendChar(unsigned char ch)
{

	while(SciaRegs.SCIFFTX.bit.TXFFST>=16);
   	SciaRegs.SCITXBUF = ch;
}

/*************************************************/
/*                                               */
/* The function uses to send a string of         */
/* characters,the length is strlen               */
/*                                               */
/* Designed by K.Shuang                          */
/* Date 2005-9-30                                */
/* Modified by                                   */
/* Date                                          */
/*                                               */
/*************************************************/
void SendString(unsigned char *str,Uint16 strlen)
{
    Uint16 k;
    unsigned char ch;
    unsigned char checksum;

    SciaRegs.SCIFFRX.bit.RXFFIL = 1;
    SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1;	
    checksum = 0;
    for (k=0;k<strlen;k++)
        {
        checksum ^= *(str+k);
        }	
    checksum |= 0x80;
    do
    {
        SendChar(checksum);
        k = 0;
        do
        {
            SendChar(*(str + k));
            k++;
        } while(k < strlen);
       	while(!SciaRegs.SCIFFRX.bit.RXFFINT);
       	SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1;
       	ch = 0x00FF & SciaRegs.SCIRXBUF.all;        	
    } while(ch!='O');
    SciaRegs.SCIFFRX.bit.RXFFIL = 5;
}	
//===========================================================================
// No more.
//===========================================================================

⌨️ 快捷键说明

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