Krimskrams: STM32G431 and DAC
Posted: Wed Jul 03, 2024 12:50 pm
Odds & Ends: various things of different types, usually small and not important, or of little value.
In german language "Krimskrams".
With 128 kB Flash and 32 RAM STM32G431 is not a memory-monster. But it has DACs and more important, I have a board at home.
I was curious, if sending signals by DAC can be done with STM32duino.
On an Arduino UNO analogWrite() will generate a PWM-signal or simply LOW/HIGH, if pin has no PWM-capability. Arduino Due, Zero and MKR-boards have a DAC and can generate a real analog signal.
So you can do with a STM32 MCUs:
As info: Sketch uses 32460 bytes (24%) of program storage space. Maximum is 131072 bytes.
Global variables use 1496 bytes (4%) of dynamic memory, leaving 31272 bytes for
local variables. Maximum is 32768 bytes.
You don’t need to define DAC-pin as output, simply analogWrite() is enough.
But frequency is really low - only 15,8 Hz!
You can find function inside wiring_analog.c
I expected commands for port-manipulations inside, but there are only several checks / function calls, if pin number is inside list, if pin is cofigured correctly and if value is inside range, before a function dac_write_value() is called.
Another try with a sawtooth signal:
Frequency is still low, so it is not the sinus-function slowing down.
In german language "Krimskrams".
With 128 kB Flash and 32 RAM STM32G431 is not a memory-monster. But it has DACs and more important, I have a board at home.

On an Arduino UNO analogWrite() will generate a PWM-signal or simply LOW/HIGH, if pin has no PWM-capability. Arduino Due, Zero and MKR-boards have a DAC and can generate a real analog signal.
So you can do with a STM32 MCUs:
Code: Select all
//--------------------------------------------------------
// Board: STM32G431CBU6 (WeactStudio "long type")
//
// https://github.com/stm32duino/Arduino_Core_STM32/blob/main/variants/STM32G4xx/G431C(6-8-B)U_G441CBU/PeripheralPins.c
// https://github.com/stm32duino/Arduino_Core_STM32/blob/main/variants/STM32G4xx/G431C(6-8-B)U_G441CBU/variant_generic.h
// DAC: PA4, PA5
// UART1: PA3 (RX), PA2 (TX)
//--------------------------------------------------------
#define LED_BUILTIN PC6 // LED is connceted to GND, so it glows with HIGH-level
#define DAC1_1 PA4
#define DAC1_2 PA5
uint16_t x = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(19200);
delay(2000);
Serial.printf("\n\n ----- DAC-Test on STM32G431CBU6 ----- \n");
analogWriteResolution(12); // set resolution to 12 bits
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
analogWrite(DAC1_1, 2048 + 2047*sin(x));
x += 0.2;
if (x > 6.28) // sin-function takes parameter as radian
x = 0;
}
Global variables use 1496 bytes (4%) of dynamic memory, leaving 31272 bytes for
local variables. Maximum is 32768 bytes.
You don’t need to define DAC-pin as output, simply analogWrite() is enough.
But frequency is really low - only 15,8 Hz!
You can find function inside wiring_analog.c
I expected commands for port-manipulations inside, but there are only several checks / function calls, if pin number is inside list, if pin is cofigured correctly and if value is inside range, before a function dac_write_value() is called.
Another try with a sawtooth signal:
Code: Select all
void loop() {
analogWrite(DAC1_1, x);
x += 100;
if (x > 4050)
x = 0;
}
Frequency is still low, so it is not the sinus-function slowing down.