Go Down

Topic: Direct pin manipulation on pin 12? (Read 522 times) previous topic - next topic

nali2001

Hello all,
Does anyone out here know why direct pin manipulation works fine on pin 4:

Code: [Select]
void setup(){
  pinMode(4, OUTPUT);
}
void loop(){
  PORTD |= _BV(4);
  PORTD &= ~_BV(4);
}


But on pin 12 with PORTD changed to PORTB it does not work:

Code: [Select]
void setup(){
  pinMode(12, OUTPUT);
}
void loop(){
  PORTB |= _BV(12);
  PORTB &= ~_BV(12);
}


As I understand from: http://www.arduino.cc/playground/Learning/PortManipulation
PORTB should be the register for pin 12.

What am I doing wrong?
Thanks for the help!

nali2001

Hmm never mind.
For pin 12 it is:

Code: [Select]
"void setup(){
 pinMode(12, OUTPUT);

}
void loop(){
 PORTB |= _BV(4);
 PORTB &= ~_BV(4);
}


As in pins_arduino.c it says:
Code: [Select]
const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
_BV(0), /* 0, port D */
_BV(1),
_BV(2),
_BV(3),
_BV(4),
_BV(5),
_BV(6),
_BV(7),
_BV(0), /* 8, port B */
_BV(1),
_BV(2),
_BV(3),
_BV(4),
_BV(5),
_BV(0), /* 14, port C */
_BV(1),
_BV(2),
_BV(3),
_BV(4),
_BV(5),

retrolefty

Correct. as ports on AVR chips are only 8 bits wide, ( BV(x); ) BV values can only be 0-7.



Go Up