Skip to content

About

STM32 embedded C coursework on the STM32L476 (Nucleo): DAC output driver. Cal Poly embedded systems assignment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Assignment 5 — SPI DAC (MCP4921) on STM32L476RG

CPE 3160. Bare-metal, register-level driver for the Microchip MCP4921 12-bit SPI DAC on an STM32L476RG (Nucleo-L476RG). No STM32 HAL is used for the DAC logic — the driver talks to SPI1 and GPIOA directly through the CMSIS register definitions in stm32l476xx.h.

The program generates a square wave on the DAC output, alternating between ~1.0 V and ~2.0 V with a 25% high duty cycle.

Project layout

a5/
├── Core/
│   ├── Inc/
│   │   ├── DAC.h              # DAC driver interface (pins, command word, prototypes)
│   │   └── main.h             # CubeMX-generated app header
│   └── Src/
│       ├── DAC.c              # DAC_init / DAC_write / DAC_volt_conv (register-level)
│       ├── main.c             # square-wave demo using the DAC library
│       └── ...                # CubeMX-generated support (startup, syscalls, IT, system)
├── Drivers/                   # CMSIS + STM32L4xx HAL driver sources (support code)
├── cmake/                     # toolchain files + CubeMX CMake integration
├── CMakeLists.txt             # top-level build (DAC.c registered as a user source)
├── CMakePresets.json          # Debug / Release presets (Ninja + arm-none-eabi)
└── a5.ioc                     # STM32CubeMX project file

Driver API (DAC.h)

Function Purpose
DAC_init(void) Configure GPIOA + SPI1 (master, mode 0, MSB-first, 8-bit, BR /16)
DAC_write(uint16_t code) Send one 16-bit MCP4921 command word with software chip-select framing
DAC_volt_conv(uint32_t mv) Convert millivolts to a 12-bit DAC code (integer math, clamped to 4095)
DAC_build_word(uint16_t code) Pure helper that builds the command word 0x3000 | (code & 0x0FFF)

Wiring (3.3 V only)

STM32 Pin Signal MCP4921 Pin
PA4 Chip Select (GPIO output, active low) 2 (CS)
PA5 SPI1_SCK (AF5) 3 (SCK)
PA7 SPI1_MOSI (AF5) 4 (SDI)

MCP4921 power/reference: VDD (1) → 3.3 V, VREF (6) → 3.3 V, VSS (5) → GND, LDAC (7) → GND, VOUTA (8) → scope/DMM. STM32 and MCP4921 must share a common ground.

VREF must be 3.3 V — the driver hardcodes DAC_VREF_MV = 3300, so the output scaling depends on it.

Building

The project builds with CMake + Ninja and the arm-none-eabi GCC toolchain (as set up by STM32CubeIDE / STM32 for VS Code).

cmake --preset Debug
cmake --build --preset Debug

The firmware image is produced under build/Debug/.

Notes

  • The build/ directory is git-ignored; it is regenerated by the build.
  • SPI transaction (per DAC_write): CS low → high byte → low byte → wait TXE/BSY → CS high. The MCP4921 latches the new value on the CS rising edge (LDAC tied low).

Academic honesty & licensing

Coursework (CPE 3160) from an embedded systems course at Cal Poly, San Luis Obispo. The DAC.c/.h driver and main.c demo are my own work. The project also bundles STMicroelectronics' STM32L4xx HAL/CMSIS drivers and CubeMX-generated files, which are under ST's own license terms and are not mine to relicense.

Shared as a learning reference only. It is intentionally not released under an open-source license, and you should not submit it as your own work for a graded assignment.

About

STM32 embedded C coursework on the STM32L476 (Nucleo): DAC output driver. Cal Poly embedded systems assignment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages