Top Banner
1 Lab2: A/D Converter
21

1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

Dec 17, 2015

Download

Documents

Kristina Craig
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

1

Lab2: A/D Converter

Page 2: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

2

Lab2: A/D Converter

• This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital value and display the percent and raw value of the voltage, VA as shown in Table 1.

• The interface consists of some analog circuitry as well as wiring the analog circuit to the STK500 and running corresponding software on the AVR mcu. Each circuit requires a unique C-routine.

Page 3: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

3

We use ATmega16 having the same pin layout

Atmeg16

Page 4: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

4

Pin 40 = PA0 = ADC0AD converter Channel 0

Pin 32 = AREF(= VTG on STK500 board)

Pin 31 = GND = AVR board groundUse the AVR board ground

Page 5: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

5

Page 6: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

6

Pin layout is different from Atmeg16

Atmega8515 is at SCKT3000D3

Port AalsoservesasAD inputs

Page 7: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

7

Lab2: A/D Converter

• Pre-Lab Assignments

• Read the ATmega16/ATmega8515 on-line data book available in the course website, pages 202 through 220.

• Read B, C, and O (course text), chapter 2.

• Read the AVR STK500 User Guide, page 3-11 and pages 3-16 through 3-18.

• Answer Pre-Lab Questions

Page 8: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

8

1.What is the range of an n-bit A/D converter?

2. Our ATmeg16/ATmega8515 has an 10-bit A/D converter. What is the resolution if the full-scale input range is 5V?

3. Briefly describe the two operating modes of the ADC in the ATmeg16/ATmega8515.

Pre_lab questions

4. What are the ADC noise canceling techniques?

5. What two registers are used by the ADC and what are their purposes?

ADCSRA ADMUX ADCW

Page 9: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

9

ADCSRA bit definitions (Textbook: page 143)

Bit 7 6 5 4 3 2 1 0

Bit7: ADC Enable bitBit6: ADC start conversion bitBit5: ADC free running bitBit0-Bit2: ADC prescaler select bit

ADCSRA=0xE9; /*enable ADC, free run, started, clock prescaler of 64 */ADCSRA = 11101110

Page 10: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

10

There are two operating modes of ADC:

1.In the interrupt-driven, (single-conversion) mode, only one conversion occurs after setting the start conversion bit and the bit must be set again before another conversion.

2. In the free-running mode, successive conversions are restarted automatically once the first conversion completes.

Page 11: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

11

Lab2 software requirements

• The ADC program can use a 100 ms clock to trigger the ISR. The ISR set a flag and the actual work of reading the ADC and running the math can be done in main().

• Every 5 times through the ISR – i.e. every 500 ms – the ISR blinked an LED. This is done to provide a heartbeat signal. If the rest of the program is not working but the LED is blinking, we know that the P is OK and that the software is not hung. This was very useful in the troubleshooting phase.

• Demo and produce Table 1

Page 12: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

12

Voltage Measured Value at Point VA

Display Voltage Digital Value Percentage of Full Scale

0.00 V 0.00 V 0x000 0%

0.50 V 0.50 V 0x068 10%

-- -- -- --

-- -- -- --

-- -- -- --

-- -- -- --

-- -- -- --

-- -- -- --

-- -- -- --

-- -- -- --

4.72 V 4.79 V 0x3D6 95%

Table 1 Voltage and Percentage of Full Scale

Page 13: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

13

Measured Voltage (V)

Displayed Voltage (V)

Digital Value (Decimal)

Percent of Full Scale (%)

0.000042 0 0 00.25 0.244 50 4

0.503 0.503 103 100.755 0.757 155 15

1 1.006 206 201.245 1.256 257 251.495 1.505 308 301.755 1.769 362 352.033 2.052 420 412.231 2.253 461 452.556 2.585 529 51

2.75 2.781 569 553.013 3.044 623 603.257 3.294 674 653.507 3.548 726 703.756 3.797 777 754.042 4.086 836 81

4.26 4.305 881 864.535 4.584 938 914.742 4.794 981 954.954 5 1023 100

Table 1. ADC Measurements for Different Potentiometer Settings

Page 14: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

14

Lab2 software requirement

For noise elimination you can make your program to measure the ADC channel 4 times, then throws out the highest and the lowest and takes an average of the remaining 2 data points.

This makes sure that outlying data points (flyers) do not affect the final result.

The main issue is an occasional data point that is obviously very far out of the normal band. The technique used for noise elimination can eliminate those data points. The resulting output was quite stable. Output data was solidly +/- 1 unit (5 mV) with an occasional jump to +/- 2 units.

Page 15: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

15

main()

Initialize peripherals in Atmeg16 P:

Port B : output, initialized to all 1’s = LED’s off.

Timer 1 : 278 s period, interrupt on, reload for 100 ms period.ADC: turn ADC on, enable the interrupt and set ADC clock to 115 kHz

Wait until the ADC flag is set by the ISR.

Clear the ADC flag.

Initialize loop variables and select channel 1.

Run ADC measure loop 4 times

………… more work.

Page 16: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

16

Timer 1, ISR()

Reload counter for 100 ms.

Increment the 1-5 counter.

If the 1-5 counter overflows, blink the heartbeat LED.

Set the ADC flag. ……..

ADC, ISR()

Set the ADC done flag.

Page 17: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

17

#include <mega16.h>// Standard Input/Output functions#include <stdio.h>

char adcFlag = 0;char ledFlag = 0;char adcDone = 0;char count = 0;unsigned char led1 = 0xFF;unsigned char led2 = 0xFE;

….

Page 18: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

// Timer 1 overflow interrupt service routineinterrupt [TIM1_OVF] void timer1_ovf_isr(void){// Reinitialize Timer 1 valueTCNT1H=0xFE;TCNT1L=0x97;// Place your code here

adcFlag = 1;

// Increment the count// Every 5 times through (500 ms) blink the LED (heartbeat LED)

…………..

Page 19: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

interrupt [ADC_INT] void adc_isr(void){

adcDone = 1;}

void main(void)// Main Control Loopwhile (1){

// When ADC flag is set by interrupt, // do conversion and show results // Wait for ISR to set flag

// Wait until conversion is donewhile(adcDone == 0){ }

// Get and process datavolts = ADCW;//printf(" v = %d", volts);if(volts < loV){ loV = volts; }if(volts > hiV){ hiV = volts; }sumV += volts;

Page 20: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

20

Another approach 1.write the read_adc function2.main calls the read_adc function

Page 21: 1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.

21

while (1) { // Place your code here delay_ms(900); // delay 900 ms; ADC_sum=0; // initialize ADC_sum; for (i=0;i < 10; i++){ // start to sum 10 ADC samples; delay_ms(10); // take ADC sample every 10 ms; ADC_sum += read_adc(0); // read ADC output from PA0 and add to ADC_sum; } ADC_output = ADC_sum/10; // averaged ADC output; ADC_percent = ADC_output*25/256; // calculate percentage of ADC full range for current ADC output; printf("ADC Output = %4d ADC Percentage = %3d%%\r", ADC_output,

ADC_percent); // Display ADC output to terminal; if (ADC_output > 0x0332) // threshold detection at 4.0V; PORTB = 0xFE; //turn on LED0 when ADC output > 4.0V; else PORTB = 0xFF; };

This is another example of reading ADC output.