Basic question understanding APIs and directly using registers on uC.

Hello,

I have a basic issue that I want to understand.

To light the onboard LED (pin13) on Arduino duamilanovae, the example code is:

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

If I look at the source of the pinMode(), it seems to refer to "_BV( 7 ) , // PB 7 ** 13 ** PWM13" in "PROGMEM digital_pin_to_bit_mask_PGM[]".
It says so even at "http://urbanhonking.com/ideasfordozens/2009/05/18/an_tour_of_the_arduino_interna/"

I did the following, and got the LED to flash. So, it should be PB5 not PB7 as above - right?

void setup ()
{
  DDRB = B00100000;  // PB7 ..... PB0, Pin 13 (Ard) is connected to PB5 (Mega)
}
void loop ()
{
  PORTB = B00100000;
  delay (800);
  
  PORTB = 0x0;
  delay (800);
}

I would rather ask a stupid question than carry on without understanding properly.

Thanks for the help.

Yes you are right and they are wrong, see also here for the right answer:-

Grumpy_Mike:
Yes you are right and they are wrong, see also here for the right answer:-
http://www.arduino.cc/en/Reference/PortManipulation

Read that link - not sure I got it..

But, I can blink the LED if I use the example program even though as per the source, pin 13 points at PB7 (as per the source code).
I can also go the register route and blink the LED using PB5.

So, am I missing something in understanding this? I just started with Arduino yesterday, although I have been reading up on Mega328 for a week or so.

Pin 13 is PB5
It says in that link

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

So if the source code appears to point to PB7 then you are either reading it wrong or something else overrides it.

"_BV( 7 ) , // PB 7 ** 13 ** PWM13" in "PROGMEM digital_pin_to_bit_mask_PGM[]".

You are looking at the pin definitions for the Mega, not Duemilanove.

Grumpy_Mike:
Pin 13 is PB5
It says in that link

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

So if the source code appears to point to PB7 then you are either reading it wrong or something else overrides it.

Mike,

Sorry - one more question..

I have the latest version of Arduino IDE. In pins_arduino.h, I have the following:

const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
	// PORTLIST		
	// -------------------------------------------		
	PE	, // PE 0 ** 0 ** USART0_RX	
	PE	, // PE 1 ** 1 ** USART0_TX	
	PE	, // PE 4 ** 2 ** PWM2	
	PE	, // PE 5 ** 3 ** PWM3	
	PG	, // PG 5 ** 4 ** PWM4	
	PE	, // PE 3 ** 5 ** PWM5	
	PH	, // PH 3 ** 6 ** PWM6	
	PH	, // PH 4 ** 7 ** PWM7	
	PH	, // PH 5 ** 8 ** PWM8	
	PH	, // PH 6 ** 9 ** PWM9	
	PB	, // PB 4 ** 10 ** PWM10	
	PB	, // PB 5 ** 11 ** PWM11	
	PB	, // PB 6 ** 12 ** PWM12	
	PB	, // PB 7 ** 13 ** PWM13	      [b]// <<<<<< This one[/b]
	PJ	, // PJ 1 ** 14 ** USART3_TX	
	PJ	, // PJ 0 ** 15 ** USART3_RX	
.
.

Also, at the beginning of this file "pins_arduino.h", it says:

// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
// Only pins available for RECEIVE (TRANSMIT can be on any pin):
// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me)
// Pins: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

What does it mean? I should be able to use almost any pin using softwareserial for RX - right?

Thanks!

Again you are looking at the pins_arduino.h file for the mega, not the Duemilanove.

James,

Sorry - didn't see your post while posting my response.

Please help me understand this..
In the tutorial at http://urbanhonking.com/ideasfordozens/2009/05/18/an_tour_of_the_arduino_interna/, it says while describing the simpe LED blink program:
** pinMode(13, OUTPUT);**
.
.
So, how do we construct the bit mask we’re going to use to make sure we only turn on pin 13? Let’s proceed through the macro in execution order starting at the center of the nested parentheses with: digital_pin_to_bit_mask_PGM + (P).
.
.
I’ve elided some array elements for the sake of brevity, but the numbering system is well indicated by the comments; the 14th element in this array, the one corresponding to our 13th pin is the element that reads: _BV( 7 ).

This is what I don't understand.

Also, does it even matter if it's Duemilanove pin? Because, Duemilanove is connected to mega - right? So, when I write a program, I have to look up the pins I'm connecting to in Duemilanove, get the corresponding pins of Mega, and then program as per the mega pins - right?

If I'm using digital pins 2,3,8,9,10 (just an example), I have to work with:
2 PD2
3 PD3
8 PB0
9 PB1
10 PB2

Is this right?

Thanks!

usb_linux:
http://urbanhonking.com/ideasfordozens/2009/05/18/an_tour_of_the_arduino_interna/, it says while describing the simpe LED blink program:

Since arduino.cc is missing from the "tutorial's" URL, I'm not sure why you would but so much faith into it.

usb_linux:
Also, does it even matter if it's Duemilanove pin? Because, Duemilanove is connected to mega - right?

The Arduino Mega uses the ATmega 1280/2560 which has a different pin out of the Duemilanove/Uno's ATmega328.

James,

Sorry - my mistake.. I just googled for a tutorial, and was reading that.

I thought mega means Atmel mega328 that's on Duemilanovae. Sorry about that. Still learning!

So, when I write a program, I have to look up the pins I'm connecting to in Duemilanove, get the corresponding pins of Mega, and then program as per the mega pins - right? If I'm using digital pins 2,3,8,9,10 (just an example), I have to work with:
2 PD2
3 PD3
8 PB0
9 PB1
10 PB2

Is this right?

Also, any idea what the following means:

At the beginning of this file "pins_arduino.h", it says:
// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
// Only pins available for RECEIVE (TRANSMIT can be on any pin):
// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me)
// Pins: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

Thanks for educating me.. Arduino is very interesting..

usb_linux:
So, when I write a program, I have to look up the pins I'm connecting to in Duemilanove, get the corresponding pins of Mega, and then program as per the mega pins - right? If I'm using digital pins 2,3,8,9,10 (just an example), I have to work with:
2 PD2
3 PD3
8 PB0
9 PB1
10 PB2

Huh? Instead of looking at the "pins_arduino.h" in the "mega" directory, look at the file in the "standard" directory. That will tell you what pins 1-13 map to on the Duemilanove board. Or, just look at the schematic of the Duemilanove.

usb_linux:
Also, any idea what the following means:

At the beginning of this file "pins_arduino.h", it says:
// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
// Only pins available for RECEIVE (TRANSMIT can be on any pin)[/quote]
Most of the pins can't be used for interrupts. But why does it matter? That file is for the ATmega1280/2560 which isn't the chip you are using.

[quote author=James C4S link=topic=100042.msg750467#msg750467 date=1333661566]

usb_linux:
So, when I write a program, I have to look up the pins I'm connecting to in Duemilanove, get the corresponding pins of Mega, and then program as per the mega pins - right? If I'm using digital pins 2,3,8,9,10 (just an example), I have to work with:
2 PD2
3 PD3
8 PB0
9 PB1
10 PB2

Huh? Instead of looking at the "pins_arduino.h" in the "mega" directory, look at the file in the "standard" directory. That will tell you what pins 1-13 map to on the Duemilanove board. Or, just look at the schematic of the Duemilanove.

usb_linux:
Also, any idea what the following means:

At the beginning of this file "pins_arduino.h", it says:
// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
// Only pins available for RECEIVE (TRANSMIT can be on any pin)[/quote]
Most of the pins can't be used for interrupts. But why does it matter? That file is for the ATmega1280/2560 which isn't the chip you are using.
[/quote]
James,
You are right.. I thought I was looking at the right file. Sorry about that. I got confused by that tutorial. May be he was using Arduino Mega?
Anyway, thanks for pointing out my mistake. Appreciate it.
Also, would it be a good idea to delete this entire conversation, just so that no one else gets confused because of this?

usb_linux:
Also, would it be a good idea to delete this entire conversation, just so that no one else gets confused because of this?

Deleting a thread is only a good idea when cross-posted.

just so that no one else gets confused because of this?

It is exactly the fact that you got confused by this that it is useful leaving the thread as it is.
Someone searching on a miss comprehension term is more likely to find it and read it through to it's resolution.