STM32 core 3.0.1-dev
Arduino core for STM32 based boards: API reference
← STM32duino documentation
Loading...
Searching...
No Matches
wiring_analog.c File Reference

Analog input (ADC) and analog output (PWM or DAC) functions. More...

#include "Arduino.h"
#include "PinConfigured.h"

Macros

#define MAX_PWM_RESOLUTION   16
 
#define MAX_ADC_RESOLUTION   12
 

Functions

static uint32_t mapResolution (uint32_t value, uint32_t from, uint32_t to)
 Map a value from one resolution to another.
 
void analogReadResolution (int res)
 Set the resolution of analogRead return values.
 
void analogReference (uint8_t mode)
 Set the analog reference voltage.
 
int analogRead (pin_size_t pinNumber)
 Read the analog value of a pin, scaled to the resolution set by analogReadResolution().
 
void analogWriteResolution (int res)
 Set the resolution of analogWrite parameters.
 
void analogWriteFrequency (uint32_t freq)
 Set the frequency of analogWrite.
 
void analogWrite (pin_size_t pinNumber, int value)
 Write an analog value to a pin, using PWM or DAC if the pin supports it.
 

Variables

uint32_t g_anOutputPinConfigured [MAX_NB_PORT] = {0}
 
static int _writeResolution = PWM_RESOLUTION
 
static int _internalWriteResolution
 
static uint32_t _writeFreq = PWM_FREQUENCY
 
static int _readResolution = ADC_RESOLUTION
 
static int _internalReadResolution
 

Detailed Description

Analog input (ADC) and analog output (PWM or DAC) functions.

Function Documentation

◆ analogRead()

int analogRead ( pin_size_t  pinNumber)

Read the analog value of a pin, scaled to the resolution set by analogReadResolution().

The initialization of the analog pin is done through this function.

Parameters
pinNumberAnalog pin number, e.g. A0 or PA0.
Returns
Converted value, between 0 and 2^resolution - 1. 0 if the pin has no ADC.

◆ analogReadResolution()

void analogReadResolution ( int  res)

Set the resolution of analogRead return values.

Default is 10 bits (range from 0 to 1023).

Note
The ADC is configured with the closest supported hardware resolution (capped to the maximum of the ADC) and the result is scaled to the requested resolution. Values outside the range 1 to 32 call Error_Handler().
See also
analogRead(), analogWriteResolution()
Parameters
resNumber of bits of the resolution, from 1 to 32.

◆ analogReference()

void analogReference ( uint8_t  mode)

Set the analog reference voltage.

Not supported on this core, the call has no effect.

Parameters
modeReference mode, ignored.

◆ analogWrite()

void analogWrite ( pin_size_t  pinNumber,
int  value 
)

Write an analog value to a pin, using PWM or DAC if the pin supports it.

Output only works on the pins with Hardware Timer (PWM) or DAC support. For the other pins, it defaults to digital output: the value is scaled to 8 bits and the pin is set HIGH if it is 128 or more, LOW otherwise. The DAC takes precedence over PWM when a pin supports both.

Parameters
pinNumberPin number to write to.
valueValue to write, scaled to the resolution set by analogWriteResolution().

◆ analogWriteFrequency()

void analogWriteFrequency ( uint32_t  freq)

Set the frequency of analogWrite.

Default is PWM_FREQUENCY (1000) in Hertz.

See also
analogWrite(), analogWriteResolution()
Parameters
freqPWM frequency in Hertz.

◆ analogWriteResolution()

void analogWriteResolution ( int  res)

Set the resolution of analogWrite parameters.

Default is 8 bits (range from 0 to 255).

Note
The PWM hardware resolution is capped to 16 bits and the value is scaled accordingly. Values outside the range 1 to 32 call Error_Handler().
See also
analogWrite(), analogReadResolution(), analogWriteFrequency()
Parameters
resNumber of bits of the resolution, from 1 to 32.

◆ mapResolution()

static uint32_t mapResolution ( uint32_t  value,
uint32_t  from,
uint32_t  to 
)
inlinestatic

Map a value from one resolution to another.

Parameters
valueValue to convert.
fromResolution of value, in bits.
toTarget resolution, in bits.
Returns
Value scaled to the target resolution.

Variable Documentation

◆ _internalReadResolution

int _internalReadResolution
static
Initial value:
=
12

◆ _internalWriteResolution

int _internalWriteResolution
static
Initial value:
=
#define PWM_RESOLUTION
Default resolution of analogWrite() values, in bits.
Definition pins_arduino.h:298