Hello,
Ive been playing around recently with David Johnson-Davies script on looping a sound from an Attiny85. I replaced his values with an appropriately scaled 0-255 sound outputs from my own wav file. Sound loops great from the Attiny85, but it is very quiet. Looking at the original code, its like its turning the 85 into its own mini Class D amp. Is there a way based on the code and the circuit to amp the signal with an external class D mono amp? Ive added the sketch and circuit below.
/* Audio Sample Player v2
David Johnson-Davies - www.technoblogy.com - 23rd October 2017
ATtiny85 @ 8 MHz (internal oscillator; BOD disabled)
CC BY 4.0
Licensed under a Creative Commons Attribution 4.0 International license:
http://creativecommons.org/licenses/by/4.0/
*/
/* Direct-coupled capacitorless output */
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#define adc_disable() (ADCSRA &= ~(1<<ADEN))
// Audio encoded as unsigned 8-bit, 8kHz sampling rate
const uint8_t scanner_wav[] PROGMEM = {
124,116, 115, 117, 124, 131, 136, 135, 134, 125, 117, 114, 115, 120, 128,142, 150,
156, 152, 141, 125, 106, 92, 90, 101, 118, 139, 156, 164, 160, 148, 131,118,
107, 107, 110, 119, 130, 138, 141, 136, 128, 120, 110, 112, 117, 128,143, 154,
157, 151, 138, 120, 100, 90, 89, 100, 117, 137, 157, 165, 162, 152, 136,119,
105, 100, 102, 115, 128, 141, 145, 142, 133, 123, 113, 111, 114, 123,137, 148,
157, 152, 143, 127, 110, 97, 91, 97, 110, 127, 145, 158, 163, 160, 148,135,
118, 105, 103, 103, 111, 123, 132, 141, 143, 139, 135, 126, 119, 119,120, 129,
139, 147, 148, 141, 132, 117, 104, 97, 97, 107, 123, 141, 157, 165, 163,150,
135, 119, 104, 95, 98, 108, 119, 133, 145, 147, 144, 132, 123, 119, 121,128,
136, 144, 146, 137, 125, 112, 99, 94, 100, 112, 128, 147, 163, 168, 164,148,
132, 112, 99, 93, 101, 111, 124, 138, 145, 145, 136, 128, 124, 125, 130,138,
146, 145, 138, 126, 109, 95, 92, 98, 111, 128, 149, 162, 169, 162, 151,133,
117, 103, 95, 96, 108, 120, 132, 140, 140, 136, 132, 130, 132, 138, 145,144,
140, 130, 115, 99, 90, 93, 105, 121, 141, 156, 167, 165, 155, 142, 125,112,
106, 101, 106, 114, 120, 124, 128, 129, 133, 137, 144, 153, 153, 151,140, 127,
107, 93, 86, 90, 104, 124, 148, 164, 169, 163, 150, 133, 120, 109, 105,108,
113, 118, 123, 126, 124, 126, 128, 137, 144, 152, 153, 148, 137, 121,105, 93,
90, 97, 113, 130, 146, 157, 159, 156, 148, 136, 124, 113, 108, 106, 108,113,
121, 126, 131, 135, 137, 139, 143, 145, 144, 139, 133, 122, 110, 102,99, 103,
114, 128, 141, 151, 157, 156, 145, 135, 124, 114, 108, 109, 114, 120,125, 130,
131, 129, 130, 132, 136, 142, 145, 143, 136, 125, 112, 102, 100, 103,117, 130,
144, 153, 151, 147, 136, 125, 117, 118, 119, 123, 126, 126, 126, 123,118, 120,
123, 128, 137, 143, 148, 148, 139, 129, 117, 105, 101, 103, 113, 129,144, 151,
149, 144, 132, 121, 115, 115, 124, 129, 137, 137, 133, 122, 116, 110,112, 123,
138, 147, 153, 151, 140, 127, 111, 105, 104, 109, 121, 135, 146, 149,143, 136,
124, 114, 111, 118, 127, 138, 144, 139, 132, 120, 107, 103, 113, 124,141, 153,
154, 147, 133, 118, 107, 104, 111, 124, 137, 145, 144, 139, 125, 118,
112, 117,126, 135, 144, 148, 140, 130, 118, 106, 106, 110, 121, 137, 147, 153,148, 137,
122, 111, 104, 110, 123, 136, 145, 143, 138, 126, 113, 108, 112, 123,136, 148,
149, 144, 131, 116, 106, 105, 110, 125, 138, 148, 149, 143, 131, 118,109, 109,
115, 128, 140, 146, 144, 136, 124, 112, 105, 109, 122, 137, 148, 151,145, 133,
117, 106, 106, 115, 129, 143, 148, 146, 131, 119, 107, 109, 120, 135,144, 152,
145, 131, 115, 106, 106, 114, 131, 145, 152, 146, 133, 118, 110, 107,116, 130,
144, 149, 144, 131, 115, 106, 104, 116, 133, 149, 154, 146, 130, 109,100, 105,
119, 139, 155, 158, 144, 124, 104, 97, 105, 123, 144, 160, 156, 139,117, 100,
100, 114, 134, 153, 159, 147, 124, 103, 93, 104, 126, 149, 162, 158,138, 113,
95, 96, 113, 139, 156, 157, 145, 122, 102, 95, 108, 129, 151, 159, 152,131,
109, 96, 98, 117, 142, 158, 158, 144, 123, 102, 94, 103, 124, 146, 159,156,
141, 122, 102, 99, 107, 126, 146, 153, 153, 137, 120, 102, 102, 111,129, 145,
156, 151, 137, 118, 104, 100, 110, 127, 147, 154, 152, 137, 118, 104,100, 112,
131, 151, 157, 147, 130, 112, 102, 106, 120, 138, 150, 152, 141, 124,113, 106,
110, 121, 137, 146, 146, 136, 128, 118, 110, 111, 118, 131, 141, 145,140, 131,
121, 111, 109, 115, 130, 142, 148, 144, 134, 118, 106, 107, 115, 131,148, 153,
144, 131, 115, 104, 107, 120, 138, 150, 151, 140, 126, 110, 101, 108,126, 145,
154, 151, 137, 119, 105, 102, 111, 132, 150, 155, 147, 132, 113, 99,103, 118,
137, 151, 153, 143, 124, 110, 103, 108, 124, 141, 151, 146, 138, 122,109, 105,
111, 125, 138, 150, 149, 141, 126, 113, 104, 109, 119, 134, 146, 149,142, 130,
118, 107, 107, 117, 129, 144, 149, 148, 138, 123, 108, 105, 108, 122,138, 150,
150, 144, 130, 115, 106, 106, 116, 131, 143, 152, 151, 139, 125, 109,99, 105,
116, 137, 152, 157, 151, 137, 117, 102, 98, 108, 125, 143, 154, 153,141, 124,
109, 102, 107, 121, 135, 147, 149, 142, 129, 117, 107, 109, 119, 131,141, 142,
140, 130, 122, 116, 117, 124, 131, 136, 136, 133, 128, 121, 119, 123,127, 134,
135, 134, 128, 122, 122, 121, 127, 134, 139, 139, 133, 124, 116, 114,116, 123,
134, 140, 145, 144, 134, 123, 114, 108, 110, 118, 129, 143, 148, 147,140, 126,
115, 106, 105, 113, 126, 138, 150, 150, 144, 134, 121, 111, 103, 106,115, 127,
139, 152, 153, 147, 134, 119, 106, 98, 104, 116, 131, 147, 157, 155,146, 131,
115, 101, 99, 103, 117, 136, 151, 157, 153, 141, 127, 112, 104, 103,112, 124,
140, 148, 152, 147, 135, 125, 113, 108, 110, 115, 128, 138, 146, 146,143, 133,
123, 114, 109, 109, 116, 126, 139, 145, 148, 145, 133, 123, 112, 106,107, 116,
129, 140, 148, 151, 144, 129, 118, 108, 105, 111, 122, 134, 146, 150,148, 138,
123, 112, 105, 106, 116, 128, 143, 149, 151, 144, 133, 118, 108, 106,109, 117,
130, 143, 151, 150, 143, 130, 116, 108, 103, 108, 121, 134, 147, 150,146, 137,
126, 112, 107, 108, 117, 130, 141, 147, 146, 140, 128, 117, 110, 110,116, 127,
133, 142, 142, 137, 130, 125, 121, 118, 122, 122
};
unsigned int scanner_wav_len = 1019;
unsigned int p = 0;
void setup () {
// Enable 64 MHz PLL and use as source for Timer1
PLLCSR = 1<<PCKE | 1<<PLLE;
// Set up Timer/Counter1 for PWM output
TIMSK = 0; // Timer interrupts OFF
TCCR1 = 1<<PWM1A | 2<<COM1A0 | 1<<CS10; // PWM A, clear on match, 1:1 prescale
GTCCR = 1<<PWM1B | 2<<COM1B0; // PWM B, clear on match
OCR1A = 128; OCR1B = 128; // 50% duty at start
// Set up Timer/Counter0 for 8kHz interrupt to output samples.
TCCR0A = 3<<WGM00; // Fast PWM
TCCR0B = 1<<WGM02 | 2<<CS00; // 1/8 prescale
TIMSK = 1<<OCIE0A; // Enable compare match
OCR0A = 124; // Divide by 1000
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
pinMode(4, OUTPUT);
pinMode(1, OUTPUT);
}
void loop () { }
// Sample interrupt
ISR (TIMER0_COMPA_vect) {
char sample = pgm_read_byte(&scanner_wav[p++]);
OCR1A = sample; OCR1B = sample ^ 255;
// End of data? Go to sleep
if (p == scanner_wav_len) {
p = 0;
//TIMSK = 0;
//adc_disable();
//sleep_enable();
//sleep_cpu(); // 1uA
}
}
