Lesson ADC + PWM

Pin Selection and Pin Mode


Know how to select a specific functionality of a given LPC17xx pin. Know how to select a pin mode.

Pin Selection

Every GPIO pin of the LPC17xx is capable of other alternative functionalities. Pin selection is the method by which a user is able to designate the functionality of any given pin. For example, GPIO Pin 0.0 can alternatively by used for CAN channel 1 receive, UART channel 3 transmit, and I2C channel 1 data line. 



Figure 1A. LPC17xx User Manual PINSEL0


Figure 1B. I/O Pin Select Mux (from LPC2148, for illustration purposes only)

In order to select the SDA2 functionality of pin 0.10, one must set bits 21 & 20 of PINSEL0 register to 1 & 0 respectively.

// Using the memory address from the datasheet
*(0x4002C000) &= ~(0x3 << 20);		// Clear bits 20 & 21
*(0x4002C000) |= (0x2 << 20);		// Set bit 21

// Using LPC17xx.h pointers
LPC_PINCON->PINSEL0 &= ~(0x3 << 20);		// Clear bits 20 & 21
LPC_PINCON->PINSEL0 |= (0x2 << 20);			// Set bit 21

Pin Mode

The LPC17xx has several registers dedicated to setting a pin's mode. Mode refers to enabling/disabling pull up/down resistors as well as open drain configuration. PINMODE registers allow users to enable a pull-up (00), enable pull up and pull down (01), disable pull up and pull down (10), and enable pull down (11). PINMODE_OD registers allow users to enable/disable open drain mode. 


Figure 2. LPC17xx User Manual PINMODE & PINMODE_OD



Figure 3. LPC17xx User Manual PINMODE0 


Figure 4. LPC17xx User Manual PINMODE_OD0

For example, if one desires to configure pin 0.09 to enable a pull-up resistor and open drain mode, one must clear bits 18 & 19 of PINMODE0 register, and set bit 9 of register PINMODE_OD0.

// Using the memory address from the datasheet
*(0x4002C040) &= ~(0x3 << 18);		// Clear bits 18 & 19
*(0x4002C068) |= (0x1 << 9);		// Set bit 9

// Using LPC17xx.h pointers
LPC_PINCON->PINMODE0 &= ~(0x3 << 18);		// Clear bits 18 & 19
LPC_PINCON->PINMODE_OD0 |= (0x1 << 9);		// Set bit 9

You may find it helpful to automate register setting and/or clearing. Per our Coding Standards, inline functions should be used (not Macros).


Figure 5. LPC17xx Pin Registers & Circuit (credit: https://sites.google.com/site/johnkneenmicrocontrollers/input_output/io_1768) 

ADC (Analog to Digital Converter)


To learn about the use of ADCs, their different types, their related parameters, and how to set up an ADC driver for the LPC17xx.

    What does ADC accomplish?

    An Analog to Digital Converter is needed whenever one needs to interface a digital system with an analog device. For example, if one needs to read the voltage across a resistor, and use the value within an algorithm running on the SJOne board, an ADC circuit is needed to convert the analog voltage to a discrete digital value. Luckily, the LPC17xx, like most microcontrollers, includes an ADC circuit that we can utilize.

    Different types of ADC circuits

    Flash ADC

    The simplest and fastest ADC circuit relies on a series of comparators that compare the input voltage to a range of voltage reference values. The digital output of the comparators is wired to a priority encoder. The output of the priority encoder represents the binary value of the input voltage.

    Note that the number of bits of the binary output (n) requires 2n comparators. Therefore, the circuit complexity grows exponentially with respect to the number of bits used to represent the converted value (resolution). 



    Figure 1. Flash ADC Circuit (credit: allaboutcircuits.com)



    Figure 2. Flash ADC Timing (credit: allaboutcircuits.com)


    Digital Ramp ADC

    This type of ADC utilizes an up counter, a comparator, a DAC, and a register. DACs (Digital Analog Converters), as their name suggests, perform the inverse operation of an ADC, i.e. They convert a binary input into an analog voltage output. The up counter starts at zero and counts up synchronously. The output of the counter is wired to the DAC. The analog output of the DAC is compared to the analog input signal. As long as the comparator indicates that the input voltage is larger than the DAC's value, the counter continues to increment. Eventually, the DAC's output will exceed the input voltage, and the comparator will activate the counter's reset signal as well as the register's load signal. The register's output represents the binary value of the input analog signal.

    Note that because the counter starts from zero with every sample, the time it takes the circuit to produce the digital output is inconsistent. 


    Figure 3. Digital Ramp ADC Circuit (credit: allaboutcircuits.com)




    Figure 4a. Digital Ramp ADC Timing (credit: allaboutcircuits.com)


    Figure 4b. Digital Ramp ADC Timing Variance (credit: allaboutcircuits.com)

    Successive Approximation ADC

    A successive approximation ADC works very similarly to a digital ramp ADC, except it utilizes a successive approximation register (SAR) in place of the counter. The SAR sets each bit from MSB to LSB according to its greater/less than logic input signal.

    This type of ADC is more popular than flash and digital ramp due to its consistent timing and relatively scalable design.


    Figure 5. Successive Approximation ADC Circuit (credit: allaboutcircuits.com)


    Figure 6. Successive Approximation ADC Timing (credit: allaboutcircuits.com)

    Tracking ADC

    A Tracking ADC works similarly to the Digital Ramp ADC, except instead of an up counter, it utilizes an up-down counter. The output of the comparator determines whether the counter increments or decrements. It doesn't use a register to hold the processed value since it's constantly tracing the input value.

    Note that this type of ADC does not respond well to large changes of the input analog signal. Additionally, it suffers from constantly going over and under the input value. This is known as bit bobble. 


    Figure 7. Tracking ADC Circuit (credit: allaboutcircuits.com)




    Figure 8. Tracking ADC Timing (credit: allaboutcircuits.com)

    DAC-free ADCs

    Besides Flash ADC, all previous ADC circuits rely on using DACs to convert an estimated digital value to an analog one and compare it to the input signal. There are other types of ADC technologies that do not use DACs. They rely on the known time it takes an RC circuit to discharge to match the input analog signal. Single Slope, Dual Slope, and Delta-Sigma ADCs implement this concept.

    ADC Parameters


    This is typically the most-highlighted aspect of any ADC technology. Resolution refers to the number of bits of the ADC's output. It's a measurement of how coarse/fine the converted value is. A four bit 5V ADC offers 16 values for the voltage range 0 V to 5 V (i.e. roughly 312 mV per bit increment). A 10 bit 5V ADC offers 1024 values for the same voltage range (roughly 5 mV per bit increment). 

    Sampling Frequency

    This is simply the circuit's latency (i.e. the rate of converting an analog input signal to digital bits). The highest frequency of an analog signal that a given ADC circuit is able to adequately capture is known as Nyquist frequency. Nyquist frequency is equal to one-half of the circuits sampling frequency. Therefore, to adequately convert an analog signal of frequency n Hz, one must have an ADC circuit with 2n Hz sampling frequency. Otherwise, aliasing happens. Aliasing occurs when an ADC circuit samples an input signal too slowly, thus producing an output signal that is not the true input signal, but rather an alias of it.


    Figure 9. ADC Aliasing

    Step Recovery

    This is a measurement of how quickly an ADC's output is able to respond to a sudden change in input. For example, flash and successive approximation ADCs are able to adjust relatively quickly to input changes while tracking ADC struggles with large input changes.


    This is a measurement of the range of voltages that an ADC circuit is able to capture and output. For example, the LPC1758 has a range of 0V to 3.3V. Other ADCs may have bigger ranges or even variable ranges that a user can select, such as this device: https://www.mouser.com/ds/2/609/AD7327-EP-916882.pdf


    This is a measurement of the systematic error of any given ADC circuit. This is measured by comparing the actual input signal to its digital output equivalent. Note that, this error measurement is only valid within the range of the ADC in question. 

    ADC Driver for LPC17xx


    Figure 10. LPC17xx User Manual ADC Instructions