Pages: [1]   Go Down
Author Topic: Control output pins using 8-bit number  (Read 2413 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to know how to control eight output pins using an 8-bit binary number (ie. 00001100 would turn outputs 2 and 3 on). In actual fact, it would be even better if I could control them with a decimal number (0-255) - depending on the state of a potentiometer. But they must output a binary value: my project involves reading an analog sensor (eventually, it will be serial data coming from the computer (in decimal format)), and creating another analog voltage using an Arduino and a DAC0800.

I am going to connect it like it is attached to the ROM in the following diagram:
http://www.cgs.synth.net/modules/pic/schem_cgs02_wavetable.gif

Do you know what the output rate would be (ie. changes possible per second)? I am hoping for about 100 if possible. The more the better.

Thanks.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Easy way is to use bitRead.
More difficult is direct port manipulation.
100Hz should be easy.
Logged

Per Arduino ad Astra

The Hague Holland
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Let's build things
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when you use this syntax to send the data to the dac:
(assuming that the dac is on portd == pins 0 through 7) it works fast and simple:
setup(){

  DDRD=0xFF;        //set portd as output
  PORTD = 0;         //set dac to 0
}


loop{

    int potmeter = 0;
    
    potmeter = analogRead(0); //pick a analog port..
    potmeter &= 0xFF;
    PORTD = portmeter;
}

Good luck!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
potmeter = analogRead(0); //pick an analog port..
   potmeter &= 0xFF;

maybe "potmeter /=4;" would be a little less erratic.
« Last Edit: December 18, 2010, 05:17:57 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 38
Turn it off!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

potmeter /=4; is what you want to use for another reason also.  If you and with 0xFF, every time the pot goes over that value, it loops and starts counting on the pins again.  Divide by 4 will give you 255 steps between 0v and 5v.  However, you don't actually need the variable there:

PORTD = byte(analogRead(0)/4);

will work just fine.  You may not need the byte().
Logged

The Hague Holland
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Let's build things
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry,
Guys tour write, dividing is better than chopping bits off
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thanks for the replies. However, the only problem is that I would like the DAC to be on pins 2-9, so I have access to the Serial Tx/Rx lines (pins 0 and 1). Is this possible to do? Any ideas? Unfortunately, there are only six data lines on Port B.
Thanks.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your simplest option is probably 'bitRead', or if you're feeling advanced, a bit of port manipulation for the bulk of the bits on a single port, and bitRead for the remainder.
Logged

Per Arduino ad Astra

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I understand the question correctly something along the lines of:

Code:
byte    pins[2, 3, 4, 5, 6, 7, 8, 9];
byte*   p = pins;

unsigned byte   mask_bit = 0b00000001;
do
{
    digitalWrite(*p++, (value & mask_bit) ? HIGH : LOW);
} while ( mask_bit <<= 1 );

EDIT:  Changed per below
« Last Edit: December 22, 2010, 03:34:30 pm by lloyddean » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your 'while' is too long - make the mask a byte.
Logged

Per Arduino ad Astra

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks I thought I'd corrected that before the post but apparently not.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lloyddean and Groove, could you please format your example below in a way a beginner like me can use it  smiley

I presume it's something like this:
Code:
void setup() {
  Serial.begin(9600);
}

void loop() {
  byte p = analogRead(0);

  byte pins[2, 3, 4, 5, 6, 7, 8, 9]; //an array

  unsigned byte mask_bit = 0b00000001;
  do
  {
    digitalWrite(*p++, (value & mask_bit) ? HIGH : LOW);
  }
  while ( mask_bit <<= 1 );
}

Unfortunately, the compiler doesn't like this. Could you help? I just want to read a pot, and output it's value to the DAC for now.

I would eventually like to control the DAC through MATLAB as seen at http://www.mathworks.com/matlabcentral/fileexchange/27843

Thanks.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your pins array declaration/initialisation is incorrect.
Look for some examples to see how it should be.
also I don't know what your doing withe the *p++, but you should be indexing the pins array, not trying to dereference a non-existent pointer.
Sorry, on train, posting via phone.
« Last Edit: December 23, 2010, 08:27:54 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const  byte pins [8] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  Serial.begin(9600);
}

void loop() {
  byte p = analogRead(0) / 4;
  for (int i = 0, unsigned byte mask_bit = 1; i < 8; ++i, mask_bit<<=1) {  {
    digitalWrite(pins [i],  !!(p & mask_bit));
  }
}
 

You should be aware that because you're not writing the DAC in a single operation, the output will wander around a bit, though if you're driving it with a pot, you probably won't notice this.
« Last Edit: December 23, 2010, 08:37:15 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys, thanks very much for your help so far. I have got a prototype up and running, however, the issue is at the moment how the 8-bit number updates one bit at a time. It is causing the DACs output to swing all over the place when the update speed is high (ie. 100 times per second).

Is there any way to output the 8-bit number at once? Perhaps I may have to use an Arduino Mega instead so I have full access to one of the PORT outputs, and the serial outputs.
Logged

Pages: [1]   Go Up
Jump to: