Go Down

Topic: Control output pins using 8-bit number (Read 2871 times) previous topic - next topic

dannydig

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.

Groove

Easy way is to use bitRead.
More difficult is direct port manipulation.
100Hz should be easy.
Per Arduino ad Astra

markohiero

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!

AWOL

#3
Dec 18, 2010, 11:11 pm Last Edit: Dec 18, 2010, 11:17 pm by AWOL Reason: 1
Code: [Select]
potmeter = analogRead(0); //pick an analog port..
  potmeter &= 0xFF;


maybe "potmeter /=4;" would be a little less erratic.
"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.

Darth Maker

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().

markohiero

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

dannydig

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.

Groove

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.
Per Arduino ad Astra

lloyddean

#8
Dec 22, 2010, 06:16 pm Last Edit: Dec 22, 2010, 09:34 pm by lloyddean Reason: 1
If I understand the question correctly something along the lines of:

Code: [Select]

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

Groove

Your 'while' is too long - make the mask a byte.
Per Arduino ad Astra

lloyddean

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

dannydig

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

I presume it's something like this:
Code: [Select]

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.

Groove

#12
Dec 23, 2010, 02:25 pm Last Edit: Dec 23, 2010, 02:27 pm by GrooveFlotilla Reason: 1
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.
Per Arduino ad Astra

AWOL

#13
Dec 23, 2010, 02:32 pm Last Edit: Dec 23, 2010, 02:37 pm by AWOL Reason: 1
Code: [Select]

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

dannydig

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.

Go Up