使用TI DSP 2407A 进行开发的源代码
源代码在线查看: sci.c
#include "sci2407.h"
#define TESTING_OUTPUT 0
#define TEST_UART 0
//unsigned int uart_send_char[]= {"dfsrterhg34e54y6hggfdgdfsgsdf"};
unsigned int uart_rec_char[256];
unsigned int send_integrar;
unsigned int send_flag;
//unsigned char uart1_test_msg[] = {"This is a test message from uart #1"};
void Init_Sci(void)
{
int TEMPREG;
TEMPREG = MCRA;
TEMPREG |= (SCITXD | SCIRXD);
MCRA = TEMPREG; //配置串行口为特殊功能
/* 串口初始化*/
SCICCR = 0;
SCICTL1 = 0;
wait_us( 1 );
SCICCR = (unsigned int) (SCI_CHAR_8);
//空闲线多处理器模式,8位数据,一位停止位,无校验
SCICTL2 = (unsigned int) (RX_INT_ENA | TX_INT_ENA);
//接受中断使能
// sciptr->ctl2 = (unsigned int) 0x0000; /* no interrupts */
/* load the baud rate */
SCIHBAUD = (unsigned int) (BAUD_38400 / 0x100);
SCILBAUD = (unsigned int) (BAUD_38400 % 0x100);
SCIPRI = PRI_SOFT;
/*-------------------------------------------------------------*/
/* Set mode to soft */
/*-------------------------------------------------------------*/
SCICTL1 = (unsigned int) (SW_RESET | TX_ENA | RX_ENA);
//使能SCI,
}
//设定通信接口通信控制寄存器参数
//SCICCR_PARA_SET(STOP_BITS),两个停止位,默认为1个
//SCICCR_PARA_SET(EVEN_PARITY),偶校验,默认为奇校验
//SCICCR_PARA_SET(PARITY_ENA),使能奇、偶校验,默认为不校验
//SCICCR_PARA_SET(LOOP_BACK),使能自测试模式,
//SCICCR_PARA_SET(ADDR_IDLE)选择地址位多处理器模式
//SCICCR_PARA_SET(SCI_CHAR_x )SCI字符长度选择
void SCICCR_PARA_SET(unsigned int para)
{
SCICCR |= para;
}
//取消通信接口通信控制寄存器参数设定
//SCICCR_PARA_UNSET(STOP_BITS),设置为一个停止位
//SCICCR_PARA_UNSET(EVEN_PARITY)奇校验
//SCICCR_PARA_UNSET(PARITY_ENA),不进行奇偶校验
//SCICCR_PARA_UNSET(LOOP_BACK),禁止自测试模式,
//SCICCR_PARA_SET(ADDR_IDLE)选择空闲线多处理器模式
void SCICCR_PARA_UNSET(unsigned int para)
{
unsigned int temp;
temp = ~para;
SCICCR &= temp;
}
//SCI软件复位位,在系统复位后,应该将运行此函数来重新使能SCI
void SCI_RESET(void)
{
SCICTL1 |= SW_RESET;
}
//设定通信接口通信控制寄存器参数
//SCICTL1_PARA_SET(RX_ERR_INT_ENA),使能接受错误中断
//SCICTL1_PARA_SET(SW_RESET),SCI软件复位
//SCICTL1_PARA_SET(TWAKE),SCI发送器唤醒方法选择位,发送特征由SCICCR的ADDR_IDLE位决定
//SCICTL1_PARA_SET(SLEEP),使能SCI休眠方式,
//SCICTL1_PARA_SET(TX_ENA),使能SCI发送
//SCICTL1_PARA_SET(RX_ENA),使能SCI接收
void SCICTL1_PARA_SET(unsigned int para)
{
SCICTL1 |= para;
}
//设定通信接口通信控制寄存器1参数
//SCICTL1_PARA_UNSET(RX_ERR_INT_ENA),禁止接受错误中断
//SCICTL1_PARA_UNSET(SW_RESET),初始化SC状态机和操作标志至复位条件
//SCICTL1_PARA_UNSET(TWAKE),SCI发送器无选定的发送特征
//SCICTL1_PARA_UNSET(SLEEP),禁止SCI休眠方式,
//SCICTL1_PARA_UNSET(TX_ENA),禁止SCI发送
//SCICTL1_PARA_UNSET(RX_ENA),禁止SCI接收
void SCICTL1_PARA_UNSET(unsigned int para)
{
unsigned int temp;
temp = ~para;
SCICTL1 &= temp;
}
//通信接口通信控制寄存器2参数获取
//SCICTL1_PARA_GET(SLEEP)休眠模式参数获得
unsigned int SCICTL1_PARA_GET(unsigned int para)
{
unsigned int temp;
temp = SCICTL2;
temp &= para;
if(temp>0)
return 1;
else
return 0;
}
//通信接口通信控制寄存器2参数设定
//SCICTL2_PARA_SET(RX_INT_ENA),使能RXRDY中断
//SCICTL2_PARA_SET(TX_INT_ENA),使能TXRDY中断
unsigned int SCICTL2_PARA_SET(unsigned int para)
{
SCICTL2 |= para;
}
//通信接口通信控制寄存器2参数设定
//SCICTL2_PARA_UNSET(RX_INT_ENA),禁止RXRDY中断
//SCICTL2_PARA_UNSET(TX_INT_ENA),禁止TXRDY中断
unsigned int SCICTL2_PARA_UNSET(unsigned int para)
{
unsigned int temp;
temp = ~para;
SCICTL2 &= temp;
}
//通信接口通信控制寄存器2参数获取
//SCICTL2_PARA_GET(TX_RDY),返回1,发送缓冲寄存器空,准备发送下一个数据
//返回0,发送缓冲寄存器满,无法发送下一个数据
//SCICTL2_PARA_GET(STX_EMPTY),S返回1,发送缓冲寄存器SCITXBUF和TXSHF空,准备发送下一个数据
//返回0,发送缓冲寄存器SCITXBUF和TXSHF满,无法发送下一个数据
unsigned int SCICTL2_PARA_GET(unsigned int para)
{
unsigned int temp;
temp = SCICTL2;
temp &= para;
if(temp>0)
return 1;
else
return 0;
}
//通信接口通信控制寄存器2参数获取
//SCICTL2_PARA_GET(TX_RDY),返回1,发送缓冲寄存器空,准备发送下一个数据
//返回0,发送缓冲寄存器满,无法发送下一个数据
//SCICTL2_PARA_GET(STX_EMPTY),S返回1,发送缓冲寄存器SCITXBUF和TXSHF空,准备发送下一个数据
//返回0,发送缓冲寄存器SCITXBUF和TXSHF满,无法发送下一个数据
void SCI_BAUD_SET(unsigned int baud)
{
SCIHBAUD = (unsigned int) (BAUD_9600 / 0x100);
SCILBAUD = (unsigned int) (BAUD_9600 % 0x100);
}
//通信接口接受状态寄存器参数获取
//SCIRXST_PARA_GET(RX_ERROR),有错误标志被置位
//SCIRXST_PARA_GET(RX_RDY),SCI接收器准备好标志位,如果RX_INT_ENA为1,则产生接收中断
//SCIRXST_PARA_GET(BRKDT),是否满足中断条件
//SCIRXST_PARA_GET(FE),是否检测到帧错误
//SCIRXST_PARA_GET(OE),是否检测到超时错误
//SCIRXST_PARA_GET(PE),是否检验到奇偶校验错误
//SCIRXST_PARA_GET(RXWAKE),接收器唤醒检测标志位
unsigned int SCIRXST_PARA_GET(unsigned int para)
{
unsigned int temp;
temp = SCIRXST;
temp &= para;
if(temp>0)
return 1;
else
return 0;
}
//检查接收状态
unsigned int Chk_Sci_Rec_Status(void)
{
unsigned int status;
status = 0;
if(!status)
{
status = SCIRXST;
status &= RX_RDY;
}
return status;
}
//检查发送状态
unsigned int Chk_Sci_Send_Status(void)
{
unsigned int status;
status = 0;
if( !status)
{
status = SCICTL2;
status &= TX_RDY;
}
return status;
}
unsigned int Sci_Get_Cmd_Idle(void)
{
unsigned int in_char;
unsigned int status;
status = Chk_Sci_Rec_Status();
if ( SCICTL1_PARA_GET(SLEEP) && status)
{
in_char = SCIRXBUF; /* get data from rec buffer */
in_char &= 0x00ff;
return in_char; /* return data */
}
return 0;
}
unsigned int Sci_Get_Char(void)
{
unsigned int in_char,status;
status = Chk_Sci_Rec_Status();
if( (! SCICTL1_PARA_GET(SLEEP)) && status )
{
in_char = SCIRXBUF;
in_char &= 0x00ff;
return in_char;
}
return 0xFFFF;
}
void Sci_Get_String(unsigned int *p)
{
unsigned int temp,nSciCount = 0;
temp = Sci_Get_Char();
if(temp != 0xFFFF)
{
nSciCount = p[0];
p[nSciCount+1] = temp;
(p[0])++;
if(nSciCount>254)
return;
}
}
void Sci_Add_String(unsigned int num,unsigned int * source,unsigned int *dest)
{
unsigned int i,temp;
temp = dest[0];
dest[0] = dest[0]+num*2;
for(i=0;i {
*(dest+temp+2*i+1) = *(source+i) / 0x100;
*(dest+temp+2*i+2) = *(source+i) % 0x100;
}
}
unsigned int Sci_Put_Char(unsigned int out_char)
{
unsigned int status = 0;
status = Chk_Sci_Send_Status();
if(status)
{
SCITXBUF = (0x00ff & out_char); /* send data */
return 0; /* return no error */
}
return 0xffff; /* return error */
}
unsigned int Sci_Put_String(unsigned int * out_string)
{
unsigned int out_data,i,num;
unsigned *p = out_string;
num = *p;
if(num>0)
{
out_data = * (p+num);
if(Sci_Put_Char(out_data) != 0xFFFF)
(*p)--;
}
return 0;
}
void SCI_TEST()
{
unsigned int i =0;
for(i=0;i uart_rec_char[i] = 0;
}