Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
Is this valid?if( (PIND) & (1 << 2) ) { //Do something when port D2 input is high.}
Shouldn't this work? DDRD = 0x14; while (1) { if (PIND2) { PORTD = 0x04; } else { PORTD = 0x14; } }However, it doesn't.If someone could post some code which works which I could reverse-engineer it would be much appreciated.Thanks in advance.
I've got a switch attached from VCC to PD2 and an LED from VCC to PD4.My code is:DDRD = 0x10;while(1) { if ((PIND) & (0x03)) { PORTD = 0x00; } else { PORTD = 0x10; }}
#include <avr/io.h>#include <util/delay.h>#define led PB2#define sw PB0int main(){ DDRB |= (1 << led); for (;;) { if (PINB & (1 << sw)) PORTB |= (1 <<led); else PORTB &= ~(1 << led); _delay_ms (100); }}
Quote from: jonagik on August 19, 2011, 10:57:52 PMI've got a switch attached from VCC to PD2 and an LED from VCC to PD4.My code is:DDRD = 0x10;while(1) { if ((PIND) & (0x03)) { PORTD = 0x00; } else { PORTD = 0x10; }}You need to test for a specific pin. When you say "((PIND) & (0x03))" you're testing to see if both pin 0 and pin 1 are on, which they won't be. This is for a tiny85 but the idea is the same.Code: [Select]#include <avr/io.h>#include <util/delay.h>#define led PB2#define sw PB0int main(){ DDRB |= (1 << led); for (;;) { if (PINB & (1 << sw)) PORTB |= (1 <<led); else PORTB &= ~(1 << led); _delay_ms (100); }}Joe
PORTD &= !(1 << 4);
PORTB &= ~(1 << 4);
This line:Code: [Select] PORTD &= !(1 << 4);should be:Code: [Select]PORTB &= ~(1 << 4);
PORTD &= ~(1 << 4);
I've got a switch attached from VCC to PD2 and an LED from VCC to PD4.
Isn't ((PIND) & (0x03)) the same as ((PIND) & (1 << 2))?
DDRD |= 0x10; //or DDRD |= (1<<4) or DDRD |= 0b00010000 --> PD4 used for LED, so make it output.for(;;){ if( PIND & (1<<2) ){ PORTD |= (1<<4); }else{ PORTD &= ~(1<<4); }}
VCC to switch to PD2 (pin 12)