tone-generator/dac.hpp

23 lines
380 B
C++
Raw Normal View History

2024-08-19 22:43:24 +10:00
#pragma once
#include "util.hpp"
namespace dac {
constexpr int BITMASK_B = 0x07;
constexpr int BITMASK_D = 0xf8;
inline void init() {
DDRB |= BITMASK_B;
DDRD |= BITMASK_D;
}
inline void set(float v_f) {
uint8_t v = clamp((int)(v_f * -128) + 127, 0, 255);
PORTB = (PORTB & ~BITMASK_B) | (v & BITMASK_B);
PORTD = (PORTD & ~BITMASK_D) | (v & BITMASK_D);
}
};