Presentation is loading. Please wait.

Presentation is loading. Please wait.

H. Huang Transparency No.11-1 The 68HC11 Microcontroller Chapter 11: 68HC11 Analog to Digital Converter The 68HC11 Microcontroller Han-Way Huang Minnesota.

Similar presentations


Presentation on theme: "H. Huang Transparency No.11-1 The 68HC11 Microcontroller Chapter 11: 68HC11 Analog to Digital Converter The 68HC11 Microcontroller Han-Way Huang Minnesota."— Presentation transcript:

1 H. Huang Transparency No.11-1 The 68HC11 Microcontroller Chapter 11: 68HC11 Analog to Digital Converter The 68HC11 Microcontroller Han-Way Huang Minnesota State University, Mankato

2 H. Huang Transparency No.11-2 The 68HC11 Microcontroller Basics on A/D Conversion -Almost any measurable quantity, for example, current, voltage, temperature, speed, and time, is analog in nature. -Analog signals must be represented in digital format in order to be processed by the digital computer. -An analog to digital (A/D) converter can convert a electrical voltage to a digital value. -A non-electrical quantity must be converted into electrical voltage before it can be converted into digital value. -A transducer is normally used to convert a non-electrical quantity into a electrical voltage so that it can be further processed by a computer. -The accuracy of an A/D converter is dictated by the number of bits it used to represent the digital value. -An A/D conversion system is illustrated in Figure 10.1

3 H. Huang Transparency No.11-3 The 68HC11 Microcontroller

4 H. Huang Transparency No.11-4 The 68HC11 Microcontroller Successive Approximation Method

5 H. Huang Transparency No.11-5 The 68HC11 Microcontroller Algorithm of Successive Approximation Method Starting the most significant bit of SAR, for each bit 1. Guess the bit to be a 1. 2. Converts the value of the SAR to an analog voltage 3. Compares the D/A output with the analog input. 4.Clears the bit to 0 if the D/A output is larger.

6 H. Huang Transparency No.11-6 The 68HC11 Microcontroller Optimal Voltage Range for the A/D Converter - A/D converter needs a low reference voltage and a high reference voltage to operate. - The low reference voltage (V LREF ) is often set to 0 and the high reference voltage (V HREF ) is often set to V CC. - Most A/D converters are ratiometric. - To take advantage the whole dynamic range of the A/D converter, we should set scale and shift the sensor output to V LREF ~ V HREF. - The A/D conversion result x corresponds to an analog voltage given by Vx = V LREF + (range  x)  (2 n – 1) where, range = V HREF – V HREF

7 H. Huang Transparency No.11-7 The 68HC11 Microcontroller Example 11.1 Assume there is a 12-bit A/D converter with V LREF = 0V and V HREF = 5V. Find out the corresponding voltage values for A/D conversion results of 100, 400, 800, 1200, and 2400. Solution: range = V HREF – V LREF = 5 V. V(100) = 0V + (100  5)  (2 12 – 1) = 0.12 V V(400) = 0V + (400  5)  (2 12 – 1) = 0.49 V V(800) = 0V + (800  5)  (2 12 – 1) = 0.98 V V(1200) = 0V + (1200  5)  (2 12 – 1) = 1.46 V V(2400) = 0V + (2400  5)  (2 12 – 1) = 2.93 V

8 H. Huang Transparency No.11-8 The 68HC11 Microcontroller Voltage Scaling Circuit Example 11.2 Convert the output voltage of an A/D converter from 0-200mV to the range of 0-5V. Solution: A V = 1 + (R2/R1) = (5V/0.2V) = 25  R2/R1 = 24 Choose 10K for R1, then R2 = 240K.

9 H. Huang Transparency No.11-9 The 68HC11 Microcontroller Voltage Scaling and Shifting Circuit Can translate a voltage from V 1 - V 2 to the range of 0V – 5V.

10 H. Huang Transparency No.11-10 The 68HC11 Microcontroller Example 11.3 Use the circuit in Figure 11.5 to scale and shift the transducer output from -2.5 ~ 2.5 V to 0 ~ 5V. Solution: 0 = R f  R 1 × (- 2.5) – R f  R 2 × V 1 ---- (1) 5 = R f  R 1 × (2.5) – R f  R 2 × V 1 ---- (2) By choosing V 1 = - 12V and R f = 5K , R 1 and R 2 are solved to be 5K  and 24K . R 0 is independent of other parameters, we arbitrarily set it to 5 K .

11 H. Huang Transparency No.11-11 The 68HC11 Microcontroller An Overview of the 68HC11 A/D Converter -Eight-channel, 8-bit, multiplexed input, successive-approximation conversion method. -A weighted array of capacitors are used to implement the successive-approximation method. -A clock signal is required to control the A/D conversion that must have a frequency no lower than 750 KHz. -Reference voltages are required for the conversion: one is high reference (V RH ) voltage, the other is low reference (V RL ) voltage. The difference between V RH and V RL cannot be lower than 2.5 V. -Accuracy is only guaranteed for V RL = 0 V and V RH = 5 V. -The conversion is ratiometric. The input voltage V RL converts to $00 and the input voltage V RH converts to $FF.

12 H. Huang Transparency No.11-12 The 68HC11 Microcontroller The Clock Frequency Issue -The A/D converter requires a clock to operate. -Either the E clock or the on-chip RC clock signal can be used. -The RC clock runs at 1.5 MHz. -To choose the E clock signal, clear the bit 6 of the OPTION register to 0. -To select RC clock signal, set the bit 6 of the OPTION register to 1. This circuit requires 10 ms to start and settle. -The 68HC11 completes the conversion of one sample in 32 clock cycles. Registers Related to the A/D Operation -ADCTL:A/D control/status register -OPTION: bits 7 and 6 -ADR1-4:A/D result registers 1 to 4

13 H. Huang Transparency No.11-13 The 68HC11 Microcontroller A/D Control Register (ADCTL)

14 H. Huang Transparency No.11-14 The 68HC11 Microcontroller

15 H. Huang Transparency No.11-15 The 68HC11 Microcontroller The OPTION Register ADPU:A/D power up. When set to 1, it enables the A/D converter. After setting this bit, the user must wait at least 100  s before using the A/D converter. CSEL:clock select. When set to 1, the RC clock signal is selected. Otherwise, the E clock is selected. It takes 10 ms for RC clock to stabilize.

16 H. Huang Transparency No.11-16 The 68HC11 Microcontroller The Procedure for Using the A/D Converter Step 1. Connect the hardware properly. Scale and shift the analog inputs, when necessary, so that they fall between V RH and V RL. Step 2.Set the ADPU bit of OPTION register to enable the A/D converter. Step 3.Select the appropriate clock signal by setting or clearing the CSEL bit of the OPTION register. Step 4.Wait for the A/D converter to stabilize. Step 5.Select the appropriate channel(s) and operation modes by programming the ADCTL register. Step 6.Wait until the CCF flag of the ADCTL register becomes 1 and collect the conversion results.

17 H. Huang Transparency No.11-17 The 68HC11 Microcontroller Example 11.5 Write an instruction sequence to set up the following A/D conversion parameters: Nonscan mode Single-channel mode Select channel AN0 Choose the E clock as the clock source for the A/D converter Enable A/D converter Solution: Set bit 5 of ADCTL to 0 to select nonscan mode. Set bit 4 of ADCTL to 0 to select single-channel mode. Set bits 3-0 of ADCTL to 0000 to select channel AN0. Write the value $00 into the ADCTL register. Set the bit 7 of the OPTION register to enable A/D charge pump. Clear the bit 6 of the OPTION register to select E clock as the A/D control clock signal. Wait for 100  s for the converter to stabilize.

18 H. Huang Transparency No.11-18 The 68HC11 Microcontroller regbasEQU $1000 ADCTLEQU $30 OPTIONEQU $39 LDX #regbas BCLR OPTION,X $40 ; select E clock for the A/D clock signal BSETOPTION,X $80 ; enable the A/D charge pump LDY#30; delay for 105  s for delayDEY; the charge pump to BNEdelay; stabilize LDAB#$00 STAAADCTL,X ; start the conversion END

19 H. Huang Transparency No.11-19 The 68HC11 Microcontroller Example 11.6 Write an instruction sequence to set up the following A/D conversion parameters: Non-scan mode Multiple-channel mode Select channels AN4-AN7 Choose E clock as the clock source for the A/D converter Enable the A/D converter Solution: Set bit 5 of ADCTL to 0 to select non-scan mode Set bit 4 of ADCTL to 1 to select multi-channel mode Set bits 3-0 of ADCTL to 0100 to select channels AN4-AN7 Write the value $14 into the ADCTL register. Set the bit 7 of the OPTION register to enable A/D charge pump. Clear the bit 6 of the OPTION register to select E clock as the A/D control clock signal. Wait for 100  s for the converter to stabilize.

20 H. Huang Transparency No.11-20 The 68HC11 Microcontroller regbasEQU$1000 ADCTLEQU$30 OPTIONEQU$39 LDX#regbas BCLROPTION,X $40; select the E clock as the A/D control clock BSETOPTION,X $80; enable the A/D converter LDY#30; wait for 105  s delayDEY;“ BNEdelay;” LDAB#$14; start the A/D conversion STAAADCTL,X;“ END

21 H. Huang Transparency No.11-21 The 68HC11 Microcontroller Example 11.7 Write an instruction sequence to convert the analog signal connected to channel AN0 into digital form. Perform four conversions and stop. Assume the frequency of the E clock is 2 MHz. Solution: 1.Circuit connection is shown in Figure 11.6. 2.Write one byte into the ADCTL register and four conversions will be performed. 3.Perform A/D conversion on the AN0 input in single-channel and nonscan mode. Write $00 into ADCTL.

22 H. Huang Transparency No.11-22 The 68HC11 Microcontroller regbasequ$1000 OPTIONequ$39 ADCTLequ$30 ADR1equ$31 ADR2equ$32 ADR3equ$33 ADR4equ$34 org$00 resultrmb4; result four bytes to hold A/D results org$C000 ldx#regbas bsetOPTION,X $80; enable the charge pump to start A/D conversion bclrOPTION,X $40; select E clock as the clock source to A/D converter ldy#30; delay for 105  s to wait for the charge pump delaydey; to stabilize bnedelay;“ ldaa#$00 staaADCTL,X; initialize ADCTL and start the A/D conversion againldaaADCTL,X; check CCF bit bplagain; wait until CCF flag is 1 ldaaADR1,X; get the first result

23 H. Huang Transparency No.11-23 The 68HC11 Microcontroller staaresult; save it ldaaADR2,X; get the second result staaresult+1; save it ldaaADR3,X; get the third result staaresult+2; save it ldaaADR4,X; get the fourth result staaresult+3; save it end

24 H. Huang Transparency No.11-24 The 68HC11 Microcontroller In C Language, #include unsigned char result [4]; main ( ) { OPTION &= 0xBF;/* select E clock as the A/D clock source */ OPTION |= 0x80;/* enable A/D converter */ TFLG1 = 0x40;/* clear OC2F flag */ TOC2 = TCNT + 200;/* start an OC2 operation with 100  s delay */ while (!(TFLG1 & 0x40));/* wait for 100  s */ ADCTL = 0x00;/* start an A/D conversion */ while (!(ADCTL & 0x80));/* wait until A/D conversion is complete */ result [0] = ADR1;/* save A/D conversion results */ result [1] = ADR2; result [2] = ADR3; result [3] = ADR4; return 0; }

25 H. Huang Transparency No.11-25 The 68HC11 Microcontroller Example 11.8 Take 20 samples from each of the A/D channels AN0 to AN3, convert them to digital values, and store them in memory locations from $D000 to $D04F. Solution: 1.Circuit connection is shown in Figure 11.7. 2. Perform A/D conversion on channels AN0 to AN3 in multi-channel, non-scan mode. Write the value $10 into ADCTL. 3.Take samples at as regular intervals as possible. This can be achieved by starting the next conversion immediately after the previous conversion has been completed but before collecting the result.

26 H. Huang Transparency No.11-26 The 68HC11 Microcontroller NEQU20; number of samples to be taken per channel ORG$D000 resultRMB80; reserve 80 bytes to store A/D conversion results ORG$C000 LDX#regbas BSETOPTION,X $80; enable A/D charge pump BCLROPTION,X $40; select E clock to control A/D conversion LDY#30; wait for 105  s for charge pump to stabilize delayDEY;“ BNEdelay;“ LDAA#$10; initialize ADCTL STAAADCTL,X;“ LDAB#N; number of samples remained to be taken on each channel LDY#result; Y points to the buffer that holds the result waitLDAAADCTL,X; wait until the current A/D conversion is completed BPLwait;“ * Start the next conversion immediately so that samples can be taken more uniformly in time LDAA#$10 ; start the next A/D conversion STAAADCTL,X;“

27 H. Huang Transparency No.11-27 The 68HC11 Microcontroller * The following eight instructions collect the previous results LDAAADR1,X; fetch the result from channel 1 STAA0,Y; save the result LDAAADR2,X; fetch the result from channel 2 STAA1,Y; save the result LDAAADR3,X; fetch the result from channel 3 STAA2,Y; save the result LDAAADR4,X; fetch the result from channel 4 STAA3,Y; save the result INY; move the result pointer INY; “ DECB; decrement the loop count BNEwait END

28 H. Huang Transparency No.11-28 The 68HC11 Microcontroller In C Language, #include unsigned char result[80]; main ( ) { int i; OPTION &= 0xBF;/* select E clock as the A/D conversion clock source */ OPTION |= 0x80;/* enable A/D converter */ TFLG1 = 0x40;/* clear OC2F flag */ TOC2 = TCNT + 200;/* create 100  s delay */ while (!(TFLG1 & 0x40));/*“*/ ADCTL = 0x10;/* start an A/D conversion */ for (i = 0; i < 20; i++) { while (!(ADCTL & 80)); ADCTL = 0x10; result [4*i] = ADR1; result [4*i + 1] = ADR2; result [4*i + 2] = ADR3; result [4*i + 3] = ADR4; } return 0; }

29 H. Huang Transparency No.11-29 The 68HC11 Microcontroller The LM35 Precision Centigrade Temperature Sensors Pins and circuit connections Features -No external calibration required. -Very linear over the temperature range -Draws only 60  A from power supply and has very low self-heating. -Converts the ambient temperature into voltage

30 H. Huang Transparency No.11-30 The 68HC11 Microcontroller Example 11.9 Use the circuit shown in Figure 11.8b as a building block in a system to measure room temperature. Display the result in two integer digits and one fractional digit using the seven-segment displays. Assume the room temperature never goes below 0 o C and never goes above 42.5 o C so that the A/D converter of the 68HC11 can be used to perform the conversion and drive the seven-segment displays. Solution: 1.The voltage output from the circuit shown in Figure 10.6b will be between 0V and 425 mV. 2.Better precision can be obtained by scaling the voltage corresponding to 42.5 o C to 5 V. 3.Voltage scaling circuit is shown is Figure 11.9.

31 H. Huang Transparency No.11-31 The 68HC11 Microcontroller Display Circuit

32 H. Huang Transparency No.11-32 The 68HC11 Microcontroller Setting of Configuration Data for MC14489 bit 7: no decode, set to 0. bit 6: special decode, set to 1. bit 5 to 3: bank 5 to 3 hex decode, set to 0. bit 2: bank 2 special decode, set to 1. bit 1: bank 1 hex decode, set to 0. bit 0: normal mode, set to 1. Format of Display Data

33 H. Huang Transparency No.11-33 The 68HC11 Microcontroller REGBASEQU$1000 SPCREQU$28 SPDREQU$2A SPSREQU$29 DDRDEQU$09 ADR1EQU$09 OPTIONEQU$39 ADCTLEQU$30 PORTBEQU$04 PORTDEQU$08 TCNTEQU$0E TOC2EQU$18 TFLG1EQU$23 OC2EQU$40; mask to check the OC2F flag of TFLG1 OC2MEQU$BF; mask to clear OC2F flag for the BCLR instruction SP_DIREQU$3A; value to configure SPI pins directions SPCR_INEQU$54; value to initialize SPCR register ADPUEQU$80; mask to select ADPU bit of the OPTION register ADCLKEQU$40; mask to select CSEL bit of the OPTION register A2D_INIEQU$00; value to configure A/D control register TCNTEQU$0E TOC2EQU$18 TFLG1EQU$23

34 H. Huang Transparency No.11-34 The 68HC11 Microcontroller ORG$00 byte1RMB1; storage for temperature data byte2RMB1;“ byte3RMB1;“ remainRMB2; to hold the remainder of division oc2cntRMB1; output comparison count of OC2 ORG$C000 LDX#regbas LDAA#$C0 STAAbyte1; store the fixed value of display data LDAA#$FC;“ STAAbyte3;“ * The following 4 instructions initialize the SPI system LDAA#SPDIR; set up SPI pin directions STAADDRD,X;“ LDAA#SPCR_INI; initialize SPI parameters STAASPCR,X;“ BCLRPORTD,X $20; enable SPI transfer to MC14489 LDAA#$45; send configuration data to MC14489 STAASPDR,X;“ BRCLRSPSR,X $80 * BSETPORTD,X $20; load data into configuration register

35 H. Huang Transparency No.11-35 The 68HC11 Microcontroller * The following 5 instructions enable the A/D converter and select E clock to control A/D * conversion process and wait for the charge pump to stabilize BCLROPTION,X ADCLK; select E clock for A/D conversion BSETOPTION,X ADPU; enable A/D converter LDY#30; delay 105 ms so charge pump can stabilize delayDEY;“ BNEdelay;“ foreverLDAA#A2D_INI; start an A/D conversion STAAADCTL,X ;“ hereLDAAADCTL,X BPLhere LDABADR1,X; read the A/D conversion result CLRA; convert to temperature LDX#6; reading IDIV; “ STDremain; save the remainder XGDX; swap integer part to D LDX#10; separate tens and ones digits IDIV LSLB; shift ones digits to upper half of B LSLB;“

36 H. Huang Transparency No.11-36 The 68HC11 Microcontroller STABbyte2 XGDX; swap the tens temperature digit in B ADDBbyte1; combine the decimal pointer specifier with tens digit STABbyte1;“ LDDremain; get back the remainder (in B) LDAA#10 MUL LDX#6 IDIV; compute remainder × 10  6 XGDX ADDBbyte2; combine the ones digit and the fractional digit STABbyte2;“ BCLRPORTD,X $20; enable SPI transfer to MC14489 LDAAbyte1; get byte1 and send it out STAASPDR,X BRCLRSPSR,X $80 *; wait until 8 bits have been shifted out LDAAbyte2; send out byte2 STAASPDR,X;“ BRCLRSPSR,X $80 *;“ LDAAbyte3; send out byte3 STAASPDR,X;“ BRCLRSPSR,X $80 *;“ BSETPORTD,X $20; load data into the display register of the MC14489

37 H. Huang Transparency No.11-37 The 68HC11 Microcontroller * Use OC2 to create 1 second delay LDAB#100 STABoc2cnt; initialize OC2 count to create 1 s delay LDX#REGBAS BCLRTFLG1,X $BF; clear OC2F flag LDDTCNT,X repeatADDD#20000 STDTOC2,X waitBRCLRTFLG1,X $40 *; wait for 10 ms BCLRTFLG1,X $BF; clear the OC2F flag LDDTOC2,X DECoc2cnt BNErepeat JMPforever END

38 H. Huang Transparency No.11-38 The 68HC11 Microcontroller C Program for Temperature Measurement #include main ( ) { unsigned char byte1, byte2, byte3, temp; byte1 = 0xC0; byte3 = 0xFC;/* store characters “degree” and “C” in byte3 */ DDRD = 0x3A;/* configure SPI pin directions */ SPCR = 0x56;/* initialize SPI function */ OPTION |= 0x80;/* start the A/D charge pump */ OPTION &= 0xBF;/* select E clock as the clock source for A/D conversion */ TFLG1 = 0x40;/* clear OC2F flag */ TOC2 = TCNT + 200/* wait for 100  s */ while (!(TFLG1 & 0x40)); PORTD &= 0xDF;/* enable SPI transfer */ SPDR = 0x45;/* send configuration data to MC14489 */ while (!(SPSR & 0x80)); PORTD |= 0x20;/* disable SPI transfer to MC14489 */ while (1) { ADCTL = 0x00/* start and A/D conversion */ while (!(ADTCL & 0x80));

39 H. Huang Transparency No.11-39 The 68HC11 Microcontroller temp = ADR1/6; byte1 += temp / 10;/* place the ten’s digit in the lower half of byte1 */ byte2 = 4 << (temp % 10);/* place the one’s digit in the upper half of byte2 */ temp = (ADR1 % 6) * 10 /6;/* compute the first fractional digit */ byte2 += temp;/* combine the one’s and fractional digits */ PORTD &= 0xDF;/* enable SPI transfer to MC14489 */ SPDR = byte1;/* send out byte1 */ while (!(SPSR & 0x80)); SPDR = byte2;/* send out byte2 */ while (!(SPSR & 0x80));/*“*/ SPDR = byte3;/* send out byte3 */ while (!(SPSR & 0x80)):/*“*/ PORTD |= 0x20;/* transfer data to the display register */ TOC2 = TCNT + 20000;/* start OC2 operation with 10 ms delay */ TFLG1 = 0x40;/* clear OC2 flag */ for (temp = 0; temp < 100; temp -- ) { while (!(SPSR & 0x80)); TOC2 += 20000;/* start the next OC2 operation */ TFLG1 = 0x40;/* clear the OC2 flag */ } return 0; }

40 H. Huang Transparency No.11-40 The 68HC11 Microcontroller Measuring the Humidity - The IH-3605 is a humidity sensor made by HyCal Engineering, a division of Honeywell. - The voltage output for relative humidity 0 to 100% is 0.8V to 3.9V. - Pin assignment is shown in Figure 11.12. - Specifications are shown in Table 11.2.

41 H. Huang Transparency No.11-41 The 68HC11 Microcontroller Example 11.10 Construct a humidity measurement system that consists of the 68HC11, an IH-3605 humidity sensor, and four 7-segment displays. Solution: 1. Use a signal conditioning circuit to scale and shift the voltage to 0-5V. 2. Use a 1K  resistor and a 0.16  F capacitor to construct a 1KHz low pass filter. 3. Use a MC14489 to drive four seven-segment displays. 4. Blank the most significant digit when the humidity is not 100%. - Send $91 as the configuration data to blank the most significant digit. - Send $01 as the configuration data when the most significant digit is a 1. 5. Use table lookup method to translate from A/D result to the relative humidity.

42 H. Huang Transparency No.11-42 The 68HC11 Microcontroller

43 H. Huang Transparency No.11-43 The 68HC11 Microcontroller Relative Humidity Data Format

44 H. Huang Transparency No.11-44 The 68HC11 Microcontroller REGBASEQU$1000 SPCREQU$28 SPSREQU$29 SPDREQU$2A DDRDEQU$09 ADR1EQU$31 OPTIONEQU$39 ADCTLEQU$30 PORTBEQU$04 PORTDEQU$08 TCNTEQU$0E TOC2EQU$18 TFLG1EQU$23 SP_DIREQU$3A SPCR_INIEQU$54 APDUEQU$80; mask to select ADPU bit of the OPTION register ADCLKEQU$40; mask to select CSEL bit of the OPTION register A2D_INIEQU$00; value to initialize the ADCTL register CONF1EQU$91; configuration data that need to blank msd CONF2EQU$01; configuration data that display the msd

45 H. Huang Transparency No.11-45 The 68HC11 Microcontroller ORG$00 byte1RMB1; storage for display data byte2RMB1;“ byte3RMB1;“ oc2cntRMB1 ORG$C000 LDX#REGBAS LDAA#$A0 STAAbyte1; first byte of the display data is a constant LDAA#SPDIR STAADDRD,X LDAA#SPCR_INI; initialize the SPI system STAASPCR,X;“ BSETOPTION,X ADPU ; start A/D charge pump BCLROPTION,X ADCLK ; select E clock as the clock source of the A/D LDY#30 delayDEY BNEdelay foreverLDAA#A2D_INI STAAADCTL,X hereLDAAADCTL,X; wait until A/D conversion is complete BPLhere

46 H. Huang Transparency No.11-46 The 68HC11 Microcontroller LDABADR1,X LDY#humid_up ABY LDAA0,Y; lookup the upper byte of the humidity STAAbyte2 LDY#humid_lo ABY LDAA0,Y; lookup the lower byte of the humidity STAAbyte3 JSRdisp_humid JSRwait_1s JMPforever disp_humidPSHA PSHB PSHY PSHX LDX#REGBAS BCLRPORTD,X $20; enable SPI transfer to MC14489 LDAAbyte2 ANDA#$F0; check the upper four bits BEQblank LDAA$01; choose normal hex decode BRAsend

47 H. Huang Transparency No.11-47 The 68HC11 Microcontroller blankLDAA#$91; choose special decode to blank display sendSTAASPDR,X BRCLRSPSR,X $80 *; wait until the SPI transfer is complete BSETPORTD,X $20; load data into configuration register BCLRPORTD,X $20; enable SPI transfer to MC14489 LDY#byte1 LDAB#3 loop_dispLDAA0,Y STAASPDR,X BRCLRSPSR,X $80 *; wait until SPI transfer is complete INY DECB BNEloop_disp BSETPORTD,X $20; load data into display register PULX PULY PULB PULA RTS wait_1sPSHA PSHB PSHY PSHX

48 H. Huang Transparency No.11-48 The 68HC11 Microcontroller LDX#REGBAS DES TSY LDAA#50 STAA0,Y; initialize OC2 count to 50 LDDTCNT,X loop_50ADDD#40000; start an OC2 operation with 20 ms delay STDTOC2,X BCLRTFLG1,X $BF ; clear OC2F BRCLRTFLG1,X $40 *; wait until OC2F is set LDDTOC2,X DEC0,Y; decrement OC2 count BNEloop_50 INS PULX PULY PULB PULA RTS humid_upFCB$00,$00,…,$09,$10 humid_loFCB$00,$04,…,$96,$00

49 H. Huang Transparency No.11-49 The 68HC11 Microcontroller C Program for Humidity Measurement #include unsigned char humid_up [ ] = {…}; unsigned char humid_lo [ ] = {…}; main ( ) { unsigned bytes[3];/* display data */ unsigned char i, conf_dat;/* loop index and configuration data */ DDRD = 0x3A; SPCR = 0x54; OPTION &= 0xBF;/* select E clock to control A/D converter */ OPTION |= 0x80;/* start the A/D charge pump */ bytes[0] = 0xA0; TOC2 = TCNT + 200;/* create a delay of 100 ms */ TFLG1 = 0x40;/*“*/ while (!(TFLG1 & 0x80)):/*“*/ while (1) { ADCTL = 0x00;/* start an A/D conversion */ while (!(ADCTL & 0x80)); /* wait until A/D conversion is complete */ if (!(humid_up [ADR1] & 0xF0)) conf_dat = 0x91;/* choose special decode mode */ elseconf_dat = 0x01;/* choose normal decode mode */

50 H. Huang Transparency No.11-50 The 68HC11 Microcontroller PORTD &= 0xDF;/* enable SPI transfer to MC14489 SPDR = conf_dat;/* send out configuration data */ while (!(SPSR & 0x80));/* wait until SPI transfer is complete */ PORTD |= 0x20;/* load data into configuration register */ bytes[1] = humid_up[ADR1]; bytes[2] = humid_lo[ADR1]; PORTD &= 0xDF; for (i = 0; i < 3; i++) {/* send out humidity data for display */ SPDR = bytes[i]; while (!(SPSR & 0x80)); } PORTD |= 0x20;/* load data into display register */ TFLG1 = 0x40;/* clear OC2F flag */ TOC2 = TCNT + 20000;/* start an OC2 operation */ for (i = 0; i < 100; i++) { while (!(TFLG1 & 0x40)); TFLG1 = 0x40;/* clear OC2F flag */ TOC2 += 20000; } return 0; }

51 H. Huang Transparency No.11-51 The 68HC11 Microcontroller Processing the Results of A/D Conversions -The results of A/D conversion often need to be processed. -One of the most useful measurements of an AC signal is its root-mean-square (RMS) value. -The RMS value of an AC signal is defined as: V RMS = T 1 0 T V 2 (t)dt 11.7 -The following equation is used to approximate the RMS value: V RMS  N 1  N-1 i = 0 Vi2Vi2 11.8 -To make equation 10.4 a good approximation of the real RMS value, samples must be as equally spaced in time as possible. The more samples collected, the better the result.

52 H. Huang Transparency No.11-52 The 68HC11 Microcontroller Example 11.11 Write a program to compute the average of the squared values of 64 samples stored at memory locations starting with the label sample and save the result at the memory locations starting with the label sq_ave. Solution: -Two bytes are needed to hold the square of an 8-bit value. -Three bytes are needed to hold the sum of 64 16-bit values. x -Divide-by-64 can be performed by shifting the dividend to the right by 6 positions.

53 H. Huang Transparency No.11-53 The 68HC11 Microcontroller ORG$D000 samplesFCB… sq_sumRMB3 sq_aveRMB2 org$C000 LDX#samples CLRsq_sum; initialize the sum to 0 CLRsq_sum+1;“ CLRsq_sum+2;“ LDY#64; initialize the loop count loopLDAA0,X; get a sample TAB; duplicate the sample in B MUL; compute the square of the sample ADDDsq_sum+1; add to the running sum STDsq_sum+1;“ LDABsq_sum; add the carry to the upper byte ADCB#0;“ STABsq_sum INX; move to the next sample DEY; decrement the loop count BNEloop LDAB#6; prepare to shift right 6 places

54 H. Huang Transparency No.11-54 The 68HC11 Microcontroller * the next five instructions divide the sum by 64 loop1LSRsq_sum RORsq_sum+1 RORsq_sum+2 DECB BNEloop1 * transfer the result to sq_sum and sq_sum+1 LDAAsq_sum+1 STAAsq_ave LDAAsq_sum+2 STAAsq_ave+1 END

55 H. Huang Transparency No.11-55 The 68HC11 Microcontroller Computing the Square Root -A technique for approximating the square root is based on the following equation:  i = n-1 i=0 2 n(n-1) 11.9 -Equation 10.5 can be transformed into n 2 =  n-1 i=0 (2i + 1) 11.10 -The value n is the square root to be found. Suppose we want to find the square root of p, and n is the integer closest to the square root of p, then one of the following relationships is satisfied: n 2 < p n 2 = p n 2 > p - By incrementing i from 0 to n-1, the square root can be found. For the first approximation, we will stop when the running sum is larger than p. The flowchart of the program is shown in Figure 11.15.

56 H. Huang Transparency No.11-56 The 68HC11 Microcontroller

57 H. Huang Transparency No.11-57 The 68HC11 Microcontroller Example 11.12 Write a program to compute the square root of the average value computed in Example 11.11 (stored at sq_ave) and save the square root at sq_root. Solution: sq_rootrmb2; to hold the value of  (2i + 1) irmb1; … CLRsq_root; initialize  (2i + 1) to 0 CLRsq_root+1;“ LDAB#-1 STABi; initialize i to 0 repeatINCi LDABi CLRA LSLD; compute 2i + 1 ADDD#1;“ ADDDsq_root; add 2i + 1 to accumulating STDsq_root; sum CPDsq_ave; compare to n 2 BLOrepeat; repeat when  (2i + 1) < n 2 INCi; add 1 to i to obtain the square root LDAAi; place the square root in sq_root STAAsq_root;“ END

58 H. Huang Transparency No.11-58 The 68HC11 Microcontroller Example 11.13 Add a sequence of instructions to the previous program to find the closest square root of the square sum. Solution: -If n is not the true square root, then either n or n - 1 is the closest square root. -The choice can be made by comparing the following two expressions: n 2 - p(1) p - (n - 1) 2 (2) -The value n is selected if the expression (1) is smaller. Otherwise, n - 1 is selected -The program on the right-hand side should be appended to the program of the previous example.

59 H. Huang Transparency No.11-59 The 68HC11 Microcontroller sq_diff1RMB2 tempRMB2 LDAAi; place n in A TAB; also place n in B MUL; compute n 2 CPDsq_ave; compare to p BEQexit * We need to compare expression (1) and (2) in the following SUBDsq_ave; compute n 2 - q STDsq_diff1 LDAAi DECA; compute n - 1 TAB MUL; compute (n - 1) 2 STDtemp LDDsq_ave SUBDtemp; compute p - (n - 1) 2 CPDsq_diff1; compare p - (n - 1) 2 with n 2 - p BHIexit; n is closer to the true square root DECi; n - 1 is closer to the true square root exit…

60 H. Huang Transparency No.11-60 The 68HC11 Microcontroller Example 11.14 Write a C routine to compute the root-mean-square value of an array of 8-bit unsigned integers. Solution: unsigned char root_mean_sq (unsigned char *samples, unsigned int n) { inti; unsigned intsq_ave; unsigned long intsq_sum; unsigned inttemp; sq_sum = 0; for (i = 0; i < n; i++) sq_sum += samples[i] * samples [i]; sq_ave = sq_sum / n; temp = 0; i = 0; while (temp < sq_ave) { temp += 2 * i + 1; i ++; } if (temp == sq_ave) return i; if ((i * i - sq_ave) < (sq_ave - (i - 1) * (i - 1)) return i; else return (i - 1); }

61 H. Huang Transparency No.11-61 The 68HC11 Microcontroller Using External A/D Converter MAX1241 -12-bit resolution -Use successive-approximation method to perform conversion and completes one conversion in 7.5  s. -Direct interface to SPI interface. -Analog input ranges from 0 to 5V with 5V power supply

62 H. Huang Transparency No.11-62 The 68HC11 Microcontroller MAX1241 Signal Pins CS: chip select. The falling edge of this signal initiates the conversion. DOUT: serial data output. Data changes state at SCLK’s falling edge. AIN: analog input. SCLK: serial clock input. SCLK clocks data out at rates up to 2.1 MHz. GND and V DD : device supply pins. REF: analog reference voltage. Reference voltage for A/D conversion. SHDN: Shut-down input. Pulling SHDN to low shuts the MAX1241 down to 15  A supply current. Chip Functioning -Pulling CS to low initiates the conversion. -The serial data stream consists of a high bit, signaling the end of conversion (EOC), followed by the data bits (MSB first). -End of conversion is signaled by DOUT going high.

63 H. Huang Transparency No.11-63 The 68HC11 Microcontroller Timing and Control -The CS and SCLK signals control conversion-start and data-read operations. -After DOUT goes high, it transitions on the first falling edge of the SCLK signal. The next falling clock edge shifts out the most significant bit of the conversion result at DOUT, followed by the remaining bits. -Since there are 12 data bits and one leading high bit, at least 13 clock periods are needed to shift out a conversion result. -Extra clock signals only shift out trailing zeros at DOUT.

64 H. Huang Transparency No.11-64 The 68HC11 Microcontroller

65 H. Huang Transparency No.11-65 The 68HC11 Microcontroller Interfacing the MAX1241 with the 68HC11 Procedure for A/D conversion Step 1. Use a general-purpose I/O line (SS) to pull CS low. Keep SCLK low. Step 2. Wait for maximum conversion time. Alternately, look for DOUT rising edge to determine the end of conversion. Step 3. Activate SCLK for a minimum 13 clock cycles. When interfacing with SPI, two bytes are transferred. The first bit is a 1 followed by 12 data bits and three zeros. Step 4. Pull CS high at or after 13th falling clock edge (actually 16th clock edge) Step 5. With CS = high, wait for the minimum specified time, t CS, before initiating a new conversion by pulling CS low.

66 H. Huang Transparency No.11-66 The 68HC11 Microcontroller Reading A/D Result with two SPI Transfers

67 H. Huang Transparency No.11-67 The 68HC11 Microcontroller Example 11.15 Write a subroutine to perform an A/D conversion operation for the circuit shown in Figure 11.19 and return the result in double accumulator D. Solution: REGBASEQU$100 DDRDEQU$09 SPCREQU$28 SPSREQU$29 SPDREQU$2A CSEQU$20; value to select the CS pin DOUTEQU$04; value to select the DOUT pin get_samplePSHX PSHY LDX#REGBAS LDAA#$3A STAADDRD,X LDAA#$50 STAASPCR,X; set transfer rate to 1 Mbits/s, set master mode BCLRPORTD,X CS; start an A/D conversion BRCLRPORTD,X DOUT *;wait until DOUT goes high STAASPDR,X BRCLRSPSR,X $80 *; wait until the SPI transfer is complete LDAASPDR,X; place the upper byte of the A/D result in A

68 H. Huang Transparency No.11-68 The 68HC11 Microcontroller STABSPDR,X; shift in the lower byte of the conversion result BRCLRSPSR,X $80 *; wait until SPI transfer is complete LDABSPDR,X; place the lower byte in B BSET PORTD, X CS; prepare for the next A/D conversion LSLA; clear the bit 7 of accumulator A LSRA;“ LSRD; place the 12-bit A/D result in the lower 12 bits of D LSRD;“ PULY PULX RTS

69 H. Huang Transparency No.11-69 The 68HC11 Microcontroller In C language, unsigned int get_sample ( ) { unsigned char x1; unsigned int a2d_result; DDRD = 0x3A; SPCR = 0x50; PORTD &= 0xDF;/* start an A/D conversion */ while (!(PORTD & 0x04));/* wait until conversion is complete */ SPDR = 0x00;/* shift in the upper byte of the A/D result */ while (!(SPSR & 0x80));/* “*/ x1 = SPDR; SPDR = 0x00;/* shift in the lower byte */ while(!(SPSR & 0x80));/*“*/ PORTD |= 0x20;/* disable A/D converter */ a2d_result = x1 * 256 + SPDR;/* combine upper and lower bytes */ a2d_result = a2d_result << 1;/* place the A/D result in the lower 12 bits */ a2d_result = a2d_result >> 4;/* “*/ return a2d_result; }


Download ppt "H. Huang Transparency No.11-1 The 68HC11 Microcontroller Chapter 11: 68HC11 Analog to Digital Converter The 68HC11 Microcontroller Han-Way Huang Minnesota."

Similar presentations


Ads by Google