这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 接收中断处理程序里的全局变量无法返回接收值

共2条 1/1 1 跳转至

接收中断处理程序里的全局变量无法返回接收值

院士
2006-09-17 18:14:16     打赏
接收中断处理程序里的全局变量无法返回接收值



关键词: 接收     中断     处理     程序     全局     变量     无法     返回    

院士
2006-12-22 22:43:00     打赏
2楼
问 我在做AVRMAGE32L与计算机串口通信时,发现接收中断处理程序里的全局变量无法返回接收值,请大虾指点。谢谢!

unsigned char preceive,rec_len,send_ok,psend;  
unsigned char received_ok;
unsigned char receivebuf[20];
unsigned char sendbuf[20];
unsigned char send_max,i;
     .
     .
     .

interrupt [USART_RXC] void usart_rx_isr(void)
{
         unsigned char data;     
    #asm("cli")    

    if (UCSRA&(1<<RXC))  
       {
        data=UDR;
        receivebuf[preceive]=data;  
           if(receivebuf[0]==0x02)      //字头
                     {    
                         preceive++;
                       if(preceive==3)
                        rec_len=receivebuf[1];  
               }  
               if((preceive==rec_len) &&preceive>3))                //接收完received_ok=1
            {
                   if(data==3)     //字结束
                    {                                             received_ok=1;
                            send_ok=0;
                    }   
                         preceive=0;
                         rec_len=0;
                }
       }   
       #asm("sei")   
       
}


interrupt [USART_TXC] void usart_tx_isr(void)
{
    #asm("cli")
    if(send_ok==0)
        {
           UDR=sendbuf[psend];    
                      psend++;
                      if(psend>=send_max)
                  {
                     psend=0;
                     received_ok=0;
                      send_ok=1;
                  }
       }
       #asm("sei")
}

void main(void)
{
         .
         .
         .
         UCSRA=0x00;           
    UCSRB=0xD8;     
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=51;
         #asm("sei")
             received_ok=0;
    preceive = 0;
    send_ok=0xff;
    while (1)
          {
                     
                      if(received_ok==1)
                   //接收中断退出后received_ok又是以前值,所以进不来
            {
                data1=1;    
                data2=2;           
                data3=0;           
                data4=0xcc;
                   send_read(receivebuf[2],0x09);  
            }
               delay(1000);
          };
}

1: 谁帮帮我,郁闷!!!我在做AVRMAGE32L与计算机串口通信时,发现接收中断处理程序里的全局变量无法返回接收值,请大虾指点。谢谢!

unsigned char preceive,rec_len,send_ok,psend;  
unsigned char received_ok;
unsigned char receivebuf[20];
unsigned char sendbuf[20];
unsigned char send_max,i;
     .
     .
     .

interrupt [USART_RXC] void usart_rx_isr(void)
{
         unsigned char data;     
    #asm("cli")    

    if (UCSRA&(1<<RXC))  
       {
        data=UDR;
        receivebuf[preceive]=data;  
           if(receivebuf[0]==0x02)      //字头
                     {    
                         preceive++;
                       if(preceive==3)
                        rec_len=receivebuf[1];  
               }  
               if((preceive==rec_len) &&preceive>3))                //接收完received_ok=1
            {
                   if(data==3)     //字结束
                    {                                             

              received_ok=1;
                            send_ok=0;
                    }   
                         preceive=0;
                         rec_len=0;
                }
       }   
       #asm("sei")   
       
}


interrupt [USART_TXC] void usart_tx_isr(void)
{
    #asm("cli")
    if(send_ok==0)
        {
           UDR=sendbuf[psend];    
                      psend++;
                      if(psend>=send_max)
                  {
                     psend=0;
                     received_ok=0;
                      send_ok=1;
                  }
       }
       #asm("sei")
}

void main(void)
{
         .
         .
         .
         UCSRA=0x00;           
    UCSRB=0xD8;     
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=51;
         #asm("sei")
             received_ok=0;
    preceive = 0;
    send_ok=0xff;
    while (1)
          {
                     
                      if(received_ok==1)
                   //接收中断退出后received_ok又是以前值,所以进不来
            {
                data1=1;    
                data2=2;           
                data3=0;           
                data4=0xcc;
                   send_read(receivebuf[2],0x09);  
            }
               delay(1000);
          };
}

2: 被编译器优化了吧加关键词 volatile unsigned char received_ok. 3: 谢谢hover99

共2条 1/1 1 跳转至

回复

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