I'm learning to use AVRs (Attiny 2313A) with a simple led-blinking program:
#define F_CPU 10000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRB = 0xFF;//B output DDRD = 0x00;//D input PORTB = 0xFF;//LED off PORTD = 0x11111111; while(1) { if(PIND & (1 << 0) == 1) { PORTB &= ~(1<<PB0); /* LED on */ _delay_ms(100); PORTB |= 1<<PB0; /* LED off */ _delay_ms(100); }} return 0; }
It works fine. Led blinks as long as I don't touch the buton. When I press it the led goes off.
However there are couple of problems.
First, if I move the button to pin PD1 and change
PIND & (1 << 0)
to
PIND & (1 << 1)
the code works no more. The led stays off.
Second problem is that I can't make led blink while I press the button. I've tried
if(PIND & (1 << 0) == 0)
but the led stays off again.
This is the first program made (mostly) on my own so there is probably some beginner's mistake in there.
I hope it isn't very stupid one.