Avr Atmel
2008
![]() |
![]() New 2 Channel 5V Relay Module Shield for Arduino AVR UNO MEGA 2560 R3 ATMEL $4.29 Time Remaining: 24d 21h 40m Buy It Now for only: $4.29 |
![]() NEW 8 Channel 24V Relay Shield Module for Arduino UNO MEGA 2560 R3 AVR ATMEL $17.50 Time Remaining: 24d 21h 38m Buy It Now for only: $17.50 |
![]() SainSmart 8 Channel Signal Relay 4 Arduino ARM ATMEL ATMEGA AVR UNO MEGA2560 R3 $37.99 Time Remaining: 1d 2h 11m Buy It Now for only: $37.99 |
Avr Atmel

AVR doubt regarding while loop?
Hello,
I am using atmel atmega16 and I'm using a while statement. Only if the condition is satisfied, the program should flow on. But even idf the condition is not satisfied the program flows on. That is,
#include
#include
#include
#include
void main(void)
{
int c,d,e,f,g;
c=0;d=0;e=0;f=0;g=0;
DDRA=0x00;
DDRC=0x00;
DDRD=0x00;
DDRB=0xff;
PORTB |= _BV(PB1);
while(!(PINA & (1<
c++;
}
PORTB |= _BV(PB2);
}
Both pins PB1 and PB2 are going HIGH even though PA0 is low (given by me).
Any idea whats the problem.
Thank you
You are seting the PB1 & PB2 bits.
The sbi and cbi command have been depreciated and you now access these commands using
sbi(PORTB, PB1); is now PORTB |= _BV(PB1);
cbi (sfr,bit) is now sfr &= ~(_BV(bit));
Your while statement should loop as long as PA0 is held low.
This appears to not be happening as PB2 is being set.
SO.... are you establishing that PA0 is being held low at the start of the microcontroller? Remember that all inputs have internal pull up resistors. So if you do not specifically tie PA0 to ground its default input will be high and at power up your code will execute all the way through to the end.
Either establish a hard low at PA0 with a wired connection and reset the uC
OR change your logic in the while statement to loop while high and advance when low ( just get rid of the !)
Another trouble shooting aid is to force and endless loop by using a literal value.
while(!(PINA & (1< becomes: Then recompile and reload and test to see that only PB1 is high. This validates your loop.and compilier. Your value of c in this case is not be used other than a loop counter. I suspect that since your clock speed is so fast that the value in c will most likely overflow several times. again to make shure that this isn't an issue replace the increment with either a NOP or a literal assignment like c=0. This prevents c from overflowing and will help you determine if the overflow is causing an issue. You should also join AVR Freaks the forums there will offer much helpful advice. EDIT: (1< In this case PA0 = bit 0 so you really aren't doing a shift. Assume PINA has a value of 0x06 or b00000110 Then PINA is going to return the 8 bit value for port A Atmel AVR Studio 5 Introduction
while(!(0)) or While (1)
After setting the DDR activate the internal pull up if you are not using external pull up resistors. Set Bit values in PORTA to activate the pull up after the DDR has been configured as input. See page 52 in the data sheet
Your saying load 1 and left shift zero positions. Had you choosen another pin on port A then an actual shift will ocurr based on the value of PAx
But you are increasing the readability of the code as PA0 is being set (=1).
This is easier to read than a lieral value such as 0x01 or binary b00000001
_00000110
&00000001
----------------
=00000000 invert this with a not you get 0xFF or b11111111



