ADC (adc)
Rounds its input to a set number of bits, with optional sampling.
Group: Control · Kind: {"block":"adc","kind":"control"}
When to use it
Use it to see how converter resolution and sample rate affect a current or voltage loop. Use Sample and Hold when only the sampling matters.
Pins
| Pin | Domain | Meaning |
|---|---|---|
in |
signal | Signal to convert, kept between min and min plus range |
out |
signal | Rounded value from the last sample |
Common parameters
bits: Number of bits in the conversion (Default: 12)range: Full input span the converter covers (Default: required)min: Input value that gives code zero (Default: 0)fs: How often the input is sampled (Default: 0)
Parameters
| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
bits |
— | 12 | 8 – 16 | Number of bits in the conversion |
range |
— | required | Full input span the converter covers | |
min |
— | 0 | Input value that gives code zero | |
fs |
Hz | 0 | How often the input is sampled |
Parameter descriptions are in English in both languages.
Model
The step size is lsb = range/2^bits. The output is min + round((in - min)/lsb)*lsb, with the code limited to 0 ... 2^bits - 1, so the largest output is min + range - lsb. With fs = 0 the block converts all the time and has direct feedthrough. With fs above 0 it converts every 1/fs seconds and holds the code in between, and it has no feedthrough. Noise, offset, gain error and conversion delay are not modelled.
Common mistakes
- range has no default and must be positive, so the build stops without it. bits must be a whole number from 1 to 24, and for plus or minus 20 A you use min = -20 and range = 40.
- An input beyond the range is cut off without a warning, which looks like a saturated sensor. With fs above 0 the ADC adds a sample delay, so match fs to the controller period and keep the maximum step well below the sample period.
Problems rules: compile.error
Datasheet and sources
The value of bits comes from the ADC resolution in the MCU datasheet, for example 12. The values of range and min come from the input span of the ADC, and fs is the conversion rate you set in the firmware.
Example
Open the example with the command file.openExample and the name demo:adc.
Generated by cargo xtask reference from the usage texts and parameter specs. Do not edit by hand.