本帖最后由 work67 于 2013-11-8 16:08 编辑
这是我找的一个UART的通讯例程,DSP实现将接收到的数据转发回去,不过我对这UART通讯的本质还是弄不清楚,先改成自由发送跟接收的程序,但感觉拎不出来单纯发数据的那部分代码,哪位帮忙给解释一下,感激不尽啊void main(void)
{
/* Initialize CSL library - This is REQUIRED !!! */
/*初始化CSL库*/
CSL_init();
/*设置系统的运行速度为144MHz*/
PLL_config(&myConfig);
/*初始化McBSP1*/
hMcbsp = MCBSP_open(MCBSP_PORT1,MCBSP_OPEN_RESET);
/*设置McBSP1*/
MCBSP_config(hMcbsp,&Mcbsp1Config);
MCBSP_getConfig(hMcbsp,&Mcbsptest);
settimer();
/*************************以下程序实现软件Uart************************/
/* 参考:HX-5509A 开发板使用手册 */
while(TRUE)
{
if(uart_recflag==1)
{
uart_recflag = 0;
if(uart_sendflag==0)
{
uart_bitsendcnt = 0;
uart_sendend = 0;
uart_sendcnten = 0;
uart_bitsend = 0;
uart_send = 0x41;
uart_sendflag = 1;
uart_sendbitcnt = 0;
}
}
};
}
interrupt void timer0Isr(void)
{
temp = REG_PCR1;
//Send
if(uart_sendflag==1)
{
if(uart_sendcnten==0)
{
REG_PCR1 &= 0xffdf;
uart_bitsend = 1;
uart_sendcnten = 1;
}
if(uart_sendcnten==1)
{
uart_bitsendcnt++;
if(uart_bitsendcnt==10)
{
uart_sendbitcnt++;
if(uart_sendend==1)
{
uart_sendflag = 0;
}
else
{
if(uart_sendbitcnt<9)
{
if((uart_send & uart_bitsend)==0)
{
REG_PCR1 &= 0xffdf;
}
else
{
REG_PCR1 |= 0x0020;
}
uart_bitsend <<= 1;
}
else
{
REG_PCR1 |= 0x0020;
uart_sendflag = 0;
}
}
uart_bitsendcnt = 0;
}
}
}
//Receive
if((uart_bitcnt==0)&&(uart_recstart==0))
{
if((temp&0x0010)==0)
{
timer0_cnt = 0;
timer0_cnten = 1;
uart_bitscan = 0;
uart_rec = 0;
uart_recstart = 1;
}
}
if(timer0_cnten==1)
{
timer0_cnt++;
if((temp&0x0010)!=0)
{
uart_bitscan++;
}
if(timer0_cnt==10)
{
timer0_cnt = 0;
if(uart_bitscan>5)
{
switch(uart_bitcnt)
{
case 0: uart_rec = 0;
timer0_cnten = 0;
uart_recstart = 0;
break;
case 1: uart_bitcnt++;
uart_rec |= 0x01;
break;
case 2: uart_bitcnt++;
uart_rec |= 0x02;
break;
case 3: uart_bitcnt++;
uart_rec |= 0x04;
break;
case 4: uart_bitcnt++;
uart_rec |= 0x08;
break;
case 5: uart_bitcnt++;
uart_rec |= 0x10;
break;
case 6: uart_bitcnt++;
uart_rec |= 0x20;
break;
case 7: uart_bitcnt++;
uart_rec |= 0x40;
break;
case 8: uart_bitcnt++;
uart_rec |= 0x80;
break;
case 9: uart_bitcnt = 0;
timer0_cnten = 0;
uart_recflag = 1;
uart_recstart = 0;
break;
default: break;
}
}
else
{
uart_bitcnt++;
if(uart_bitcnt>9)
{
uart_bitcnt = 0;
timer0_cnten = 0;
}
}
uart_bitscan = 0;
}
}
}
|