51串口中断的问题

wangjianqun 2008-09-04 04:00:28
刚接触单片机不久,想做一个串口收发的实验,可是 怎么也收不到数据,各位谁帮忙看看这段程序那里有问题呢?
#include<reg52.h>
unsigned char rxbuf[8];
unsigned char tx_count=0,rx_count=0,rx_temp;
void initUart()
{
SCON=0x50;
TMOD=0x20;
PCON=0x80;
TH1=0xf3;
TL1=0xf3;
TR1=1;
ES=1;
EA=1;
}
main()
{
initUart();
while(1)
{
if(rx_count==8)
{
TI=1;
rx_count=0;
}
}
}
void serial() interrupt 4 //串口中断
{
if(TI)
{
if(tx_count < 8)
{

SBUF = rxbuf[tx_count];
tx_count++;
}

if(tx_count>=8)
{
TI = 0;
tx_count=0;
}
}
if(RI)
{

rx_temp=SBUF;
if(rx_count < 8)
{
rxbuf[rx_count]=rx_temp;
rx_count++;
}
RI=0;
}
}
...全文
700 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
azmao 2008-09-05
  • 打赏
  • 举报
回复
TI没有清零,每发一个字节都需要清零。
ysysbaobei 2008-09-05
  • 打赏
  • 举报
回复
不懂,帮顶
azmao 2008-09-05
  • 打赏
  • 举报
回复
tx_count=7时,执行发送,+1,此时tx_count=8,被if(tx_count>=8)判断,tx_count=0了。发送会引起的中断,在下一次进入时,由于tx_count=0,从而引起发个不停。
if(TI)
{
TI = 0;
if(tx_count < 8) //是否发送结束
{
SBUF = rxbuf[tx_count];

}

tx_count++;
}
在主程序中将tx_count清零比较好
while(1)
{
if(rx_count==8)
{
rx_count=0;
tx_count=0;//增加这一句
TI=1;
}
}
接收部分是没有问题的。

正5 2008-09-04
  • 打赏
  • 举报
回复
先查硬件边接有没有问题,然后用现存的软件测试一下,再调试程序。
色郎中 2008-09-04
  • 打赏
  • 举报
回复
再补充下

你的程序不够规范也,刚开始嘛, 最好先把流程画好, 再写代码,
色郎中 2008-09-04
  • 打赏
  • 举报
回复

//send interrupt flag occur
if(TI0)
{
TI0 = FALSE;
if(usart0_sending)
{
usart0_expect_send_len--;
if(usart0_expect_send_len > 0) //continue send data
{
usart0_sended_length++;
SBUF0 = usart0_send_buf[usart0_sended_length];
return;
}
else //send over
{
if(usart0_current_command == USART0_CMD_SET_BAUT_RATE)
{
set_usart0_baut_rate(*(usart0_recive_buf + 3));
}
usart0_sending = FALSE;
USART_LED = LED_OFF;
return;
}
}
}

发送部分


首先你要保证你配置没有问题, 另,进了就要清标志
色郎中 2008-09-04
  • 打赏
  • 举报
回复
void usart0_isr(void) interrupt 4 using 1
{
unsigned char temp;
SFRPAGE = 0;
if(RI0) //receive interrupt flag occur
{
RI0 = FALSE;
temp = SBUF0;

#if 1 //i could delete the routine for some season.
if(usart0_receive_ok)
{
return; //discard the received data or send a error command
//to report communication over-run
}
#endif

if(usart0_receiveing)
{

if(usart0_received_len)//receive the data
{
usart0_recive_buf[usart0_received_length] = temp;
usart0_received_length++;
usart0_expect_receive_len--;
if(usart0_expect_receive_len==0)
{
usart0_receive_ok = TRUE;
USART_LED = LED_OFF;
}
return;
}
else //receive the length for data
{
usart0_expect_receive_len = temp;
usart0_recive_buf[1] = temp;
usart0_received_length = 2;
usart0_received_len = TRUE;
return;
}
}

else if(temp == RECEIVE_BOOT_CODE)//receive boot code
{
usart0_receiveing = TRUE;
usart0_recive_buf[0] = RECEIVE_BOOT_CODE;
USART_LED = LED_ON;
usart0_RS232_1s = 0;
return;
}
//return; //discard the received data
}
}


给你一段参考
wangjianqun 2008-09-04
  • 打赏
  • 举报
回复
我改了一下
if(rx_count==8)
{
TI=1;
rx_count=0;
}
改成
if(rx_count==8)
{
rx_count=0;
TI=1;
}
有返回数据 比如我用PC端输入 01 02 03 04 05 06 07 08 正确应该回显和这个一样的数值,
可是却返回一堆80,而且是不停的发,没找到原因
xuelian922 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jennyvenus 的回复:]
你的发送函数不对吧,在串口中断中应该写发送
if( tx_count < 8 )
{
SBUF = rxbuf[ tx_count ];
while( !TI );
TI = 0;
tx_count++;
}
[/Quote]
还有这也是,要清标志位
wangjianqun 2008-09-04
  • 打赏
  • 举报
回复
引用错了 汗。。。
wangjianqun 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangjianqun 的回复:]
引用 3 楼 wanyeye 的回复:
收到一个字节就要青标致位了啊

好准备收下一个字节

RI我清0了呀
[/Quote]
我在main函数里清的0
xuelian922 2008-09-04
  • 打赏
  • 举报
回复
学习贴,感觉协议有点奇怪。
if(rx_count < 8)
{
rxbuf[rx_count]=rx_temp;
rx_count++;
}

当rx_count 大于或等于的时候楼主没有将其清0;当程序执行到
TI=1;的时候就进入了串口中断里面,这时候的rx_count 应该还是等于8的吧?
我也是新手,个人感觉是这有问题,楼主可以试下。
wangjianqun 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wanyeye 的回复:]
收到一个字节就要青标致位了啊

好准备收下一个字节
[/Quote]
RI我清0了呀
色郎中 2008-09-04
  • 打赏
  • 举报
回复
收到一个字节就要青标致位了啊

好准备收下一个字节
用户 昵称 2008-09-04
  • 打赏
  • 举报
回复
还要多多试验inituart的配置才行。
用户 昵称 2008-09-04
  • 打赏
  • 举报
回复
你的发送函数不对吧,在串口中断中应该写发送
if( tx_count < 8 )
{
SBUF = rxbuf[ tx_count ];
while( !TI );
TI = 0;
tx_count++;
}

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧