0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« on: December 18, 2010, 08:48:45 am » |
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.gifDo 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
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #1 on: December 18, 2010, 09:33:02 am » |
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
Newbie
Karma: 0
Posts: 11
Let's build things
|
 |
« Reply #2 on: December 18, 2010, 04:24:23 pm » |
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
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: December 18, 2010, 05:11:43 pm » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 1
Posts: 38
Turn it off!!!!
|
 |
« Reply #4 on: December 19, 2010, 03:29:42 pm » |
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
Newbie
Karma: 0
Posts: 11
Let's build things
|
 |
« Reply #5 on: December 19, 2010, 05:11:00 pm » |
Sorry, Guys tour write, dividing is better than chopping bits off
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #6 on: December 22, 2010, 06:43:42 am » |
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
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #7 on: December 22, 2010, 07:24:26 am » |
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
God Member
Karma: 21
Posts: 703
|
 |
« Reply #8 on: December 22, 2010, 12:16:58 pm » |
If I understand the question correctly something along the lines of: 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
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #9 on: December 22, 2010, 12:36:52 pm » |
Your 'while' is too long - make the mask a byte.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #10 on: December 22, 2010, 12:42:11 pm » |
Thanks I thought I'd corrected that before the post but apparently not.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #11 on: December 23, 2010, 08:13:53 am » |
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: 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/27843Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #12 on: December 23, 2010, 08:25:06 am » |
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
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: December 23, 2010, 08:32:23 am » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 15
Arduino rocks
|
 |
« Reply #14 on: January 17, 2011, 08:00:45 am » |
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
|
|
|
|
|
|