Need to program ATmega for duplicate outputs on 2 different pins

I an looking for a way to configure 2 different PWM pins to have the same output. I am building a project that will use the same chip on 2 different boards and due to the size and location I will need to access the output signal from both sides from the board. In the example code below output PWM pin 11, is physically on the right side of the chip at pin 19. The chip which works fine on board which I will be building but on a second board that will use the same "pre-programmed"ATmrga328 chip i need the output on PWM pin 5, which is pin 11 on the left side of the chip. Hope this makes sense.

example code:

int ledPin = 11; // LED connected to digital pin 11

void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

Can't you simply configure both PWM output pins in the same way, so they do the same thing? Or, if you only want one output at a time but want to be able to select which one is used, you need to tell your sketch which one to use. For example:

  • Define it at compile time and upload a different sketch
  • Infer it from the state of the environment (input pin jumpered high/low etc)
  • Configure it in EEPROM or any other similar persistent storage
  1. you can simply hard-wire them; simplest and most effective solution;
  2. you can rewrite the pwm driver so in the isr you flip both pins; requires some work on your part;
  3. you can put an interrupt on the master pin, trigger by its state changes. in the isr, you flip your slave pin(s); requires the most amount of work but it is fully transparent: once set-up, no user intervention is required.

I was hoping to configure it something like this but I can not find any documentation that says I can.

example code:

int ledPin = 11 and 5; // LED connected to digital pin 11 and 5

Will this cause a call to ledPin to change both outputs at the same time?

Are you going to be controlling the two pins with digitalWrite commands only or with analogWrite commands also?

Also if board #1 is going to use digital pin 11 for something that you want board #2 to use pin #5 for the same function, What will board #1 have wired to it's digital pin 5 and what will board #2 have wired to it's digital pin #11?

I assume you are trying to allow it so you can write just one sketch that will be loaded into both boards but you want to be able to use different digital pin numbers on the two boards. This is really a screwy way to proceed as you can't load one sketch into two boards in a single upload operation anyway, why not just maintain two different sketches that just control the pins that board is using?

Lefty

I will only be controlling the two pins with digitalWrite commands. The 2 boards I am using are not "arduinos" but circuit board that I will fabricate for a small scale production. The pin issue comes from my need to have these boards as small as possible so getting off the chip on one side vs. the other becomes important. Since I am fabricating the boards and am limited in resources a double sided board is not a choice, else I would just run an extra trace underneath the chip. Two different sketches is a possible path but one run of chips that fit both scenarios is preferable.

snobound:
I was hoping to configure it something like this but I can not find any documentation that says I can.

example code:

int ledPin = 11 and 5; // LED connected to digital pin 11 and 5

Will this cause a call to ledPin to change both outputs at the same time?

No.

The pin issue comes from my need to have these boards as small as possible so getting off the chip on one side vs. the other becomes important.

No that is totally the wrong way to get round your problem, please do not think like this.
Find some other way like a flying lead if you PCB layout skills are not up to it.

Thanks everyone for the feedback.

Just to close the loop, here's what I ended up doing that it worked for my application.

example code:

int ledPin1 = 11; // LED connected to pin 11 used on one board
int ledPin2 = 5; // LED connected to pin 5 used on the other board

void setup()
{
pinMode(ledPin1, OUTPUT); // sets the pin as output
pinMode(ledPin2, OUTPUT); // sets the pin as output

analogWrite(ledPin1, red); // write to pin from variable "red"
analogWrite(ledPin2, red); // write to pin from variable "red"

...

By linking both pins to the same variable I can drive both pins at the same time. A little more code than I had planned on, but since my first shortcut did not work this will do the job.

Excuse us if we still have little confidence in what and why you are doing what you are doing. :wink:

I asked:

Are you going to be controlling the two pins with digitalWrite commands only or with analogWrite commands also?

You responded:

I will only be controlling the two pins with digitalWrite commands.

But then you posted code that uses:

analogWrite(ledPin1, red);        // write to pin from variable "red"
  analogWrite(ledPin2, red);        // write to pin from variable "red"

Lefty

Sorry about the confusion - mistake in my response to you earlier... I am doing analogWrite.
This portion of my program is for dimming an LED. I now have the same(equivalent) PWM signal on both pins so I can connect my LED driver circuit to either one with the same result.

snobound:
Sorry about the confusion - mistake in my response to you earlier... I am doing analogWrite.
This portion of my program is for dimming an LED. I now have the same(equivalent) PWM signal on both pins so I can connect my LED driver circuit to either one with the same result.

But again, why must only one sketch program work for both chips? You can't upload a single sketch into both chips at the same time, so why not have two sketches that operate only the pins that chip is using? This is really a paint-yourself-into-a-corner approach to programming AVR chips.

Lefty

I have built 2 boards (old version and new and improved version) that are very similar except for this pinout. I want to have only one version of the programmed chip (backwards compatible) in inventory that will work with whichever board I need to use it on.

This might make it a bit simpler.

#define bothPinMode(x) pinMode(ledPin1, x); pinMode(ledPin2, x)
#define bothAnalogWrite(x) analogWrite(ledPin1, x); analogWrite(ledPin2, x)

You could (slightly) simplify your code to

void setup()
{
  bothPinMode(OUTPUT);
  bothAnalogWrite(red);
}

snobound:
Since I am fabricating the boards and am limited in resources a double sided board is not a choice, else I would just run an extra trace underneath the chip.

I used to fabricate my own single-sided and occasionally double-side boards. But double-sided boards fabricated in China are so inexpensive (even for small quantities) that it's no longer worthwhile.