DACライブラリ。シンプルすぎてライブラリにする必要もなさそうな気もするが...
【サンプルコード (Microchip Studio)】
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include "avr8_dac.h" Dac dac0(DAC0); int main(void) { #if defined(__AVR_1__) dac0.begin(Vref::DACREF_2V5); #else dac0.begin(Vref::DACREF_2V500); #endif /* DAC Output 0% */ dac0.data(0); /* DAC Output 50% */ dac0.data(dac0.mask() >> 1); /* DAC Output 100% */ dac0.data(dac0.mask()); while (1) continue; return 0; } |
【ライブラリ】
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
/* avr8_dac.h - DAC Driver for Microchip AVR8 Series Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include "avr8_config.h" #include "avr8_vref.h" #if defined(DAC_ENABLE_bm) #if !defined(DAC_DATA_gm) #define DAC_DATA_gm 0xFF #define DAC_DATA_gp 0 #endif class Dac { public: #if defined(DAC_OUTRANGE_gm) /* Output Buffer Range select */ typedef enum { OUTRANGE_AUTO = DAC_OUTRANGE_AUTO_gc, /* Output buffer automatically choose best range */ OUTRANGE_LOW = DAC_OUTRANGE_LOW_gc, /* Output buffer configured to low range */ OUTRANGE_HIGH = DAC_OUTRANGE_HIGH_gc, /* Output buffer configured to high range */ OUTRANGE_DEFAULT = OUTRANGE_AUTO, } OUTRANGE; #endif Dac(DAC_t& dac) : _dac(&dac), _ch((&dac - &DAC0) / sizeof(dac)) { } /* virtual */ ~Dac(void) { end(); } void begin(Vref::DACREF refsel, bool alwayson = false) { end(); Vref::dac(_ch, refsel, alwayson); _dac->DATA = 0; _dac->CTRLA = DAC_ENABLE_bm; } void end(void) { _dac->CTRLA = 0; } void runstdby(bool enable = true) { _dac->CTRLA = (_dac->CTRLA & ~DAC_RUNSTDBY_bm) | (enable ? DAC_RUNSTDBY_bm : 0); } #if defined(DAC_OUTRANGE_gm) void output(bool enable = true, OUTRANGE outrange = OUTRANGE_AUTO) { _dac->CTRLA = (_dac->CTRLA & ~(DAC_OUTEN_bm | DAC_OUTRANGE_gm)) | (enable ? DAC_OUTEN_bm : 0) | outrange; } #else void output(bool enable = true) { _dac->CTRLA = (_dac->CTRLA & ~DAC_OUTEN_bm) | (enable ? DAC_OUTEN_bm : 0); } #endif void data(uint16_t value) { _dac->DATA = value; } uint16_t data(void) { return _dac->DATA; } uint16_t mask(void) { return DAC_DATA_gm; } protected: DAC_t *_dac; uint8_t _ch; }; #endif |
