tms320f2812串口发送数据的代码
时间: 2023-08-06 22:20:25 浏览: 150
以下是基于TMS320F2812的串口发送数据的代码示例:
```c
#include "DSP281x_Device.h"
#include "DSP281x_Examples.h"
void InitSciaGpio()
{
EALLOW;
GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1;
GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1;
GpioCtrlRegs.GPADIR.bit.GPIO19 = 1;
GpioCtrlRegs.GPADIR.bit.GPIO18 = 0;
EDIS;
}
void InitScia(int baudrate)
{
int sciaClk = 150000000; // 150 MHz
int divisor = (int)(sciaClk / (baudrate * 8.0)) - 1;
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 = 0; // disable TX interrupt
SciaRegs.SCICTL2.bit.RXBKINTENA = 0;// disable RXBK interrupt
SciaRegs.SCIHBAUD = (divisor >> 8) & 0xFF; // high byte of divisor
SciaRegs.SCILBAUD = divisor & 0xFF; // low byte of divisor
SciaRegs.SCICCR.bit.LOOPBKENA = 0; // disable loopback
SciaRegs.SCIFFTX.all = 0xC022; // enable FIFO, TX FIFO interrupt when 2 words are present
SciaRegs.SCIFFRX.all = 0x0022; // enable FIFO, RX FIFO interrupt when 2 words are present
SciaRegs.SCICTL1.all = 0x0023; // Relinquish SCI from Reset
}
void scia_fifo_init()
{
// Clear the FIFO
SciaRegs.SCIFFTX.bit.TXFIFOXRESET = 1;
SciaRegs.SCIFFTX.bit.TXFIFOXRESET = 0;
SciaRegs.SCIFFTX.bit.TXFFIENA = 0; // disable TX FIFO interrupt
SciaRegs.SCIFFTX.bit.SCIFFENA = 1; // enable FIFO
SciaRegs.SCIFFRX.bit.RXFIFORESET = 1;
SciaRegs.SCIFFRX.bit.RXFIFORESET = 0;
SciaRegs.SCIFFRX.bit.RXFFIENA = 0; // disable RX FIFO interrupt
SciaRegs.SCIFFRX.bit.RXFFIL = 0x4; // set RX FIFO interrupt level to 4
}
void scia_xmit(int a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}; // Wait for space in FIFO
SciaRegs.SCITXBUF = a;
}
void scia_msg(char *msg)
{
int i;
i = 0;
while (msg[i] != '\0')
{
scia_xmit(msg[i]);
i++;
}
}
void main(void)
{
InitSysCtrl();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitSciaGpio();
InitScia(115200);
scia_fifo_init();
scia_msg("Hello World\r\n");
while (1) {};
}
```
该示例使用 SCI_A 模块进行串口数据发送,使用了 FIFO 缓冲区来提高发送效率。在 main 函数中,调用了 scia_msg 函数发送字符串 "Hello World"。注意,串口发送的波特率需要与外部设备的波特率匹配。
阅读全文