ATTiny85 based class D amplifier.

Status
Not open for further replies.
This is my first foray into class D amplifiers. It is an implementation of Dimitrov's ATTiny based design which can be found here. I modified the code a bit to remove a DC offset and programmed it with the Arduino IDE.

My circuit has a high pitched noise problem, but otherwise seemed to do a decent job. I just thought someone might be interested. Although, I am certain you are all familiar with it already.

My goal was a small amplifier for my bass guitar. This does not have enough gain to take the raw signal and send it to the speaker. I'm working on a different circuit that uses a TPA3125 and includes a pre-amp.

Code:
/*
Class D Amplifier ATTiny85 Controller Sketch
last updated 6-23-2014
*/

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdint.h>

#define DACP OCR1A
#define DACN OCR1B

// variable declarations

byte time;

unsigned int pos;
unsigned int neg;
unsigned int adc;

int val1 = 0;
int errorSum = 0;

void setup() {    
    
// Disable global interrupts
    cli();

// Configure ports
    DDRB = B00010010;
    PORTB = B11100100;

// Configure ADC
    ADMUX = B11010011;
    ADCSRA = B11000100;
    
// Power reduction
    PRR = B00000110;
    MCUCR = B00100000;
    
// Configure PWM
    TCCR1 = B01100010;
    GTCCR = B01100000;
    TIMSK = B100;
    PLLCSR = B110;
    
// Enable global interrupts
    sei();        
}

void loop() {
    sleep_cpu();
}
   

ISR(TIMER1_OVF_vect) {
    if(1 & ++time){
      adc = ADC;
      val1 = (adc >> 2) - 127 - (errorSum >> 10);
      errorSum = errorSum + val1;
      
      pos = 127 + val1;
      neg = 128 - val1;

      DACP = (pos & 255);
      DACN = (neg & 255);
    } else {
      DACP = (pos & 255);
      DACN = (neg & 255);    
    }
}
 

Attachments

  • ATTinyAmpView01.jpg
    ATTinyAmpView01.jpg
    393.9 KB · Views: 1,173
  • ATTinyAmpView02.jpg
    ATTinyAmpView02.jpg
    349.9 KB · Views: 1,142
Last edited:
The ATTiny85 has plenty of processing grunt. I'm using an ATTiny25 (85 w/less flash) in this guy:

https://www.youtube.com/watch?v=SlrnY7rLIJ4

I'm reading 31.25KHz, 8-bit data from a SPI flash chip using the USI peripheral, doing a 4x linear interpolation up to 125KHz, and feeding the output timer with the upsampled signal. Including wake from sleep and interrupt overhead, I'm using ~50% of the available 8MHz cycles.

Main limitations of this chip as a class D amplifier are going to be the limited dynamic range and accuracy of the 10 bit ADC.
 
I don't know what I/O features are offered in ATTtiny.
But wouldn't an analog feedback loop built around a
comparator (if by chance pins can be configured that
way) be of higher fidelity than any number ADC bits
input with an open loop PWM output?
 
Caleb6543, thanks for posting! At some point I wondered, if the chip can be programmed successfully for this project with Arduino as ISP at all. Your code showed me, where I was wrong.

My circuit has a high pitched noise problem, but otherwise seemed to do a decent job.
Did you try to use Dimitrov's code without changes? I find this line particularly interesting:

Code:
      val1 = (adc >> 2) - 127 - (errorSum >> 10);
It gave me a clicking sound once every second or so. Removing (errorSum >> 10) helped. But I ended up using original Rouslan Dimitrov's code anyway (with slightest changes).

Also, burning correct fuses on ATtiny might help in your case.

My version of the amp is here. I was also planning to make a small amplifier for my acoustic-electric guitar.

I have one unexpected effect with the amp, however:
when the input gets too loud, the speakers get absolutely quiet (stuck in a certain position) and the sound doesn't return even after making input quiet again. I have to turn the amp off and on again to return the music in cases like that. I don't understand what's going on.
 
I plan to build this but with an external A / D converter.

Can you tell me what cheap A/D converter can I use?
Or what are the characteristics of an acceptable quality for music A/D converter?

How can I get a PWM with better resolution? With an IC not with synchronized PWMs

The PIC 30F2020 has 16 PWM fast but is 4 times more expensive.
Is It Correct What Do I Write? Attiny at 20MHz = 20 M Instructions per second

Pic30F2020 at 120MHz = 30 M Instructions per second
 
This is my first foray into class D amplifiers. It is an implementation of Dimitrov's ATTiny based design which can be found here. I modified the code a bit to remove a DC offset and programmed it with the Arduino IDE.

My circuit has a high pitched noise problem, but otherwise seemed to do a decent job.

Please tell me, answer me:
-
I have made the amplifier with Attiny25 but it has great noise. The program was written by me and it is quite simple.


Attiny25 / 45/85 has several modes of sleep.
One of sleep mode is for reduce ADC noise



I do not know if I use this type of sleep, because I tried to set it up and I do not know if it was set correctly.

This may be the reason why he has such a big noise?

You could also post, please, the HEX file, because I have an Atmel programmer
 
Status
Not open for further replies.