Lesson 7: Part 2: ADC - Reading Sensor Voltage Input

Previously we work on the ADC function in reading on chip temperature sensor. In this tutorial we will be working on external sensor.
 
To simplify the work require, we will be using a potentiometer to simulate the change of resistor. A sample of potentiometer is this. By applying the potentiometer in a voltage divider circuit, we will be getting an output voltage that change when potentiometer resistor is changing.

Tutorial Setup

This tutorial will be reading performing ADC read at channel ADC_IN0 every seconds (using systick), then output it through USART interface(USART) into laptop hyperterminal.

Below shown the setup require on ADC section


Using Vin=5V,  R1=6.8K Ohm, potentiometer at R2 (with resisitance range of 0Ohm ~ 10K Ohm). The voltage output sample is shown in below:

R2 Resistance              Vout
0 Ohm                                 0V
1K Ohm                         0.64V
2K Ohm                         1.13V
5K Ohm                         2.11V
10K Ohm                       2.98V


The Vout will be connect into PA0, correspond to ADC_IN0 pin.

Actual Setup

The MCU is actually running at 3V, the ADC will give maximum reading of 0xFFF when input voltage is 3V.


Source Code Descriptions

SFR Settings Descriptions

The following SFR setup is require to enable ADC_IN0

A) ADC function

Power up initialisation
  1.  Enable GPIO-A block
  2. Change PA0 pin function to alternate function (ADC function)
  3. Enable ADC block
  4. Perform ADC calibration and wait for completion
  5. PowerUp ADC block and wait for completion
During Run time on Reading ADC result
  1. Select ADC channel (in our case it would be ADC_IN0:Channel 0)
  2. Enable single conversion mode/
  3. Select conversion resolution (in our case we use 12 bits)
  4. Start ADC process and wait for completion
  5. Read the result
B) USART
  1. Enable GPIO-A block
  2. Change PA9, PA10 pin function to alternate function( USART)
  3. Enable USART block
  4. USART settings on the following: 8 databit, 115200 baud rate, 1 stop bit
  5. USART transmit/receive enable
  6. Enable USART interrrupt receive
  7. Enable USART
  8. * For more detail on USART, refer to tutorial here.
C) SysTick
  1.  Set SysTick timer reload value
  2. Clear SysTick timer current value to 0
  3. Select SysTick timer clock source
  4. Enable timeout interrupt, SysTick timer
  5. *For more detail on SysTick, refer to this tutorial.

Tutorial Source Code

The tutorial source code can get clone from here.
I have purposely combine the previous tutorial (USART, SysTick) to show how a source code can be 'expand' when more features is being added. Now each MCU peripheral will have its respective source file (e.g. gpio.c, systick.c, uart.c)

Even though we are using potentionmeter to simulate the voltage change, a real life example work similar way. For example, a temperature sensor of LM35, is giving out a voltage change base on temperature. And the above code can be use with some tweak on the ADC result, to translate the result into temperature.

Lesson 7: Part 1: ADC - Reading On Chip Temperature Sensor

MCU is consider the processing unit of the embedded system. In order to allow MCU 'process', certain data need to be collect and feed into the system. For example, for a temperature monitoring system, it may require to pickup the current temperature from a temperature sensor. Or a burglar system will require to read current door contact sensor and feed into MCU. Base on these input then MCU will act accordingly.

There are may method the data (or sensor data) can be receive by MCU such as: SPI bus, IIC bus, ADC. We will be touching on SPI/IIC in future, for this lesson, our focus will be on ADC, which stands for 'Analog to Digital Converter'. As the name imply, the functionality of ADC is to convert analog signal(in this case is voltage) to binary data.

Take temperature sensor as example, when the temperature is changing, the resistance of the sensor will varies. Using this characteristic and apply the sensor into voltage divider circuit, we will have a voltage output that change according to the temperature. By using ADC, MCU can know the current voltage level, thus enabling system to know current temperature.

After reading the ADC section in Reference Manual (RM0360), there is actually an internal temperature sensor (Vsense). This would be an good example to demonstrate ADC function in reading temperature.

p.s:
Despite the idea of demostrating tempeature reading, it appears that much more work is require in order to perform a proper temperature reading, as described in RM0360: Section 12.9. Thus, the example would only demonstrate a ADC value change (increase) correspond to temperature increase.

Controlling ADC

Key function to enabling ADC is summarizes as below:
  1. Enable ADC peripheral
  2. Configurations on ADC
  3. ADC channel selection
  4. Start ADC process
  5. Wait for ADC process complete, then read the result
The source code can get obtain from here.