Dein Warenkorb ist gerade leer!
STM32 – Bibliothekensammlung
MCP4728 .c/.h (DAC)
------------------------------------------------------------------
mcp4728.h:
------------------------------------------------------------------
#ifndef MCP4728_H
#define MCP4728_H
#include "stm32h7xx_hal.h"
typedef struct {
I2C_HandleTypeDef *hi2c;
uint8_t address; // Standard: 0x60
} MCP4728_HandleTypeDef;
typedef enum {
MCP4728_CHANNEL_A = 0,
MCP4728_CHANNEL_B,
MCP4728_CHANNEL_C,
MCP4728_CHANNEL_D
} MCP4728_Channel;
HAL_StatusTypeDef MCP4728_WriteChannel(MCP4728_HandleTypeDef *hmcp, MCP4728_Channel channel, uint16_t value); // 12-bit
#endif // MCP4728_H
------------------------------------------------------------------
mcp4728.c:
------------------------------------------------------------------
#include "mcp4728.h"
HAL_StatusTypeDef MCP4728_WriteChannel(MCP4728_HandleTypeDef *hmcp, MCP4728_Channel channel, uint16_t value) {
if (channel > MCP4728_CHANNEL_D || value > 0x0FFF) return HAL_ERROR;
uint8_t command = 0x40 | (channel << 1); // Fast write
uint8_t data[3];
data[0] = command;
data[1] = (value >> 4) & 0xFF;
data[2] = (value << 4) & 0xF0;
return HAL_I2C_Master_Transmit(hmcp->hi2c, hmcp->address << 1, data, 3, HAL_MAX_DELAY);
}
------------------------------------------------------------------
Anwendungsbeispiel:
------------------------------------------------------------------
...
extern I2C_HandleTypeDef hi2c1;
void Test_MCP4728(void) {
MCP4728_HandleTypeDef dac = {
.hi2c = &hi2c1,
.address = 0x60 // Standardadresse
};
// Setze Kanal A auf halbe Spannung
MCP4728_WriteChannel(&dac, MCP4728_CHANNEL_A, 2048);
// Erhöhe langsam Kanal B
for (uint16_t i = 0; i < 4096; i += 256) {
MCP4728_WriteChannel(&dac, MCP4728_CHANNEL_B, i);
HAL_Delay(100);
}
}
...
Tags:

Schreibe einen Kommentar