Download Solved Examples on Fundamentals of Microcontrollers | EMCH 367 and more Study notes Mechanical Engineering in PDF only on Docsity! Dr. Victor Giurgiutiu Page 1 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER EXAMPLE – A/D CONVERTER OBJECTIVE This example will help you learn how to use the M68HC11 microcontroller to collect analog data in digital form. The following objectives are considered: Review the use of A/D conversion function of the M68HC11 microcontroller. Illustrate various modes of performing A/D conversion. This example covers the following topics: Single channel, single conversion, MULT=0, SCAN=0; Single channel, continuous conversion, MULT =0, SCAN =1; Multiple channel, single conversion, MULT =1, SCAN =0; Multiple channel, continuous conversion, MULT =1, SCAN =1; PROGRAM A/D CONVERTER SINGLE CHANNEL, SINGLE CONVERSION, MULT=0, SCAN=0 In this section, we will study the single-channel single-conversion. The channel selected in this example will be the pin PE1. The program will do the following a) Initialize the A/D conversion b) Set ADCTL to reflect the following control bits: i) CD=0, CC=0, CB=0, CA=1 to select pin PE1 ii) SCAN=0, i.e., no scanning iii) MULT=0, i.e., single channel c) Check if the A/D conversion has finished. The conversion is finished when the flag CCF is set. d) Load the results from the AD registers ADR1 – ADR4 and store them into memory locations VAL1 – VAL4. e) Loop back to b) FLOWCHART AND CODE The program flowchart is show below. Two flowchart levels are presented: the big-picture and the details. The big-picture is used to understand the overall architecture of the program. The details are used to explain some of the blocks. (Details are given only for those blocks which are somehow new, and have not been used in previous programs.) The essential code for this program is shown to the right of the flowchart. The essential code was incorporated into the standard template asm to generate the code file Ex_AD_1.asm. Dr. Victor Giurgiutiu Page 2 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER Step through the program until all the four AD registers get transferred into the memory storage locations VAL1 – VAL4. The screen looks like this: Remove the breakpoint from $c010. Enter 1500 mV into PE1. Run. The program will run continuously. The converted value $4c (1500*$ff/5000=$4c, check with your hex calculator!) appears in the ADR1 – ADR4 registers and is transferred into VAL1 – VAL4. The screen looks like this: Dr. Victor Giurgiutiu Page 5 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER Put value 2000 mV into PE1. The converted value $66 (2000*$ff/5000=$66, check with your hex calculator!) appears in ADR1 – ADR4 and then in VAL1 – VAL4. The screen looks like this: Dr. Victor Giurgiutiu Page 6 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER Put value 2500 mV into PE1. The converted value $80 appears in ADR1 – ADR4 and then in VAL1 – VAL4 (2500*$ff/5000=$7f with normal 1 bit convention, check with your hex calculator!. But M68HC11 uses ½ bit accuracy convention, and hence rounds up to $80). The screen looks like this: Put value 3500 mV into PE1. The converted value $b3 appears in ADR1 – ADR4 and then in VAL1 – VAL4 (3500*$ff/5000=$b2 with normal 1 bit convention, check with your hex calculator!. But M68HC11 uses ½ bit accuracy convention, and hence rounds up to $b3). The screen looks like this: Dr. Victor Giurgiutiu Page 7 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER EXECUTION Open and assemble Ex_AD_2.asm. Reset registers and memory. Set standard labels (Label/Set Standard Labels). Run the program. Notice that after addressing ADCTL once at the beginning, the program now loops only for storing the values of ADR1 – ADR4 into memory locations VAL1 – VAL4. Enter 1000 mV in PE1. The value is converted to $33 and stored in memory. The screen looks like this: Continue with the other values in Table 1, until you are satisfied with the functioning of the program. MULTIPLE CHANNEL, SINGLE CONVERSION, MULT=1, SCAN=0 We will now perform AD conversion on multiple channels, single conversion. This mode is selected by setting MULT=1 and SCAN=0 such that the conversion is performed once. The program flowchart is shown below. Two flowchart levels are presented: the big-picture and the details. The essential code for this program is shown to the right of the flowchart. The essential code was incorporated into the standard template asm to generate the code file Ex_AD_3.asm. Dr. Victor Giurgiutiu Page 10 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER Big-picture Flowchart Flowchart Details VAL1 1 byte VAL2 1 byte VAL3 1 byte VAL4 1 byte Define variables X=REGBAS AD converter Initialize CCF=0? N Y MULT = 1 SCAN = 0 CD,CC = 0 LABEL1 ADC Perform A/D conversion Store conversion results Store ADR1, ADR2, ADR3, ADR4 to VAL1, VAL2, VAL3, VAL4 Code ORG DATA VAL1 RMB 1 VAL2 RMB 1 VAL3 RMB 1 VAL4 RMB 1 * Main program ORG PROGRAM START LDX #REGBAS * switch on A/D converter BSET OPTION,X,%10000000 * Perform a single A/D conversion on PE0, PE1, PE2, PE3 ADC LDAA #%00010000 ; SCAN=0, MULT=1 STAA ADCTL,X LABEL1 LDAA ADCTL,X ANDA #%10000000 BEQ LABEL1 * Store the four consecutive conversion values LABEL2 LDAA ADR1,X STAA VAL1 LDAA ADR2,X STAA VAL2 LDAA ADR3,X STAA VAL3 LDAA ADR4,X STAA VAL4 BRA ADC ;BACK TO TOP ORG RESET FDB STAR EXECUTION Open and assemble Ex_AD_3.asm. Reset registers and memory. Set standard labels (Label/Set Standard Labels). The screen looks like this: Dr. Victor Giurgiutiu Page 11 11/29/2020 EMCH 367 Fundamentals of Microcontrollers Example A/D CONVERTER Enter 2500 mV in PE0, 1000 mV in PE1, 3500 mV in PE2, 2000 mV in PE3. Run the program. The converted value $80, $33, $b3, $66 appear in ADR1 – ADR4, and then get stored in memory locations VAL1 – VAL4. The screen looks like this: Dr. Victor Giurgiutiu Page 12 11/29/2020