实现一个功能的时候首先查看相关的资料,例如数据手册,论坛、百度、书籍等,搜集各种相关资料,然后看别人是如何实现的,分析下相关步骤,理清好思路。针对不懂的地方继续查找资料,层层递进。(如果想省事,可以在别人正确代码的基础上进行修改,看他配置了什么寄存器,实现了什么功能,然后根据自己的需求,查看数据手册重新配置),实现的时候可以一个个小功能的实现,遇到疑惑的除求助外,也可试着观察不同的情况下会出现什么结果。总之就是多搜,多想,多动手,多总结。
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define SCIB 0
#define SCIC 1
// Prototype statements for functions found within this file.
void scic_fifo_init(void);
void scic_xmit(int a);
interrupt void uart_send(void);
// Global counts used in this example
Uint16 isrCount=0;
Uint16 ErrorCount=0;
Uint16 sdata[4]={5,5,5,5};//要发送的数据
void main(void)
{
Uint16 i=0;
// Step 1. Initialize System Control registers, PLL, WatchDog, Clocks to default state:
// This function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Select GPIO for the device or for the specific application:
// This function is found in the DSP2833x_Gpio.c file.
// InitGpio(); skip this as this is example selects the I/O
// for SCI-A in this file itself
InitSciGpio();
// Step 3. Initialize PIE vector table:
// The PIE vector table is initialized with pointers to shell Interrupt
// Service Routines (ISR). The shell routines are found in DSP2833x_DefaultIsr.c.
// Insert user specific ISR code in the appropriate shell ISR routine in
// the DSP28_DefaultIsr.c file.
// Disable and clear all CPU interrupts:
DINT;
IER = 0x0000;
IFR = 0x0000;
// Initialize Pie Control Registers To Default State:
// This function is found in the DSP2833x_PieCtrl.c file.
// InitPieCtrl(); PIE is not used for this example
// Initialize the PIE Vector Table To a Known State:
// This function is found in DSP2833x_PieVect.c.
// This function populates the PIE vector table with pointers
// to the shell ISR functions found in DSP2833x_DefaultIsr.c.
InitPieVectTable();
// Enable CPU and PIE interrupts
// This example function is found in the DSP2833x_PieCtrl.c file.
EnableInterrupts();
// Step 4. Initialize all the Device Peripherals to a known state:
// This function is found in DSP2833x_InitPeripherals.c
// InitPeripherals(); skip this for SCI tests
// Step 5. User specific functions, Reassign vectors (optional), Enable Interrupts:
isrCount = 0;
ErrorCount = 0;
#if SCIB
scib_fifo_init(); // Initialize the SCI FIFO
scib_loopback_init(); // Initalize SCI
#elif SCIC
scic_fifo_init(); // Initialize the SCI FIFO
#endif
// Send a character ,先给发送缓冲寄存器赋值。
for(i=0;i<4;i++)
{
scic_xmit(sdata[i]+0x30);
}
scic_xmit(' ');
// Step 6. Send Characters forever starting with 0x00 and going through
// 0xFF. After sending each, check the recieve buffer for the correct value
EALLOW;
// PieVectTable.SCIRXINTC = &uartIsr;
PieVectTable.SCITXINTC = &uart_send;
EDIS;
PieCtrlRegs.PIECTRL.bit.ENPIE=1;
// PieCtrlRegs.PIEIER8.bit.INTx5=1;
PieCtrlRegs.PIEIER8.bit.INTx6=1;
IER|=M_INT8;
EINT;
ERTM;
for(;;){ }
}
interrupt void uart_send(void)
{
Uint16 i;
isrCount++;
sdata[3]=isrCount;
for(i=0;i<4;i++)
{
scic_xmit(sdata[i]+0x30);
}
scic_xmit(' ');
if(isrCount==10)
{
ScicRegs.SCICTL1.bit.TXENA =0;//禁止发送缓冲器工作。
}
PieCtrlRegs.PIEACK.all=0xffff;//0x0080;
ScicRegs.SCIFFTX.bit.TXFFINTCLR=1; // Clear SCI Interrupt flag
}
// Transmit a character from the SCI'
void scic_xmit(int a)//发送一个数据a,类型为int
{
ScicRegs.SCITXBUF=a;//向数据缓冲寄存器中写入数据即可发送该数据
}
// Initalize the SCI FIFO
void scic_fifo_init()
{
// Test 1,SCIC DLB, 8-bit word, baud rate 9.6k, default, 1 STOP bit, no parity
//功能是配置发送模式
// Note: Clocks were turned on to the SCIC peripheral
// in the InitSysCtrl() function
// ScicRegs.SCICTL1.all =0x0000; //开始的时候先禁止接收与发送功能
ScicRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
//数据长度8位,一个结束位,无奇偶校验,空闲线模式,禁止回送
// ScicRegs.SCICTL1.all =0x0003; // enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
ScicRegs.SCICTL1.all =0x0002; //允许发送,禁止接收
// ScicRegs.SCICTL2.all =0x0001;//发送缓冲器中断使能。似乎与下面的重复了
ScicRegs.SCICTL2.bit.TXINTENA =1;//发送缓冲器中断使能。
// ScicRegs.SCICTL2.bit.RXBKINTENA =1;
ScicRegs.SCIHBAUD =0x0001;
ScicRegs.SCILBAUD =0x00e7;
//上面是波特率设置,书上写的0x00e7
// ScicRegs.SCICCR.bit.LOOPBKENA =0;// enable(Disable) loop back
ScicRegs.SCICTL1.all =0x0022; // Relinquish SCI from Reset
//FIFO设置
ScicRegs.SCIFFTX.bit.TXFIFOXRESET=0;
// ScicRegs.SCIFFRX.bit.RXFIFORESET=0;
ScicRegs.SCIFFTX.all=0xE060;
// ScicRegs.SCIFFRX.all=0x204f;
ScicRegs.SCIFFCT.all=0x0;
}
//===========================================================================
// No more.
//===========================================================================