Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 389c24b

Browse files
committed
samd/docs: Document the extensions to DAC and ADC.
1 parent 68132ce commit 389c24b

File tree

1 file changed

+157
-13
lines changed

1 file changed

+157
-13
lines changed

docs/samd/quickref.rst

Lines changed: 157 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,34 +251,178 @@ The resolution of the ADC is 12 bit with 12 bit accuracy, irrespective of the
251251
value returned by read_u16(). If you need a higher resolution or better accuracy, use
252252
an external ADC.
253253

254+
ADC (analog to digital conversion)
255+
----------------------------------
256+
254257
ADC Constructor
255258
```````````````
256259

257-
.. class:: ADC(dest, *, average=16)
260+
.. class:: ADC(dest, *, average=16, bits=12, vref=3, callback=None)
258261
:noindex:
259262

260-
Construct and return a new ADC object using the following parameters:
263+
On the SAMD21/SAMD51 ADC functionality is available on Pins labelled 'Ann'.
261264

262-
- *dest* is the Pin object on which the ADC is output.
265+
Use the :ref:`machine.ADC <machine.ADC>` class::
263266

264-
Keyword arguments:
267+
from machine import ADC
265268

266-
- *average* is used to reduce the noise. With a value of 16 the LSB noise is about 1 digit.
269+
adc0 = ADC(Pin("A0")) # create ADC object on ADC pin, average=16
270+
adc0.read_u16() # read value, 0-65536 across voltage range 0.0v - 3.3v
271+
adc1 = ADC(Pin("A1"), average=1) # create ADC object on ADC pin, average=1
267272

268-
DAC (digital to analog conversion)
269-
----------------------------------
273+
The resolution of the ADC is set by the bits keyword option. The default is 12.
274+
Suitable values are 8, 10 and 12. If you need a higher resolution or better
275+
accuracy, use an external ADC. The default value of average is 16.
276+
Averaging is used to reduce the noise. With a value of 16 the LSB noise is
277+
about 1 digit. The vref=n option sets the reference voltage for the ADC.
278+
The default setting is for 3.3V. Other values are:
279+
280+
==== ============================== ===============================
281+
vref SAMD21 SAMD51
282+
==== ============================== ===============================
283+
0 1.0V voltage reference internal bandgap reference (1V)
284+
1 1/1.48 Analogue voltage supply Analogue voltage supply
285+
2 1/2 Analogue voltage supply 1/2 Analogue voltage supply
286+
3 External reference A External reference A
287+
4 External reference B External reference B
288+
5 - External reference C
289+
==== ============================== ===============================
290+
291+
The callback keyword option is used for timed ADC sampling. The callback is executed
292+
when all data has been sampled.
293+
294+
ADC Methods
295+
```````````
296+
297+
.. method:: read_u16()
298+
299+
Read a single ADC value as unsigned 16 bit quantity. The voltage range is defined
300+
by the vref option of the constructor, the resolutions by the bits option.
301+
302+
.. method:: read_timed(data, freq)
303+
304+
Read adc values into the data buffer at a supplied frequency. The buffer
305+
must be preallocated. Values are stored as 16 bit quantities in the binary
306+
range given by the bits option. If bits=12, the value range is 0-4095.
307+
The voltage range is defined by the vref option.
308+
The sampling frequency range depends on the bits and average setting. At bits=8
309+
and average=1, the largest rate is >1 MHz for SAMD21 and 350kHz for SAMD21.
310+
the lowest sampling rate is 1 Hz. The call to the method returns immediately,
311+
The data transfer is done by DMA in the background, controlled by a hardware timer.
312+
If in the constructor a callback was defined, it will be called after all data has been
313+
read. Alternatively, the method busy() can be used to tell, if the capture has finished.
314+
315+
Example for a call to adc.read_timed() and a callback::
316+
317+
from machine import ADC
318+
from array import array
319+
320+
def finished(adc_o):
321+
print("Sampling finished on ADC", adc_o)
322+
323+
# create ADC object on ADC pin A0, average=1
324+
adc = ADC(Pin("A0"), average=1, callback=finished)
325+
buffer = array("H", bytearray(512)) # create an array for 256 ADC values
326+
adc.read_timed(buffer, 10000) # read 256 12 bit values at a frequency of
327+
# 10 kHz and call finished() when done.
328+
329+
.. method:: busy()
330+
331+
busy() returns `True` while the data acquisition using read_timed() is ongoing, `False`
332+
otherwise.
333+
334+
.. method deinit()
335+
336+
Deinitialize as ADC object and release the resources used by it, especially the ADC
337+
channel and the timer used for read_timed().
270338

271-
The DAC class provides a fast digital to analog conversion. Usage example::
339+
340+
DAC (digital to analogue conversion)
341+
------------------------------------
342+
343+
DAC Constructor
344+
```````````````
345+
346+
.. class:: DAC(id, *, vref=3, callback=None)
347+
:noindex:
348+
349+
350+
The DAC class provides a fast digital to analogue conversion. Usage example::
272351

273352
from machine import DAC
274353

275-
dac0 = DAC(0) # create DAC object on DAC pin A0
276-
dac0.write(1023) # write value, 0-4095 across voltage range 0.0v - 3.3v
277-
dac1 = DAC(1) # create DAC object on DAC pin A1
278-
dac1.write(2000) # write value, 0-4095 across voltage range 0.0v - 3.3v
354+
dac0 = DAC(0) # create DAC object on DAC pin A0
355+
dac0.write(1023) # write value, 0-4095 across voltage range 0.0V - 3.3V
356+
dac1 = DAC(1) # create DAC object on DAC pin A1
357+
dac1.write(2000) # write value, 0-4095 across voltage range 0.0V - 3.3V
279358

280359
The resolution of the DAC is 12 bit for SAMD51 and 10 bit for SAMD21. SAMD21 devices
281-
have 1 DAC channel at GPIO PA02, SAMD51 devices have 2 DAC channels at GPIO PA02 and PA05.
360+
have 1 DAC channel at GPIO PA02, accepting only 0 as id. SAMD51 devices have
361+
2 DAC channels at GPIO PA02 and PA05 with values 0 and 1 for the id.
362+
The vref arguments defines the output voltage range, the callback option is used for
363+
dac_timed(). Suitable values for vref are:
364+
365+
==== ============================ ================================
366+
vref SAMD21 SAMD51
367+
==== ============================ ================================
368+
0 Internal voltage reference Internal bandgap reference (~1V)
369+
1 Analogue voltage supply Analogue voltage supply
370+
2 External reference Unbuffered external reference
371+
3 - Buffered external reference
372+
==== ============================ ================================
373+
374+
375+
DAC Methods
376+
```````````
377+
378+
.. method:: write(value)
379+
380+
Write a single value to the selected DAC output. The value range is 0-1023 for
381+
SAMD21 and 0-4095 for SAMD51. The voltage range depends on the vref setting.
382+
383+
.. method:: write_timed(data, freq [, count=1])
384+
385+
The call to dac_timed() allows to output a series of analogue values at a given rate.
386+
data must be a buffer with 16 bit values in the range of the DAC (10 bit of 12 bit).
387+
freq may have a range of 1Hz to ~200kHz for SAMD21 and 1 Hz to ~500kHz for SAMD51.
388+
The optional argument count specifies, how often data output will be repeated. The
389+
range is 1 - 2**32. If count == 0, the data output will be repeated until stopped
390+
by a call to deinit(). If the data has been outout count times, a callback will
391+
be called, if given.
392+
393+
Example::
394+
395+
from machine import DAC
396+
from array import array
397+
398+
data = array("H", [i for i in range(0, 4096, 256)]) # create a step sequence
399+
400+
def done(dac_o):
401+
print("Sequence done at", dac_o)
402+
403+
dac = DAC(0, callback=done)
404+
dac.write_timed(data, 1000, 10) # output data 10 times at a rate of 1000 values/s
405+
# and call done() when finished.
406+
407+
The data transfer is done by DMA and not affected by python code execution.
408+
It is possible to restart dac.write_timed() in the callback function with changed
409+
parameters.
410+
411+
412+
.. method:: busy()
413+
:noindex:
414+
415+
Tell, whether a write_timed() activity is ongoing. It returns `True` if yes, `False`
416+
otherwise.
417+
418+
419+
.. method:: deinit()
420+
421+
Deinitialize the DAC and release the resources used by it, especially the DMA channel
422+
and the Timer. On most SAMD21 boards, there is just one timer available for
423+
dac.write_timed() and adc.read_timed_into(). So they cannot run both at the same time,
424+
and releasing the timer may be important. The DAC driver consumes a substantial amount
425+
of current. deinit() will reduce that as well.
282426

283427
Software SPI bus
284428
----------------

0 commit comments

Comments
 (0)