# Using DAC on STM32

### GPIO Configuration

The DAC module does not require much config. Enable the peripheral and configure the output to the desired pins.

### Start the DAC

```c
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
```

### Update DAC value

```c
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_8B_R, 100);
```

### Example program

an example program to generate triangle wave on output.

```c
void APP_init() {
  HAL_DAC_Start(&hdac, DAC_CHANNEL_1);

  uint8_t value = 0;
  while (1) {
    HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_8B_R, value);
    value += 1;
  }
}
```

<figure><img src="/files/23HJy9jSxYtbUSjp3G4D" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tk233.gitbook.io/notes/stm32/analog/dac.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
