【发布时间】:2015-07-05 03:58:04
【问题描述】:
/*This code takes in inputs from IR sensors connected on PA0 and PA1
and glows LEDs connected on PB0 and PB1*/
#include <avr/io.h>
int main()
{
DDRA=0x00; // PORTA (PA0 and PA1) will act as input
DDRB=0x03; //The last two pins PBO and PB1 will act as output.
int x=0x03 & PINA; //Initially when PINA=0, x=0
if (x==0x00)
{
PORTB=0x00;
}
else if (x==0x01)
// If input is given to PA0 then PINA=0x01 and x=0x01
{
PORTB=0x01;
}
else if (x==0x02)
// If input is given to PA1 then PINA=0x02 and x=0x02
{
PORTB=0x02;
}
else if(x==0x03)
// If input is given to PA0 and PA1 then PINA=0x03 and x=0x03
{
PORTB=0x03;
}
return 0;
}
【问题讨论】:
-
在 AVR 平台的嵌入式代码中从 main 返回几乎总是一个坏主意。它会跳回到代码中的其他地方,检查生成的程序集输出以找出确切的位置,如果你想要一个循环,你应该使用适当的主循环,而不是依赖返回的副作用来执行主循环.
-
另外,在这样的帖子中解释会发生什么与您预期会发生什么会很有帮助。这里到底出了什么问题?
-
如果你不使用循环,main 只会执行一次。 if 应该更好为
if ( x&3 == valore )。 -
它将返回到重置的 ISR,然后程序将从那里崩溃并烧毁。