搜索
bottom↓
回复: 2

请教关于usart的问题

[复制链接]

出0入0汤圆

发表于 2012-1-3 21:49:26 | 显示全部楼层 |阅读模式
在p404
有这样一段程序
if (  rx_counter == RX_BUFFER_SIZE)        
  {            
rx_counter=0;              rx_buffer_overflow=1;      
   };
这里当接收8个字符后将rx_counter清零,但为什么置位溢出中断,接受8个字符并没有溢出啊?注释说最后的数据已经把前面的数据覆盖了,不明白   请教下

出0入0汤圆

发表于 2012-1-4 00:08:00 | 显示全部楼层
谢谢你的问题指出,此处应该为;
if (rx_counter > RX_BUFFER_SIZE)        
{            
     rx_counter=0; rx_buffer_overflow=1;      
}

==============================================
    这段代码是由cvavr系统中的程序向导功能自动生成的代码(书中有说明),在书中引用这段代码是因为它对串行通信采用中断+缓冲的思想和处理方法非常值得学习和借鉴,效率高,不会浪费MCU的时间。

    由于是CVAVR自己生成的代码,我没有做多的修改,基本是引用的。但尽管思想方法非常好,但在具体的接收处理上还是有些不足,比如在getchar()函数中的“while(rx_counter == 0);”就容易造成死循环的现象发生(通信对方系统出问题或通信连接线路故障),你现在发现的if(rx_counter == RX_BUFFER_SIZE)也是一个BUG。

    这里也就提醒我们,不能简单的照搬别人的代码!哪怕是商业软件的代码也会有BUG。应该是学习好的方法,然后根据实际情况灵活运用。

     我书中后面的实际例子中,使用USART基本上都是采用这个方法处理的,串口发送部分基本相同,但串口的接收部分就做了比较大的变化,但基本思想和理念是一致的。

     再次感谢你的问题,如果有下一次加印机会,这个地方我会做修改的。

==================================================================
以下贴上原CVAVR中CodeWizardAVR自动生成的代码,版本是2.05.3,仍旧存在这两个问题。

#include <mega16.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];

#if RX_BUFFER_SIZE <= 256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif

// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index++]=data;
#if RX_BUFFER_SIZE == 256
   // special case for receiver buffer size=256        //<====== bug2!!!
   if (++rx_counter == 0) rx_buffer_overflow=1;         
#else
   if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)                  //<====== bug2!!!
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      }
#endif
   }
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);                                  // <====== bug1
data=rx_buffer[rx_rd_index++];
#if RX_BUFFER_SIZE != 256
if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#endif
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];

#if TX_BUFFER_SIZE <= 256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif

// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   --tx_counter;
   UDR=tx_buffer[tx_rd_index++];
#if TX_BUFFER_SIZE != 256
   if (tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
#endif
   }
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index++]=c;
#if TX_BUFFER_SIZE != 256
   if (tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
#endif
   ++tx_counter;
   }
else
   UDR=c;
#asm("sei")
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

// Declare your global variables here

void main(void)
{
   。。。。省略。。。
}

出0入0汤圆

发表于 2012-1-4 00:48:01 | 显示全部楼层
www.ourdev.cn/ 真不错
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-8 19:49

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表