Pragma 和中断向量表:TI MSP430
我的程序包含很少的全局变量,其值是在中断服务例程(USCI_A0_ISR())执行期间设置的。
一旦 USCI_A0_ISR() 执行完成,全局变量将保持分配的值还是将被设置回 void/0。???
//Global variables
int ptr = 0;
char rxBuffer[16];
int flag = -1;
int check[2];
void __set_flag(void)
{
if (strcmp(rxBuffer,"OK") == 0) flag = 0;
else if (strcmp(rxBuffer,"CONNECT") == 0) flag = 1;
else if (strcmp(rxBuffer,"NO CARRIER") == 0) flag = 3;
else if (strcmp(rxBuffer,"ERROR") == 0) flag = 4;
}
void __GSM_client(void)
{
while (flag == -1);
if (flag == 0) check[0] = buflen(rxBuffer);
}
void main(void)
{
__Buffer_init();
__low_level_init(); //WDT
__UART0_init(); //UART
__bis_SR_register(GIE); //interrupts enabled
__delay_cycles(1000); // wait till UART intial
__GSM_client();
__no_operation(); // For debugger
}
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
char byte;
while (!(UCA0IFG&UCTXIFG));
byte= UCA0RXBUF;
UCA0TXBUF = byte;
if (byte == '\r') {
//push_char(byte);
ptr = 0;
__set_flag();
//__Buffer_init();
}
else{
push_char(byte);
}
}
这是我正在做的事情的代码片段。我根据获得的响应设置“flag”
。当我在 Code Composer Studio 中看到寄存器视图时,"flag"
值已正确设置,但如果尝试在其他地方使用 "flag"
的值,则 >“flag”
未反映出来。
关于中断服务例程概念或我的编码方法中的漏洞的任何指针 提前致谢 AK
My program contains few global variables , whose values are set during the interrupt service routine (USCI_A0_ISR()) execution.
Once the execution of USCI_A0_ISR() is done , will the global variables hold the value assigned or will be set back to void/0.????
//Global variables
int ptr = 0;
char rxBuffer[16];
int flag = -1;
int check[2];
void __set_flag(void)
{
if (strcmp(rxBuffer,"OK") == 0) flag = 0;
else if (strcmp(rxBuffer,"CONNECT") == 0) flag = 1;
else if (strcmp(rxBuffer,"NO CARRIER") == 0) flag = 3;
else if (strcmp(rxBuffer,"ERROR") == 0) flag = 4;
}
void __GSM_client(void)
{
while (flag == -1);
if (flag == 0) check[0] = buflen(rxBuffer);
}
void main(void)
{
__Buffer_init();
__low_level_init(); //WDT
__UART0_init(); //UART
__bis_SR_register(GIE); //interrupts enabled
__delay_cycles(1000); // wait till UART intial
__GSM_client();
__no_operation(); // For debugger
}
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
char byte;
while (!(UCA0IFG&UCTXIFG));
byte= UCA0RXBUF;
UCA0TXBUF = byte;
if (byte == '\r') {
//push_char(byte);
ptr = 0;
__set_flag();
//__Buffer_init();
}
else{
push_char(byte);
}
}
Here is the code snippet of what I am doing. I am setting the "flag"
based on the response obtained . When I see the register view in Code Composer Studio , the "flag"
value is set correctly , but if try using the value of "flag"
elsewhere the value of "flag "
is not reflected.
Any pointers over concepts of the interrupt service routine or when loopholes in my coding method appreciated
Thanks in Advance
AK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在中断内,您直接或间接更改几个全局变量,例如 ptr、flag,我假设 rxBuffer[?]。它们没有被声明为“易失性”,因此当您从中断返回时,它们的值可能会或可能不会改变。这是一个错误,因为行为可能会根据代码执行中发生中断的位置以及优化级别而改变。根据经验,任何由中断例程修改的变量都应始终声明为易失性的。
Within the interrupt, you are directly or indirectly changing several global variables, e.g. ptr, flag, and I'm assuming rxBuffer[?]. They are not declared "volatile" so their value may or may not change when you return from the interrupt. This is a bug because the behavior can change based on where in the execution of the code the interrupt occurs and what the level of optimization is. As a rule of thumb, any variable modified by an interrupt routine should always be declared volatile.
如果您确定使共享变量变得易失性不起作用,那么我怀疑您已在某处将全局变量重新定义为局部变量。在调试时检查标志变量的地址,并确保它在 __set_flag() 中和中断外(您认为它尚未更新)中是相同的。
我还认为 ISR 中的轮询循环是糟糕的代码,您应该找到一种更好的方法来等待发射器准备好下一个字符。
If you're sure that making the shared variables volatile isn't working then I'd suspect you have redefined a global variable as a local variable somewhere. Check the address of the flag variable when you are debugging and make sure it is the same in __set_flag() and outwith the interrupt, where you think it has not been updated.
I also think that the polling loop in your ISR is poor code and you should find a better way to wait for the transmitter to be ready for the next character.
感谢我从成员那里得到的所有反馈。好吧,声明所有“变量易失性” 的想法成功了。
strcmp()
使用 const var* 所以我无法使用它。我必须编写自己的自定义字符串比较函数。所有这些小事都解决了我的问题。Thanks to all the feedback i got from the members. Well the idea of declaring all the "variables volatile" did the trick .
strcmp()
uses const var* so i couldn't use it . I had to write my own custom string compare function. All this minor things solved my problems.