I am using an Atmega 328 and version 1.0.2 IDE but can not make digitalWrite work for pin 14 (or 15,16 etc). The below code works fine and blinks but if I use the commented out code instead of the existing code I get no blink? I could not find any documented prohibition on using digitalWrite with PORTB. I even replaced the variable led with 14 but still no blink. I know the circuitry is ok because the register oriented code works. (I even wrote my own 4 character 7 segment display and it works fine using registers, just can not use pinMode and digitalWrite)
I am a bit confused as to what actually does not work.
Can you post the code that does not work and use the correct code tags when you do. It is the # icon in the reply box.
Sorry but I do not know what you mean by code tags (I am new and this is my first post).
The top post in this section says Read this before posting. That tells you all about how to post code.
When you post it correctly people can copy your code and try it out. It looks fine so I would like to try it to attemp to help you, please help me to help you.
Thank you for your quick response. Again, the code is below.
NOTE I changed the int led = 14 to int led = 13 and the on board LED blinked fine.
It simply will not blink with pin 14 of register B using digitalWrite().
#define MS 500
int led = 14;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH); // no LED output
delay(MS);
digitalWrite(led, LOW);
delay(MS);
}
Thanks for posting the code. I tried it exactly as posted and it flashes fine.
I am using an Arduino Uno.
Pin 14 is analogue pin 0, are you connecting your LED and resistor to this pin?
To all who so kindly responded. I have found the problem. In my ignorance I assumed the pin number for digitalWrite was the chip (Atmega 328) pin number. I now know that it is for the BOARD pin numbers. Since my board is a Boarduino, which apparently has different pin numbers for the digitalWrite and pinMode which are board specific the compiled code was wrong. I got the register information directly from Atmega documentation for the chip, which is why the register referenced code worked.
In my defense I am very new to these platforms and I have not coded for hardware for about thirty years so please excuse my blunder. What would be nice if someone could refer me to some documentation that separates the board specific code from the underlying C, and C++ code. That way I could avoid those pitfalls.
Good to know that even with different pin (board) numbers the basic chip specific code works great.
Basically there are a load of functions that the compiler pulls in that relate to the board and the pin numbers. These are mainly inside the Arduino application, you can open this up and have a look at them, but it makes heavy reading as it is just a load of defines.
Since my board is a Boarduino, which apparently has different pin numbers for the digitalWrite and pinMode
You sure of that? It is not something I remember any one else reporting.