【DSP】TMS320F28035串口SCI不定长字节数据FIFO收发,数据帧格式:数据+0X0D 0X0A(结尾)

 【DSP】TMS320F28035串口SCI不定长字节数据FIFO收发,数据帧格式:数据+0X0D 0X0A(结尾)_第1张图片

/*************************************************************************
	   串口中断接收数据帧格式:数据+0X0D 0X0A(结尾)
*************************************************************************/
#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File

#define CPU_FREQ    60E6
#define LSPCLK_FREQ CPU_FREQ/4
#define SCI_FREQ    100E3
#define SCI_PRD     (LSPCLK_FREQ/(SCI_FREQ*8))-1
#define USART_REC_LEN           200     //定义最大接收字节数 200

__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);


Uint8 USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15,    接收完成标志
//bit14,    接收到0x0d
//bit13~0,  接收到的有效字节数目
Uint16 USART_RX_STA=0;       //接收状态标记

Uint16 sdataA[20];           // Send data for SCI-A
Uint16 rdataA[20];           // Received data for SCI-A

void main(void)
{
    Uint16 i;
    InitSysCtrl();
    InitSciGpio();
    DINT;
    InitPieCtrl();
    IER = 0x0000;
    IFR = 0x0000;
    InitPieVectTable();
    EALLOW;         // This is needed to write to EALLOW protected registers
    PieVectTable.SCIRXINTA = &sciaRxFifoIsr;
    PieVectTable.SCITXINTA = &sciaTxFifoIsr;
EDIS;   // This is needed to disable write to EALLOW protected registers

    scia_fifo_init();                     // Init SCI-A
    PieCtrlRegs.PIECTRL.bit.ENPIE = 1;   // Enable the PIE block
    PieCtrlRegs.PIEIER9.bit.INTx1=1;     // PIE Group 9, INT1
    PieCtrlRegs.PIEIER9.bit.INTx2=1;     // PIE Group 9, INT2
    IER = 0x100;                         // Enable CPU INT
    EINT;

    while(1)
    {
        char* msg;
        Uint8 len,t;
        if(USART_RX_STA&0x8000)
        {
            len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度

            msg = "\r\n您发送的消息为:\r\n\r\n";
            scia_msg(msg);

            for(t=0;t(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
           }
       }
   }

    SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1;   // Clear Overflow flag
    SciaRegs.SCIFFRX.bit.RXFFINTCLR=1;   // Clear Interrupt flag

    PieCtrlRegs.PIEACK.all|=0x100;       // Issue PIE ack
}

void
scia_xmit(int a)
{
    while (SciaRegs.SCIFFTX.bit.TXFFST != 0)
    {

    }
    SciaRegs.SCITXBUF=a;
}

void
scia_msg(char * msg)
{
    int i;
    i = 0;
    while(msg[i] != '\0')
    {
        scia_xmit(msg[i]);
        i++;
    }
}

void
scia_fifo_init()
{
    SciaRegs.SCICCR.all =0x0007;        //8位数据格式,选择地址位协议
    SciaRegs.SCICTL1.all =0x0003;       //使能SCI发送和接收
    SciaRegs.SCICTL2.bit.TXINTENA =1;  //使能发送中断(由于下面配置失能了FIFO发送中断,所以这里使不使能都没用)
    SciaRegs.SCICTL2.bit.RXBKINTENA =1;//使能接收中断
    SciaRegs.SCIHBAUD = 0x0000;
    SciaRegs.SCILBAUD = 0x00c2;        //设置波特率9600
    SciaRegs.SCICCR.bit.LOOPBKENA =0; // 关闭自测模式
    SciaRegs.SCIFFTX.all=0xC001;       //失能FIFO发送中断,设置发送FIFO深度为1
    SciaRegs.SCIFFRX.all=0x0021;       //使能FIFO接收中断,设置接收FIFO深度为1,接收一个字节数据就产生FIFO接收中断,效率不高,面对少量不定长数据发送比较合适。
    SciaRegs.SCIFFCT.all=0x00;         //FIFO控制寄存器
    SciaRegs.SCICTL1.all =0x0023;      // 禁止软复位
    SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1; //发送FIFO复位
    SciaRegs.SCIFFRX.bit.RXFIFORESET=1;  //接收FIFO复位
}



 

你可能感兴趣的:(DSP)