Pages: [1]   Go Down
 Author Topic: binary output for an R/2R ladder.  (Read 1383 times) 0 Members and 1 Guest are viewing this topic.
0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
 « on: May 15, 2009, 10:01:08 pm » Bigger Smaller Reset

So, first post. I'm not sure if something like this is posted around here somewhere so here goes.

I'm trying to take an analog input from a pot (0-255) and send that to an 8-bit R/2R ladder connected to the digital pins. I know the easiest way to get the 0-255 value is to use:

map(potin, 0, 1023, 0, 255)

My question is, is there an easier way to do the binary output on the digital pins than using a switch/case with 256 cases?

-modius13
 Logged

Global Moderator
Dallas
Online
Shannon Member
Karma: 200
Posts: 12779
 « Reply #1 on: May 15, 2009, 10:11:18 pm » Bigger Smaller Reset

The simplest way would be to put all the eight lines of the ladder on a single I/O port (like port C).  You would then be able to output the value with a simple assignment...

PORTC = MyAnalogValue;

There's a description here that will hopefully get you started...

http://www.arduino.cc/en/Reference/PortManipulation

Good luck,
Brian
 Logged

0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
 « Reply #2 on: May 15, 2009, 10:32:16 pm » Bigger Smaller Reset

ah, that is cool. So, something along the lines of:

switch (potin)
{
case 1:
PORTD = B00000000
break;

case 2:
PORTD = B00000001
break;

...

case 256:
PORTD = B11111111
}

would be the way to go? Just want to make sure. I'm new to Arduino but have a basic grasp of C++

-modius13
 Logged

0
Offline
Karma: 16
Posts: 2857
ruggedcircuits.com
 « Reply #3 on: May 15, 2009, 10:41:05 pm » Bigger Smaller Reset

No, you're making it too complicated! What Coding Badly was coding properly was much simpler:
Code:
val = map(potin, 0, 1023, 0, 255);
PORTC = val;
The end. That's it! Assuming, of course, all your pins are on PORTC and you've configured all pins to be outputs.

The bigger question is: why? It sounds like you're trying to "reflect" an analog voltage set by a potentiometer to go somewhere else. Why not just use an op-amp in a noninverting buffer configuration? One little chip...
 Logged

0
Offline
Newbie
Karma: 0
Posts: 27
Arduino rocks
 « Reply #4 on: May 15, 2009, 10:55:43 pm » Bigger Smaller Reset

Ah, that makes a lot more sense. Thank you RuggedCircuits. As to why: It's going to be a part of something bigger. My own take on an Arduino synthesizer. I'll be sure to post results as I go along. Just waiting for my Arduino to arrive in the mail to implement things.

-modius13
 Logged

U of A, Tucson, AZ
Offline
Full Member
Karma: 0
Posts: 164
Hello World
 « Reply #5 on: May 15, 2009, 11:41:24 pm » Bigger Smaller Reset

Is there a port that you could actually do this with on a Duemilanove?  PORTD looks like the only one where you might be able to use all 8 pins, but 0 and 1 are the serial Tx/Rx pins, so you might have to connect/disconnect them every time you uploaded a new sketch.

You could also use a shift register chip like a 74HC595 and use shiftOut.  Modified from http://www.arduino.cc/en/Reference/ShiftOut ...
Code:
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

////Pin connected to your analog pot
int potPin = 5;

void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(potPin, INPUT);
}

void loop() {
val = map(potin, 0, 1023, 0, 255);

//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, MSBFIRST, val);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
delay(1000);
}

 Logged

Kenosha, WI
Offline
Newbie
Karma: 0
Posts: 16
Arduino
 « Reply #6 on: June 03, 2009, 06:01:29 pm » Bigger Smaller Reset

If you don't want to use a continuous port and you don't have a shift register chip "handy" you could just define a eight digital pins (I'm using 2 through 9, but you could use others) and use the following code:
Code:
val = map(potin, 0, 1023, 0, 255);
pin2 = (val&B1?HIGH:LOW);                 // LSB
pin3 = (val&B10?HIGH:LOW);
pin4 = (val&B100?HIGH:LOW);
pin5 = (val&B1000?HIGH:LOW);
pin6 = (val&B10000?HIGH:LOW);
pin7 = (val&B100000?HIGH:LOW);
pin8 = (val&B1000000?HIGH:LOW);
pin9 = (val&B10000000?HIGH:LOW);          // MSB

 Logged

 Pages: [1]   Go Up