reworked adc dac i2c

This commit is contained in:
gijsio
2020-08-12 17:12:12 +02:00
parent 5d480314e0
commit 75581df556
3 changed files with 44 additions and 50 deletions

View File

@@ -11,7 +11,6 @@ aliases:
### Quick Usage Example
```python
import machine
adc = machine.ADC() # create an ADC object
@@ -19,81 +18,76 @@ apin = adc.channel(pin='P16') # create an analog pin on P16
val = apin() # read an analog value
```
### Constructors
## Constructors
#### class machine.ADC(id=0)
### class machine.ADC([id=0])
Create an ADC object; associate a channel with a pin. For more info check the hardware section.
### Methods
## Methods
#### adc.init( \* , bits=12)
### adc.init([bits=12])
Enable the ADC block. This method is automatically called on object creation.
* `Bits` can take values between 9 and 12 and selects the number of bits of resolution of the ADC block.
#### adc.deinit()
### adc.deinit()
Disable the ADC block.
#### adc.channel(\* , pin, attn=ADC.ATTN\_0DB)
### adc.channel(\* , pin, attn=ADC.ATTN_0DB)
Create an analog pin.
* `pin` is a keyword-only string argument. Valid pins are `P13` to `P20`.
* `attn` is the attenuation level. The supported values are: `ADC.ATTN_0DB`, `ADC.ATTN_2_5DB`, `ADC.ATTN_6DB`, `ADC.ATTN_11DB`
Returns an instance of `ADCChannel`. Example:
Returns an instance of `ADCChannel`.
```python
# enable an ADC channel on P16
apin = adc.channel(pin='P16')
```
#### adc.vref(vref)
### adc.vref(vref)
If called without any arguments, this function returns the current calibrated voltage (in millivolts) of the `1.1v` reference. Otherwise it will update the calibrated value (in millivolts) of the internal `1.1v` reference.
#### adc.vref\_to\_pin(pin)
### adc.vref_to_pin(pin)
Connects the internal `1.1v` to external `GPIO`. It can only be connected to `P22`, `P21` or `P6`. It is recommended to only use `P6` on the WiPy, on other modules this pin is connected to the radio.
### Constants
## Constants
* ADC channel attenuation values: `ADC.ATTN_0DB`, `ADC.ATTN_2_5DB`, `ADC.ATTN_6DB`, `ADC.ATTN_11DB`
* ADC channel attenuation values: (value references are approximations)
* `ADC.ATTN_0DB`: 0dB attenuation. 1V will be registered as 1V
* `ADC.ATTN_2_5DB`: 2.5dB attenuation. 1V will be registered as 0.75V
* `ADC.ATTN_6DB`: 6dB attenuation. 1V will be registered as 0.5V
* `ADC.ATTN_11DB`: 11dB attenuation. 1V will be registered as 0.3V
## class ADCChannel
>Note: The voltages will automatically be corrected by `adcchannel.voltage()`
Read analog values from internal/external sources. ADC channels can be connected to internal points of the `MCU` or to `GPIO` pins. ADC channels are created using the `ADC.channel` method.
### Methods
The following methods can be applied on the `adcchannel()` instance. ADC channels are created using the `ADC.channel` method.
#### adcchannel()
### adcchannel()
Fast method to read the channel value.
#### adcchannel.value()
### adcchannel.value()
Read the channel value.
#### adcchannel.init()
### adcchannel.init()
(Re)init and enable the ADC channel. This method is automatically called on object creation.
#### adcchannel.deinit()
### adcchannel.deinit()
Disable the ADC channel.
#### adcchannel.voltage()
### adcchannel.voltage()
Reads the channels value and converts it into a voltage (in millivolts)
#### adcchannel.value\_to\_voltage(value)
### adcchannel.value_to_voltage(value)
Converts the provided value into a voltage (in millivolts) in the same way voltage does.
{{% hint style="danger" %}}
ADC pin input range is `0-1.1V`. This maximum value can be increased up to `3.3V` using the highest attenuation of `11dB`. **Do not exceed the maximum of 3.3V**, to avoid damaging the device.
{{% /hint %}}
> ADC pin input range is `0-1.1V`. This maximum value can be increased up to `3.3V` using the highest attenuation of `11dB`. **Do not exceed the maximum of 3.3V**, to avoid damaging the device.

View File

@@ -23,30 +23,31 @@ dac_tone.tone(1000, 0) # set tone output to 1kHz
## Constructors
#### class class machine.DAC(pin)
### class class machine.DAC(pin)
Create a DAC object, that will let you associate a channel with a `pin`. `pin` can be a string argument.
## Methods
#### dac.init()
### dac.init()
Enable the DAC block. This method is automatically called on object creation.
#### dac.deinit()
### dac.deinit()
Disable the DAC block.
#### dac.write(value)
### dac.write(value)
Set the DC level for a DAC pin. `value` is a float argument, with values between 0 and 1.
#### dac.tone(frequency, amplitude)
### dac.tone(frequency, amplitude)
Sets up tone signal to the specified `frequency` at `amplitude` scale. `frequency` can be from `125Hz` to `20kHz` in steps of `122Hz`. `amplitude` is an integer specifying the tone amplitude to write the DAC pin. Amplitude value represents:
* `0` is 0dBV (~ 3Vpp at 600 Ohm load)
* `1` is -6dBV (~1.5 Vpp), `2` is -12dBV (~0.8 Vpp)
* `1` is -6dBV (~1.5 Vpp),
* `2` is -12dBV (~0.8 Vpp)
* `3` is -18dBV (~0.4 Vpp).
The generated signal is a sine wave with an DC offset of VDD/2.

View File

@@ -73,61 +73,60 @@ i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x
## Constructors
#### class machine.I2C(bus, ...)
### class machine.I2C([bus=0], ...)
Construct an I2C object on the given `bus`. `bus` can only be `0, 1, 2`. If the `bus` is not given, the default one will be selected (`0`). Buses `0` and `1` use the ESP32 I2C hardware peripheral while bus `2` is implemented with a bit-banged software driver.
## Methods
## General Methods
### General Methods
#### i2c.init(mode, \* , baudrate=100000, pins=(SDA, SCL))
### i2c.init([mode=I2C.MASTER, baudrate=100000, pins=(SDA='P9', SCL='P10)])
Initialise the I2C bus with the given parameters:
* `mode` must be I2C.MASTER
* `baudrate` is the SCL clock rate
* pins is an optional tuple with the pins to assign to the I2C bus. The default I2C pins are `P9` (SDA) and `P10` (SCL)
* `pins` is an optional tuple with the pins to assign to the I2C bus. The default I2C pins are `P9` (SDA) and `P10` (SCL)
#### i2c.scan()
### i2c.scan()
Scan all I2C addresses between `0x08` and `0x77` inclusive and return a list of those that respond. A device responds if it pulls the SDA line low after its address (including a read bit) is sent on the bus.
### Standard Bus Operations
## Standard Bus Operations
The following methods implement the standard I2C master read and write operations that target a given slave device.
#### i2c.readfrom(addr, nbytes)
### i2c.readfrom(addr, nbytes)
Read `nbytes` from the slave specified by `addr`. Returns a bytes object with the data read.
#### i2c.readfrom\_into(addr, buf)
### i2c.readfrom_into(addr, buf)
Read into `buf` from the slave specified by `addr`. The number of bytes read will be the length of `buf`.
Return value is the number of bytes read.
#### i2c.writeto(addr, buf, \* , stop=True)
### i2c.writeto(addr, buf, * , [stop=True])
Write the bytes from `buf` to the slave specified by `addr`. The argument `buf` can also be an integer which will be treated as a single byte. If `stop` is set to `False` then the stop condition won't be sent and the I2C operation may be continued (typically with a read transaction).
Return value is the number of bytes written.
### Memory Operations
## Memory Operations
Some I2C devices act as a memory device (or set of registers) that can be read from and written to. In this case there are two addresses associated with an I2C transaction: the slave address and the memory address. The following methods are convenience functions to communicate with such devices.
#### i2c.readfrom\_mem(addr, memaddr, nbytes, \*, addrsize=8)
### i2c.readfrom_mem(addr, memaddr, nbytes, [addrsize=8])
Read `nbytes` from the slave specified by `addr` starting from the memory address specified by `memaddr`. The `addrsize` argument is specified in bits and it can only take 8 or 16.
#### i2c.readfrom\_mem\_into(addr, memaddr, buf, \*, addrsize=8)
### i2c.readfrom_mem_into(addr, memaddr, buf, *, [addrsize=8])
Read into `buf` from the slave specified by `addr` starting from the memory address specified by `memaddr`. The number of bytes read is the length of `buf`. The `addrsize` argument is specified in bits and it can only take 8 or 16.
The return value is the number of bytes read.
#### i2c.writeto\_mem(addr, memaddr, buf \*, addrsize=8)
### i2c.writeto_mem(addr, memaddr, buf, [addrsize=8])
Write `buf` to the slave specified by `addr` starting from the memory address specified by `memaddr`. The argument `buf` can also be an integer which will be treated as a single byte. The `addrsize` argument is specified in bits and it can only take 8 or 16.