ece 4510/5530 microcontroller applications week 9bazuinb/ece4510/week9_2.pdf · week 9 dr. bradley...

29
ECE 4510/5530 Microcontroller Applications Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences

Upload: others

Post on 16-Oct-2020

2 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

ECE 4510/5530Microcontroller Applications

Week 9

Dr. Bradley J. BazuinAssociate Professor

Department of Electrical and Computer EngineeringCollege of Engineering and Applied Sciences

Page 2: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

ECE 2510 2

Lab 7 & 8 Elements

• SPI

• DAC

• Analog-to-Digital Conveter

• Temperature Measurement– LM35DZ

Page 3: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Temperature Measurement with a LM35

ECE 4510/5530

3

Page 4: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

The LM35

Features– Calibrated directly in ° Celsius (Centigrade)– Linear + 10.0 mV/°C scale factor– 0.5°C accuracy guaranteeable (at +25°C)– Rated for full −55° to +150°C range– Suitable for remote applications– Low cost due to wafer-level trimming– Operates from 4 to 30 volts– Less than 60 μA current drain– Low self-heating, 0.08°C in still air– Nonlinearity only ±1⁄4°C typical– Low impedance output, 0.1 W for 1 mA load

ECE 4510/5530

4

From Texas Instruments data sheet SNIS159B

Page 5: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Three Terminal Device

CAPACITIVE LOADS• Like most micropower circuits, the LM35 has a limited ability to drive heavy capacitive

loads. The LM35 by itself is able to drive 50 pf without special precautions. If heavier loads are anticipated, it is easy to isolate or decouple the load with a resistor; see Figure 3. Or you can improve the tolerance of capacitance with a series R-C damper from output to ground; see Figure 4.

ECE 4510/5530

5From Texas Instruments data sheet SNIS159B

Page 6: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Analog to Digital Converter:AD7705

ECE 4510/5530

6

Page 7: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Types of ADC

• Parallel or Flash– Fast, parallel converter

• Slope and Double Slope– Charge and discharge a CAP with reference and unknown voltage.

• Successive Approximation (SAR)– Check above or below ½ ref, then ¼ ref, then 1/8 ref etc.– Form ADC value one bit at a time MSB to LSB

• Sigma-Delta (Σ-Δ)– Uses lower bit precision, high sample rates and DSP multirate

signal processing techniques to provide high accuracy, low sample rate outputs. Lower cost.

ECE 4510/5530

7

Page 8: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Analog DevicesThe Data Conversion Handbook

• The handbook can be found at:– http://www.analog.com/library/analogDialogue/archives/39-

06/data_conversion_handbook.html

• Chapter 3: Data Converter Architectures– http://www.analog.com/library/analogDialogue/archives/39-

06/Chapter%203%20Data%20Converter%20Architectures%20F.pdf– Figure 3-48, p. 48 Flash Converter– Figure 3-53, p. 53 Flash Converter– Figure 3-56, p. 56 SAR– Figure 3-113, p. 98 Dual-Slope– Figure 3-119, p. 112 Sigma-Delta– Figure 3-125, p. 118 Sigma-Delta with DSP

Page 9: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

The AD7705

FEATURES• AD7705: 2 fully differential input channel ADCs– 16 bits no missing codes– 0.003% nonlinearity

• Programmable gain front end: gains from 1 to 128• 3-wire serial interface– SPI®-, QSPI™-, MICROWIRE™-, and

DSP-compatible Schmitt-trigger input on SCLK

• Ability to buffer the analog input• 2.7 V to 3.3 V or 4.75 V to 5.25 V operation• Power dissipation 1 mW maximum @ 3 V• Standby current 8 μA maximum• 16-lead PDIP, 16-lead SOIC, and 16-lead TSSOP packages

ECE 4510/5530

9

Page 10: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Basic Connections

Lab 8 Configuration– 5 V Supply (bypass Caps)– 2.5 V reference TLE2425– 4-wire SPI (include CSn)– AIN1+ and AIN1-

differential input– External Clock – 1 MHz

(PWM or ECT)400kHz to 2.5 MHz

– DRDYn Data Ready Poll/Interrupt Line

ECE 4510/5530

10

CSn

CLK

Page 11: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

General Operation

• Configuration and calibration• Start Conversion

– Note that there are “periodicity concerns”

• Wait for Data ReaDY– Either polling the DRDYn line– Possible interrupt when DRDYn

• Read Value

• Options:– Periodic continuous conversion Mode

ECE 4510/5530

11

Page 12: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Performance Based on Clock Rate

• Assume 1 MHz MCLK IN provided by HC12– Bit precision based on “Data Rate”. Slower sampling is more

precise. (16 bits for <=25 Hz, 10 bits at 200 Hz)– Fine for steady state signal monitoring or taking snap-shot samples

of sensors. (temperature, pressure, some position measurements, etc.)

ECE 4510/5530

12

Page 13: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

ADC SPI Interface

• Much more complicated than the DAC …

• 1st 8-bits – All communication to the AD7705/AD7706 must start with a write

operation to the communication register. After a poweron or reset, the device expects a write to its communication register. The data written to this register determines whether the next operation is a read or write operation and to which register this operation occurs.

• Subsequent 8-bits– Communicate with register defined in 1st 8-bits.

ECE 4510/5530

13

Page 14: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

ADC Registers

• Setup – set operating mode. – Normal or Calibration (3 types), Channel gain setting (x1), bipolar,

buffer off, etc.

• Clock – define clock frequency and update/output rates– CLK = 0, FS1:0=01 is 25 Hz with a 6.55 Hz -3dB filter cutoff

• Data – read the ADC conversion result

ECE 4510/5530

14

Page 15: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Power-Up and Calibration

• Upon power-up, the AD7705/AD7706 internally reset, setting the contents of the internal registers to a known state. Default values are loaded to all registers after a power-on or reset. The default values contain nominal calibration coefficients for the calibration registers. However, to ensure correct calibration for the devices, a calibration routine should be performed after power-up.

• The power dissipation and temperature drift of the AD7705/AD7706 are low, and no warm-up time is required before the initial calibration is performed. However, if an external reference is used, it must be stabilized before calibration is initiated. Similarly, if the clock source for the part is generated from a crystal or resonator across the MCLK pins, the start-up time for the oscillator circuit should elapse before a calibration is initiated on the parts (see Figure 11).

ECE 4510/5530

15

Page 16: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Calibration

• The AD7705/AD7706 provide a number of calibration options that can be programmed via the MD1 and MD0 bits of the setup register.

– A calibration cycle can be initiated at any time by writing to these bits of the setup register. Calibration on the AD7705/AD7706 removes offset and gain errors from the devices.

– A calibration routine should be initiated on these devices whenever there is a change in the ambient operating temperature or supply voltage. It should also be initiated if there is a change in the selected gain, filter notch, or bipolar/unipolar input range.

• Self-Calibration (runs both zero and full-scale cal)– A self-calibration is initiated on the AD7705/AD7706 by writing the appropriate

values (0, 1) to the MD1 and MD0 bits of the setup register. In self-calibration mode with a unipolar input range, the zero-scale point used to determine the calibration coefficients is with the inputs of the differential pair internally shorted on the part (i.e., AIN(+) = AIN(−) = internal bias voltage on the AD7705). The PGA is set for the selected gain for this zero-scale calibration conversion, as per the G1 and G0 bits in the communication register. The full-scale calibration conversion is performed at the selected gain on an internally generated voltage of VREF/selected gain.

ECE 4510/5530

16

Page 17: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

SPI Cycles

ECE 4510/5530

17

Page 18: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Configuration Flow Chart

ECE 4510/5530

18

Page 19: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Data Sheet Code#include <io6811.h>#define NUM_SAMPLES 1000 /* change the number of data samples */#define MAX_REG_LENGTH 2 /* this says that the max length of a register is 2 bytes */Writetoreg (int);Read (int,char);char *datapointer = store;char store[NUM_SAMPLES*MAX_REG_LENGTH + 30];void main(){/* the only pin programmed from the 68HC11 is the /CS and this is why the PC2 bit of PORTC is made as an output */char a;DDRC = 0x04; /* PC2 is an output the rest of the port bits are inputs */PORTC | = 0x04; /* make the /CS line high */Writetoreg(0x20); /* Active Channel is Ain1(+)/Ain1(−), next operation as write to the clock register */Writetoreg(0x0C); /* master clock enabled, 4.9512MHz Clock, set output rate to 50Hz*/Writetoreg(0x10); /* Active Channel is Ain1(+)/Ain1(−), next operation as write to the setup register */Writetoreg(0x40); /* gain = 1, bipolar mode, buffer off, clear FSYNC and perform a Self Calibration*/while(PORTC & 0x10); /* wait for /DRDY to go low */for(a=0;a<NUM_SAMPLES;a++); {

Writetoreg(0x38); /*set the next operation for 16 bit read from the data register */Read(NUM_SAMPES,2);

}}ECE

4510/553019

Page 20: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Writetoreg

Writetoreg(int byteword);{int q;SPCR = 0x3f;SPCR = 0X7f; // this sets the WiredOR mode(DWOM=1), Master mode(MSTR=1), SCK idles high(CPOL=1), /SS can be

//low always (CPHA=1), lowest clock speed(slowest speed which is master clock /32 */DDRD = 0x18; /* SCK, MOSI outputs */q = SPSR;q = SPDR; /* the read of the status register and of the data register is needed to clear the interrupt which tells the user that the data transfer is complete */

PORTC &= 0xfb; /* /CS is low */SPDR = byteword; /* put the byte into data register */while(!(SPSR & 0x80)); /* wait for /DRDY to go low */PORTC |= 0x4; /* /CS high */}

ECE 4510/5530

20

Port/Peripheral initialization code

Page 21: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Read

Read(int amount, int reglength){int q;SPCR = 0x3f;SPCR = 0x7f; /* clear the interrupt */DDRD = 0x10; /* MOSI output, MISO input, SCK output */while(PORTC & 0x10); /* wait for /DRDY to go low */PORTC & 0xfb ; /* /CS is low */for(b=0;b<reglength;b++){

SPDR = 0;while(!(SPSR & 0x80)); /* wait until port ready before reading */*datapointer++=SPDR; /* read SPDR into store array via datapointer */

}PORTC|=4; /* /CS is high */}

ECE 4510/5530

21

Port/Peripheral initialization code

Page 22: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

Why Dr. Bazuin doesn’t like it.

• Each 8-bit transfer is control by it’s own CS.I prefer that the “communications” is controlled by the CS.– CS low– transmit(0x20);– transmit(0x0C);– CS high

• The initialization of the port should be done as one init_ADCSPI routine

• At startup, a calibration sequence should be run– It could be part of initialization.

• Initialization Sequence: – Clock write, Setup write, Self-calibration, Place in standby

ECE 4510/5530

22

Page 23: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705 Initialization

• Write clock register• Write setup register with proper gain• Write setup register to self-cal CH0• Write setup register to self-cal CH1, if needed• Place the device in standby until needed

ECE 4510/5530

23

Page 24: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705.c elements

#define adc_select PTX &= ~ADC_CSn;#define adc_deselect PTX |= ADC_CSn;

void adc_reset() //reset the ADC{// Software Reset

adc_select;adcspi_transmit(0xFF); adcspi_transmit(0xFF); adcspi_transmit(0xFF); adcspi_transmit(0xFF); adc_deselect;

}

ECE 4510/5530

24

Page 25: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705.c elementsvoid adc_init() //reset the ADC{// Initialize clock

adc_select;adcspi_transmit(CLOCK_REG); // Select clock register for write, device enabled, register pair 0 adcspi_transmit(FS25); // CLKDIV=0, CLK is 1 MHz (0), 25 Hz (40 msec) adc_deselect;

// Initialize setupadc_select;adcspi_transmit(SETUP_REG); // Select setup register adcspi_transmit(0x00); // Mode=Normal, Gain = 1 (000), Bipolar (0), FSYNC=0adc_deselect; while(PORTC & 0x10); /* wait for /DRDY to go low */

// Run Self-Caladc_select;adcspi_transmit(SETUP_REG); // Select setup register adcspi_transmit(0x40); // Mode=Self Cal adc_deselect; while(PORTC & 0x10); /* wait for /DRDY to go low */

// Place in Standbyadc_select;adcspi_transmit(STANDBY); // Select comm register for write, standby, register pair 0 adc_deselect;

} 25

Page 26: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705.c elementsint adc_convert 1() //reset the ADC{

unsigned int temp0, temp1, ADC_value;// Start ADC

adc_select;adcspi_transmit(COMM_REG); // Select comm register for write, device enabled, register pair 0 adc_deselect; while(PORTC & 0x10); /* wait for /DRDY to go low */

// Read Dataadc_select;adcspi_transmit( DATA_REG | READ_REG); // Select data reg. for read temp1 = adcspi_exchange(0x00); temp0 = adcspi_exchange(0x00); adc_deselect;

ADC_value = (temp1<<8) | temp0;return(ADC_value);

// Place in Standbyadc_select;adcspi_transmit(STANDBY); // Select comm register for write, standby, register pair 0 adc_deselect;

}26

Page 27: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705.h elements

#define adc_select PTX &= ~ADC_CSn;#define adc_deselect PTX |= ADC_CSn;

#define COMM_REG 0x00;#define SETUP_REG 0x10;#define CLOCK_REG 0x20;#define DATA_REG 0x30;#define CAL_REG 0x60;#define GAIN_REG 0x70;

#define READ_REG 0x08;#define WRITE_REG 0x00;

#define STANDBY 0x04;

#define CH0 0x00;#define CH1 0x01;#define CHGNDREF 0x02;#define CHGNDS 0x03;

• Compose a register read/write request by oring the fields together

void adc_setup_norm(void) //setupthe ADC{

adc_select;adcspi_transmit(SETUP_REG|WRITE_REG|CH0);adcspi_transmit(GAIN0); adc_deselect;

}

ECE 4510/5530

27

Page 28: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705.h elements

#define adc_select PTX &= ~ADC_CSn;#define adc_deselect PTX |= ADC_CSn;

#define MD_NORM 0x00;#define MD_SCAL 0x40;#define MD_ZCAL 0x80;#define MD_FCAL 0xC0;

#define GAIN1 0x00;#define GAIN2 0x08;#define GAIN4 0x10;#define GAIN8 0x18;#define GAIN16 0x20;#define GAIN32 0x28;#define GAIN64 0x30;#define GAIN128 0x30;

#define BIPOLAR 0x00;#define UNIPOLAR 0x04;

#define BUFFON 0x02;#define BUFFOFF 0x00;#define FSYNC 0x01;

• Compose a register read/write request by oring the fields together

void adc_setup_selfcal(void) //self cal the ADC{

adc_select;adcspi_transmit(SETUP_REG|WRITE_REG|CH0)adcspi_transmit(MD_SCAL |GAIN0|BUFFON); adc_deselect;

}

• You may want to wait for ready and then repeat for CH1

ECE 4510/5530

28

Page 29: ECE 4510/5530 Microcontroller Applications Week 9bazuinb/ECE4510/Week9_2.pdf · Week 9 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering

AD7705.h elements

#define adc_select PTX &= ~ADC_CSn;#define adc_deselect PTX |= ADC_CSn;

#define CLKDISABLE 0x10;#define CLKDIV 0x08;#define CLK2.4MHZ 0x04;

#define FS20 0x00;#define FS25 0x01;#define FS100 0x02;#define FS200 0x03;#define FS50 0x00;#define FS60 0x01;#define FS250 0x02;#define FS500 0x03;

• Compose a register read/write request by oring the fields together

void adc_set_clock (void) //clock definition for ADC{

adc_select;adcspi_transmit(CLOCK_REG|WRITE_REG); adcspi_transmit(FS25); adc_deselect;

}

ECE 4510/5530

29