Gadget Renesas

Linking Ideas and Electronics

がじぇるね工房

Using the function on GR-KURUMI, Analog input ADC vol.

GR-KURUMIの機能を使う アナログ入力(ADC)編

####

Handling analog signal by microcontroller

image01

In real world, all sort of phenomena are filled with analog signal while MCUs are in the digital world. Analog signal needs to be converted into digital signal somehow.
An ADC ‘Analog to Digital Converter’ function, which converts analog signal into digital one, fortunately equipped with MCU in many cases, MCU can handle analog signals.

Or HDC1000, temperature-humidity sensor, also has ADC, MCU can use digital signals directly.

####

Unsettled analog signal

image02

Remember learning how to use the scale when we are elementary school student.
Take an average by measurement several times or several rulers.

The degree of the scale for typical measuring tools are at variance.

In the analog world, every amounts has an error, which is unavoidable. The degree of measured values are unstable, of scale, the sensibility of visual inspection as well.
Even the scale of a ruler made of metal,it's known to expand or shrink depend on ambient temperature.

We can’t pursue the accuracy endlessly, it’s important to find an acceptable range, up to the xxx decimal place.


When you measure analog signals, set the accuracy in advance.

####

Roughness of Digital signal

image03

In digital world, defined clearly, the accuracy of the figure is infinite.
On the other hand,

analog signal has continuity, every point has error range.

Think about resolution of ADC built in microcontroller.For example, the resolution of ADC on GR-KURUMI is 10bit which is allowed to show the figure in 1024 ways.

divide by 1024 gives 3.2mV. Converting into digital signal; 0~1023. 0 is 0mV. 1 is 3.2mV, 2 is 6.4mV….. 1023 is 3296.8mV. So what's 1.6mV going to be?The output of ADC, 1.6mV will be either 0 (0mV) or 1 (3.2mV), the error is 1.6mV against actual value. If the resolution of ADC is 11bit, 1.6mV will be 1. How about 0.8mV? If the resolution of ADC is 12bit, 0.8mV will be 1.

Converting from analog signal to digital signal, this approach of increment of resolution is often used to improve accuracy.However, in RL78/G13 case, the resolution of ADC is fixed at 10bit.

image04

To improve accuracy on 10bit resolution of ADC, by converting analog signal several times, statistically we can set the 11bit.
This approach is like the way of measurement with scale several times. We must consider that we need to spend a lot of loads/money to improve accuracy.

####

Measuring temperature with a thermometer build in microcontroller

RL78/G13 on GR-KURUMI has built in temperature inside, we can see temperature easily.
Let’s try.
Nothing special. Generate new project on WEB compiler, realize the function in source code 'gr_sketch.cpp' automatically.

getTemperature(TEMP_MODE_CELSIUS);

is the function which take temperature data with Celsius from a built-in thermometer.
Open this program on GR-KURUMI through COM port, output the temperature only once.

####

Measuring temperature with an analog temperature sensor

image05 image06

When we say analog temperature sensor, there are several types. When we say analog temperature sensor, there are several types. Thermistor, platinum are popular, here we use TMP36GT9Z, IC type. Since supply voltage range is 2.7V~5.5V, MCU can be used with 3.3 V supply voltage.

When outputting, sensor convert temperature into voltage in a ratio of 10mV per 1℃. Rising temperature by 1℃, 10mV increase in voltage.

As for temperature sensor, usually making 25℃ a criterion since it’s normal temperature.
Since TMP36GT9Z is set at 750mA when it is 25℃, when the voltage taking from ADC convert into temperature, specify the reference point at 25℃. With this sensor, measure temperature range up to 50℃.
output value by sensor is 1.25V when its 50℃.

Buy TMP36GT9Z from Marutsu.
http://www.marutsu.co.jp/pc/i/171271/

####

Setting the accuracy

At the beginning, need to Know the accuracy on TMP36GT9Z. Datasheet shows the error range ±1℃ at 25℃, ±2℃ during -40℃~125℃. To recognize a change easier, set the accuracy to the first decimal place.

####

Connection with temperature sensor

image07

Connect A0 port on GR-KURUMI with output of temperature sensor. As datasheet shows, prepare bypass capacitor.
Connect A0 port on GR-KURUMI with Vout of sensor.

####

Operation

Here is the sample code for the program which output temperature to serial regularly.

####

Improving accuracy

Here is the output data as below.

image08

In a ratio of 10mV per 1℃, rising temperature by 0.1℃, 1mV increase in voltage.
Considering that the resolution of ADC is 10bit, as I already mentioned, 3.2mV is the lowest figure digital signal can show.
Is there any good ideas to improve accuracy to the first decimal place?

The effective resolution come from standard voltage divided by ADC resolution.
As for standard voltage, RL78/G13 on GR-KURUMI has three options such as 1.45V standard voltage generated inside, outside as well as power supply voltage (VCC).
Please refer to the function "analogReference".

Arguments
   DEFAULT:VCC
   INTERNAL:Built in voltage 1.45V
   EXTERNAL:Choose an option of “fixed at VCC on board”

Assume the standard voltage is 1.45V, resolution is around 1.4mV, we can see some progress.
However, as datasheet shows, internal voltage has an error range, 1.38V at minimum, 1.5V at maximum.
Though it’s more stable than VCC which is affected by possible fluctuation of supply voltage.

Usually, we take advantage of external standard voltage.
Unfortunately it's not available on GR-KURUMI.

There are many types of IC which allow to generate standard voltage. Pick from them on demand.

Again, to improve accuracy, rather than the method of using hardware, extract the necessary data compensated from the deviation such as average of taking analog signal data several times, running mean, least-squares method.

####

Verifying the measurement of the temperature

image09 image10

How do we check whether the measured temperature by GR-KURUMI is corrected? A standard measuring tool or thermometer is required for comparing.

Checking to see the error range of the thermometer widely appeared on the market.The error range ±1℃~2℃ is normal at 25℃。
Other temperature sensors of IC type as well.

Searching high-precision measurement of temperature on the web, found this document. Click here.
http://www.el.gunma-u.ac.jp/~kobaweb/lecture/hiaccusensor.pdf

An error range ±1℃ would be no problem in the case of measurement of room temperature. ※Need to pick higher – precision one to reduce the electrical charges for air conditioner even a little.

As standard, found the tester with thermometer function and its accessory K type of heat electric body in my house. K- type of heat electric body is a small sensor made by bonding together sheets of two different metals. As picture show, fitting on the tip of a cable in a bared state.
IC type temperature sensor is much bigger than K- type of heat electric body, the difference in size affect on the one in thermal capacity.
Bring in 50℃ ambient temperature, IC type temperature sensor react slow while K type of heat electric body react quickly.
More over K type of heat electric body is sensitive to the slightly flowing air.

Comparing the two, the same condition is required.

As one of methods, standard thermometer and evaluation object are packed with cotton and aluminum foil to avoid wind, take some time to compare the two altering temperature in refrigerator or heating hotplate slowly.
Record the output of the IC temperature sensor at each temperature and plot in the graph. In normal temperature range, as the output of IC temperature sensor tends to become the straight line on a certain degree, Let’s multiple the value coming from ADC by compensate coefficient taking from plot to approach actual measurement figure.

####

Notice on mounting on board

Even improved accuracy or compensated, if IC temperature sensor is mounted near the devices which consume big power such as micro controller, wireless module or power supply, it would be affected by them and can’t measure temperature correctly.
Keeping away from the device consuming big power, need to mount the position not filled with heat.

Also keep away from the case as IC temperature sensor is affected by heat capacity if it adhere to the case.

####

Handling AC signal

image11 image12

The trans has been distributed to measure AC current, widely used for the electronic kit because of energy-saving boom.
http://www.u-rd.com/
http://www.multimic.com/
Also used for measuring consumption current on electric apparatus or current on switchboard.
The signal which ADC handle on GR-KURUMI would be DC in 0~3.3V or 0~1.45V.
When measure AC, signal swing in a plus or minus direction. Acting isolation, take trans as picture shows, convert the output current at secondary side into voltage, then apply DC bias on the voltage so that microcontroller can handle. Take advantage of ADC dynamic range by adjustment of middle point and oscillation. For example, If standard voltage is 3.3V, set the middle point as 1.65V, oscillation as ±1.5V at maximum current.

The value of the AC is calculated by Root Mean Squire (RMS).

image13

Graph shows the simulation plot that the oscillation of current is assumed to be ±1A in the peak.
Actually, it's approximately 0.707Arms.

Actual current waveform, it won’t be sine wave in good shape like this. Electric apparatus using switching power supply has a more complex waveform.

This graph in excel file consist of 32pcs of data per one wave, described with plot by every 0.625ms. In East Japan, the frequency of power supply is 50Hz, 20ms period. 20ms divided by 32 data equals 0.625ms.

When described in excel file, go backward the process that set the oscillation from the current value of 0.707Arms, then draw up 32 data, plot this data to draw sign waveform.

image14

Using ADC, let AC waveform convert into digital data by every 0.625ms. Remove the DC portion from each data due to applying DC bias.
Definition of AC waveform is the waveform which the total amount of the oscillation will be zero for a period, if add digital data for a waveform, only DC portion will be left, average will be the DC value which is applied DC bias. Please refer to the formula in the left.
If remove the DC portion from original data, only AC portion will be left. Average of integral each value’s squire for AC portion per one wave, take root of the value as actual data.
Please refer to he formula in the left. Provisionally, take AC waveform as digital data with 1/32 period of one wave,The more complicate actual waveforms are,, the more data is needed to be converted with shorter period and higher resolution.

####

Automation of taking analog signal

Taking AC is necessary to be executed with right period and high frequency.
With analogRead, it’s difficult to perform with a fixed time period and a constant CPU load since software is trigger in conversion to digital data. However RL78/G13 will be convert it automatically, storage it into RAM since it has timer coincide with ADC and DMAC.
Let’s try this function.

※This function is not supported by library, need to set the resister of the peripheral function by yourself.

####

Direct access to the resister of RL78/G13

As an advantage of library conforming to Arduino, even you don’t have much knowledge about hardware of RL78/G13, it would help operation in its own way.Apart from library support, let’s operate microcontroller.
Handling register, include these file as below.


#include <iodefine.h>
#include <iodefine_ext.h>

####

An option for trigger of ADC conversion

image15

Here is the block diagram for ADC on RL78/G13. Please refer to the point in the red circle for the input for the trigger of ADC conversion.

image16

The interruption generated at RTC would be a trigger for INTRTC. The interruption of timer 0, channel 1 would be a trigger for INTTM01.
The interruption of 12bit interval timer would be a trigger for INTIT. INTRTC is not available since RTC is going to be used as clock.As for INTTM01, original oscillation of timer is 32MHz, the system clock generated by high speed on-chip oscillator.
Accuracy of oscillation is within +/-1% in normal temperature range if operation voltage is 3.3V.

INTIT can’t be used since it’s working on management per millimeters unit.

INTTM01 is used by divided original oscillation. The issue is if we can set the ratio by which original oscillation is divided without residue.
Divided 32MHz by 50Hz x 32 gives you just 20000.

INTTM01 is an option for trigger.




Showing how to reset the value of timer as below.

####

Using scan mode

image17 image18

ADC on RL78/G13 has scan mode which can convert several analog inputs all at once.
It’s helpful when you need to handle several inputs.
Please keep in mind the followings.

Under scan mode function, make sure to select four channels, moreover there is a constraint on channel combination.
※Please refer to the chart.
Please don’t select the terminals having set as digital for conversion channel.

Let’s scan ANI0~ANI3 this time
※In GR-KURUMI case, ANI0 is connect to 3.3V, ANI1 connected to GND. Both ANI2 and ANI3 are available for analog input. Triggering as INTTM01 50HZ x 32 , generate the conversion data with 8byte by every 0.625ms. Showing how to reset the value of ADC.

####

Using DMAC

The data converted by scan mode will be signal-processed after it’s transferred into memory.
To have AC wave form signal processed, the data is required for a wave portion. It’s assumed to be 32 data this case.

For scan mode, 32 x 4 x 2byte of the data is transferred to memory.

DMAC 'Direct Memory Access Controller' would be help you.Under specified condition, DMAC automatically transfer the data from peripheral I/O to memory or from memory to peripheral I/O, instead of CPU.

The interruption from ADC is a trigger for DMAC this time.When the data transfer is completed preset number of times, DMAC can happen interruption to CPU.CPU can execute other tasks until the data transfer is complete, process the data later. As of setting of DMAC, original data is data-register of ADC and transferred to SRAM area secured in global variables.The size of data is 16bit, the times of transfer is 32x4times.

Laminate buffer for data storage, in order to prevent it from being overwritten by DMAC while CPU signal processing.

####

Registration of user interruption

For procedure for interruption, please refer to interrupt_handlers.c under gr_common/RLduino78/portable/e2studio/RL78
DMA1 interruption will be INT_DMA1.
This is the program calling for procedure of DMA interruption.

【The example for procedure of triggering ADC】
This is the program for procedure of triggering ADC.
Actual signal processing is not mentioned.

MVP information

@chobichan

General hard engineer.. submitting his writing for technical magazine occasionally.
twitter account:
https://twitter.com/chobichan

Chobichan avatar

Follow him

All articles from @chobichan

share