发表于:2005-05-23 11:08:00
楼主
请大虾帮忙看下下面的小程序,用bit对象进行数据传输。当clk在250HZ以上时,数据传输就不正确了,不知道是什么原因。希望大虾帮忙解答。不知道利用IO8,IO9,IO10这三个管脚进行数据传输,有什么更好更快捷的方法。谢谢了
IO_8 output bit tx;
IO_9 input bit clk;
IO_10 input bit rx;
char rxState,txState;
char rxData;
char rxBuffer,txBuffer;
char rflag;
msg_tag OUT1;
mtimer repeating mt = 500;
domain_struct card_domain1;
when(reset)
{
update_clone_domain(&card_domain1,0);
io_change_init(clk);
rxState = 0;
txState = 0;
rxBuffer = 0;
txBuffer = 0x55;
rflag = 0;
}
when(io_changes(clk))
{
char temp;
if(input_value == 1)
{//posedge:detect rx
switch(rxState)
{
case 0:
{
if(io_in(rx) == 0)
{//the start bit
rxData = 0;
rxState = 1;
}
break;
}
case 1:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = temp;
rxState = 2;
break;
}
case 2:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 1;
rxState = 3;
break;
}
case 3:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 2;
rxState = 4;
break;
}
case 4:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 3;
rxState = 5;
break;
}
case 5:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 4;
rxState = 6;
break;
}
case 6:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 5;
rxState = 7;
break;
}
case 7:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 6;
rxState = 8;
break;
}
case 8:
{
temp = io_in(rx);
temp = temp & 0x01;
rxData = rxData | temp << 7;
rxState = 9;
break;
}
case 9:
{
temp = io_in(rx);
temp = temp & 0x01;
if(temp == 0x01)
{//end bit
rxBuffer = rxData;
msg_out.tag = OUT1;
msg_out.code = 0;
msg_out.data[0] = rxBuffer;
msg_out.service = UNACKD;
msg_out.dest_addr.bcast.type=3;
msg_out.dest_addr.bcast.domain=0;
msg_out.dest_addr.bcast.backlog=1;
msg_out.dest_addr.bcast.rpt_timer=0;
msg_out.dest_addr.bcast.retry=0;
msg_out.dest_addr.bcast.tx_timer=0;
msg_out.dest_addr.bcast.subnet=0;
msg_send();
// txBuffer = rxBuffer + 0x10;
// rflag = 1;
}
rxState = 0;
break;
}
}
}
else if(rflag == 1)
{//negedge:send data
switch(txState)
{
case 0:
{
io_out(tx,1);
txState = 10;
break;
}
case 10:
{
io_out(tx,0);
txState = 1;
break;
}
case 1:
{
txState = 2;
if((txBuffer & 0x01) == 0x01)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 2:
{
txState = 3;
if((txBuffer & 0x02) == 0x02)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 3:
{
txState = 4;
if((txBuffer & 0x04) == 0x04)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 4:
{
txState = 5;
if((txBuffer & 0x08) == 0x08)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 5:
{
txState = 6;
if((txBuffer & 0x10) == 0x10)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 6:
{
txState = 7;
if((txBuffer & 0x20) == 0x20)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 7:
{
txState = 8;
if((txBuffer & 0x40) == 0x40)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 8:
{
txState = 9;
if((txBuffer & 0x80) == 0x80)
io_out(tx,1);
else
io_out(tx,0);
break;
}
case 9:
{
txState = 0;
io_out(tx,1);
rflag = 0;
break;
}
default:
{
txState = 0;
io_out(tx,1);
rflag = 0;
b