/* dtmfgen_dac.cpp author: 6v6gt 18.09.2025 https://forum.arduino.cc/t/dtmf-code-generator-using-an-attiny1614-dtmfgen-dac/1406768 */ #include "dtmfgen_dac.h" static DTMFGenerator *gI = NULL; // alias short form generatorInstance DTMFGenerator::DTMFGenerator() { // constructor } void DTMFGenerator::init( uint16_t sampleRate, uint8_t dacBitWidth ) { this->sampleRate = sampleRate ; this->dacBitWidth = dacBitWidth ; gI = this ; inGen = false ; // TCA0 clean out // see: http://ww1.microchip.com/downloads/en/AppNotes/TB3217-Getting-Started-with-TCA-90003217A.pdf TCA0.SINGLE.CTRLA = 0 ; TCA0.SINGLE.CTRLB = 0 ; TCA0.SINGLE.CTRLC = 0 ; TCA0.SINGLE.CTRLD = 0 ; TCA0.SINGLE.INTCTRL = 0 ; // TCA0 configuration TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc ; TCA0.SINGLE.PER = ((float)F_CPU / (float)sampleRate) ; // no prescaler #ifdef DEBUG_ Serial.printf( "TCA0.SINGLE.PER =%d \n", TCA0.SINGLE.PER ) ; #endif TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm ; TCA0.SINGLE.CTRLA = TCA_SINGLE_ENABLE_bm | TCA_SINGLE_CLKSEL_DIV1_gc ; // enable + prescaler. // DAC0 https://ww1.microchip.com/downloads/en/Appnotes/TB3210-Getting-Started-with-DAC-90003210A.pdf /* VREF_DAC0REFSEL_0V55_gc = (0x00 << 0), // Voltage reference at 0.55V VREF_DAC0REFSEL_1V1_gc = (0x01 << 0), // Voltage reference at 1.1V VREF_DAC0REFSEL_2V5_gc = (0x02 << 0), // Voltage reference at 2.5V VREF_DAC0REFSEL_4V34_gc = (0x03 << 0), // Voltage reference at 4.34V VREF_DAC0REFSEL_1V5_gc = (0x04 << 0) // Voltage reference at 1.5V */ // DAC output voltage (see choices above) VREF.CTRLA |= VREF_DAC0REFSEL_4V34_gc; VREF.CTRLB |= VREF_DAC0REFEN_bm; PORTA.PIN6CTRL &= ~PORT_ISC_gm; PORTA.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; PORTA.PIN6CTRL &= ~PORT_PULLUPEN_bm; DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm | DAC_RUNSTDBY_bm; DAC0.DATA = 0 ; } uint16_t DTMFGenerator::getSinDeg( uint16_t degrees ) { // Serial.println( degrees ) ; // degrees must be 0 to 360 // returns a function of sin depending on the selected bit width. For example, // for 8 bits, the return value is between 0 and 255 int32_t sin16bit = 0 ; // bad name ! uint32_t rv = 0 ; if ( degrees <= 90 ) sin16bit = sinQ1[degrees] ; else if ( degrees > 90 && degrees <= 180 ) sin16bit = sinQ1[180 - degrees] ; else if ( degrees > 180 && degrees <= 270 ) sin16bit = 0L - sinQ1[degrees - 180] ; else if ( degrees > 270 && degrees <= 360 ) sin16bit = 0L - sinQ1[360 - degrees] ; rv = sin16bit + 65536 ; // offset switch ( dacBitWidth ) { case BIT8 : rv >>= 9 ; break ; case BIT10 : rv >>= 7 ; break ; case BIT12 : rv >>= 5 ; break ; case BIT14 : rv >>= 3 ; break ; case BIT16 : rv >>= 1 ; break ; } return rv ; } void DTMFGenerator::generate( int8_t key, uint32_t durationMs ) { // sets up tone burst so it is then handled in the ISR if ( key >= 'a' && key <= 'd' ) key &= 0b11011111 ; // force key to upper case if a..d bool found = false ; for ( uint8_t y = 0 ; y < 4 && !found ; y++ ) { for ( uint8_t x = 0 ; x < 4 && !found ; x++ ) { if ( decoder[y][x] == key ) { // set up tone burst found = true ; uint16_t frequencyL = freqX[x] ; uint16_t frequencyH = freqY[y] ; degPerSampleHx16 = 360.0 * 16.0 * frequencyH / (float) sampleRate ; // 1/16 degree precision degPerSampleLx16 = 360.0 * 16.0 * frequencyL / (float) sampleRate ; samplesRemaining = sampleRate * durationMs / 1000 ; # ifdef DEBUG_ Serial.printf( "key=%c, x=%d, y=%d, frequencyH=%d, frequencyL=%d, degPerSampleHx16=%d, degPerSampleLx16=%d, sampleRate=%d \n", key, x, y, frequencyH, frequencyL, degPerSampleHx16, degPerSampleLx16, sampleRate ) ; # endif inGen = true ; } } } } bool DTMFGenerator::isBusy() { return inGen ; } ISR(TCA0_OVF_vect) { /* called at sample rate while samplesRemaining > 0 calculates the amplitude to be applied to the DAC based on a table lookup of amplitude for high and low frequencies. */ static uint32_t count = 0 ; // current sample count // to increase resolution we work in units of tenths of degrees static uint32_t degLx16 = 0 ; // running total degPerSampleLx16 % 360*16 static uint32_t degHx16 = 0 ; // running total degPerSampleHx16 % 360*16 if ( gI->inGen ) { uint32_t amplitude = ( gI->getSinDeg( degLx16/16 ) + gI->getSinDeg( degHx16/16 ) ) / 2 ; DAC0.DATA = amplitude ; count++ ; gI->samplesRemaining-- ; degLx16 += gI->degPerSampleLx16 ; while ( degLx16 >= 360*16 ) degLx16 -= 360*16 ; degHx16 += gI->degPerSampleHx16 ; while ( degHx16 >= 360*16 ) degHx16 -= 360*16 ; if ( gI->samplesRemaining <= 0 ) { # ifdef DEBUG_ Serial.printf( "finished. count=%lu \n", count ) ; # endif // clean/reset count = 0 ; degLx16 = 0 ; degHx16 = 0 ; DAC0.DATA = 0 ; gI->inGen = false ; } } TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ; // clear flags }