Last Updated on 2026-03-07 by researcher
TCEは16ビットカウンターで動作し高分解能(最大160MHz)やWEXと連携できる最大4CHの出力が特徴。
【サンプル】
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include "avr8_tce.h" void sample(void) { /* 高分解能の指定方法 */ Tce::begin(..., Tce::HREN_OFF); /* default */ Tce::begin(..., Tce::HREN_4X); /* 80MHz (CONFIG_CLOCK_TCE_HREN=1の設定必要) */ Tce::begin(..., Tce::HREN_8X); /* 160Mz (CONFIG_CLOCK_TCE_HREN=1の設定必要) */ /* 周波数生成 */ Tce::begin(1000000, Tce::WGMODE_FRQ, Tce::HREN_OFF); Tce::output(Tce::CMP0); Tce::run(); /* PWM */ Tce::begin(100000, Tce::WGMODE_SINGLESLOPE, Tce::HREN_OFF); Tce::output(Tce::CMP0); Tce::pwmduty(Tce::CMP0, Tce::PWM_MAX_COUNT >> 1); /* Duty = 50% */ Tce::run(); } |
【ライブラリ】
|
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
/* avr8_tce.h - TCE Driver for Microchip AVR8 Series Copyright (c) 2026 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_clock.h" #if defined(TCE0) class Tce { public: static const uint16_t PWM_MAX_COUNT; /* CMP */ typedef enum { CMP0, CMP1, CMP2, CMP3, } CMP; /* Clock Selection */ typedef enum { CLKSEL_DIV1 = TCE_CLKSEL_DIV1_gc, /* System Clock */ CLKSEL_DIV2 = TCE_CLKSEL_DIV2_gc, /* System Clock / 2 */ CLKSEL_DIV4 = TCE_CLKSEL_DIV4_gc, /* System Clock / 4 */ CLKSEL_DIV8 = TCE_CLKSEL_DIV8_gc, /* System Clock / 8 */ CLKSEL_DIV16 = TCE_CLKSEL_DIV16_gc, /* System Clock / 16 */ CLKSEL_DIV64 = TCE_CLKSEL_DIV64_gc, /* System Clock / 64 */ CLKSEL_DIV256 = TCE_CLKSEL_DIV256_gc, /* System Clock / 256 */ CLKSEL_DIV1024 = TCE_CLKSEL_DIV1024_gc /* System Clock / 1024 */ } CLKSEL; /* Waveform generation mode select */ typedef enum { WGMODE_NORMAL = TCE_WGMODE_NORMAL_gc, /* Normal Mode */ WGMODE_FRQ = TCE_WGMODE_FRQ_gc, /* Frequency Generation Mode */ WGMODE_SINGLESLOPE = TCE_WGMODE_SINGLESLOPE_gc, /* Single Slope PWM */ WGMODE_DSTOP = TCE_WGMODE_DSTOP_gc, /* Dual Slope PWM, overflow on TOP */ WGMODE_DSBOTH = TCE_WGMODE_DSBOTH_gc, /* Dual Slope PWM, overflow on TOP and BOTTOM */ WGMODE_DSBOTTOM = TCE_WGMODE_DSBOTTOM_gc /* Dual Slope PWM, overflow on BOTTOM */ } WGMODE; /* Compare # Event select */ typedef enum { CMP0EV_PULSE = TCE_CMP0EV_PULSE_gc, /* Event output for CMP is a pulse */ CMP0EV_WAVEFORM = TCE_CMP0EV_WAVEFORM_gc /* Event output for CMP is equal to waveform */ } CMP0EV; /* Compare # Event select */ typedef enum { CMP1EV_PULSE = TCE_CMP1EV_PULSE_gc, /* Event output for CMP is a pulse */ CMP1EV_WAVEFORM = TCE_CMP1EV_WAVEFORM_gc /* Event output for CMP is equal to waveform */ } CMP1EV; /* Compare # Event select */ typedef enum { CMP2EV_PULSE = TCE_CMP2EV_PULSE_gc, /* Event output for CMP is a pulse */ CMP2EV_WAVEFORM = TCE_CMP2EV_WAVEFORM_gc /* Event output for CMP is equal to waveform */ } CMP2EV; /* Compare # Event select */ typedef enum { CMP3EV_PULSE = TCE_CMP3EV_PULSE_gc, /* Event output for CMP is a pulse */ CMP3EV_WAVEFORM = TCE_CMP3EV_WAVEFORM_gc /* Event output for CMP is equal to waveform */ } CMP3EV; /* Direction select */ typedef enum TCE_DIR_enum { DIR_UP = TCE_DIR_UP_gc, /* Count up */ DIR_DOWN = TCE_DIR_DOWN_gc /* Count down */ } DIR; /* Event Action A select */ typedef enum { EVACTA_CNT_POSEDGE = TCE_EVACTA_CNT_POSEDGE_gc, /* Count on positive edge event */ EVACTA_CNT_ANYEDGE = TCE_EVACTA_CNT_ANYEDGE_gc, /* Count on any edge event */ EVACTA_CNT_HIGHLVL = TCE_EVACTA_CNT_HIGHLVL_gc, /* Count on prescaled clock while event line is 1. */ EVACTA_CNT_UPDOWN = TCE_EVACTA_UPDOWN_gc /* Count on prescaled clock. Event controls count direction. Up-count when event line is 0, down-count when event line is 1. */ } EVACTA; /* Event Action B select */ typedef enum { EVACTB_NONE = TCE_EVACTB_NONE_gc, /* No Action */ EVACTB_UPDOWN = TCE_EVACTB_UPDOWN_gc, /* Count on prescaled clock. Event controls count direction. Up-count when event line is 0, down-count when event line is 1. */ EVACTB_RESTART_POSEDGE = TCE_EVACTB_RESTART_POSEDGE_gc, /* Restart counter at positive edge event */ EVACTB_RESTART_ANYEDGE = TCE_EVACTB_RESTART_ANYEDGE_gc, /* Restart counter on any edge event */ EVACTB_RESTART_HIGHLVL = TCE_EVACTB_RESTART_HIGHLVL_gc /* Restart counter while event line is 1. */ } EVACTB; /* High Resolution Enable select */ typedef enum { HREN_OFF = TCE_HREN_OFF_gc, /* High Resolution Disable */ HREN_4X = TCE_HREN_4X_gc, /* Resolution increased by 4 (2 bits) */ HREN_8X = TCE_HREN_8X_gc /* Resolution increased by 4 (3 bits) */ } HREN; /* Scaled Write select */ typedef enum { SCALE_NORMAL = TCE_SCALE_NORMAL_gc, /* Absolute values used when writing to CMPn, CMPnBUF and registers */ SCALE_FRACTIONAL = TCE_SCALE_FRACTIONAL_gc /* Fractional values used when writing to CMPn, CMPnBUF and registers */ } SCALE; /* Scaling Mode select */ typedef enum { SCALEMODE_CENTER = TCE_SCALEMODE_CENTER_gc, /* CMPn registers scaled vs center (50% duty cycle) */ SCALEMODE_BOTTOM = TCE_SCALEMODE_BOTTOM_gc, /* CMPn registers scaled vs BOTTOM (0% duty cycle) */ SCALEMODE_TOP = TCE_SCALEMODE_TOP_gc, /* CMPn registers scaled vs TOP (100% duty cycle) */ SCALEMODE_TOPBOTTOM = TCE_SCALEMODE_TOPBOTTOM_gc /* CMPn registers scaled vs TOP or BOTTOM depending on written value. */ } SCALEMODE; /* TCE.INTCTRL bit masks and bit positions */ typedef enum { INTCTRL_OVF = TCE_OVF_bm, /* Overflow Interrupt Enable bit mask. */ INTCTRL_CMP0 = TCE_CMP0_bm, /* Compare 0 Interrupt Enable bit mask. */ INTCTRL_CMP1 = TCE_CMP1_bm, /* Compare 1 Interrupt Enable bit mask. */ INTCTRL_CMP2 = TCE_CMP2_bm, /* Compare 2 Interrupt Enable bit mask. */ INTCTRL_CMP3 = TCE_CMP3_bm, /* Compare 3 Interrupt Enable bit mask. */ } INTCTRL; /* TCE.INTFLAGS bit masks and bit positions */ typedef enum { INTFLAGS_OVF = TCE_OVF_bm, /* Overflow Interrupt Enable bit mask. */ INTFLAGS_CMP0 = TCE_CMP0_bm, /* Compare 0 Interrupt Enable bit mask. */ INTFLAGS_CMP1 = TCE_CMP1_bm, /* Compare 1 Interrupt Enable bit mask. */ INTFLAGS_CMP2 = TCE_CMP2_bm, /* Compare 2 Interrupt Enable bit mask. */ INTFLAGS_CMP3 = TCE_CMP3_bm, /* Compare 3 Interrupt Enable bit mask. */ } INTFLAGS; /* Type of Callback Function */ typedef void (*callback_t)(INTFLAGS flags); Tce(void) { } /* virtual */ ~Tce(void) { } static bool begin(uint32_t frq, WGMODE mode, HREN hren = HREN_OFF) { if (frq) { uint8_t lsb = 0; if (mode == WGMODE_NORMAL) hren = HREN_OFF; else { switch (hren) { case HREN_4X: lsb = 2; break; case HREN_8X: lsb = 3; break; default: break; } } uint32_t clk = Clock::frequency() << lsb; if ((mode == WGMODE_FRQ) || (mode >= WGMODE_DSTOP)) clk >>= 1; uint16_t div = (clk / frq + MAX_COUNT) / (MAX_COUNT + 1UL); for (uint8_t i = 0; i < sizeof(CLKDIV) / sizeof(CLKDIV[0]); ++i) { if (div <= CLKDIV[i]) { begin((CLKSEL)(CLKSEL_DIV1 + (i << TCE_CLKSEL_gp)), mode, clk / CLKDIV[i] / frq - (1 << lsb), hren); return true; } } begin((CLKSEL)TCE_CLKSEL_gm, mode, MAX_COUNT, hren); } return false; } static void begin(CLKSEL clksel, WGMODE wgmode, uint16_t period, HREN hren = HREN_OFF) { end(); TCE0.CTRLA = clksel; TCE0.CTRLB = wgmode; TCE0.CTRLC = 0; TCE0.CTRLD = hren; TCE0.CTRLECLR = TCE_LUPD_bm | TCE_DIR_bm; TCE0.CTRLFCLR = 0xFF; TCE0.EVGENCTRL = 0; TCE0.EVCTRL = 0; TCE0.INTCTRL = 0; TCE0.INTFLAGS = 0xFF; TCE0.DBGCTRL = 0; TCE0.CNT = 0; TCE0.PER = period; TCE0.CMP0 = _compare[CMP0] = period; TCE0.CMP1 = _compare[CMP1] = 0; TCE0.CMP2 = _compare[CMP2] = 0; TCE0.CMP3 = _compare[CMP3] = 0; TCE0.AMP = _amplitude = MAX_DUTY; TCE0.OFFSET = 0; _frequency = 0; _callback = 0; } static void end(void) { TCE0.CTRLA = 0; reset(); } static uint32_t frequency(void) { if (_frequency == 0) _frequency = Clock::frequency() / CLKDIV[(TCE0.CTRLA & TCE_CLKSEL_gm) >> TCE_CLKSEL_gp]; return _frequency; } static void runstdby(bool enable = true) { TCE0.CTRLA = (TCE0.CTRLA & ~TCE_RUNSTDBY_bm) | (enable ? TCE_RUNSTDBY_bm : 0); } static void dbgctrl(bool dbgrun = true) { TCE0.DBGCTRL = dbgrun ? TCE_DBGRUN_bm : 0; } static void alupd(bool enable = true) { TCE0.CTRLB = (TCE0.CTRLB & ~TCE_ALUPD_bm) | (enable ? TCE_ALUPD_bm : 0); } static void output(CMP cmp, bool enable = true, bool invert = false, bool ov = false) { uint8_t enm = TCE_CMP0EN_bm << cmp; uint8_t pom = TCE_CMP0POL_bm << cmp; uint8_t ovm = TCE_CMP0OV_bm << cmp; TCE0.CTRLB = (TCE0.CTRLB & ~enm) | (enable ? enm : 0); TCE0.CTRLC = (TCE0.CTRLC & ~(pom | ovm)) | (invert ? pom : 0) | (ov ? ovm : 0); } static void scale(bool enable = true, bool ampen = true, SCALEMODE mode = SCALEMODE_CENTER) { TCE0.CTRLD = (TCE0.CTRLD & ~(TCE_SCALEMODE_gm | TCE_SCALE_bm | TCE_AMPEN_bm)) | mode | (enable ? TCE_SCALE_bm : 0) | (ampen ? TCE_AMPEN_bm : 0); amp(_amplitude); } static void amp(uint16_t value) { TCE0.AMP = _amplitude = value; pwmduty(CMP0, _compare[CMP0]); pwmduty(CMP1, _compare[CMP1]); pwmduty(CMP2, _compare[CMP2]); pwmduty(CMP3, _compare[CMP3]); } static void lockupd(bool enable = true) { if (enable) TCE0.CTRLESET = TCE_LUPD_bm; else TCE0.CTRLECLR = TCE_LUPD_bm; } static void dir(DIR dir = DIR_DOWN) { if (dir) TCE0.CTRLESET = TCE_DIR_bm; else TCE0.CTRLECLR = TCE_DIR_bm; } static void event(CMP cmp, bool enable = true) { uint8_t mask = TCE_CMP0EV_bm << cmp; TCE0.EVGENCTRL = (TCE0.EVGENCTRL & ~mask) | (enable ? mask : 0); } static void evacta(EVACTA evact = EVACTA_CNT_POSEDGE, bool enable = true) { TCE0.EVCTRL = (TCE0.EVCTRL & ~(TCE_EVACTA_gm | TCE_CNTAEI_bm)) | evact | (enable ? TCE_CNTAEI_bm : 0); } static void evactb(EVACTB evact = EVACTB_NONE, bool enable = true) { TCE0.EVCTRL = (TCE0.EVCTRL & ~(TCE_EVACTB_gm | TCE_CNTBEI_bm)) | evact | (enable ? TCE_CNTBEI_bm : 0); } static uint16_t cnt(void) { return TCE0.CNT; } static void per(uint16_t value) { TCE0.PER = value; amp(_amplitude); } static void pwmduty(CMP cmp, uint16_t value) { if ((TCE0.CTRLB & TCE_WGMODE_gm) >= TCE_WGMODE_SINGLESLOPE_gc) { _compare[cmp] = value; if ((TCE0.CTRLD & (TCE_SCALE_bm | TCE_AMPEN_bm)) == (TCE_SCALE_bm | TCE_AMPEN_bm)) value = (uint32_t)value * MAX_DUTY / PWM_MAX_COUNT; else value = (uint32_t)value * TCE0.PER / PWM_MAX_COUNT; ((register16_t *)&TCE0.CMP0)[cmp] = value; } } static void run(void) { TCE0.CTRLA |= TCE_ENABLE_bm; } static void update(void) { command(CMD_UPDATE); } static void restart(void) { command(CMD_RESTART); } static void reset(void) { command(CMD_RESET); } static void callback(callback_t func) { _callback = func; } static void interrupt(INTCTRL ctrl, bool enable = true) { if (enable) { TCE0.INTFLAGS = ctrl; #if CONFIG_TCE_ISR TCE0.INTCTRL |= ctrl; #endif } else { #if CONFIG_TCE_ISR TCE0.INTCTRL &= ~ctrl; #endif TCE0.INTFLAGS = ctrl; } } static void poll(void) { #if !CONFIG_TCE_ISR isr(); #endif } private: static const uint16_t MAX_COUNT; static const uint16_t MAX_DUTY; static const uint16_t CLKDIV[8]; /* Command select */ typedef enum { CMD_NONE = TCE_CMD_NONE_gc, /* No Command */ CMD_UPDATE = TCE_CMD_UPDATE_gc, /* Force Update */ CMD_RESTART = TCE_CMD_RESTART_gc, /* Force Restart */ CMD_RESET = TCE_CMD_RESET_gc /* Force Hard Reset */ } CMD; static uint16_t _amplitude; static uint16_t _compare[4]; static uint32_t _frequency; static callback_t _callback; #if CONFIG_TCE_ISR friend void tce_isr(void); #endif static inline void isr(void) { uint8_t flags = TCE0_INTFLAGS = TCE0_INTFLAGS; if (flags && _callback) _callback((INTFLAGS)flags); } static void command(CMD cmd = CMD_NONE) { TCE0.CTRLESET = cmd; } }; #endif |
|
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 |
/* avr8_tce.cpp - TCE Driver for Microchip AVR8 Series Copyright (c) 2026 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/>. */ #include "avr8_tce.h" #if defined(TCE0) const uint16_t Tce::MAX_COUNT = 0xFFFF; const uint16_t Tce::PWM_MAX_COUNT = 0xFFFF; const uint16_t Tce::MAX_DUTY = 0x8000; const uint16_t Tce::CLKDIV[] = { 1, 2, 4, 8, 16, 64, 256, 1024 }; uint16_t Tce::_amplitude; uint16_t Tce::_compare[]; uint32_t Tce::_frequency; Tce::callback_t Tce::_callback; #if CONFIG_TCE_ISR inline void tce_isr(void) { Tce::isr(); } ISR(TCE0_OVF_vect) { tce_isr(); } ISR(TCE0_CMP0_vect) { tce_isr(); } ISR(TCE0_CMP1_vect) { tce_isr(); } ISR(TCE0_CMP2_vect) { tce_isr(); } ISR(TCE0_CMP3_vect) { tce_isr(); } #endif #endif |
【関連投稿】
Microchip AVR8 用のライブラリを自作する。(GPIO)
Microchip AVR8 用のライブラリを自作する。(FUSE)
Microchip AVR8 用のライブラリを自作する。(CLOCK)
Microchip AVR8 用のライブラリを自作する。(RESET)
Microchip AVR8 用のライブラリを自作する。(PORTMUX)
Microchip AVR8 用のライブラリを自作する。(USART)
Microchip AVR8 用のライブラリを自作する。(RTC)
Microchip AVR8 用のライブラリを自作する。(TCA)
Microchip AVR8 用のライブラリを自作する。(TCB)
Microchip AVR8 用のライブラリを自作する。(VREF)
Microchip AVR8 用のライブラリを自作する。(DAC)
Microchip AVR8 用のライブラリを自作する。(AC)
Microchip AVR8 用のライブラリを自作する。(ADC)
Microchip AVR8 用のライブラリを自作する。(ZCD)
Microchip AVR8 用のライブラリを自作する。(SPI)
Microchip AVR8 用のライブラリを自作する。(TWI)
Microchip AVR8 用のライブラリを自作する。(MAIN)
Microchip AVR8 用のライブラリを自作する。(CONFIG)
Microchip AVR8 用のライブラリを自作する。(ALARM)
Microchip AVR8 用のライブラリを自作する。(TASK)
Microchip AVR8 用のライブラリを自作する。(CCL)
Microchip AVR8 用のライブラリを自作する。(TCD)
Microchip AVR8 用のライブラリを自作する。(TCE)
Microchip AVR8 用のライブラリを自作する。(WEX)
Microchip AVR8 用のライブラリを自作する。(TCF)
Microchip AVR8 用のライブラリを自作する。(DOWNLOAD)
