AGH University of Science and Technology Faculty of Computer Science, Electronics and Telecommunication Department of Electronics MICROPROCESSOR TECHNICS II Tutorial 5 Combining ADC & PWM Mariusz Sokołowski English translation: Paweł Russek http://www.fpga.agh.edu.pl/upt2 13.11.2017
1. INTRODUCTION 1.1. GOAL The main objective of this tutorial is to make student familiar with: the correct initialization procedure of AD converter, the Pulse-Width Modulation (PWM) mode of TPM timer, a technics of combining functions of different peripheral devices with the examples of AD converter used for light detector and PWM module used for an LED intensity control, the noise filtering that is gained thanks to an averaging technics and reference signal of 10 Hz in Kinetis L series of microcontroller from Freescale Semiconductor. 1.2. REQUIREMENTS Hardware and software platforms: PC with Keil s uvision v.5 development software installed, the FRDM-KL46Z microcontrollers development kit from Freescale Semiconductor. Prerequisites: fundamental concepts of Digital Electronics, fundamental ideas from the course of Microprocessors Technics part 1. Literature: Lecture materials: Timers and Analog Interfacing. KL46 Sub-Family Reference Manual, Freescale Semiconductor. Kinetis L Peripheral Module Quick Reference, Freescale Semiconductor.
2. ANALOG TO DIGITAL CONVERTER The microcontroller MKL46Z256VLL4, which is the heart of FRDM-KL46Z development kit, offers the multichannel, 16-bit Analog-to-Digital Converter (ADC) as its standard peripheral (Fig. 1). Figure 1. A block diagram of the Analog to Digital Converter All ADC inputs can be configured as single-ended inputs that measure voltage with respect to the VREFL signal (typically GND). Some inputs (DADP0..3 on Figure 1) can work as differential inputs (with respect to DADM0..3). These input s properties impact a binary format and resolution (bit-width) of ADC results. There are 8, 10, 12, and 16-bit modes, and the results are unsigned data for single-ended measures. Respectively, there are 9, 11, 13, and 16-bit modes, and the results are U2-coded signed data for differential measures. The
reference voltage for ADC (VREFH) is 3.3V on the FRDM-KL46Z board. Please remember, to not exceed the 3.3V value on the ADC inputs. The calibration is the very first step that must be conducted before an ADC can be used for voltage measurements. In your program code, a procedure of the calibration should be preceded by a set-up of the appropriate values in microcontroller s registers. The preparation of your program code for the tutorial exercise goes as follows. Change the definition of CLOCK_SETUP symbol to 1 in the system_mkl46z4.c file. For that selection, the clock configuration for the microcontroller is: o 8 MHz reference clock for the MCG module, o 48 MHz core clock, o 24 MHz bus clock. Enable the module clock for the ADC in the SIM->SCGC6 register. Select the source of the ADC conversion clock, its divider ratio, and the long conversion time in the ADC0->CFG1 (ADICLK, ADIV, ADLSMP). Choose appropriate parameters to provide a conversion clock that does not exceed 4 MHz. Enable the support for the long sample time (ADC0->CFG1[ADLSMP]), select the additional long sample time, and allow 2 additional ADCK cycles to total conversion time to allow higher speed conversion clocks (ADC0->CFG2[ADLSTS:ADHSC]). Select an external reference voltage in ADC0->SC2[REFSEL]. Enable hardware averaging and select the maximum samples averaged in ADC0->SC3[AVGE:AVGS]. Set ADC0->SC3[CAL] to start calibration. The selection of the channel number, conversion mode (single, continuous), compare parameters, result precision, and input mode (single-ended, differential) are not necessary for calibration. Put your code in a waiting loop to allow the calibration to finish; check the eadc0->sc3[cal] bit for 0 to detect that the calibration completed. Check the bit ADC0- >SC3[CALF] is clear to ensure that the calibration was successful. If CALF is set, the error should be handled; for example the calibration procedure should be repeated. On success, the calibration results have to be processed and the results have to be written to appropriate ADC registers. The procedure is as follows: Declare and clear uint16_t variable Add all values in the ADC0->CLPx registers up. Divide the result by 2. Set the most significant bit of the result. Write the result to the ADC0->PG register. Add all values in the ADC0->CLMx registers up. Divide the result by 2. Set the most significant bit of the result. Write the result to the ADC0->MG register The ADC is calibrated and ready to work now, but some other parameters have to be set additionally. Perform the following steps in your program code:
Select the input clock as Bus clock/2, set the clock divide ratio to 1, enable the long sample time, and 12-bit resolution mode in ADC0->CFG1 Enable interrupt in the ADC0->SC1[0][AIEN]. Other ADC parameters, like the software triggering of a conversion (ADTRG=0 in ADC0->SC2), are set to their default values after the Reset signal and there is no need to set them in the program. The measurement is triggered by a software when the input channel number is written to the ADC0->SC1[0][ADCH] field. The conversion result is available in the ADC0->R[0] register when the conversion complete flag is set in the SC1[0] register (ADC0->SC1[0][COCO]). 3. PWM MODE OF THE TPM TIMER The TMP timers of Kinetis L microcontrollers (TPM0, TPM1, and TPM2) provide six channels that can be used in various working modes. Here, we will focus our attention on the Pulse Width Modulation (PWM) working mode of the TMP. Other working modes of TPMs are introduced in more details by Tutorial 5 of the laboratory course. For the reference, Table 1 presents all the modes of TMP channels. Table 1. TPM working modes
We will put our attention to "Edge-aligned PWM" and "Center-aligned PWM" modes in Table 1. The first mode regards pulse width modulation that is synchronized to a pulse edge. The other mode is pulse width modulation that is synchronized to a pulse center (see lecture notes for detailed explanation). We will choose the "Edge-aligned PWM" in this laboratory exercise. Add the following steps to your program code to initialize the TPM0 timer: Enable the port D and E modules clock in the SIM->SCGC5 register. Enable TMP0 module clock in the SIM->SCGC6 register. Select the TMP0 clock source in SIM->SOPT2[TPMSRC:PLLFLLSEL]. We will use "MCGFLLCLK clock or MCGPLLCLK/2" and "MCGPLLCLK clock with fixed divide by two" in this exercise. Select up counting mode of the TMP0 counter and set the clock divider to 128 in TPM0->SC[CPWMS:PS] Set a range of the TMP0 counter to 4095 (this corresponds to 12-bit range) in the TPM0->MOD register. Choice the "Edge-aligned PWM High-true pulses" in channel 2 of TMP0 in TPM0->CONTROLS[2].CnSC (see Table 1 for right bit values) Specify the "Edge-aligned PWM Low-true pulses" in channel 5 of TMP0 in TPM0->CONTROLS[5].CnSC (see Table 1 for right bit values) Set the PWM duty cycle to 0 in TPM0->CONTROLS[x].CnV for both channels. Enable TMP0 counter in TPM0->SC[CMOD]. 4. EXERCISE Use the red and green LEDs and the light sensor of FRDM-KL46Z to develop the application: Measures the voltage on the collector of the Q1 fototransistor (Fig. 2) and presents the value on LCD display of FRDM-KL46Z (use all four digits of the LCD). The light sensor input is connected to pin PTE22, which corresponds to channel 3 of ADC0. Use an interrupt to collect ADC data. The channel 2 of TPM0, which is in the "Edge-aligned PWM High-true pulses" mode, controls the red LED, and its brightness is proportional to the ambient light intensity (it is brighter in the light). The channel 5 of TPM0, which is in the "Edge-aligned PWM High-true pulses" mode, controls the green LED, and its brightness is reverse proportional to the ambient light intensity (it is brighter in the dark). Change the TMP0 mode from the "Edge-aligned PWM" to "Center-aligned PWM". 5. HOMEWORK 1. Use the SysTick timer to generate 100ms interrupt and implement light measure averaging in your program. Display average value on the LCD. 2. Uses ADC0 to measure the VREFH reference voltage. Display the measured value on LCD. 3. Uses ADC0 to measure the voltage from the temperature sensor that is embedded in the microcontroller. Display the value on LCD.
Figure 2. The light sensor of FRDM-KL46z.