Arduino DAC Multiport
This is an Arduino program to make a 6 bit DAC using an R2R ladder connected to the corresponding pins on the Arduino.
The code is a slight modified version of a previously existing one provided by FIUBA's professors from lecture "Introducción a la Ingeniería Electrónica", and corresponds to the Final Group Project. The modification adds the ability to use pins from different ports instead of all the pins from the same port.
The ports are 8 bits registers that Arduino uses to internally represents the output pins. You can find which pin correspond to which port by looking on any Arduino pinout diagram, being the most common ports PORTB, PORTC and PORTD.
The code uses PORT manipulation since its faster than using builtin function like pinMode(), and thus improving the softness of the recreated wave by the R2R.
The following statements produce the same effect on the arduino output pins and shows the difference in speed of using builtin functions and port manipulation (Taken from this arduino forum question)
pinMode(5, OUTPUT); // takes 71 clock cycles (~4.430us@16Mhz)
DDRD |= 0b00010000; // takes 2 clock cycles (~0.125us@16Mhz)
bitSet(PORTD, 5, 1); // takes 2 clock cycles (~0.125us@16Mhz)
Also assigning the new value to the port allow for multiple ports to be updated on a single statement, for example turning on both pin 4 and 5 at the same time can be done with the following statement
DDRD |= 0b00011000;
For more information I suggest reading Arduino Reference on Port Manipulation
1 # Arduino DAC Multiport 2 3 This is an Arduino program to make a 6 bit DAC using an [R2R 4 ladder](https://en.wikipedia.org/wiki/Resistor_ladder) connected to the 5 corresponding pins on the Arduino. 6 7 The code is a slight modified version of a previously existing one provided by 8 [FIUBA](https://es.wikipedia.org/wiki/Facultad_de_Ingenier%C3%ADa_(Universidad_de_Buenos_Aires))'s 9 professors from lecture "Introducción a la Ingeniería Electrónica", and 10 corresponds to the Final Group Project. The modification adds the ability to use 11 pins from different ports instead of all the pins from the same port. 12 13 The ports are 8 bits registers that Arduino uses to internally represents the 14 output pins. You can find which pin correspond to which port by looking on any 15 Arduino pinout diagram, being the most common ports PORTB, PORTC and PORTD. 16 17 The code uses PORT manipulation since its faster than using builtin function 18 like pinMode(), and thus improving the softness of the recreated wave by the 19 R2R. 20 21 The following statements produce the same effect on the arduino output pins and 22 shows the difference in speed of using builtin functions and port manipulation 23 (Taken from [this arduino forum 24 question](https://forum.arduino.cc/t/ddr-vs-pinmode-solved/497927/3)) 25 26 ```c 27 pinMode(5, OUTPUT); // takes 71 clock cycles (~4.430us@16Mhz) 28 DDRD |= 0b00010000; // takes 2 clock cycles (~0.125us@16Mhz) 29 bitSet(PORTD, 5, 1); // takes 2 clock cycles (~0.125us@16Mhz) 30 ``` 31 32 Also assigning the new value to the port allow for multiple ports to be updated 33 on a single statement, for example turning on both pin 4 and 5 at the same time 34 can be done with the following statement 35 36 ```c 37 DDRD |= 0b00011000; 38 ``` 39 40 For more information I suggest reading [Arduino Reference on Port 41 Manipulation](https://www.arduino.cc/en/Reference/PortManipulation)
