目录
1. SCI标准模式下发送
- ScibRegs.SCITXBUF = ‘A’;
- 在ScibRegs.SCICTL2.bit.TXEMPTY=1之后SCITXBUF中的数据自动到TXSHF(移位寄存器中)
- TXSHF移位到硬件引脚TXD
2. SCI标准模式下接收
- 数据到达RXD引脚之后,检测启动位
- TXSHF移位到ScibRegs.SCIRXBUF当中,产生一个中断申请
- ScibRegs.SCIRXST.bit.RXRDY标志位置1,表示接收到一个字节
- 通过读取g_recv_buff[s_recv_count++] =ScibRegs.SCIRXBUF.all,标志位RXRDY自动清除
3. 示例说明
void main(void)
{
char *msg;
InitSysCtrl();
InitScibGpio(); //GPIO14、GPIO15
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW;
PieVectTable.SCIRXINTB = &scibRxIsr;
// PieVectTable.SCITXINTB = &scibTxIsr;
EDIS;
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER9.bit.INTx3=1; // PIE Group 9, INT3
// PieCtrlRegs.PIEIER9.bit.INTx4=1; // 发送中断没必要开启
IER = M_INT9; // Enable CPU INT
EINT;
scib_fifo_init(); // Initialize the SCI
scib_echoback_init(); // Initalize SCI for echoback
msg = "Hello World!\n\0";
scib_msg(msg,strlen(msg));
for(;;)
}
/*
初始化SCIB
*/
void scib_echoback_init()
{
// ScibRegs.SCICCR.all =0x0007; //等价于下面的5条语句
ScibRegs.SCICCR.bit.STOPBITS = 0; //1 stop bit
ScibRegs.SCICCR.bit.PARITYENA = 0; //NO PARITY
ScibRegs.SCICCR.bit.LOOPBKENA = 0; //No loopback 1-自测模式即硬件内部收发引脚连在一起
ScibRegs.SCICCR.bit.ADDRIDLE_MODE = 0; //地址协议选择空闲线协议,1-选择地址位协议
ScibRegs.SCICCR.bit.SCICHAR = 7; //数据位8位
ScibRegs.SCICTL1.bit.RXENA = 1;//使能接收
ScibRegs.SCICTL1.bit.TXENA = 1;//使能发送
// ScibRegs.SCICTL2.bit.TXINTENA =1; //发送中断禁用使能
ScibRegs.SCICTL2.bit.RXBKINTENA =1; //接收中断使能
//波特率设置寄存器BRR = LSPCLK/(波特率*8)-1
#if (CPU_FRQ_150MHZ)
ScibRegs.SCIHBAUD =0x0001; // 9600 baud @LSPCLK = 37.5MHz.
ScibRegs.SCILBAUD =0x00E7;
#endif
ScibRegs.SCICTL1.bit.SWRESET =1; // 通过写1重新使能SCI,该复位每种外设都有
}
void scib_xmit(char a)
{
ScibRegs.SCITXBUF=a;
while(ScibRegs.SCICTL2.bit.TXEMPTY == 0){;}//等待发送结束,否则会覆盖之前要发送的内容
}
void scib_msg(char * msg, Uint16 len)
{
int i;
for(i = 0;i < len;i++)
{
scib_xmit(msg[i]);
}
}
/*
其实该函数没必要写,默认初值就是如此;这里写上是为了加深理解
*/
void scib_fifo_init()
{
ScibRegs.SCIFFTX.bit.TXFFIENA = 0;//禁用FIFO
}
__interrupt void scibRxIsr(void)
{
static unsigned char s_recv_count = 0;
g_recv_buff[s_recv_count++] =ScibRegs.SCIRXBUF.all; // Read data
if(s_recv_count >= 255)
s_recv_count = 0;
PieCtrlRegs.PIEACK.all|=0x100; // Issue PIE ack
}