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

📄 example_281xsci_ffdlb_int.c

📁 TMS320F2812 SCI串口实验程序
💻 C
字号:

#include "DSP28_Device.h"
#include "ext_inf.h"
//#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
//#include "DSP281x_Examples.h"   // DSP281x Examples Include File

#define CPU_FREQ 	150E6
#define SCI_FREQ 	100E3
#define SCI_PRD 	CPU_FREQ/(SCI_FREQ*8)
unsigned int Sci_VarRx[100];
unsigned int i,j;
unsigned int Send_Flag;


unsigned int * LedReg  = (unsigned int *) 0x23FF;  //发光二极管控制寄存器	
unsigned int * Led8Lock= (unsigned int *) 0x25FF;  //数码管控制寄存器

//unsigned  int  *LedReg  = (unsigned int *)0x23FF;
//unsigned  int  *Led8Lock = (unsigned int *)0x25FF;
const	Uint16	LedCode[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF};
const	Uint16	SpiCode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
Uint16	LedCount = 0;
Uint16	SpiCount = 0;



// Prototype statements for functions found within this file.
interrupt void sciaTxFifoIsr(void);
interrupt void sciaRxFifoIsr(void);
interrupt void scibTxFifoIsr(void);
interrupt void scibRxFifoIsr(void);
void scia_fifo_init(void);
void scib_fifo_init(void);
void error(void);

// Global variables
Uint16 sdataA[8];    // Send data for SCI-A
Uint16 sdataB[8];    // Send data for SCI-B
Uint16 rdataA[8];    // Received data for SCI-A
Uint16 rdataB[8];    // Received data for SCI-A
Uint16 rdata_pointA; // Used for checking the received data
Uint16 rdata_pointB;
int a=0,z=0;

void main(void)
{ 
   Uint16 i;

// 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();    
// Setup only the GP I/O only for SCI-A and SCI-B functionality
   EALLOW;
   GpioMuxRegs.GPFMUX.bit.SCITXDA_GPIOF4 = 1;
   GpioMuxRegs.GPFMUX.bit.SCIRXDA_GPIOF5 = 1; 									         
   GpioMuxRegs.GPGMUX.bit.SCITXDB_GPIOG4 = 1;
   GpioMuxRegs.GPGMUX.bit.SCIRXDB_GPIOG5 = 1; 									         
   EDIS;

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts 
   DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.  
// This function is found in the DSP281x_PieCtrl.c file.
   InitPieCtrl();

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

// Initialize the PIE vector table with pointers to the shell Interrupt 
// Service Routines (ISR).  
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
   InitPieVectTable();

// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.  
   EALLOW;	// This is needed to write to EALLOW protected registers
   PieVectTable.RXAINT = &sciaRxFifoIsr;
   PieVectTable.TXAINT = &sciaTxFifoIsr;
   PieVectTable.RXBINT = &scibRxFifoIsr;
   PieVectTable.TXBINT = &scibTxFifoIsr;
   EDIS;   // This is needed to disable write to EALLOW protected registers 
 
 
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
   scia_fifo_init();  // Init SCI-A
   //scib_fifo_init();  // Init SCI-B
	
// Step 5. User specific code, enable interrupts:
 
// Init send data.  After each transmission this data
// will be updated for the next transmission
   for(i = 0; i<8; i++)
   {
      sdataA[i] = i;
      sdataA[i] = 0x00;
   }

   for(i = 0; i<8; i++)
   {
      sdataB[i] = 0xFF - i;
      sdataB[i] = 0;
   }

   rdata_pointA = sdataA[0];
   rdata_pointB = sdataB[0]; 
 
// Enable interrupts required for this example
   PieCtrl.PIECRTL.bit.ENPIE = 1;   // Enable the PIE block
   PieCtrl.PIEIER9.bit.INTx1=1;     // PIE Group 9, INT1
   PieCtrl.PIEIER9.bit.INTx2=1;     // PIE Group 9, INT2
   PieCtrl.PIEIER9.bit.INTx3=1;     // PIE Group 9, INT3
   PieCtrl.PIEIER9.bit.INTx4=1;     // PIE Group 9, INT4
   IER = 0x100;	// Enable CPU INT
   EINT;   

// Step 6. IDLE loop. Just sit and loop forever (optional):	
   for(;;);

} 	

void error(void)
{
    asm("     ESTOP0"); // Test failed!! Stop!
    for (;;);
}


interrupt void sciaTxFifoIsr(void)
{   
    Uint16 i;
    /*for(i=0; i< 8; i++)
    {
 	   //SciaRegs.SCITXBUF=sdataA[i];     // Send data
 	   SciaRegs.SCITXBUF=0x00;
	}

    for(i=0; i< 8; i++)                 //Increment send data for next cycle
    {
 	   sdataA[i] = (sdataA[i]+1) & 0x00FF; 
 	    
	}*/
	if(a==1)
	{
	   for(i=0; i< 8; i++)
       {
 	     SciaRegs.SCITXBUF=rdataA[i];     // Send data
 	   
	   }
	   a=0;
	 }
	// SciaRegs.SCITXBUF=0x01;/*在此处修改任何值都会被发送出去*/
	

	SciaRegs.SCIFFTX.bit.TXINTCLR=1;	// Clear SCI Interrupt flag
	PieCtrl.PIEACK.all|=0x100;      // Issue PIE ACK
}

interrupt void sciaRxFifoIsr(void)
{   
    Uint16 i;
	for(i=0;i<8;i++)
	{
	   rdataA[i]=SciaRegs.SCIRXBUF.all;	 // Read data
	   rdataA[i] &=0x00FF;
	}
	/*for(i=0;i<8;i++)                     // Check received data
	{
	   if(rdataA[i] != ( (rdata_pointA+i) & 0x00FF) ) error();
	}
	rdata_pointA = (rdata_pointA+1) & 0x00FF;*/
	a=1;                           
    //a=SciaRegs.SCIRXBUF.all;	 // Read data
	SciaRegs.SCIFFRX.bit.RXOVF_CLR=1;   // Clear Overflow flag
	SciaRegs.SCIFFRX.bit.RXFFOVF=1;   // Clear Interrupt flag

//	SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1;   // Clear Overflow flag
//	SciaRegs.SCIFFRX.bit.RXFFINT_CLR=1;   // Clear Interrupt flag
	PieCtrl.PIEACK.all|=0x100;       // Issue PIE ack
	
}

void scia_fifo_init()										
{
   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.bit.TXINTENA =1;
   SciaRegs.SCICTL2.bit.RXBKINTENA =1;
   /*SciaRegs.SCIHBAUD = 0x0000;
   SciaRegs.SCILBAUD = SCI_PRD;*/
   SciaRegs.SCIHBAUD = 0x00;
   SciaRegs.SCILBAUD = 0xA2;   //波特率19200
   //SciaRegs.SCICCR.bit.LOOPBKENA =1; // Enable loop back  
   SciaRegs.SCIFFTX.all=0xC028;   
   SciaRegs.SCIFFRX.all=0x0028;
   SciaRegs.SCIFFCT.all=0x00;

   SciaRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset 
   SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
   SciaRegs.SCIFFRX.bit.RXFIFORESET=1;	 
     									
     									    									    									
}  

interrupt void scibTxFifoIsr(void)
{       
    Uint16 i;
    for(i=0; i< 8; i++)
    {
 	   ScibRegs.SCITXBUF=sdataB[i];     // Send data
	}

    for(i=0; i< 8; i++)                 //Increment send data for next cycle
    {
 	   sdataB[i] = (sdataB[i]-1) & 0x00FF;  
	}	

	ScibRegs.SCIFFTX.bit.TXINTCLR=1;    // Clear Interrupt flag
	PieCtrl.PIEACK.all|=0x100;      // Issue PIE ACK
}

interrupt void scibRxFifoIsr(void)
{
    Uint16 i;
	for(i=0;i<8;i++)
	{
	   rdataB[i]=ScibRegs.SCIRXBUF.all;	 // Read data
	}
	for(i=0;i<8;i++)                     // Check received data
	{
	   //if(rdataB[i] != ( (rdata_pointB-i) & 0x00FF) ) error();
	}
	rdata_pointB = (rdata_pointB-1) & 0x00FF; 
	ScibRegs.SCIFFRX.bit.RXOVF_CLR=1;   // Clear Overflow flag
	ScibRegs.SCIFFRX.bit.RXFFOVF=1;   // Clear Interrupt flag
    PieCtrl.PIEACK.all|=0x100;  	// Issue PIE ack
}

void scib_fifo_init()										
{
   ScibRegs.SCICCR.all =0x0007;    // 1 stop bit,  No loopback 
                                   // No parity,8 char bits,
                                   // async mode, idle-line protocol
   ScibRegs.SCICTL1.all =0x0003;   // enable TX, RX, internal SCICLK, 
                                   // Disable RX ERR, SLEEP, TXWAKE
   ScibRegs.SCICTL2.bit.TXINTENA =1;
   ScibRegs.SCICTL2.bit.RXBKINTENA =1;
   /*ScibRegs.SCIHBAUD    =0x0000;
   ScibRegs.SCILBAUD    =SCI_PRD;*/
   SciaRegs.SCIHBAUD = 0x00;
   SciaRegs.SCILBAUD = 0xA2;
   ScibRegs.SCICCR.bit.LOOPBKENA =1; // Enable loop back  
   ScibRegs.SCIFFTX.all=0xC028;
   ScibRegs.SCIFFRX.all=0x0028;
   ScibRegs.SCIFFCT.all=0x00;
   ScibRegs.SCICTL1.all =0x0023;     // Relinquish SCI from Reset 
   ScibRegs.SCIFFTX.bit.TXFIFOXRESET=1;
   ScibRegs.SCIFFRX.bit.RXFIFORESET=1;	 
      
}  

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

⌨️ 快捷键说明

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