共2条
1/1 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);
};
}
答 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
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 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
vscode+cmake搭建雅特力AT32L021开发环境被打赏30分 | |
【换取逻辑分析仪】自制底板并驱动ArduinoNanoRP2040ConnectLCD扩展板被打赏47分 | |
【分享评测,赢取加热台】RISC-V GCC 内嵌汇编使用被打赏38分 | |
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 |