📄 sci.c
字号:
// filename: sci.c
// By RealSYS 2003/6/4
#include "..\DSP281x_Device.h"
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long LONG;
interrupt void scib_tx_isr(void);
interrupt void scib_rx_isr(void);
#define CPUCLK 150000000L
#define LSPCLK (CPUCLK/4)
#define BAUDRATE 9600L
#define BRR_VAL (LSPCLK/(8*BAUDRATE)-1)
/*
BPS CPUCLK LSPCLK BRR_VAL BRR_VAL calc. BPS error
4800 150000000 37500000 975.5625 976 4797.851 -0.045
9600 150000000 37500000 487.28125 487 9605.533 0.058
19200 150000000 37500000 243.140625 243 19211.066 0.058
38400 150000000 37500000 121.0703125 121 38422.131 0.058
57600 150000000 37500000 80.38020833 80 57870.370 0.469
115200 150000000 37500000 39.69010417 40 114329.268 -0.756
*/
#define SCI_TX_START { if(ScibRegs.SCICTL2.bit.TXEMPTY){ \
ScibRegs.SCICTL2.bit.TXINTENA=1; \
ScibRegs.SCITXBUF = sci_tx_buf[sci_tx_pos++]; \
if(sci_tx_pos >= SCI_TX_BUF_SIZE) sci_tx_pos=0;}\
}
/*
#define SCI_TX_START { ScibRegs.SCICTL2.bit.TXINTENA=1; \
ScibRegs.SCITXBUF = sci_tx_buf[sci_tx_pos++]; \
if(sci_tx_pos >= SCI_TX_BUF_SIZE) sci_tx_pos=0; \
}
*/
#define SCI_TX_STOP ScibRegs.SCICTL2.bit.TXINTENA=0
#define SCI_TX_BUF_SIZE 100
typedef union
{
WORD w[2];
LONG l;
}ltype;
typedef union
{
WORD w[16];
LONG l[8];
}htype;
#define CR 0x0d
#define ASC_HEX_ERROR 0xff
/* Variables for Serial Communication */
BYTE sci_tx_buf[SCI_TX_BUF_SIZE+1];
BYTE sci_tx_pos=0,sci_tx_end=0;
char rxd=' ';
void sci_tx_start(void){
SCI_TX_START;
}
void sci_init(void){
// SCI(UART) init.
// Note: Clocks were turned on to the SCIB peripheral
// in the InitSysCtrl() function
ScibRegs.SCIFFTX.all = 0xa000; // FIFO reset
ScibRegs.SCIFFCT.all = 0x4000; // Clear ABD(Auto baud bit)
ScibRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
ScibRegs.SCICTL1.all =0x0003; // enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
ScibRegs.SCICTL2.bit.RXBKINTENA =1; // RX/BK INT ENA=1,
ScibRegs.SCICTL2.bit.TXINTENA =1; // TX INT ENA=1,
ScibRegs.SCIHBAUD = BRR_VAL >> 8; // High Value
ScibRegs.SCILBAUD = BRR_VAL & 0xff; // Low Value
ScibRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.RXBINT = &scib_rx_isr;
PieVectTable.TXBINT = &scib_tx_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Enable CPU INT9 for SCI-B
IER |= M_INT9;
// Enable SCI-B RX INT in the PIE: Group 9 interrupt 3
PieCtrlRegs.PIEIER9.bit.INTx3 = 1;
// Enable SCI-B TX INT in the PIE: Group 9 interrupt 4
PieCtrlRegs.PIEIER9.bit.INTx4 = 1;
}
/************ Polling send ***************/
void send_char(char c){
while(!ScibRegs.SCICTL2.bit.TXRDY);
ScibRegs.SCITXBUF=c;
}
void send_str(char *p){
char rd;
while(rd = *p++) send_char(rd);
}
void send_hex4(WORD d){
send_char(hex_to_asc(d >> 12));
send_char(hex_to_asc(d >> 8));
send_char(hex_to_asc(d >> 4));
send_char(hex_to_asc(d));
}
/************ Interrupt send ***************/
void sci_putc(char d){
sci_tx_buf[sci_tx_end++] = d;
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
}
void sci_puts(char *p){
char rd;
while(rd = *p++){
sci_tx_buf[sci_tx_end++] = rd;
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
}
}
void sci_hex2(BYTE d){
sci_tx_buf[sci_tx_end++] = hex_to_asc(d >> 4);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(d);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
}
void sci_hex4(WORD d){
sci_tx_buf[sci_tx_end++] = hex_to_asc(d >> 12);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(d >> 8);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(d >> 4);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(d);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
}
void sci_hex8(LONG d){
ltype l;
l.l = d;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[1]>>12);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[1]>>8);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[1]>>4);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[1]);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[0]>>12);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[0]>>8);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[0]>>4);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
sci_tx_buf[sci_tx_end++] = hex_to_asc(l.w[0]);
if(sci_tx_end >= SCI_TX_BUF_SIZE) sci_tx_end = 0;
}
void sci_decimal_byte(BYTE b){
WORD w;
w = hex2_to_decimal(b);
sci_putc(hex_to_asc(w>>8));
sci_putc(hex_to_asc(w>>4));
sci_putc(hex_to_asc(w));
}
void sci_decimal_word(WORD w){
LONG l;
l = hex4_to_decimal(w);
sci_putc(hex_to_asc(l>>16));
sci_putc(hex_to_asc(l>>12));
sci_putc(hex_to_asc(l>>8));
sci_putc(hex_to_asc(l>>4));
sci_putc(hex_to_asc(l));
}
void sci_decimal_int(WORD w){
LONG l;
int f=0;
char b;
l = hex4_to_decimal(w);
b = hex_to_asc(l>>16);
if((b=='0') && (f==0)) sci_putc(' ');
else{
sci_putc(b); f = 1;
}
b = hex_to_asc(l>>12);
if((b=='0') && (f==0)) sci_putc(' ');
else{
sci_putc(b); f = 1;
}
b = hex_to_asc(l>>8);
if((b=='0') && (f==0)) sci_putc(' ');
else{
sci_putc(b); f = 1;
}
b = hex_to_asc(l>>4);
if((b=='0')&&(f==0)) sci_putc(' ');
else{
sci_putc(b); f = 1;
}
sci_putc(hex_to_asc(l));
}
interrupt void scib_tx_isr(void){
if(sci_tx_pos != sci_tx_end){
ScibRegs.SCITXBUF = sci_tx_buf[sci_tx_pos++];
if(sci_tx_pos >= SCI_TX_BUF_SIZE) sci_tx_pos = 0;
}
else{
SCI_TX_STOP;
}
// Acknowledge this interrupt to recieve more interrupts from group 9
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}
interrupt void scib_rx_isr(void){
rxd = ScibRegs.SCIRXBUF.all;
sci_puts("\n\rReceived char = ");
sci_hex2(rxd);
SCI_TX_START;
// Acknowledge this interrupt to recieve more interrupts from group 9
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -