Pages: [1]   Go Down
Author Topic: Need to program ATmega for duplicate outputs on 2 different pins  (Read 455 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}

« Last Edit: December 11, 2012, 09:02:50 am by snobound » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12557
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12557
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33373
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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

I asked:

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

You responded:

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

But then you posted code that uses:

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

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3364
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might make it a bit simpler.

Code:
#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

Code:
void setup()
{
  bothPinMode(OUTPUT);
  bothAnalogWrite(red);
}
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: