这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » 帮忙分析一下串口中断接收程序!ATMEGA128单片机

共1条 1/1 1 跳转至

帮忙分析一下串口中断接收程序!ATMEGA128单片机

菜鸟
2009-08-23 16:12:54     打赏

#include  <io.h>
#include  <interrupt.h>
#define   fosc    11059200                                           //时钟频率
#define   baud    9600                                               //串口波特率
#define RXB8 1
#define TXB8 0
//#define PE 2    //M16
#define UPE 2    //M128
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

//宏定义
#define FRAMING_ERROR (1<<FE)
//#define PARITY_ERROR (1<<PE)    //M16
#define PARITY_ERROR (1<<UPE)    //M128
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

#define IGTON     PORTD|= 0x10         //IGT点火信号
#define IGTOFF    PORTD&=~0x10     
#define RTSON     PORTD|= 0x40         //RTS信号
#define RTSOFF    PORTD&=~0x40
#define DTRON     PORTD|= 0x20         //DTR信号
#define DTROFF    PORTD&=~0x20


unsigned char MEMSBUF0[255],MEMSBUF1[255];

#define RX_BUFFER_SIZE0 255       
volatile unsigned char rx_buffer0[RX_BUFFER_SIZE0];
volatile unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
volatile unsigned char rx_buffer_overflow0; 

#define TX_BUFFER_SIZE0 255
volatile unsigned char tx_buffer0[TX_BUFFER_SIZE0];
volatile unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;

#define RX_BUFFER_SIZE1 255       
volatile unsigned char rx_buffer1[RX_BUFFER_SIZE1];
volatile unsigned char rx_wr_index1,rx_rd_index1,rx_counter1;
volatile unsigned char rx_buffer_overflow1; 

#define TX_BUFFER_SIZE1 255
volatile unsigned char tx_buffer1[TX_BUFFER_SIZE1];
volatile unsigned char tx_wr_index1,tx_rd_index1,tx_counter1;

void mdelay(unsigned int ms)                                         //0.1ms delay
{
  unsigned int m=0;
  for(;ms>0;ms--)
    {
      for(m=0;m<56;m++)
   {;}
 }
}

void delay50ms(unsigned int ms)
{
   for(;ms>0;ms--)
   {
     mdelay(50);
   }
}

void uart0_init(void)                                                //串口初始化
{
  UCSR0B = 0x00;                                                  //disable while setting baud rate
  UCSR0A = 0x00;
  //UCSR0C =(1<<UPM01)|(1<<UCSZ01)|(1<<UCSZ00);                     //8bit+1bit stop(偶校验)
  UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);                                   //无校验
  UBRR0L=(fosc/16/(baud+1))%256;
  UBRR0H=(fosc/16/(baud+1))/256;                                  //波特率设置
  UCSR0B =(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
}


void uart1_init(void)
{
  UCSR1B = 0x00;                                                  //disable while setting baud rate
  UCSR1A = 0x00;
  UCSR1C =(1<<UCSZ11)|(1<<UCSZ10);                                //8bit+1bit stop
  UBRR1L=(fosc/16/(baud+1))%256;
  UBRR1H=(fosc/16/(baud+1))/256;
  UCSR1B =(1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1);                       //RXCEN TXCEN
}

void putchar0(unsigned char c)
 { 
     while (!(UCSR0A&(1<<UDRE0)));
       UDR0=c;  
 }

void putchar1(unsigned char c)
 { 
     while (!(UCSR1A&(1<<UDRE1)));
       UDR1=c;  
 }

SIGNAL(SIG_USART0_RECV)
{
 unsigned char status,data;
 status=UCSR0A;
 data=UDR0;
 if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
    {
    rx_buffer0[rx_wr_index0]=data;
    if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
    if (++rx_counter0 == RX_BUFFER_SIZE0)
       {
    rx_counter0=0;
       rx_buffer_overflow0=1;
       }
    }
}

unsigned char get0_char(void)
{
 unsigned char data;
 while (rx_counter0==0);
 data=rx_buffer0[rx_rd_index0];
 if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
 cli();
 --rx_counter0;
 sei();
 return data;
}

SIGNAL(SIG_USART1_RECV)
{
 unsigned char status,data;
 status=UCSR1A;
 data=UDR1;
 if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
    {
    rx_buffer1[rx_wr_index1]=data;
    if (++rx_wr_index1 == RX_BUFFER_SIZE1) rx_wr_index1=0;
    if (++rx_counter1 == RX_BUFFER_SIZE1)
       {      
    rx_counter1=0;
       rx_buffer_overflow1=1;
       }
    }
}

unsigned char get1_char(void)
{
 unsigned char data;
 while (rx_counter1==0);
 data=rx_buffer1[rx_rd_index1];
 if (++rx_rd_index1 == RX_BUFFER_SIZE1) rx_rd_index1=0;
 cli();
 --rx_counter1;
 sei();
 return data;
}

int main(void)
{
    unsigned char i=0,j=0,k=0;
    rx_buffer_overflow0=0;
    rx_wr_index0=rx_rd_index0=rx_counter0=0;
    rx_buffer_overflow1=0;
    rx_wr_index1=rx_rd_index1=rx_counter1=0;
 PORTD = 0xF3;
 DDRD  = 0xff;
 uart1_init();
 uart0_init();
 
 sei();
    while(1)
 {
    i = 0;
    while (!rx_counter0) ;
    //delay50ms(5000);
  while (rx_counter0)
  {
        MEMSBUF0[i] = get0_char();
        i++;
  }
       for(j=0;j<i;j++)
    {
      putchar0(MEMSBUF0[j]);
    }
      }
我使用avrstudio编译的,为什么程序最多只能接受98个字节的数据,超过98字节的数据就丢失,请帮忙分析一下原因了!




关键词: 帮忙     分析     一下     串口     中断     接收     程序     ATME    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]