Pages: [1]   Go Down
Author Topic: Direct pin manipulation on pin 12?  (Read 453 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm never mind.
For pin 12 it is:

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

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

As in pins_arduino.c it says:
Code:
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),
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 
Logged

Pages: [1]   Go Up
Jump to: