Lecture_Lab4

Download Report

Transcript Lecture_Lab4

Lab 4: D/A Converter
1
Lab 4: D/A Converter
1000
1000 ohms
This is a simple resistive network for a D/A converter
Port 1, Port 0 are digital inputs ==> 00 (minimum), 01,
10, 11 (maximum)
You need to design the resistive network to generate
proper outputs.
For ECE5430 students, a four-bit D/A is recommended.
2
Lab4: D/A Converter
STK500
357
PB1
R3
VS
833
PB0
R2
ATMEGA16
RXD
AREF
TXD
ADC0
(PA0)
To Computer Serial Port
1000
R1
VTG
STK500 reads the analog value from VS using ADC channel 0
3
Lab 4: D/A Converter
• Equations for D/A Outputs:
• B’00:
0.0 VS = (R1 || R2 || R3 ) * (0 Volts)
• B’01:
0.3 VS = (R1 || R3 ) * (5 volts) / ((R1 || R3 ) +
R2 )
• B’10:
0.7 VS = (R1 || R2 ) * (5 volts) / ((R1 || R2 ) +
R3 )
• B’11:
1.0 VS = R1 * (5 volts) / ((R2 || R3 ) + R1 )
• Given: VS = 4 Volts, R1 = 1000
• Find R2 and R3 by solving 2 equations with 2 variables
using the equations for B’01 and B’11.
Here we choose R1 = 1000 ohms.
You can choose any proper value you want for R1.
If R1 is too small, it may require large currents resulting in
overheat problem.
4
Lab 4: D/A Converter
Theoretical vs Actual DAC
5.0
4.5
4.0
DAC Output (V)
3.5
3.0
2.5
Theoretical DAC
Actual DAC
2.0
1.5
1.0
0.5
0.0
0
1
2
3
4
Binary Output
5
Lab 4: D/A Converter
B'00
B'01
R3
R6
357
357
R2
R5
0V
1.200V
833
833
V1
R1
5Vdc
R4
1k
1k
0V
0
0V
0
0
B'10
5.000V
0
B'11
R9
R12
357
357
R8
R11
2.800V
4.000V
5.000V
833
V2
833
V3
5Vdc
R7
5Vdc
R10
1k
1k
0V
0
0
0V
0
0
6
Lab 4: D/A Converter
• In-Lab Tasks
• Construct Circuit 4-1 with appropriate values of
resistance. Do NOT connect Circuit 4-1 to the AVR mcu.
• Given your computed values of R2 and R3, verify the
voltage of VA for the four possible port values.
• Connect Circuit to the AVR mcu.
• Write software for the AVR mcu that sets analog voltages
at VA. Have the software loop through the voltages from
0.0VS, 0.30VS, xVS, 1.0VS and step back down to
0.0VS. Include a delay between each value. Use the Croutine delay_ms() in <delay.h> to generate the delay.
• Verify and document the resulting voltages of VA.
7
Lab 4: D/A Converter
Sample of Software flowchart
Start
Initialize peripherals in ATMEGA16 P:
•Port x : Output to LED’s
Port x : DAC output on pins 0 & 1
Port D : UART on pins 0 & 1
UART : 9600 baud, 8-N-1
Display opening marquee
Start counter at 0
Port x = counter (to DAC)
Port x = ~ counter (to LED’s)
Increment counter so it runs: 1, 2, 3, 4, 1, 2 …
Delay 2.5 seconds for voltage measurement
8
Lab 4: D/A Converter
• Initialization code for the ADC:
• // ADC initialization
•
// ADC Clock frequency: 115.000 kHz
•
// ADC Voltage Reference: AREF pin
•
// ADC High Speed Mode: Off
•
// ADC Auto Trigger Source: None
9
Lab 4: D/A Converter
•
•
•
•
•
•
•
•
•
•
•
•
Code for reading an ADC channel:
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX = adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA |= 0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
10
Lab 4: D/C Converter
• Suppose PB1 and PB0 are used as bit 1
and bit 0. They need to be set up as
outputs and initially cleared.
• Initialization code for PORT B:
• // Port B initialization
• // Use b1 and b0 as the output pins to
drive the resistor network.
• PORTB=0x00; // Clear output.
• DDRB=0x03; // Set up bit1-0 as outputs.
11
Lab 4: D/A Converter
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
UART Design
Set up the UART port to display the voltage at each step of the digital output.
Initialization code for the UART:
// Use UART for initial debug and later to output the count after
// the count has been stopped.
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRL=0x17;
UBRRH=0x00;
12
Lab 4: D/A Converter
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
while (1)
{
// Loop from b00, b01, b10, b11.
for( i = 0; i < 4; i++ )
{
// Change to the next D to A step.
PORTB = i;
// Delay for 2 seconds
delay_ms( 2000 );
// Print the voltage to the screen.
value = read_adc( 0x0 );
voltage = value * 5 / 1023;
??????
??????
13
Lab 4: D/A Converter
• Terminal need to display:
– Binary Output (first column)
– ADC Reading (second column)
– Voltmeter Reading (third column)
00
0.00 Volts
0.0012 Volts
01
1.20 Volts
1.194 Volts
10
2.75 Volts
2.710 Volts
11
3.96 Volts
3.906 Volts
14