PDA

View Full Version : Digital-to-Analog Converter



deathcrow
03-29-2013, 02:37 AM
This lab I am working on is for digital to analog conversion. It is a simple device that converts a 3-bit digital number into an analog voltage between 0 and 5 volts. It is a digital-to-analog convertor [DAC].

So pretty much I wrote a C-language program to increment a variable when one button is pushed and decrement a variable when the other button is pushed. It then takes that base 10 value and outputs the binary value to the R2R ladder network on my circuit to produce an output voltage that I will be calling "V2".

A picture of my circuit is below. The bottom part is the microstamp11, the push buttons, and the 7-segment display. The top part is the R2R ladder network which I will explain later on in this thread.
http://puu.sh/2pRBz

And below is the code:


#include "vector.c"
#include "kernel5.c"

void display_digit (int data) {
int i;
for (i=1;i<8;i++)set_pin(i); //
switch(data){
case 0: //light segments a,b,c,d,e,f
clear_pin(1);clear_pin(2);clear_pin(3);
clear_pin(4);clear_pin(5);clear_pin(6);
break;
case 1: //light segements b,c
clear_pin(2);clear_pin(3);
break;
case 2: //light segments a,b,d,e,g
clear_pin(1);clear_pin(2);clear_pin(4);
clear_pin(5);clear_pin(7);
break;
case 3: //light segements a,b,c,d,g
clear_pin(1);clear_pin(2);clear_pin(3);
clear_pin(4);clear_pin(7);
break;
case 4: //light segments b,c,f,g
clear_pin(2);clear_pin(3);clear_pin(6);
clear_pin(7);
break;
case 5: //light segements a,c,d,f,g
clear_pin(1);clear_pin(3);clear_pin(4);
clear_pin(6);clear_pin(7);
break;
case 6: //light segments a,c,d,e,f,g
clear_pin(1);clear_pin(3);clear_pin(4);
clear_pin(5);clear_pin(6);clear_pin(7);
break;
case 7: //light segements a,b,c
clear_pin(1);clear_pin(2);clear_pin(3);
break;
case 8: //light segments a,b,c,d,e,f,g
clear_pin(1);clear_pin(2);clear_pin(3);
clear_pin(4);clear_pin(5);clear_pin(6);
clear_pin(7);
break;
case 9: //light segements a,b,c,f,g
clear_pin(1);clear_pin(2);clear_pin(3);
clear_pin(6);clear_pin(7);

}
}

void binary_output (int data) {
DDRD |= bit(0); //set the direction of PD0 to 1 (output)
DDRD |= bit(1); //set the direction of PD1 to 1 (output)
switch(data){
case 0: // b2b1b0 = 000
PORTD &=~ bit(0); //b2 = 0
PORTD &=~ bit(1); //b1 = 0
clear_pin(0); //b0 = 0
break;
case 1: // b2b1b0 == 001
PORTD &=~ bit(0); //b2 = 0
PORTD &=~ bit(1); //b1 = 0
set_pin(0); //b0 = 1
break;
case 2: // b2b1b0 == 010
PORTD &=~ bit(0); //b2 = 0
PORTD |= bit(1); //b1 = 1
clear_pin(0); //b0 = 0
break;
case 3: // b2b1b0 == 011
PORTD &=~ bit(0); //b2 = 0
PORTD |= bit(1); //b1 = 1
set_pin(0); //b0 = 1
break;
case 4: // b2b1b0 == 100
PORTD |= bit(0); //b2 = 1
PORTD &=~ bit(1); //b1 = 0
clear_pin(0); //b0 = 0
break;
case 5: // b2b1b0 == 101
PORTD |= bit(0); //b2 = 1
PORTD &=~ bit(1); //b1 = 0
set_pin(0); //b0 = 1
break;
case 6: // b2b1b0 == 110
PORTD |= bit(0); //b2 = 1
PORTD |= bit(1); //b1 = 1
clear_pin(0); //b0 = 0
break;
case 7: // b2b1b0 == 111
PORTD |= bit(0); //b2 = 1
PORTD |= bit(1); //b1 = 1
set_pin(0); //b0 = 1
break;
}
}

void main (void){

//int count1=0; //incremented by button 1 (pin 6 - hardware pin 8)
//unsigned int count2=0; //decremented by button 2 (pin 7 - hardware pin 7)
int Count = 0;
init();
disable_sci(); //disable serial comm so that pins 19-20 can be used as outputs
display_digit(Count);
while(1){ //infinite loop
if (read_pin(6) == 1 && read_pin(7) == 0) //increment button pressed
{button(6); //use modified button command in kernel5.c
Count++;} //increment counter
if (read_pin(6) == 0 && read_pin(7) == 1) //increment button pressed
{button(7); //use modified button command in kernel5.c
Count--;} //decrement counter
display_digit(Count); //call function display_digit to light segments
binary_output(Count); //call function to output binary values to the R2R ladder
}
}


The left of the image below is the R2R ladder network which transforms the digital voltage levels into an analog voltage. On the right of the image below is just how I solve for the output voltage when the inputs are 100 which is binary for 4. The output voltage when the 7-segment display shows 4 should be 2.5V.
http://puu.sh/2pS4A

Yes. I know I have horrible hand writing. Don't judge. =p


The image below simply shows what the output voltage should be when a certain number is shown on the 7-segment display.
http://puu.sh/2pS5B


Forgot to mention how one is supposed to make an R2R ladder network. An R2R ladder network is pretty much a large resistive circuit with independent sources (inputs). The network requires two different resistor values which should be R and 2R (2 times the value of R). In my network I used 2.2k ohm resistors (Red, Red, Red, Gold). I used 10 of them since I need to place 2 in series for every "2R".

Here is a diagram of it:
http://puu.sh/2pSnQ

tealc
03-29-2013, 02:51 AM
That stuff isn't fun I had to drop a similar class this semester.

tehq
03-29-2013, 03:42 AM
Interesting procedure & data. This reminds me of an experiment which I recently worked on (delta-sigma modulation in motor controllers).

Although I'm assuming this was completed for an introduction to electrical engineering course. Good luck!