281x-SCI和PC间的通讯
// $ Date: 30/11/2009 整理: eyes417 $
//###########################################################################
//
// FILE : 281x-SCI_PC.c
//
// TITLE: 281x-SCI和PC间的通讯 --- 查询方式,使用FIFO
// PC发送至串口,DSP再回送至PC //
//
// ASSUMPTIONS:
//
// As supplied, this project is configured for "boot to H0" operation.
// Other then boot mode pin configuration, no other hardware configuration is required.
//
//###########################################################################
# include "DSP281x_Device.h" // DSP281x Headerfile Include File
# include "DSP281x_Examples.h" // DSP281x Examples Include File
void scia_echoback_init(void);
void scia_fifo_init(void);
void scia_xmit(int a); //串口发送--数据
void scia_msg(char *msg); //串口发送--字符串
Uint16 LoopCount;
void main(void)
{
Uint16 ReceivedChar;
char *msg;
// Step 1. Initialize System Control:
InitSysCtrl();
// Step 2. Initalize GPIO:
// InitGpio(); //配置IO口功能为SCIA和SCIB模式
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:
DINT; // Disable CPU interrupts
InitPieCtrl(); //初始化PIE控制寄存器
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable(); //初始化PIE中断向量表
// Step 4. Initialize all the Device Peripherals:
// InitPeripherals(); //初始化所有外设
// Step 5. User specific code:
scia_fifo_init();
scia_echoback_init();
//从PC上的串口调试助手输入要发送的数据至DSP
msg = "/r/nYou will enter a character, and the DSP will echo it back! /n/0";
scia_msg(msg);
for(;;)
{
msg = "/r/nEnter a character: /0";
scia_msg(msg);
// 查询方式--接收--使用FIFO
while(SciaRegs.SCIFFRX.bit.RXFIFST!=1) { } // wait for XRDY =1 for empty state
//XRDY =1 (空状态)等待数据发送
// 接收数据
ReceivedChar = SciaRegs.SCIRXBUF.all;
// Echo character back
msg = " You sent: /0";
scia_msg(msg);
scia_xmit(ReceivedChar); //串口发送函数
LoopCount++;
} //end for
} //end main
//SCI-A初始化
void scia_echoback_init()
{
//通信控制寄存器,1个停止位,无奇偶校验,自测试禁止,空闲线模式,字符长度8位
SciaRegs.SCICCR.all =0x0007;
//禁止接收错误中断,禁止休眠,使能发送,接收
SciaRegs.SCICTL1.all =0x0003;
SciaRegs.SCICTL2.all =0x0003;
SciaRegs.SCICTL2.bit.TXINTENA =1; //使能TXRDY中断
SciaRegs.SCICTL2.bit.RXBKINTENA =1; //接收缓冲器中断使能
//波特率设置 SCI-Band=LSPCLK/((BRR+1)*8)---9600K
SciaRegs.SCIHBAUD = 0x0001;
SciaRegs.SCILBAUD = 0x00E7;
SciaRegs.SCICTL1.all =0x0023; // SCI初始化完成退出复位重启状态
}
//串口发送--变量
void scia_xmit(int a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
SciaRegs.SCITXBUF=a;
}
//串口发送--字符或字符串
void scia_msg(char * msg)
{
int16 i;
i = 0;
while(msg[i] != '/0')
{
scia_xmit(msg[i]);
i++;
}
}
// 初始化 SCI--FIFO
void scia_fifo_init()
{
SciaRegs.SCIFFTX.all=0xE040; //允许接收,使能FIFO,清除TXFIFINT
SciaRegs.SCIFFRX.all=0x204f; //使能FIFO接收,清除RXFFINT,16级FIFO
SciaRegs.SCIFFCT.all=0x0000; //禁止波特率校验
}
//===========================================================================
// No more.
//===========================================================================