一涉及到通信就会比较令人头痛。好在不需要用到很高级的功能,直接看例程也算是一种学习方式吧。
280049C支持自动波特率,在手册的23.13.2节有介绍。
本文记录一下280049C自己使用的程序和TI提供的例程。
例程的位置在C:\ti\c2000\C2000Ware_3_01_00_00\driverlib\f28004x\examples\sci
LaunchPad板子的仿真器中会集成一个串口,所以不需要其他硬件就可以调试串口;需要一个软件:串口调试助手。
自己常用
贴一个自己常用的代码,串口收一个16进制数然后回送,感觉一般实验室的控制系统用这个就足够了。
#include "driverlib.h"
#include "device.h"
#ifdef _FLASH
// These are defined by the linker (see device linker command file)
extern uint16_t RamfuncsLoadStart;
extern uint16_t RamfuncsLoadSize;
extern uint16_t RamfuncsRunStart;
#endif
uint16_t msg=0; // 串口接收到的数据
__interrupt void sciaRxISR(void);
void mySCIAconfig(void); //GPIO 28 29 SCIARX中断 波特率115200 接收一位16进制并回送
void main(void)
{
// Configure PLL, disable WD, enable peripheral clocks.
Device_init();
// Disable pin locks and enable internal pullups.
Device_initGPIO();
// Disable global interrupts.
DINT;
// Initialize interrupt controller and vector table.
Interrupt_initModule();
Interrupt_initVectorTable();
IER = 0x0000;
IFR = 0x0000;
mySCIAconfig();
// Enable global interrupts.
EINT;
for(;;)
{
}
}
void mySCIAconfig(void)
{
/*
SCIA配置函数,GPIO28为Rx,GPIO29为Tx
波特率115200,映射并启用SCIA_RX中断
*/
// GPIO28 is the SCI Rx pin.
GPIO_setMasterCore(DEVICE_GPIO_PIN_SCIRXDA, GPIO_CORE_CPU1);
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCIRXDA);
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCIRXDA, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCIRXDA, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCIRXDA, GPIO_QUAL_ASYNC);
// GPIO29 is the SCI Tx pin.
GPIO_setMasterCore(DEVICE_GPIO_PIN_SCITXDA, GPIO_CORE_CPU1);
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCITXDA);
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCITXDA, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCITXDA, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCITXDA, GPIO_QUAL_ASYNC);
// Map the ISR to the wake interrupt.
Interrupt_register(INT_SCIA_RX, sciaRxISR);
// Initialize SCIA and its FIFO.
SCI_performSoftwareReset(SCIA_BASE);
// 波特率115200 8位数据 1位停止 无校验位
SCI_setConfig(SCIA_BASE, 25000000, 115200, (SCI_CONFIG_WLEN_8 |
SCI_CONFIG_STOP_ONE |
SCI_CONFIG_PAR_NONE));
SCI_resetChannels(SCIA_BASE);
SCI_clearInterruptStatus(SCIA_BASE, SCI_INT_RXRDY_BRKDT);
SCI_enableModule(SCIA_BASE);
SCI_performSoftwareReset(SCIA_BASE);
// Enable the TXRDY and RXRDY interrupts.
SCI_enableInterrupt(SCIA_BASE, SCI_INT_RXRDY_BRKDT);
// Clear the SCI interrupts before enabling them.
SCI_clearInterruptStatus(SCIA_BASE, SCI_INT_RXRDY_BRKDT);
// Enable the interrupts in the PIE: Group 9 interrupts 1 & 2.
Interrupt_enable(INT_SCIA_RX);
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
}
// sciaRxISR - Read the character from the RXBUF and echo it back.
__interrupt void sciaRxISR(void)
{
uint16_t receivedChar;
// Read a character from the RXBUF.
receivedChar = SCI_readCharBlockingNonFIFO(SCIA_BASE);
msg=receivedChar; // 更新全局变量
// Echo back the character.
SCI_writeCharBlockingNonFIFO(SCIA_BASE, receivedChar);
// Acknowledge the PIE interrupt.
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
}
示例1. SCI Echoback
此测试通过SCI-A端口接收和回送数据。串口的设置为:
波特率:9600
数据位:8
校验:无
停止位:1
程序将打印出一个问候语,然后要求您输入一个字符,它将回显到终端。
外部接线:
GPIO28是 SCI_A-RXD 需要连接PC-TX;
GPIO29是 SCI_A-TXD 需要连接PC-RX。
示例2. SCI Interrupt Echoback
此测试使用中断通过SCI-A端口接收和回送数据。
这是一个比较贴近实际应用的示例。
外部配置与上例相同。
#include "driverlib.h"
#include "device.h"
#ifdef _FLASH
// These are defined by the linker (see device linker command file)
extern uint16_t RamfuncsLoadStart;
extern uint16_t RamfuncsLoadSize;
extern uint16_t RamfuncsRunStart;
#endif
//
// Defines
//
// Define AUTOBAUD to use the autobaud lock feature
//#define AUTOBAUD
//
// Globals
//
uint16_t counter = 0;
unsigned char *msg;
//
// Function Prototypes
//
__interrupt void sciaTxISR(void);
__interrupt void sciaRxISR(void);
//
// Main
//
void main(void)
{
//
// Configure PLL, disable WD, enable peripheral clocks.
//
Device_init();
//
// Disable pin locks and enable internal pullups.
//
Device_initGPIO();
//
// GPIO28 is the SCI Rx pin.
//
GPIO_setMasterCore(DEVICE_GPIO_PIN_SCIRXDA, GPIO_CORE_CPU1);
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCIRXDA);
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCIRXDA, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCIRXDA, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCIRXDA, GPIO_QUAL_ASYNC);
//
// GPIO29 is the SCI Tx pin.
//
GPIO_setMasterCore(DEVICE_GPIO_PIN_SCITXDA, GPIO_CORE_CPU1);
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCITXDA);
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCITXDA, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCITXDA, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCITXDA, GPIO_QUAL_ASYNC);
//
// Disable global interrupts.
//
DINT;
//
// Initialize interrupt controller and vector table.
//
Interrupt_initModule();
Interrupt_initVectorTable();
IER = 0x0000;
IFR = 0x0000;
//
// Map the ISR to the wake interrupt.
//
Interrupt_register(INT_SCIA_TX, sciaTxISR);
Interrupt_register(INT_SCIA_RX, sciaRxISR);
//
// Initialize SCIA and its FIFO.
//
SCI_performSoftwareReset(SCIA_BASE);
//
// Configure SCIA for echoback.
//
SCI_setConfig(SCIA_BASE, 25000000, 9600, (SCI_CONFIG_WLEN_8 |
SCI_CONFIG_STOP_ONE |
SCI_CONFIG_PAR_NONE));
SCI_resetChannels(SCIA_BASE);
SCI_clearInterruptStatus(SCIA_BASE, SCI_INT_TXRDY | SCI_INT_RXRDY_BRKDT);
SCI_enableModule(SCIA_BASE);
SCI_performSoftwareReset(SCIA_BASE);
//
// Enable the TXRDY and RXRDY interrupts.
//
SCI_enableInterrupt(SCIA_BASE, SCI_INT_TXRDY | SCI_INT_RXRDY_BRKDT);
#ifdef AUTOBAUD
//
// Perform an autobaud lock.
// SCI expects an 'a' or 'A' to lock the baud rate.
//
SCI_lockAutobaud(SCIA_BASE);
#endif
//
// Send starting message.
//
msg = "\r\n\n\nHello World!\0";
SCI_writeCharArray(SCIA_BASE, (uint16_t*)msg, 17);
msg = "\r\nYou will enter a character, and the DSP will echo it back!\n\0";
SCI_writeCharArray(SCIA_BASE, (uint16_t*)msg, 62);
//
// Clear the SCI interrupts before enabling them.
//
SCI_clearInterruptStatus(SCIA_BASE, SCI_INT_TXRDY | SCI_INT_RXRDY_BRKDT);
//
// Enable the interrupts in the PIE: Group 9 interrupts 1 & 2.
//
Interrupt_enable(INT_SCIA_RX);
Interrupt_enable(INT_SCIA_TX);
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
//
// Enable global interrupts.
//
EINT;
for(;;)
{
}
}
//
// sciaTxISR - Disable the TXRDY interrupt and print message asking
// for a character.
//
__interrupt void
sciaTxISR(void)
{
//
// Disable the TXRDY interrupt.
//
SCI_disableInterrupt(SCIA_BASE, SCI_INT_TXRDY);
msg = "\r\nEnter a character: \0";
SCI_writeCharArray(SCIA_BASE, (uint16_t*)msg, 22);
//
// Ackowledge the PIE interrupt.
//
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
}
//
// sciaRxISR - Read the character from the RXBUF and echo it back.
//
__interrupt void
sciaRxISR(void)
{
uint16_t receivedChar;
//
// Enable the TXRDY interrupt again.
//
SCI_enableInterrupt(SCIA_BASE, SCI_INT_TXRDY);
//
// Read a character from the RXBUF.
//
receivedChar = SCI_readCharBlockingNonFIFO(SCIA_BASE);
//
// Echo back the character.
//
msg = " You sent: \0";
SCI_writeCharArray(SCIA_BASE, (uint16_t*)msg, 13);
SCI_writeCharBlockingNonFIFO(SCIA_BASE, receivedChar);
//
// Acknowledge the PIE interrupt.
//
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP9);
counter++;
}
//
// End of File
//
示例3. SCI Interrupt Echoback with FIFO
此测试通过SCI-A端口接收和回送数据,一次中断传送两个字符。在FIFO状态级别为2或更高之后触发Rx中断。一旦RXFIFO中有两个字符,SCI Rx ISR将被触发,并从FIFO中读取两个字符并将其写入传输缓冲区。然后,SCI Tx ISR将再次被触发,以从终端请求更多数据。
外部配置与上例相同。
参考文献
- 《手把手教你学DSP——基于TMS320x281x》