Playing with analog-to-digital converter on Arduino Due
Today I’m going to present some of more advanced capabilities of ADC built in ATSAM3X8E – the heart of Arduino Due.
I like the Arduino platform. It makes using complex microcontrollers much simpler and faster. Lets take for example the analog-to-digital converter. To configure it even on Atmega328 (Arduino Uno/Duemilanove) you must understand and set correct values in 4 registers. And it can be much more in complex device, like 14 in ATSAM3X8E (Arduino Due)!
In Arduino, for no matter which processor, all you need to do is:
It’s simple and useful. But there are situations, where you need to use more potential of your chip. Arduino allows you to do so – after all it’s just C++ with some additions.
In my project on Arduino Due I need to sample voltage continuously and as fast as possible. Lets try it the simplest way:
Yes, time between samples is not always even. It wiggles a microsecond or less from time to time. It is due to other tasks that Arduino are performing in background, like counting the time. That’s bad. Sampling frequency should be constant. To unleash full potential of this chip, different approach is needed.
Good news is that Arduino let’s you use almost all the capabilities of microcontroller by using low level C/C++ programming. Bad news – this is quite hard, especially with complex ARM processors. Processors peripherals’ documentation is large (about 100 pages in datasheet and two application notes for ADC alone). And it’s barely enough to understand all the details. Another useful info are examples available in Atmel Studio and the Arduino libraries’ source (\%arduino%\hardware\arduino\sam\cores\arduino).
Using ADC, just like any peripheral, is done by setting appropriate values to related registers. It can be done directly in program, but then it’s quite susceptible to errors. 14 registers, 32 bits in each of them – it’s 448 places to make a mistake. But fortunately, Atmel provides some libraries to make the task easier. And they are bundled in Arduino (\%arduino%\hardware\arduino\sam\system\libsam\). With them, instead of writing to registers, you call corresponding functions. They are handling all the bit masking, shifting and similar stuff. So lets look at the next version of program:
For More Details: Playing with analog-to-digital converter on Arduino Due